Skip to content

Commit

Permalink
Tweaks to enable reuse of CreateMetadata for rasters
Browse files Browse the repository at this point in the history
  • Loading branch information
nathreed committed May 17, 2024
1 parent 825ad98 commit d9125e9
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func main() {
tileJSON.MinZoom = zooms[0]
tileJSON.MaxZoom = zooms[len(zooms)-1]

tiles := listTiles(zooms, tileJSON)
tiles := ListTiles(zooms, tileJSON)
if args.TilesFile != "" {
extraTiles, err := tilesFromFile(args.TilesFile)
if err != nil {
Expand All @@ -271,7 +271,7 @@ func main() {
numWorkers = tileLen
}
// round robin the tiles so workers are hitting similar geospatial entries and zoom at the same time
rrTiles := roundRobinTiles(tiles, numWorkers)
rrTiles := RoundRobinTiles(tiles, numWorkers)
for i := 0; i < numWorkers; i++ {
wg.Add(1)
workerTiles := rrTiles[i]
Expand Down
28 changes: 24 additions & 4 deletions mbtiles.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,32 @@ import (

type MbTilesMetadata map[string]string

type MbTilesFormat string

const (
Pbf MbTilesFormat = "pbf"
Jpg MbTilesFormat = "jpg"
Png MbTilesFormat = "png"
WebP MbTilesFormat = "webp"
)

type CreateMetadataOptions struct {
Filename string
Version string
Format MbTilesFormat
}

// CreateMetadata generates the (name,value) metadata pairs for .mbtiles files.
// Since name is required, it falls back to the filename if not provided.
// format is also required, so it falls back to pbf if not provided.
func CreateMetadata(tj *TileJSON, opts CreateMetadataOptions) MbTilesMetadata {
format := opts.Format
if string(format) == "" {
format = Pbf
}
meta := MbTilesMetadata{
"name": tj.Name,
"format": "pbf",
"format": string(format),
"type": "baselayer",
}
if tj.Name == "" && opts.Filename != "" {
Expand Down Expand Up @@ -59,10 +74,15 @@ func CreateMetadata(tj *TileJSON, opts CreateMetadataOptions) MbTilesMetadata {
}
meta["center"] = center
}
metaJSONField := CreateMetadataJSON(tj)
if metaJSONBytes, err := json.Marshal(metaJSONField); err == nil {
meta["json"] = string(metaJSONBytes)

// mbtiles spec requires the json field for vector format and it's not meaningful for rasters
if opts.Format == Pbf {
metaJSONField := CreateMetadataJSON(tj)
if metaJSONBytes, err := json.Marshal(metaJSONField); err == nil {
meta["json"] = string(metaJSONBytes)
}
}

return meta
}

Expand Down
1 change: 1 addition & 0 deletions writers.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ func NewWriters(args Args, tj *TileJSON) (writer TileWriter, bulkWriter TileBulk
meta := CreateMetadata(tj, CreateMetadataOptions{
Filename: args.TileJSON,
Version: args.Version,
Format: Pbf,
})
err = mbWriter.BulkWriteMetadata(meta)
return
Expand Down

0 comments on commit d9125e9

Please sign in to comment.