paulmach / orb

Types and utilities for working with 2d geometry in Golang
MIT License
886 stars 103 forks source link

Workflow to create mbtiles #149

Open jjcfrancisco opened 3 months ago

jjcfrancisco commented 3 months ago

This library seem to have most parts to create mbtiles and so I hope this is possible. Using the code below, I am somehow able to get some results. If both minZoom and maxZoom are 0, then the mbtiles displays as it should - they look jagged as I move closer but this is as expected. However, if maxZoom = 5, tiles only show within zoom 0, despite metadata having the correct information and when it moves to zoom 1, the geometry disappears.

I've debugged the tiles coming from tilecover using tiles.ToFeatureCollection and they are correct. The results of layers.Clip is also fine. It seems more linked to the ProjectToTile as the geometries dissapear after zoom 0, although I cannot confirm this. Or maybe there's something I am doing wrong? I'd appreciate some advice/tips/examples.

For this example I used this geojson of Spain.

Ps. I am aware of Tippecanoe but this is not an option I as I am looking for a Go solution.

minZoom = 0
maxZoom = 0

// data := spain.geojson...

var geoms orb.Collection
for _, feature := range data.Features {
    geoms = append(geoms, feature.Geometry)
}

covers := []cover{}
for z := minZoom; z <= maxZoom; z++ {
    tiles, err := tilecover.Collection(geoms, maptile.Zoom(z))
    if err != nil {
        log.Fatal(err)
    }
    cover := cover{tile: tiles}
    covers = append(covers, cover)

}

for cover := range covers {

    layer := mvt.Layer{Name: "spain", Version: 2, Extent: 4096, Features: data.Features}
    layers := mvt.Layers{&layer}
        tile := cover.tile
    layers.Clip(tile.Bound())
    layers.ProjectToTile(tile)
    blob, err := mvt.MarshalGzipped(layers)
    if err != nil {
        log.Fatal(err)
    }
    // Insert blob into Sqlite3 rows

}