mapbox / geobuf

A compact binary encoding for geographic data.
ISC License
967 stars 84 forks source link

Improve handling default enum values #101

Closed claws closed 4 years ago

claws commented 5 years ago

This merge request provides a fix for the problems highlighted in #77.

By default, Protobuf libraries do not encode first enum values. If the value is missing when decoded, those libraries assume the first enum value as default. According to https://developers.google.com/protocol-buffers/docs/proto3#default:

"For enums, the default value is the first defined enum value, which must be 0."

The problem is observed when trying to pass valid PBFs to the decode function that were not first encoded by geobuf.js but by another library (e.g. pygeobuf). The readCoords function is checking for type, which if it's a POINT, is undefined. Obviously, this impacts anything expecting valid GeoJSON.

The update to the readGeometry function adds behaviour similar to the functions above it (readFeatureCollection and readFeature) which set a default value. This solves the symptom observed in the readCoords function (captured in #77) by ensuring that the geom.type is not undefined.

AlexGreg commented 5 years ago

can it be merged?