paulmach / orb

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

copy the original mvt.Layers for reuse #102

Closed SulimanLab closed 1 year ago

SulimanLab commented 2 years ago

Hi there,

I am trying to build a tileserver that reads geojson and serve tiles

I started by reading the json file:

geojsonData, err := ioutil.ReadFile("./data/large_data.geojson")

then I unmarshal the file to go object:

r = map[string]*geojson.FeatureCollection{}
err = json.Unmarshal(geojsonData, &r)

layers := mvt.NewLayers(r)

when serving the tiles the original values of the layers gets mutated, which force me to read and unmarshal the geojson again,

I tried to copy layers so I don't modify the original layers, but still changing one changes the other.

How I can copy the layers by value?

SulimanLab commented 2 years ago

full example can be found here: playground

paulmach commented 2 years ago

The answer for your tool it to load the file for every request.

The layers.ProjectToTile, layers.Clip and layers.Simplify commands modify the layer data. This is by design for performance reasons. It's expected the feature collection/layers are loaded newly from a database on every request.

It possible we could add a Clone method to geojson features and feature collections so you could do that instead for just reloading the file.

SulimanLab commented 2 years ago

I have wrote a function that's deep copy the features collections, but I am not sure about the performance.

If you will do it at your end that would be great.