Skip to content

Commit

Permalink
Attempt at resolving mapbox#55 : allow serialization and deserializat…
Browse files Browse the repository at this point in the history
…ion of null feature geometry (mapbox#94)
  • Loading branch information
ivorblockley authored and mourner committed Jul 27, 2018
1 parent 1f3fd41 commit 07d7f1a
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 3 deletions.
4 changes: 3 additions & 1 deletion decode.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ function readFeatureCollection(pbf, obj) {

function readFeature(pbf, feature) {
feature.type = 'Feature';
return pbf.readMessage(readFeatureField, feature);
var f = pbf.readMessage(readFeatureField, feature);
if (!f.hasOwnProperty('geometry')) f.geometry = null;
return f;
}

function readGeometry(pbf, geom) {
Expand Down
4 changes: 2 additions & 2 deletions encode.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ function analyze(obj) {
for (i = 0; i < obj.features.length; i++) analyze(obj.features[i]);

} else if (obj.type === 'Feature') {
analyze(obj.geometry);
if (obj.geometry !== null) analyze(obj.geometry);
for (key in obj.properties) saveKey(key);

} else if (obj.type === 'Point') analyzePoint(obj.coordinates);
Expand Down Expand Up @@ -98,7 +98,7 @@ function writeFeatureCollection(obj, pbf) {
}

function writeFeature(feature, pbf) {
pbf.writeMessage(1, writeGeometry, feature.geometry);
if (feature.geometry !== null) pbf.writeMessage(1, writeGeometry, feature.geometry);

if (feature.id !== undefined) {
if (typeof feature.id === 'number' && feature.id % 1 === 0) pbf.writeSVarintField(12, feature.id);
Expand Down
25 changes: 25 additions & 0 deletions test/fixtures/issue55.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"type": "FeatureCollection", "features":
[{
"type": "Feature",
"properties": {"miles hiked ": 12},
"geometry": {
"type": "LineString",
"coordinates": [
[-1.1, 2.1],
[2, -999.9]
]
}
},{
"type": "Feature",
"properties": {" profit": -999.9},
"geometry": null
},{
"type": "Feature",
"properties": {"miles hiked ": 13.1},
"geometry": {
"type": "Point",
"coordinates": [0.0001, -0.0001]
}
}]
}
1 change: 1 addition & 0 deletions test/validate.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ for (var name in geojsonFixtures) {
test('roundtrip issue', roundtripTest(getJSON('issue62.json')));

test('roundtrip custom properties', roundtripTest(getJSON('props.json')));
test('roundtrip issue55', roundtripTest(getJSON('issue55.json')));
test('roundtrip issue90', roundtripTest(getJSON('issue90.json')));
test('roundtrip single-ring MultiPolygon', roundtripTest(getJSON('single-multipoly.json')));

Expand Down

0 comments on commit 07d7f1a

Please sign in to comment.