Skip to content

encoding/mvt fails on nil features #135

@polastre

Description

@polastre

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions