Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions geojson.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@

for(var gtype in params.geom) {
var val = params.geom[gtype];
var coordinates = [];
var itemClone;
var paths;

// Geometry parameter specified as: {Point: 'coords'}
if(typeof val === 'string' && item.hasOwnProperty(val)) {
Expand Down Expand Up @@ -225,12 +228,11 @@

// Geometry parameter specified as: {Point: ['container.lat', 'container.lng', 'container.alt']}
else if(Array.isArray(val) && isNested(val[0]) && isNested(val[1]) && isNested(val[2])){
var coordinates = [];
for (var i = 0; i < val.length; i++) { // i.e. 0 and 1
var paths = val[i].split('.');
var itemClone = item;
paths = val[i].split('.');
itemClone = item;
for (var j = 0; j < paths.length; j++) {
if (!itemClone.hasOwnProperty(paths[j])) {
if (itemClone == undefined || !itemClone.hasOwnProperty(paths[j])) {
return false;
}
itemClone = itemClone[paths[j]]; // Iterate deeper into the object
Expand All @@ -243,17 +245,16 @@

// Geometry parameter specified as: {Point: ['container.lat', 'container.lng']}
else if(Array.isArray(val) && isNested(val[0]) && isNested(val[1])){
var coordinates = [];
for (var i = 0; i < val.length; i++) { // i.e. 0 and 1
var paths = val[i].split('.');
var itemClone = item;
for (var j = 0; j < paths.length; j++) {
if (!itemClone.hasOwnProperty(paths[j])) {
for (var k = 0; k < val.length; k++) { // i.e. 0 and 1
paths = val[k].split('.');
itemClone = item;
for (var l = 0; l < paths.length; l++) {
if (itemClone == undefined || !itemClone.hasOwnProperty(paths[l])) {
return false;
}
itemClone = itemClone[paths[j]]; // Iterate deeper into the object
itemClone = itemClone[paths[l]]; // Iterate deeper into the object
}
coordinates[i] = itemClone;
coordinates[k] = itemClone;
}
geom.type = gtype;
geom.coordinates = [Number(coordinates[1]), Number(coordinates[0])];
Expand Down
5 changes: 3 additions & 2 deletions geojson.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,57 @@ describe('GeoJSON', function() {
});
});

it('can accept nested arguments for Point', function(done) {
var data = [
{ name: 'Location A', category: 'Store', location: { point: { lat: 39.984, lng: -75.343 } } }
];

GeoJSON.parse(data, { Point: ['location.point.lat', 'location.point.lng'] }, function(geojson) {
expect(geojson.type).to.be('FeatureCollection');
expect(geojson.features).to.be.an('array');
expect(geojson.features.length).to.be(1);
expect(geojson.features[0].geometry.coordinates[0]).to.equal(-75.343);
expect(geojson.features[0].geometry.coordinates[1]).to.equal(39.984);
done();
});
});

it('can handle null or undefined values when parsing nested arguments', function(done) {
var data = [
{ geo: null },
{ geo: { line: [[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0] ] } },
{ geo: { polygon: [ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ] } },
{ geo: { multipoint: [ [100.0, 0.0], [101.0, 1.0] ] } },
{ geo: { multipolygon: [
[[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]],
[[[100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0]],
[[100.2, 0.2], [100.8, 0.2], [100.8, 0.8], [100.2, 0.8], [100.2, 0.2]]]
]}},
{ geo: { multilinestring: [ [ [100.0, 0.0], [101.0, 1.0] ], [ [102.0, 2.0], [103.0, 3.0] ] ] } }
];

GeoJSON.parse(data, {
Point: ['geo.nope.point.lng', 'geo.nope.point.lat'],
LineString: 'geo.nope.line',
Polygon: 'geo.nope.polygon',
MultiPoint: 'geo.nope.multipoint',
MultiPolygon: 'geo.nope.multipolygon',
MultiLineString: 'geo.nope.multilinestring'
}, function(geojson) {
expect(geojson.type).to.be('FeatureCollection');
expect(geojson.features).to.be.an('array');
expect(geojson.features.length).to.be(6);
expect(geojson.features[0].geometry).to.be(false);
expect(geojson.features[1].geometry).to.be(false);
expect(geojson.features[2].geometry).to.be(false);
expect(geojson.features[3].geometry).to.be(false);
expect(geojson.features[4].geometry).to.be(false);
expect(geojson.features[5].geometry).to.be(false);
done();
});

});

it('can accept up to three arguments for Point in a nested structure', function(done) {
var data = [
{ name: 'Location A', category: 'Store', location: { lat: 39.984, lng: -75.343, alt: 22026.46 }, street: 'Market' },
Expand Down