Skip to content

Commit

Permalink
Expose public funcs (#3)
Browse files Browse the repository at this point in the history
* Expose some tile util functions as public

* Tweaks to enable reuse of CreateMetadata for rasters

* run gofmt

* golang conventions on enum
  • Loading branch information
nathreed authored May 17, 2024
1 parent 4f2ed08 commit 2bca1ba
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 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 (
MbTilesFormatPbf MbTilesFormat = "pbf"
MbTilesFormatJpg MbTilesFormat = "jpg"
MbTilesFormatPng MbTilesFormat = "png"
MbTilesFormatWebP 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 = MbTilesFormatPbf
}
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 == MbTilesFormatPbf {
metaJSONField := CreateMetadataJSON(tj)
if metaJSONBytes, err := json.Marshal(metaJSONField); err == nil {
meta["json"] = string(metaJSONBytes)
}
}

return meta
}

Expand Down
7 changes: 4 additions & 3 deletions tileutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ type BoundingBox struct {
Bottom float64
}

func listTiles(zooms []int, tj *TileJSON) []TileCoords {
// ListTiles returns a list of all the tiles within the given zooms based on the TileJSON
func ListTiles(zooms []int, tj *TileJSON) []TileCoords {
tiles := make([]TileCoords, 0, 2<<zooms[len(zooms)-1])
for _, z := range zooms {
newTiles := tilesInBbox(BoundingBox{
Expand Down Expand Up @@ -90,8 +91,8 @@ func tilesInBbox(bbox BoundingBox, zoom int) []TileCoords {
return tiles
}

// roundRobinTiles assigns tiles to workers in round robin fashion
func roundRobinTiles(input []TileCoords, numWorkers int) [][]TileCoords {
// RoundRobinTiles assigns tiles to workers in round robin fashion
func RoundRobinTiles(input []TileCoords, numWorkers int) [][]TileCoords {
out := make([][]TileCoords, numWorkers)
for i, v := range input {
index := i % numWorkers
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: MbTilesFormatPbf,
})
err = mbWriter.BulkWriteMetadata(meta)
return
Expand Down

0 comments on commit 2bca1ba

Please sign in to comment.