Comparing version 0.0.1 to 0.0.4
100
geojson.js
@@ -1,2 +0,2 @@ | ||
exports.version = '0.0.1'; | ||
exports.version = '0.0.4'; | ||
@@ -8,2 +8,3 @@ exports.defaults = {}; | ||
var conf = setConf(params, objects[0]); | ||
getGeomAttrList(params); | ||
@@ -19,2 +20,13 @@ objects.forEach(function(item){ | ||
// Helper functions | ||
var geoms = [ | ||
'Point', | ||
'MultiPoint', | ||
'LineString', | ||
'MultiLineString', | ||
'Polygon', | ||
'MultiPolygon' | ||
]; | ||
var geomAttrs = []; | ||
function baseObj() { | ||
@@ -27,2 +39,15 @@ return { | ||
function getGeomAttrList(params) { | ||
for(var param in params) { | ||
if(params.hasOwnProperty(param)) { | ||
if(geoms.indexOf(param) !== -1 && param !== 'Point') { | ||
geomAttrs.push(params[param]); | ||
} else if (param === 'Point') { | ||
geomAttrs.push(params[param][0]); | ||
geomAttrs.push(params[param][1]); | ||
} | ||
} | ||
} | ||
} | ||
function getAttrList(params, item) { | ||
@@ -34,3 +59,5 @@ if (params.include) { | ||
for(var attr in item) { | ||
if (item.hasOwnProperty(attr) && params.exclude.indexOf(attr) === -1) { | ||
if (item.hasOwnProperty(attr) && | ||
params.exclude.indexOf(attr) === -1 && | ||
geomAttrs.indexOf(attr) === -1) { | ||
attrs.push(attr); | ||
@@ -46,27 +73,60 @@ } | ||
function setConf(params, item) { | ||
return { | ||
x: params.point[1], | ||
y: params.point[0], | ||
attrs: getAttrList(params, item) | ||
}; | ||
var conf = {}; | ||
conf.geom = {}; | ||
for(var param in params) { | ||
if(params.hasOwnProperty(param) && geoms.indexOf(param) !== -1){ | ||
conf.geom[param] = params[param]; | ||
} | ||
} | ||
conf.attrs = getAttrList(params, item); | ||
return conf; | ||
} | ||
function buildFeature(item, conf) { | ||
var feature = { | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [Number(item[conf.y]), Number(item[conf.x])] | ||
}, | ||
"properties" : {} | ||
}; | ||
var feature = { "type": "Feature" }; | ||
feature.geometry = buildGeom(item, conf); | ||
feature.properties = buildProps(item, conf); | ||
return feature; | ||
} | ||
function buildGeom(item, conf) { | ||
var geom = {}; | ||
for(var attr in item) { | ||
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]]]; | ||
return geom; | ||
} else { | ||
for(var gtype in conf.geom) { | ||
if(conf.geom.hasOwnProperty(gtype) && attr === conf.geom[gtype]) { | ||
geom.type = gtype; | ||
geom.coordinates = item[conf.geom[gtype]]; | ||
} | ||
} | ||
return geom; | ||
} | ||
} | ||
} | ||
} | ||
function buildProps(item, conf) { | ||
var properties = {}; | ||
if (conf.attrs !== 'all') { | ||
conf.attrs.forEach(function(attr) { | ||
feature.properties[attr] = item[attr]; | ||
properties[attr] = item[attr]; | ||
}); | ||
} else { // include or exclude not specified. Include all fields except geom/coords | ||
} else { // include or exclude not specified. Include all fields except geometry fields | ||
for(var attr in item) { | ||
if(item.hasOwnProperty(attr) && (attr !== conf.x && attr !==conf.y)) { | ||
feature.properties[attr] = item[attr]; | ||
if(item.hasOwnProperty(attr) && (geomAttrs.indexOf(attr) === -1)) { | ||
properties[attr] = item[attr]; | ||
} | ||
@@ -76,3 +136,3 @@ } | ||
return feature; | ||
return properties; | ||
} |
@@ -5,3 +5,3 @@ { | ||
"author": "Casey Thomas <c@cpt.ph>", | ||
"version": "0.0.1", | ||
"version": "0.0.4", | ||
"main": "./geojson", | ||
@@ -8,0 +8,0 @@ "repository": { |
# node-geojson | ||
Convert an array of objects with coordinates/geometry to a [GeoJSON](http://geojson.org/) feature collection. Only works with point data at the moment. | ||
Convert an array of objects with geometry to a [GeoJSON](http://geojson.org/) feature collection. | ||
@@ -15,3 +15,3 @@ ## Installation | ||
Sample Data | ||
Sample point-based data | ||
@@ -22,2 +22,3 @@ var data = [ | ||
category: 'Store', | ||
street: 'Market', | ||
lat: 39.984, | ||
@@ -29,2 +30,3 @@ lng: -75.343 | ||
category: 'House', | ||
street: 'Broad', | ||
lat: 39.284, | ||
@@ -36,2 +38,3 @@ lng: -75.833 | ||
category: 'Office', | ||
street: 'South' | ||
lat: 39.123, | ||
@@ -44,3 +47,3 @@ lng: -74.534 | ||
GeoJSON.parse(data, { point: ['lng', 'lat'] }); | ||
GeoJSON.parse(data, {Point: ['lng', 'lat']}); | ||
@@ -75,3 +78,3 @@ { "type": "FeatureCollection", | ||
GeoJSON.parse(data, { point: ['lng', 'lat'], include: ['name']}); | ||
GeoJSON.parse(data, {Point: ['lng', 'lat'], include: ['name']}); | ||
@@ -96,6 +99,82 @@ { "type": "FeatureCollection", | ||
Data with different geometry types | ||
var data2 = [ | ||
{ x: 0.5, | ||
y: 102.0, | ||
prop0: 'value0' | ||
}, | ||
{ | ||
line: [[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]], | ||
prop0: 'value0', | ||
prop1: 0.0 | ||
}, | ||
{ | ||
polygon: [ | ||
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ] | ||
], | ||
prop0: 'value0', | ||
prop1: {"this": "that"} | ||
} | ||
]; | ||
For each geometry type, specify which attribute contains the geometry data | ||
GeoJSON.parse(data2, {'Point': ['x', 'y'], 'LineString': 'line', 'Polygon': 'polygon'}); | ||
{ | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [102,0.5] | ||
}, | ||
"properties": { | ||
"prop0": "value0" | ||
} | ||
}, | ||
{ | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "LineString", | ||
"coordinates": [[102, 0], [103, 1], [104, 0],[105, 1]] | ||
}, | ||
"properties": { | ||
"prop0": "value0", | ||
"prop1": 0 | ||
} | ||
}, | ||
{ | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Polygon", | ||
"coordinates": [[[100, 0], [101, 0], [101, 1], [100, 1], [100, 0]]] | ||
}, | ||
"properties": { | ||
"prop0": "value0", | ||
"prop1": { | ||
"this": "that" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
## Parameters | ||
- `point` - array of field names that contain x and y coordinates. Example: `point: [lat, lng]` | ||
- `include` - List of fields to include as properties. All other fields will be ignored | ||
- `exclude` - List of fields to that shouldn't be included in properties. All other fields will be added (besides geometry fields) | ||
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. | ||
- `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: | ||
'ParameterName': 'attributeName' | ||
Except for Point, which uses an array: | ||
'Point': ['lat', 'lng'] | ||
The valid geometry types are `Point`, `MultiPoint`, `LineString`, `MultiLineString`, `Polygon`, and `MultiPolygon`. |
60
test.js
@@ -10,2 +10,3 @@ var GeoJSON = require('./geojson'); | ||
category: 'Store', | ||
street: 'Market', | ||
lat: 39.984, | ||
@@ -17,2 +18,3 @@ lng: -75.343 | ||
category: 'House', | ||
street: 'Broad', | ||
lat: 39.284, | ||
@@ -24,2 +26,3 @@ lng: -75.833 | ||
category: 'Office', | ||
street: 'South', | ||
lat: 39.123, | ||
@@ -30,3 +33,3 @@ lng: -74.534 | ||
var output1 = GeoJSON.parse(data, {point: ['lng', 'lat']}); | ||
var output1 = GeoJSON.parse(data, {Point: ['lng', 'lat']}); | ||
@@ -47,9 +50,10 @@ it('should return output with 3 features', function(){ | ||
var output2 = GeoJSON.parse(data, {point: ['lng', 'lat'], include: ['name']}); | ||
var output2 = GeoJSON.parse(data, {Point: ['lng', 'lat'], include: ['name']}); | ||
it('should only include attributes that are listed in the include parameter', function(){ | ||
assert.equal(output2.features[0].properties.category, undefined, "Properites shouldn't have 'category' attribute"); | ||
assert.equal(output2.features[1].properties.street, undefined, "Properites shouldn't have 'category' attribute"); | ||
}); | ||
var output3 = GeoJSON.parse(data, {point: ['lng', 'lat'], exclude: ['name']}); | ||
var output3 = GeoJSON.parse(data, {Point: ['lng', 'lat'], exclude: ['name']}); | ||
@@ -60,3 +64,53 @@ it('should only include attributes that not are listed in the exclude parameter', function(){ | ||
// Based off example spec at http://geojson.org/geojson-spec.html | ||
var data2 = [ | ||
{ x: 0.5, | ||
y: 102.0, | ||
prop0: 'value0' | ||
}, | ||
{ | ||
line: [[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]], | ||
prop0: 'value0', | ||
prop1: 0.0 | ||
}, | ||
{ | ||
polygon: [ | ||
[ [100.0, 0.0], [101.0, 0.0], [101.0, 1.0], [100.0, 1.0], [100.0, 0.0] ] | ||
], | ||
prop0: 'value0', | ||
prop1: {"this": "that"} | ||
} | ||
]; | ||
var output4 = GeoJSON.parse(data2, {'Point': ['x', 'y'], 'LineString': 'line', 'Polygon': 'polygon'}); | ||
it('should be able to handle data with different geometry types', function(){ | ||
assert.equal(output4.features.length, 3, 'Output should have 3 features'); | ||
output4.features.forEach(function(feature){ | ||
if(feature.geometry.type === 'Point') { | ||
assert.equal(feature.geometry.coordinates[1], 0.5, 'y coordinate should match input'); | ||
assert.equal(feature.geometry.coordinates[0], 102, 'y coordinate should match input'); | ||
assert.equal(feature.properties.prop0, "value0", 'Property prop0 should match input value of value0'); | ||
} else if (feature.geometry.type === 'LineString') { | ||
assert.equal(feature.geometry.coordinates.length, 4, 'Output should have same number of points as input'); | ||
assert.equal(feature.geometry.coordinates[0][1], 0, 'First y coordinate should match input'); | ||
assert.equal(feature.geometry.coordinates[0][0], 102, 'First x coordinate should match input'); | ||
assert.equal(feature.geometry.coordinates[3][1], 1, 'Last y coordinate should match input'); | ||
assert.equal(feature.geometry.coordinates[3][0], 105, 'Last x coordinate should match input'); | ||
assert.equal(feature.properties.prop0, "value0", 'Property prop0 should match input value of value0'); | ||
assert.equal(feature.properties.prop1, 0, 'Property prop1 should match input value of 0'); | ||
} else if (feature.geometry.type === 'Polygon') { | ||
assert.equal(feature.geometry.coordinates[0].length, 5, 'Output should have same number of points as input'); | ||
assert.equal(feature.geometry.coordinates[0][0][1], 0, 'First y coordinate should match input'); | ||
assert.equal(feature.geometry.coordinates[0][0][0], 100, 'First x coordinate should match input'); | ||
assert.equal(feature.geometry.coordinates[0][4][1], 0, 'Last y coordinate should match input'); | ||
assert.equal(feature.geometry.coordinates[0][4][0], 100, 'Last x coordinate should match input'); | ||
assert.equal(feature.properties.prop0, "value0", 'Property prop0 should match input value of value0'); | ||
assert.equal(feature.properties.prop1['this'], 'that', 'Property prop1.this should match input value of that'); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
14357
203
173