Skip to content

Commit

Permalink
Updated to geom v0.1.0.
Browse files Browse the repository at this point in the history
Updated to use slippy tile from geom v0.1.0.
This fixes the issues with slippy tile.
  • Loading branch information
gdey committed Jul 30, 2024
1 parent 686ce9a commit 622dd46
Show file tree
Hide file tree
Showing 22 changed files with 147 additions and 213 deletions.
2 changes: 1 addition & 1 deletion atlas/atlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func (a *Atlas) SeedMapTile(ctx context.Context, m Map, z, x, y uint) error {
return ErrMissingCache
}

tile := slippy.NewTile(z, x, y)
tile := slippy.Tile{Z: slippy.Zoom(z), X: x, Y: y}

// encode the tile
b, err := m.Encode(ctx, tile, nil)
Expand Down
27 changes: 13 additions & 14 deletions atlas/map.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ func (m Map) AddDebugLayers() Map {
}

// FilterLayersByZoom returns a copy of a Map with a subset of layers that match the given zoom
func (m Map) FilterLayersByZoom(zoom uint) Map {
func (m Map) FilterLayersByZoom(zoom slippy.Zoom) Map {
var layers []Layer

for i := range m.Layers {
if m.Layers[i].MinZoom <= zoom && m.Layers[i].MaxZoom >= zoom {
if slippy.Zoom(m.Layers[i].MinZoom) <= zoom && slippy.Zoom(m.Layers[i].MaxZoom) >= zoom {
layers = append(layers, m.Layers[i])
continue
}
Expand Down Expand Up @@ -182,7 +182,7 @@ func (m Map) FilterLayersByName(names ...string) Map {
return m
}

func (m Map) encodeMVTProviderTile(ctx context.Context, tile *slippy.Tile, params provider.Params) ([]byte, error) {
func (m Map) encodeMVTProviderTile(ctx context.Context, tile slippy.Tile, params provider.Params) ([]byte, error) {
// get the list of our layers
ptile := provider.NewTile(tile.Z, tile.X, tile.Y, uint(m.TileBuffer), uint(m.SRID))

Expand All @@ -199,7 +199,7 @@ func (m Map) encodeMVTProviderTile(ctx context.Context, tile *slippy.Tile, param

// encodeMVTTile will encode the given tile into mvt format
// TODO (arolek): support for max zoom
func (m Map) encodeMVTTile(ctx context.Context, tile *slippy.Tile, params provider.Params) ([]byte, error) {
func (m Map) encodeMVTTile(ctx context.Context, tile slippy.Tile, params provider.Params) ([]byte, error) {

// tile container
var mvtTile mvt.Tile
Expand All @@ -209,7 +209,7 @@ func (m Map) encodeMVTTile(ctx context.Context, tile *slippy.Tile, params provid
// layer stack
mvtLayers := make([]*mvt.Layer, len(m.Layers))

// set our waitgroup count
// set our WaitGroup count
wg.Add(len(m.Layers))

// iterate our layers
Expand Down Expand Up @@ -237,7 +237,7 @@ func (m Map) encodeMVTTile(ctx context.Context, tile *slippy.Tile, params provid

geo := f.Geometry

// check if the feature SRID and map SRID are different. If they are then reporject
// check if the feature SRID and map SRID are different. If they are then reprojected
if f.SRID != m.SRID {
// TODO(arolek): support for additional projections
g, err := basic.ToWebMercator(f.SRID, geo)
Expand All @@ -261,12 +261,12 @@ func (m Map) encodeMVTTile(ctx context.Context, tile *slippy.Tile, params provid
}

// TODO (arolek): change out the tile type for VTile. tegola.Tile will be deprecated
tegolaTile := tegola.NewTile(tile.ZXY())
tegolaTile := tegola.TileFromSlippyTile(tile)

sg := tegolaGeo
// multiple ways to turn off simplification. check the atlas init() function
// for how the second two conditions are set
if !l.DontSimplify && simplifyGeometries && tile.Z < simplificationMaxZoom {
if !l.DontSimplify && simplifyGeometries && tile.Z < slippy.Zoom(simplificationMaxZoom) {
sg = simplify.SimplifyGeometry(tegolaGeo, tegolaTile.ZEpislon())
}

Expand Down Expand Up @@ -340,10 +340,9 @@ func (m Map) encodeMVTTile(ctx context.Context, tile *slippy.Tile, params provid
// Do nothing, context was canceled

default:
z, x, y := tile.ZXY()
// TODO (arolek): should we return an error to the response or just log the error?
// we can't just write to the response as the waitgroup is going to write to the response as well
log.Errorf("err fetching tile (z: %v, x: %v, y: %v) features: %v", z, x, y, err)
// we can't just write to the response as the WaitGroup is going to write to the response as well
log.Errorf("err fetching tile (%v) features: %v", tile, err)
}
return
}
Expand All @@ -353,12 +352,12 @@ func (m Map) encodeMVTTile(ctx context.Context, tile *slippy.Tile, params provid
}(i, layer)
}

// wait for the waitgroup to finish
// wait for the WaitGroup to finish
wg.Wait()

// stop processing if the context has an error. this check is necessary
// otherwise the server continues processing even if the request was canceled
// as the waitgroup was not notified of the cancel
// as the WaitGroup was not notified of the cancel
if ctx.Err() != nil {
return nil, ctx.Err()
}
Expand All @@ -380,7 +379,7 @@ func (m Map) encodeMVTTile(ctx context.Context, tile *slippy.Tile, params provid
}

// Encode will encode the given tile into mvt format
func (m Map) Encode(ctx context.Context, tile *slippy.Tile, params provider.Params) ([]byte, error) {
func (m Map) Encode(ctx context.Context, tile slippy.Tile, params provider.Params) ([]byte, error) {
var (
tileBytes []byte
err error
Expand Down
8 changes: 4 additions & 4 deletions atlas/map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
func TestMapFilterLayersByZoom(t *testing.T) {
testcases := []struct {
atlasMap atlas.Map
zoom uint
zoom slippy.Zoom
expected atlas.Map
}{
{
Expand Down Expand Up @@ -216,7 +216,7 @@ func TestEncode(t *testing.T) {

type tcase struct {
grid atlas.Map
tile *slippy.Tile
tile slippy.Tile
expected vectorTile.Tile
}

Expand Down Expand Up @@ -365,7 +365,7 @@ func TestEncode(t *testing.T) {
},
},
},
tile: slippy.NewTile(2, 3, 3),
tile: slippy.Tile{Z: 2, X: 3, Y: 3},
expected: vectorTile.Tile{
Layers: []*vectorTile.Tile_Layer{
{
Expand Down Expand Up @@ -423,7 +423,7 @@ func TestEncode(t *testing.T) {
},
},
},
tile: slippy.NewTile(2, 3, 3),
tile: slippy.Tile{Z: 2, X: 3, Y: 3},
expected: vectorTile.Tile{
Layers: []*vectorTile.Tile_Layer{
{
Expand Down
15 changes: 8 additions & 7 deletions cmd/tegola/cmd/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cache

import (
"context"
"errors"
"fmt"
"os"
"strconv"
Expand All @@ -16,7 +17,7 @@ import (
"github.com/go-spatial/tegola/internal/log"
)

// the config from the main app
// Config from the main app
var Config *config.Config
var RequireCache bool

Expand Down Expand Up @@ -61,14 +62,14 @@ Use "{{.CommandPath}} [command] --help" for more information about a command.{{e
}

type TileChannel struct {
channel chan *slippy.Tile
channel chan slippy.Tile
cl sync.RWMutex
isClosed bool
l sync.RWMutex
err error
}

func (tc *TileChannel) Channel() <-chan *slippy.Tile {
func (tc *TileChannel) Channel() <-chan slippy.Tile {
if tc == nil {
return nil
}
Expand Down Expand Up @@ -111,7 +112,7 @@ func (tc *TileChannel) Close() {

type MapTile struct {
MapName string
Tile *slippy.Tile
Tile slippy.Tile
}

func doWork(ctx context.Context, tileChannel *TileChannel, maps []atlas.Map, concurrency int, worker func(context.Context, MapTile) error) (err error) {
Expand Down Expand Up @@ -150,7 +151,7 @@ func doWork(ctx context.Context, tileChannel *TileChannel, maps []atlas.Map, con
}
if cleanup {
log.Debugf("worker %v waiting on clean up of tiler", i)
for _ = range tiler {
for range tiler {
continue
}
}
Expand Down Expand Up @@ -205,7 +206,7 @@ TileChannelLoop:
close(tiler)
if cleanup {
// want to soak up any messages
for _ = range tileChannel.Channel() {
for range tileChannel.Channel() {
continue
}
}
Expand All @@ -226,7 +227,7 @@ TileChannelLoop:
if err == nil {
err = mapTileErr
}
if err == context.Canceled {
if errors.Is(err, context.Canceled) {
return nil
}
return err
Expand Down
7 changes: 3 additions & 4 deletions cmd/tegola/cmd/cache/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,10 @@ func (f Format) Parse(val string) (z, x, y uint, err error) {
return uint(zi), uint(xi), uint(yi), nil
}

func (f Format) ParseTile(val string) (tile *slippy.Tile, err error) {
func (f Format) ParseTile(val string) (tile slippy.Tile, err error) {
z, x, y, err := format.Parse(val)
if err != nil {
return nil, err
return slippy.Tile{}, err
}
tile = slippy.NewTile(z, x, y)
return tile, nil
return slippy.Tile{Z: slippy.Zoom(z), X: x, Y: y}, nil
}
18 changes: 9 additions & 9 deletions cmd/tegola/cmd/cache/seed_purge.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,27 +200,27 @@ func seedPurgeCommand(_ *cobra.Command, _ []string) (err error) {
func generateTilesForBounds(ctx context.Context, bounds [4]float64, zooms []uint) *TileChannel {

tce := &TileChannel{
channel: make(chan *slippy.Tile),
channel: make(chan slippy.Tile),
}

webmercatorGrid, err := slippy.NewGrid(3857)
if err != nil {
tce.setError(fmt.Errorf("Could not create Web Mercator grid (3857): %s", err))
tce.Close()
return tce
}
webmercatorGrid := slippy.NewGrid(3857, 0)

go func() {
defer tce.Close()

var extent geom.Extent = bounds
for _, z := range zooms {

tiles := slippy.FromBounds(webmercatorGrid, &extent, z)
tiles, err := slippy.FromBounds(webmercatorGrid, &extent, slippy.Zoom(z))
if err != nil {
tce.setError(fmt.Errorf("got error trying to get tiles: %w", err))
tce.Close()
return
}
for _, tile := range tiles {
t := tile
select {
case tce.channel <- &t:
case tce.channel <- t:
case <-ctx.Done():
// we have been cancelled
return
Expand Down
35 changes: 12 additions & 23 deletions cmd/tegola/cmd/cache/seed_purge_generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,11 @@ import (
"github.com/go-spatial/geom/slippy"
)

type sTiles []*slippy.Tile

func (st sTiles) Len() int { return len(st) }
func (st sTiles) Swap(i, j int) { st[i], st[j] = st[j], st[i] }
func (st sTiles) Less(i, j int) bool {
zi, xi, yi := st[i].ZXY()
zj, xj, yj := st[j].ZXY()
switch {
case zi != zj:
return zi < zj
case xi != xj:
return xi < xj
default:
return yi < yj
}
}
type sTiles []slippy.Tile

func (st sTiles) Len() int { return len(st) }
func (st sTiles) Swap(i, j int) { st[i], st[j] = st[j], st[i] }
func (st sTiles) Less(i, j int) bool { return st[i].Less(st[j]) }

// IsEqual report true only if both the size and the elements are the same. Where a tile is equal only if the z,x,y elements match.
func (st sTiles) IsEqual(ost sTiles) bool {
Expand Down Expand Up @@ -118,24 +107,24 @@ func TestGenerateTilesForBounds(t *testing.T) {
"max_zoom=0": {
zooms: []uint{0},
bounds: worldBounds,
tiles: sTiles{slippy.NewTile(0, 0, 0)},
tiles: sTiles{slippy.Tile{}},
},
"min_zoom=1 max_zoom=1": {
zooms: []uint{1},
bounds: worldBounds,
tiles: sTiles{
slippy.NewTile(1, 0, 0),
slippy.NewTile(1, 0, 1),
slippy.NewTile(1, 1, 0),
slippy.NewTile(1, 1, 1),
slippy.Tile{Z: 1},
slippy.Tile{Z: 1, Y: 1},
slippy.Tile{Z: 1, X: 1},
slippy.Tile{Z: 1, X: 1, Y: 1},
},
},
"min_zoom=1 max_zoom=1 bounds=180,90,0,0": {
zooms: []uint{1},
bounds: [4]float64{180.0, 90.0, 0.0, 0.0},
tiles: sTiles{
slippy.NewTile(1, 1, 0),
slippy.NewTile(1, 1, 1),
slippy.Tile{Z: 1, X: 1},
slippy.Tile{Z: 1, X: 1, Y: 1},
},
},
}
Expand Down
43 changes: 0 additions & 43 deletions cmd/tegola/cmd/cache/slippy.go

This file was deleted.

Loading

0 comments on commit 622dd46

Please sign in to comment.