Skip to content
Open
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
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,39 @@ You can also add dataset properties using the `extraGlobal` param. The value for
}
}

#### removeInvalidGeometries

If passing in a list of data, you can optionally exclude invalid geometries by setting the `removeInvalidGeometries` flag to `true`. This utilizes the `GeoJSON.isGeometryValid` function.

```javascript
GeoJSON.parse(data, {
Point: ['lat', 'lng'],
removeInvalidGeometries: true
});
```

#### isGeometryValid

If you would like to provide your own validation for geometries, you can include your own `isGeometryValid` validation function. This will be used instead of the `GeoJSON.isGeometryValid` function.

```javascript
GeoJSON.parse(data, {
Point: ['lat', 'lng'],
removeInvalidGeometries: true,
isGeometryValid: customValidationFunction
});
```

It may be easier to provide this function as a default, as mentioned above. For example:

```javascript
GeoJSON.defaults.isGeometryValid = function(geometry){
// Validation logic
};
...
GeoJSON.parse(data, {Point: ['lat', 'lng']});
```

## Tests

For node, `$ npm test`.
Expand Down
135 changes: 99 additions & 36 deletions geojson.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
GeoJSON.defaults = {
doThrows: {
invalidGeometry: false
}
},
removeInvalidGeometries: false
};

function InvalidGeometryError() {
Expand Down Expand Up @@ -46,7 +47,10 @@
if (Array.isArray(objects)) {
geojson = {"type": "FeatureCollection", "features": []};
objects.forEach(function(item){
geojson.features.push(getFeature({item:item, params: settings, propFunc:propFunc}));
var feature = getFeature({item:item, params: settings, propFunc:propFunc});
if (settings.removeInvalidGeometries !== true || isValid(feature.geometry, settings.isGeometryValid)) {
geojson.features.push(feature);
}
});
addOptionals(geojson, settings);
} else {
Expand Down Expand Up @@ -175,19 +179,49 @@
// Assembles the `geometry` property
// for the feature output
function buildGeom(item, params) {
var geom = {},
var geom,
attr;

for(var gtype in params.geom) {
var val = params.geom[gtype];
var coordinates = [];
var itemClone;
var paths;
// If we've already found a matching geometry, stop the loop.
if (geom !== undefined && geom !== false) {
break;
}

// Geometry parameter specified as: {Point: 'coords'}
if(typeof val === 'string' && item.hasOwnProperty(val)) {
if(gtype === 'GeoJSON') {
geom = item[val];
} else {
geom.type = gtype;
geom.coordinates = item[val];
geom = {
type: gtype,
coordinates: item[val]
};
}
}

// Geometry parameter specified as: {Point: 'geo.coords'}
else if(typeof val === 'string' && isNested(val)) {
geom = undefined;
paths = val.split('.');
itemClone = item;
for (var m = 0; m < paths.length; m++) {
if (itemClone == undefined || !itemClone.hasOwnProperty(paths[m])) {
m = paths.length;
geom = false;
} else {
itemClone = itemClone[paths[m]]; // Iterate deeper into the object
}
}
if (geom !== false) {
geom = {
type: gtype,
coordinates: itemClone
};
}
}

Expand All @@ -204,69 +238,90 @@
var newItem = item[key];
return buildGeom(newItem, {geom:{ Point: order}});
});
geom.type = gtype;
/*jshint loopfunc: true */
geom.coordinates = [].concat(points.map(function(p){
return p.coordinates;
}));
geom = {
type: gtype,
/*jshint loopfunc: true */
coordinates: [].concat(points.map(function(p){
return p.coordinates;
}))
};
}

// Geometry parameter specified as: {Point: ['lat', 'lng', 'alt']}
else if(Array.isArray(val) && item.hasOwnProperty(val[0]) && item.hasOwnProperty(val[1]) && item.hasOwnProperty(val[2])){
geom.type = gtype;
geom.coordinates = [Number(item[val[1]]), Number(item[val[0]]), Number(item[val[2]])];
geom = {
type: gtype,
coordinates: [ Number(item[val[1]]), Number(item[val[0]]), Number(item[val[2]]) ]
};
}

// Geometry parameter specified as: {Point: ['lat', 'lng']}
else if(Array.isArray(val) && item.hasOwnProperty(val[0]) && item.hasOwnProperty(val[1])){
geom.type = gtype;
geom.coordinates = [Number(item[val[1]]), Number(item[val[0]])];
geom = {
type: gtype,
coordinates: [Number(item[val[1]]), Number(item[val[0]])]
};
}

// 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 = [];
geom = undefined;
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])) {
return false;
if (itemClone == undefined || !itemClone.hasOwnProperty(paths[j])) {
i = val.length;
j = paths.length;
geom = false;
} else {
itemClone = itemClone[paths[j]]; // Iterate deeper into the object
}
itemClone = itemClone[paths[j]]; // Iterate deeper into the object
}
coordinates[i] = itemClone;
}
geom.type = gtype;
geom.coordinates = [Number(coordinates[1]), Number(coordinates[0]), Number(coordinates[2])];
if (geom !== false) {
geom = {
type: gtype,
coordinates: [Number(coordinates[1]), Number(coordinates[0]), Number(coordinates[2])]
};
}
}

// 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])) {
return false;
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])) {
k = val.length;
l = paths.length;
geom = false;
} else {
itemClone = itemClone[paths[l]]; // Iterate deeper into the object
}
itemClone = itemClone[paths[j]]; // Iterate deeper into the object
}
coordinates[i] = itemClone;
coordinates[k] = itemClone;
}
if (geom !== false) {
geom = {
type: gtype,
coordinates: [Number(coordinates[1]), Number(coordinates[0])]
};
}
geom.type = gtype;
geom.coordinates = [Number(coordinates[1]), Number(coordinates[0])];
}

// Geometry parameter specified as: {Point: [{coordinates: [lat, lng]}]}
else if (Array.isArray(val) && val[0].constructor.name === 'Object' && Object.keys(val[0])[0] === 'coordinates'){
geom.type = gtype;
geom.coordinates = [Number(item.coordinates[(val[0].coordinates).indexOf('lng')]), Number(item.coordinates[(val[0].coordinates).indexOf('lat')])];
geom = {
type: gtype,
coordinates: [Number(item.coordinates[(val[0].coordinates).indexOf('lng')]), Number(item.coordinates[(val[0].coordinates).indexOf('lat')])]
};
}
}

if(params.doThrows && params.doThrows.invalidGeometry && !GeoJSON.isGeometryValid(geom)){
if(params.doThrows && params.doThrows.invalidGeometry && !isValid(geom, params.isGeometryValid)){
throw new InvalidGeometryError(item, params);
}

Expand Down Expand Up @@ -324,4 +379,12 @@
return properties;
}

function isValid(geometry, custom) {
if (typeof custom === 'function') {
return custom(geometry);
} else {
return GeoJSON.isGeometryValid(geometry);
}
}

}(typeof module == 'object' ? module.exports : window.GeoJSON = {}));
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.

Loading