Skip to content

Commit fa73964

Browse files
committed
Adds "-s" toggle to enable shape simplification (defaults to false)
In running performance tests, I've found that contrary to the suggestions made in #3, the better solution in most cases is to not do shape simplification at all, which reduces CPU loads at the cost of network payload size (since the result is technically higher-fidelity vector geometries). That said, this has the practical advantage of reducing latency on higher-bandwidth network connections, by relying more heavily on network optimizations (caching, compression, etc.).
1 parent 1de3af5 commit fa73964

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

cmd/tilenol/main.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ var (
4949
Envar("TILENOL_ENABLE_CORS").
5050
Short('x').
5151
Bool()
52+
simplify = runCmd.
53+
Flag("simplify-shapes", "Simplifies geometries based on zoome level").
54+
Envar("TILENOL_SIMPLIFY_SHAPES").
55+
Short('s').
56+
Bool()
5257
cache = runCmd.
5358
Flag("cache-control", "Sets the \"Cache-Control\" header").
5459
Envar("TILENOL_CACHE_CONTROL").
@@ -101,6 +106,9 @@ func main() {
101106
if *cors {
102107
opts = append(opts, server.EnableCORS)
103108
}
109+
if *simplify {
110+
opts = append(opts, server.SimplifyShapes)
111+
}
104112

105113
s, err := server.NewServer(opts...)
106114
if err != nil {

server/config.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ func EnableCORS(s *Server) error {
3434
return nil
3535
}
3636

37+
// Simplify shapes enable geometry simplification based on the requested zoom level
38+
func SimplifyShapes(s *Server) error {
39+
s.Simplify = true
40+
return nil
41+
}
42+
3743
// CacheControl sets a fixed string to be used for the Cache-Control HTTP header
3844
func CacheControl(cacheControl string) ConfigOption {
3945
return func(s *Server) error {

server/server.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ type Server struct {
3333
InternalPort uint16
3434
EnableCORS bool
3535
CacheControl string
36+
Simplify bool
3637
ES *elastic.Client
3738
ESMappings map[string]string
3839
ZoomRanges map[string][]int
@@ -130,7 +131,7 @@ func (s *Server) getVectorTile(w http.ResponseWriter, r *http.Request) {
130131
esStart := time.Now()
131132
fc, esErr := s.doQuery(r.Context(), featureType, geometryField, extraSources, tileBounds)
132133
esElapsed := time.Since(esStart)
133-
Logger.Debugf("ES query for layer [%s] @ (%d, %d, %d) took %s", featureType, x, y, z, esElapsed)
134+
Logger.Debugf("ES query for layer [%s] @ (%d, %d, %d) with %d features took %s", featureType, x, y, z, len(fc.Features), esElapsed)
134135
if esErr != nil {
135136
Logger.Errorf("Failed to do ES query: %+v", esErr)
136137
s.handleError(esErr, w, r)
@@ -142,16 +143,19 @@ func (s *Server) getVectorTile(w http.ResponseWriter, r *http.Request) {
142143
layer.Version = 2 // Set to tile spec v2
143144
layer.ProjectToTile(tile)
144145
layer.Clip(mvt.MapboxGLDefaultExtentBound)
145-
minZoom := MinZoom
146-
maxZoom := MaxZoom
147-
zoomRange, hasZoomRange := s.ZoomRanges[featureType]
148-
if hasZoomRange {
149-
minZoom, maxZoom = zoomRange[0], zoomRange[1]
146+
147+
if s.Simplify {
148+
minZoom := MinZoom
149+
maxZoom := MaxZoom
150+
zoomRange, hasZoomRange := s.ZoomRanges[featureType]
151+
if hasZoomRange {
152+
minZoom, maxZoom = zoomRange[0], zoomRange[1]
153+
}
154+
simplifyThreshold := calculateSimplificationThreshold(minZoom, maxZoom, z)
155+
Logger.Debugf("Simplifying @ zoom [%d], epsilon [%f]", z, simplifyThreshold)
156+
layer.Simplify(simplify.DouglasPeucker(simplifyThreshold))
157+
layer.RemoveEmpty(1.0, 1.0)
150158
}
151-
simplifyThreshold := calculateSimplificationThreshold(minZoom, maxZoom, z)
152-
Logger.Debugf("Simplifying @ zoom [%d], epsilon [%f]", z, simplifyThreshold)
153-
layer.Simplify(simplify.DouglasPeucker(simplifyThreshold))
154-
layer.RemoveEmpty(1.0, 1.0)
155159

156160
// Set standard response headers
157161
w.Header().Set("Cache-Control", s.CacheControl)

0 commit comments

Comments
 (0)