Comparing version 0.0.5 to 0.0.6
120
geojson.js
@@ -1,2 +0,2 @@ | ||
exports.version = '0.0.5'; | ||
exports.version = '0.0.6'; | ||
@@ -6,9 +6,9 @@ exports.defaults = {}; | ||
exports.parse = function(objects, params) { | ||
var geojson = baseObj(); | ||
var conf = setConf(params, objects[0]); | ||
getGeomAttrList(params); | ||
var geojson = {"type": "FeatureCollection", "features": []}, | ||
settings = applyDefaults(params, this.defaults); | ||
setGeom(settings); | ||
objects.forEach(function(item){ | ||
var feature = buildFeature(item, conf); | ||
geojson.features.push(feature); | ||
geojson.features.push(getFeature(item, settings)); | ||
}); | ||
@@ -20,71 +20,48 @@ | ||
// Helper functions | ||
var geoms = [ | ||
'Point', | ||
'MultiPoint', | ||
'LineString', | ||
'MultiLineString', | ||
'Polygon', | ||
'MultiPolygon' | ||
]; | ||
var geoms = ['Point', 'MultiPoint', 'LineString', 'MultiLineString', 'Polygon', 'MultiPolygon'], | ||
geomAttrs = []; | ||
var geomAttrs = []; | ||
function applyDefaults(params, defaults) { | ||
var settings = params || {}; | ||
function baseObj() { | ||
return { | ||
"type": "FeatureCollection", | ||
"features": [] | ||
}; | ||
} | ||
function getGeomAttrList(params) { | ||
for(var param in params) { | ||
if(params.hasOwnProperty(param) && geoms.indexOf(param) !== -1) { | ||
if(typeof params[param] === 'string') { | ||
geomAttrs.push(params[param]); | ||
} else if (typeof params[param] === 'object') { // Array of coordinates for Point | ||
geomAttrs.push(params[param][0]); | ||
geomAttrs.push(params[param][1]); | ||
} | ||
for(var setting in defaults) { | ||
if(defaults.hasOwnProperty(setting) && !settings[setting]) { | ||
settings[setting] = defaults[setting]; | ||
} | ||
} | ||
} | ||
function getAttrList(params, item) { | ||
if (params.include) { | ||
return params.include; | ||
} else if (params.exclude) { | ||
var attrs = []; | ||
for(var attr in item) { | ||
if (item.hasOwnProperty(attr) && | ||
params.exclude.indexOf(attr) === -1 && | ||
geomAttrs.indexOf(attr) === -1) { | ||
attrs.push(attr); | ||
} | ||
} | ||
return attrs; | ||
} else { | ||
return 'all'; | ||
} | ||
return settings; | ||
} | ||
function setConf(params, item) { | ||
var conf = {}; | ||
conf.geom = {}; | ||
function setGeom(params) { | ||
params.geom = {}; | ||
for(var param in params) { | ||
if(params.hasOwnProperty(param) && geoms.indexOf(param) !== -1){ | ||
conf.geom[param] = params[param]; | ||
params.geom[param] = params[param]; | ||
delete params[param]; | ||
} | ||
} | ||
conf.attrs = getAttrList(params, item); | ||
setGeomAttrList(params.geom); | ||
} | ||
return conf; | ||
function setGeomAttrList(params) { | ||
for(var param in params) { | ||
if(params.hasOwnProperty(param)) { | ||
if(typeof params[param] === 'string') { | ||
geomAttrs.push(params[param]); | ||
} else if (typeof params[param] === 'object') { // Array of coordinates for Point | ||
geomAttrs.push(params[param][0]); | ||
geomAttrs.push(params[param][1]); | ||
} | ||
} | ||
} | ||
} | ||
function buildFeature(item, conf) { | ||
function getFeature(item, params) { | ||
var feature = { "type": "Feature" }; | ||
feature.geometry = buildGeom(item, conf); | ||
feature.properties = buildProps(item, conf); | ||
feature.geometry = buildGeom(item, params); | ||
feature.properties = buildProps(item, params); | ||
@@ -94,3 +71,3 @@ return feature; | ||
function buildGeom(item, conf) { | ||
function buildGeom(item, params) { | ||
var geom = {}; | ||
@@ -100,11 +77,10 @@ | ||
if(item.hasOwnProperty(attr) && geomAttrs.indexOf(attr) !== -1) { | ||
for(var gtype in conf.geom) { | ||
if(conf.geom.hasOwnProperty(gtype) && | ||
(attr === conf.geom[gtype] || attr === conf.geom[gtype][0])) { | ||
for(var gtype in params.geom) { | ||
if(params.geom.hasOwnProperty(gtype) && (attr === params.geom[gtype] || attr === params.geom[gtype][0])) { | ||
geom.type = gtype; | ||
if (typeof conf.geom[gtype] === 'string') { | ||
geom.coordinates = item[conf.geom[gtype]]; | ||
if (typeof params.geom[gtype] === 'string') { | ||
geom.coordinates = item[params.geom[gtype]]; | ||
} else { // Point with geom stored in two attributes | ||
geom.coordinates = [item[conf.geom[gtype][1]], item[conf.geom[gtype][0]]]; | ||
geom.coordinates = [item[params.geom[gtype][1]], item[params.geom[gtype][0]]]; | ||
} | ||
@@ -119,12 +95,18 @@ | ||
function buildProps(item, conf) { | ||
function buildProps(item, params) { | ||
var properties = {}; | ||
if (conf.attrs !== 'all') { | ||
conf.attrs.forEach(function(attr) { | ||
if (!params.exclude && !params.include) { | ||
for(var attr in item) { | ||
if(item.hasOwnProperty(attr) && (geomAttrs.indexOf(attr) === -1)) { | ||
properties[attr] = item[attr]; | ||
} | ||
} | ||
} else if (params.include) { | ||
params.include.forEach(function(attr){ | ||
properties[attr] = item[attr]; | ||
}); | ||
} else { // include or exclude not specified. Include all fields except geometry fields | ||
} else if (params.exclude) { | ||
for(var attr in item) { | ||
if(item.hasOwnProperty(attr) && (geomAttrs.indexOf(attr) === -1)) { | ||
if(item.hasOwnProperty(attr) && (geomAttrs.indexOf(attr) === -1) && (params.exclude.indexOf(attr) === -1)) { | ||
properties[attr] = item[attr]; | ||
@@ -131,0 +113,0 @@ } |
@@ -5,7 +5,7 @@ { | ||
"author": "Casey Thomas <c@cpt.ph>", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"main": "./geojson", | ||
"repository": { | ||
"type": "git", | ||
"url": "http://github.com/caseypt/node-geojson.git" | ||
"url": "http://github.com/caseypt/geojson.js.git" | ||
}, | ||
@@ -12,0 +12,0 @@ "dependencies": { |
@@ -1,2 +0,2 @@ | ||
# node-geojson | ||
# geojson.js | ||
@@ -15,3 +15,3 @@ Convert an array of objects with geometry to a [GeoJSON](http://geojson.org/) feature collection. | ||
The library has one method--`parse`. | ||
The library has one method, `parse`, which takes an array of objects with geometry data as the first parameter, and an object consisting of settings for the second parameter. | ||
@@ -96,3 +96,3 @@ Take the example data below: | ||
The `parse` method can handle data with different geometry types | ||
The `parse` method can handle data with different geometry types. Consider the following sample data: | ||
@@ -163,10 +163,64 @@ var data2 = [ | ||
You can also specify default settings if you will be parsing mutliple datasets with similiar attributes. | ||
var data1 = [{ | ||
name: 'Location A', | ||
street: 'Market', | ||
x: 34, | ||
y: -75 | ||
}]; | ||
var data2 = [{ | ||
name: 'Location B', | ||
date: '11/23/2012', | ||
x: 54, | ||
y: -98 | ||
}]; | ||
GeoJSON.defaults = {Point: ['x', 'y'], include: ['name']}; | ||
GeoJSON.parse(data1); | ||
{ | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [-75, 34] | ||
}, | ||
"properties": { | ||
"name": "Location A" | ||
} | ||
} | ||
] | ||
} | ||
GeoJSON.parse(data2); | ||
{ | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [-98, 54] | ||
}, | ||
"properties": { | ||
"name": "Location B" | ||
} | ||
} | ||
] | ||
} | ||
## Parameters | ||
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`. | ||
Depending on which makes more sense for the data that is being parsed, 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 feature `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 include in `properties` for each feature. All other fields will be ignored. | ||
- `exclude` - Array of attributes that shouldn't be included in feature `properties`. All other attributes will be added (besides geometry attributes) | ||
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: | ||
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 the data that is being parsed. For example, if the data contains both points and polygons, 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: | ||
@@ -187,3 +241,3 @@ ParameterName: 'attributeName' | ||
name: 'location', | ||
coords: [y, x] | ||
coords: [85, 34] | ||
}]; | ||
@@ -190,0 +244,0 @@ |
81
test.js
@@ -6,2 +6,4 @@ var GeoJSON = require('./geojson'); | ||
describe('#parse()', function(){ | ||
// Sample Data | ||
var data = [ | ||
@@ -31,15 +33,17 @@ { | ||
var output1 = GeoJSON.parse(data, {Point: ['lat', 'lng']}); | ||
var output = GeoJSON.parse(data, {Point: ['lat', 'lng']}); | ||
it('should return output with 3 features', function(){ | ||
assert.equal(output1.features.length, 3, 'Output should have 3 features'); | ||
assert.equal(output.features.length, 3, 'Output should have 3 features'); | ||
}); | ||
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"); | ||
output.features.forEach(function(feature){ | ||
assert.equal(feature.properties.lat, undefined, "Properties shouldn't have lat attribute"); | ||
assert.equal(feature.properties.lng, undefined, "Properties shouldn't have lng attribute"); | ||
}); | ||
}); | ||
it('should include all properties besides geometry attributes when include or exclude isn\'t set', function() { | ||
output1.features.forEach(function(feature){ | ||
output.features.forEach(function(feature){ | ||
assert.notEqual(feature.properties.name, undefined, "Properties should have name attribute"); | ||
@@ -51,13 +55,18 @@ assert.notEqual(feature.properties.category, undefined, "Properties should have category attribute"); | ||
var output2 = GeoJSON.parse(data, {Point: ['lat', 'lng'], include: ['name']}); | ||
it('should only include attributes that are listed in the include parameter', function(){ | ||
var output = GeoJSON.parse(data, {Point: ['lat', 'lng'], 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"); | ||
output.features.forEach(function(feature){ | ||
assert.equal(feature.properties.category, undefined, "Properites shouldn't have 'category' attribute"); | ||
assert.equal(feature.properties.street, undefined, "Properites shouldn't have 'category' attribute"); | ||
}); | ||
}); | ||
var output3 = GeoJSON.parse(data, {Point: ['lat', 'lng'], exclude: ['name']}); | ||
it('should only include attributes that not are listed in the exclude parameter', function(){ | ||
assert.equal(output3.features[0].properties.name, undefined, "Properites shouldn't have 'name' attribute"); | ||
var output = GeoJSON.parse(data, {Point: ['lat', 'lng'], exclude: ['name']}); | ||
output.features.forEach(function(feature){ | ||
assert.equal(feature.properties.name, undefined, "Properites shouldn't have 'name' attribute"); | ||
}); | ||
}); | ||
@@ -69,3 +78,4 @@ | ||
y: -74, | ||
x: 39.0 | ||
x: 39.0, | ||
foo: 'bar' | ||
}]; | ||
@@ -111,8 +121,8 @@ | ||
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'); | ||
var output = GeoJSON.parse(data2, {'Point': ['x', 'y'], 'LineString': 'line', 'Polygon': 'polygon'}); | ||
output4.features.forEach(function(feature){ | ||
assert.equal(output.features.length, 3, 'Output should have 3 features'); | ||
output.features.forEach(function(feature){ | ||
if(feature.geometry.type === 'Point') { | ||
@@ -142,3 +152,42 @@ assert.equal(feature.geometry.coordinates[1], 0.5, 'y coordinate should match input'); | ||
it('should use the default settings when they have been specified', function(){ | ||
GeoJSON.defaults = { | ||
Point: ['lat', 'lng'], | ||
include: ['name'] | ||
}; | ||
var output = GeoJSON.parse(data); | ||
output.features.forEach(function(feature){ | ||
assert.notEqual(feature.properties.name, undefined, "Properties should have name attribute"); | ||
assert.equal(feature.properties.lat, undefined, "Properties shouldn't have lat attribute"); | ||
assert.equal(feature.properties.lng, undefined, "Properties shouldn't have lng attribute"); | ||
assert.notEqual(feature.geometry.coordinates[0], undefined, "geometry.coordinates should have Y value"); | ||
assert.notEqual(feature.geometry.coordinates[1], undefined, "geometry.coordinates should have X value"); | ||
}); | ||
it('should only apply default settings that haven\'t been set in params', function(){ | ||
var output = GeoJSON.parse(data, {include: ['category', 'street']}); | ||
output.features.forEach(function(feature){ | ||
assert.equal(feature.properties.name, undefined, "Properties shouldn't 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"); | ||
}); | ||
}); | ||
it('shouldn\'t be affected from prior calls to parse that set params', function(){ | ||
var output = GeoJSON.parse(data); | ||
output.features.forEach(function(feature){ | ||
assert.notEqual(feature.properties.name, undefined, "Properties should have name attribute"); | ||
assert.equal(feature.properties.lat, undefined, "Properties shouldn't have lat attribute"); | ||
assert.equal(feature.properties.lng, undefined, "Properties shouldn't have lng attribute"); | ||
assert.notEqual(feature.geometry.coordinates[0], undefined, "geometry.coordinates should have Y value"); | ||
assert.notEqual(feature.geometry.coordinates[1], undefined, "geometry.coordinates should have X value"); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
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
19461
246
243
254134