Skip to content

Commit 22fb0e5

Browse files
author
Casey Thomas
authored
Merge pull request #25 from jkirsch/master
Extend parser to also accept geojson formatted objects
2 parents 475aecf + fd1370e commit 22fb0e5

File tree

3 files changed

+43
-3
lines changed

3 files changed

+43
-3
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,27 @@ The valid geometry types are
264264
- `Polygon`
265265
- `MultiPolygon`
266266

267+
To parse already encoded GeoJSON use
268+
269+
`GeoJSON`
270+
271+
var data = [{name: 'Location A', geo: {"type": "Point", "coordinates": [125.6, 10.1]}}];
272+
273+
GeoJSON.parse(data, {GeoJSON: 'geo'});
274+
275+
"type": "FeatureCollection",
276+
"features": [{
277+
"type": "Feature",
278+
"geometry": {
279+
"type": "Point",
280+
"coordinates": [125.6, 10.1]
281+
},
282+
"properties": {
283+
"name": "Location A"
284+
}
285+
}]
286+
}
287+
267288
#### bbox, crs
268289

269290
geojson.js also supports the optional GeoJSON properties `bbox` and `crs`.

geojson.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
};
3535

3636
// Helper functions
37-
var geoms = ['Point', 'MultiPoint', 'LineString', 'MultiLineString', 'Polygon', 'MultiPolygon'],
37+
var geoms = ['Point', 'MultiPoint', 'LineString', 'MultiLineString', 'Polygon', 'MultiPolygon', 'GeoJSON'],
3838
geomAttrs = [];
3939

4040
// Adds default settings to user-specified params
@@ -144,8 +144,12 @@
144144

145145
// Geometry parameter specified as: {Point: 'coords'}
146146
if(typeof val === 'string' && item.hasOwnProperty(val)) {
147-
geom.type = gtype;
148-
geom.coordinates = item[val];
147+
if(gtype === 'GeoJSON') {
148+
geom = item[val]
149+
} else {
150+
geom.type = gtype;
151+
geom.coordinates = item[val];
152+
}
149153
}
150154

151155
// Geometry parameter specified as: {Point: ['lat', 'lng']}

test/test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,5 +382,20 @@ describe('GeoJSON', function() {
382382

383383
expect(function(){ GeoJSON.parse(data, options); }).to.not.throwException();
384384
});
385+
386+
it("accepts already formatted GeoJSON", function() {
387+
388+
var data = [{name: 'Location A', geo: {"type": "Point", "coordinates": [125.6, 10.1]}}];
389+
var output = GeoJSON.parse(data, {GeoJSON: 'geo'});
390+
391+
expect(output.type).to.be('FeatureCollection');
392+
expect(output.features).to.be.an('array');
393+
expect(output.features.length).to.be(1);
394+
expect(output.features[0].geometry.coordinates[0]).to.equal(125.6);
395+
expect(output.features[0].geometry.coordinates[1]).to.equal(10.1);
396+
expect(output.features[0].geometry.type).to.equal('Point');
397+
expect(output.features[0].properties.name).to.equal('Location A');
398+
399+
});
385400
});
386401
});

0 commit comments

Comments
 (0)