Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/mvt fails on nil features #135

Closed
polastre opened this issue Aug 23, 2023 · 5 comments
Closed

encoding/mvt fails on nil features #135

polastre opened this issue Aug 23, 2023 · 5 comments

Comments

@polastre
Copy link

When a layer has an empty feature, often due to clipping, it causes MarshalGzipped (and probably Marshal too) to fail with a null pointer panic. The panic is on accessing f.Geometry in the marshaller. On further investigation, this happens when the geometry or feature is nil.

My workflow looks a bit like this:

// get tileData from source
layers, err := mvt.UnmarshalGzipped(tileData)
if err != nil {
	return nil, err
}
layers.ProjectToWGS84(tileInfo)
layers.Clip(requestedTile.Bound())
layers.ProjectToTile(requestedTile)
data, err := mvt.MarshalGzipped(layers)

mvt.MarshalGzipped(layers) fails with a panic.

If I add a Prune function, something along the lines of:

func (l *Layer) Prune() {
	feats := []*geojson.Feature{}
	for _, f := range l.Features {
		if f != nil && f.Geometry != nil {
			feats = append(feats, f)
		}
	}
	l.Features = feats
}

then the tiles marshal and render correctly.

Can I suggest or open a PR to provide a Prune function for both mvt.Layer and mvt.Layers?

@paulmach
Copy link
Owner

Can these features just be skipped on encoding?

@polastre
Copy link
Author

How would you propose skipping them in MarshalGzipped, which encodes the entire set?

@paulmach
Copy link
Owner

paulmach commented Jan 9, 2024

Nil features are "skipped" during encoding. Features clipped to empty/nil are remove from the features array during clipping #141

@paulmach
Copy link
Owner

I merged the above change, it is in v0.11.0

@polastre
Copy link
Author

Perfect, that's a great solution. Thanks @paulmach!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants