Skip to content

Commit

Permalink
add a Center method to MbtilesMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
sfomuseumbot committed Sep 7, 2024
1 parent 0beefab commit 0301c3e
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 22 deletions.
2 changes: 1 addition & 1 deletion cmd/build/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ func main() {
log.Print("Finished processing tiles")

err = outputter.AssignSpatialMetadata(bounds, zooms[0], zooms[len(zooms)-1])

if err != nil {
log.Printf("Wrote tiles but failed to assign spatial metadata, %v", err)
}
Expand Down
47 changes: 26 additions & 21 deletions cmd/mbtiles-assign-metadata/main.go
Original file line number Diff line number Diff line change
@@ -1,57 +1,57 @@
package main

import (
"log"
"flag"

"log"

"github.com/paulmach/orb"
"github.com/paulmach/orb/maptile"
"github.com/tilezen/go-tilepacks/tilepack"
"github.com/tilezen/go-tilepacks/tilepack"
)

func main() {

var verify bool

flag.BoolVar(&verify, "verify", false, "Verify that spatial metadata was written to each database")

flag.Parse()

for _, path := range flag.Args() {

mbtilesReader, err := tilepack.NewMbtilesReader(path)

if err != nil {
log.Fatalf("Couldn't read input mbtiles %s: %+v", path, err)
}

var bounds *orb.Bound
minZoom := uint(20)
maxZoom := uint(0)

err = mbtilesReader.VisitAllTiles(func(t maptile.Tile, data []byte) {

tb := t.Bound()

if bounds == nil {
bounds = &tb
} else {
tb = bounds.Union(tb)
bounds = &tb
}

minZoom = min(minZoom, uint(t.Z))
maxZoom = max(maxZoom, uint(t.Z))
maxZoom = max(maxZoom, uint(t.Z))
})

if err != nil {
log.Fatalf("Couldn't read tiles from %s: %+v", path, err)
}

mbtilesReader.Close()

mbtilesWriter, err := tilepack.NewMbtilesOutputter(path, 0)

if err != nil {
log.Fatalf("Couldn't read input mbtiles %s: %+v", path, err)
}
Expand All @@ -67,37 +67,42 @@ func main() {
if verify {

mbtilesReader, err := tilepack.NewMbtilesReader(path)

if err != nil {
log.Fatalf("Couldn't read input mbtiles %s: %+v", path, err)
}

metadata, err := mbtilesReader.Metadata()

if err != nil {
log.Fatalf("Unable to read metadata for %s, %v", path, err)
}

bounds, err := metadata.Bounds()

if err != nil {
log.Fatalf("Failed to derive bounds metadata after update")
}

center, err := metadata.Center()

if err != nil {
log.Fatalf("Failed to derive bounds metadata after update")
}

minZoom, err := metadata.MinZoom()

if err != nil {
log.Fatalf("Failed to derive min zoom metadata after update")
}

maxZoom, err := metadata.MaxZoom()

if err != nil {
log.Fatalf("Failed to derive max zoom metadata after update")
}

log.Printf("[%s] bounds: %v zoom: %d-%d\n", path, bounds, minZoom, maxZoom)
log.Printf("[%s] bounds: %v center: %v zoom: %d-%d\n", path, bounds, center, minZoom, maxZoom)
}
}
}

32 changes: 32 additions & 0 deletions tilepack/mbtiles_metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,38 @@ func (m *MbtilesMetadata) Bounds() (orb.Bound, error) {
return bounds, nil
}

func (m *MbtilesMetadata) Center() (orb.Point, error) {

var pt orb.Point

str_center, exists := m.Get("center")

if !exists {
return pt, fmt.Errorf("Metadata is missing center")
}

parts := strings.Split(str_center, ",")

if len(parts) != 2 {
return pt, fmt.Errorf("Invalid center metadata")
}

x, err := strconv.ParseFloat(parts[0], 64)

if err != nil {
return pt, fmt.Errorf("Failed to parse x, %w", err)
}

y, err := strconv.ParseFloat(parts[1], 64)

if err != nil {
return pt, fmt.Errorf("Failed to parse y, %w", err)
}

pt = orb.Point([2]float64{x, y})
return pt, nil
}

func (m *MbtilesMetadata) MinZoom() (uint, error) {

str_minzoom, exists := m.Get("minzoom")
Expand Down

0 comments on commit 0301c3e

Please sign in to comment.