Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

geojson

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

geojson - npm Package Compare versions

Comparing version 0.0.4 to 0.0.5

27

geojson.js

@@ -1,2 +0,2 @@

exports.version = '0.0.4';
exports.version = '0.0.5';

@@ -39,6 +39,6 @@ exports.defaults = {};

for(var param in params) {
if(params.hasOwnProperty(param)) {
if(geoms.indexOf(param) !== -1 && param !== 'Point') {
if(params.hasOwnProperty(param) && geoms.indexOf(param) !== -1) {
if(typeof params[param] === 'string') {
geomAttrs.push(params[param]);
} else if (param === 'Point') {
} else if (typeof params[param] === 'object') { // Array of coordinates for Point
geomAttrs.push(params[param][0]);

@@ -98,16 +98,15 @@ geomAttrs.push(params[param][1]);

if(item.hasOwnProperty(attr) && geomAttrs.indexOf(attr) !== -1) {
if(attr === conf.geom.Point[0]) {
geom.type = "Point";
geom.coordinates = [item[conf.geom.Point[1]], item[conf.geom.Point[0]]];
for(var gtype in conf.geom) {
if(conf.geom.hasOwnProperty(gtype) &&
(attr === conf.geom[gtype] || attr === conf.geom[gtype][0])) {
geom.type = gtype;
return geom;
} else {
for(var gtype in conf.geom) {
if(conf.geom.hasOwnProperty(gtype) && attr === conf.geom[gtype]) {
geom.type = gtype;
if (typeof conf.geom[gtype] === 'string') {
geom.coordinates = item[conf.geom[gtype]];
} else { // Point with geom stored in two attributes
geom.coordinates = [item[conf.geom[gtype][1]], item[conf.geom[gtype][0]]];
}
return geom;
}
return geom;
}

@@ -114,0 +113,0 @@ }

@@ -5,3 +5,3 @@ {

"author": "Casey Thomas <c@cpt.ph>",
"version": "0.0.4",
"version": "0.0.5",
"main": "./geojson",

@@ -8,0 +8,0 @@ "repository": {

@@ -15,3 +15,5 @@ # node-geojson

Sample point-based data
The library has one method--`parse`.
Take the example data below:

@@ -42,5 +44,5 @@ var data = [

Specify only coordinate parameters
Convert it to GeoJSON:
GeoJSON.parse(data, {Point: ['lng', 'lat']});
GeoJSON.parse(data, {Point: ['lat', 'lng']});

@@ -73,5 +75,5 @@ { "type": "FeatureCollection",

Specify coordinate parameters and `include`
Convert the example data to GeoJSON, and only include the `name` attribute in `properties` for each feature.
GeoJSON.parse(data, {Point: ['lng', 'lat'], include: ['name']});
GeoJSON.parse(data, {Point: ['lat', 'lng'], include: ['name']});

@@ -96,6 +98,7 @@ { "type": "FeatureCollection",

Data with different geometry types
The `parse` method can handle data with different geometry types
var data2 = [
{ x: 0.5,
{
x: 0.5,
y: 102.0,

@@ -118,3 +121,3 @@ prop0: 'value0'

For each geometry type, specify which attribute contains the geometry data
For each geometry type, specify which attribute contains the geometric data

@@ -165,15 +168,28 @@ GeoJSON.parse(data2, {'Point': ['x', 'y'], 'LineString': 'line', 'Polygon': 'polygon'});

Depending on which makes more sense for your data, you either specify a list of fields to include, or a list of fields to exclude. If neither `include` nor `exclude` is set, all attributes (besides the attributes containing the geometry data) will be added as properties for each feature.
Depending on which makes more sense for your data, you either specify an array of attributes to include or exclude in `properties` for each feature. If neither `include` nor `exclude` is set, all the attributes (besides the attributes containing the geometry data) will be added to `properties`.
- `include` - Array of attributes to included in properties objects. All other fields will be ignored.
- `exclude` - Array of attributes to that shouldn't be included in properties object. All other fields will be added (besides geometry fields)
- `include` - Array of attributes to included in `properties` objects. All other fields will be ignored.
- `exclude` - Array of attributes to that shouldn't be included in `properties` object. All other fields will be added (besides geometry fields)
The geometry parameters specify which attribute contains the geographic/geometric data. A geometry parameter must be specified for each type of geometry object that is present in your data. For example, if your data contains both points and polygons, you must specify both the `Point` and `Polygon` parameters. *Note that geometry parameters must be in proper case.* See the [GeoJSON spec](http://geojson.org/geojson-spec.html) for details on each geometry type. The structure of the geometry parameter is:
The geometry parameters specify which attribute(s) contain(s) the geographic/geometric data. A geometry parameter must be specified for each type of geometry object that is present in your data. For example, if your data contains both points and polygons, you must specify both the `Point` and `Polygon` parameters. **Note that geometry parameters must be in proper case.** See the [GeoJSON spec](http://geojson.org/geojson-spec.html) for details on each geometry type. The structure of the geometry parameter is:
'ParameterName': 'attributeName'
ParameterName: 'attributeName'
Except for Point, which uses an array:
Except for `Point`, which can be specified with a field name or an array of field names, i.e:
'Point': ['lat', 'lng']
data = [{
name: 'location',
x: 34,
y: 85
}];
GeoJSON.parse(data, {Point: ['lat', 'lng']});
data = [{
name: 'location',
coords: [y, x]
}];
GeoJSON.parse(data, {Point: 'coords'});
The valid geometry types are `Point`, `MultiPoint`, `LineString`, `MultiLineString`, `Polygon`, and `MultiPolygon`.

@@ -10,5 +10,5 @@ var GeoJSON = require('./geojson');

category: 'Store',
street: 'Market',
lat: 39.984,
lng: -75.343
lng: -75.343,
street: 'Market'
},

@@ -18,5 +18,5 @@ {

category: 'House',
street: 'Broad',
lat: 39.284,
lng: -75.833
lng: -75.833,
street: 'Broad'
},

@@ -26,9 +26,9 @@ {

category: 'Office',
street: 'South',
lat: 39.123,
lng: -74.534
lng: -74.534,
street: 'South'
}
];
var output1 = GeoJSON.parse(data, {Point: ['lng', 'lat']});
var output1 = GeoJSON.parse(data, {Point: ['lat', 'lng']});

@@ -39,13 +39,16 @@ it('should return output with 3 features', function(){

it('should not include coords/geom fields in feature properties', function(){
assert.equal(output1.features[0].properties.lat, undefined, "Properties shoudn't have lat field");
assert.equal(output1.features[0].properties.lng, undefined, "Properties shoudn't have lng field");
it('should not include geometry fields in feature properties', function(){
assert.equal(output1.features[0].properties.lat, undefined, "Properties shoudn't have lat attribute");
assert.equal(output1.features[0].properties.lng, undefined, "Properties shoudn't have lng attribute");
});
it('should include all properties besides coords/geom fields when include or exclude isn\'t set', function() {
assert.notEqual(output1.features[0].properties.name, undefined, "Properties should have name field");
assert.notEqual(output1.features[0].properties.category, undefined, "Properties should have category field");
it('should include all properties besides geometry attributes when include or exclude isn\'t set', function() {
output1.features.forEach(function(feature){
assert.notEqual(feature.properties.name, undefined, "Properties should have name attribute");
assert.notEqual(feature.properties.category, undefined, "Properties should have category attribute");
assert.notEqual(feature.properties.street, undefined, "Properties should have street attribute");
});
});
var output2 = GeoJSON.parse(data, {Point: ['lng', 'lat'], include: ['name']});
var output2 = GeoJSON.parse(data, {Point: ['lat', 'lng'], include: ['name']});

@@ -57,3 +60,3 @@ it('should only include attributes that are listed in the include parameter', function(){

var output3 = GeoJSON.parse(data, {Point: ['lng', 'lat'], exclude: ['name']});
var output3 = GeoJSON.parse(data, {Point: ['lat', 'lng'], exclude: ['name']});

@@ -64,5 +67,30 @@ it('should only include attributes that not are listed in the exclude parameter', function(){

it('should be able to handle Point geom with x,y stored in one or two attributes', function(){
var twoAttrs = [{
name: 'test location',
y: -74,
x: 39.0
}];
var geoTwoAttrs = GeoJSON.parse(twoAttrs, {Point: ['x', 'y']});
assert.equal(geoTwoAttrs.features[0].geometry.coordinates[0], -74, 'Y value should match input value');
assert.equal(geoTwoAttrs.features[0].geometry.coordinates[1], 39.0, 'X value should match input value');
var oneAttr = [{
name: 'test location',
coords: [-74, 39],
foo: 'bar'
}];
var geoOneAttr = GeoJSON.parse(oneAttr, {Point: 'coords'});
assert.equal(geoOneAttr.features[0].geometry.coordinates[0], -74, 'Y value should match input value');
assert.equal(geoOneAttr.features[0].geometry.coordinates[1], 39.0, 'X value should match input value');
});
// Based off example spec at http://geojson.org/geojson-spec.html
var data2 = [
{ x: 0.5,
{
x: 0.5,
y: 102.0,

@@ -69,0 +97,0 @@ prop0: 'value0'

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc