Comparing version 0.3.0 to 0.4.0
(function(GeoJSON) { | ||
GeoJSON.version = '0.3.0'; | ||
GeoJSON.version = '0.4.0'; | ||
@@ -11,3 +11,3 @@ // Allow user to specify default parameters | ||
var geojson = {"type": "FeatureCollection", "features": []}, | ||
var geojson, | ||
settings = applyDefaults(params, this.defaults), | ||
@@ -20,8 +20,12 @@ propFunc; | ||
objects.forEach(function(item){ | ||
geojson.features.push(getFeature(item, settings, propFunc)); | ||
}); | ||
if (Array.isArray(objects)) { | ||
geojson = {"type": "FeatureCollection", "features": []}; | ||
objects.forEach(function(item){ | ||
geojson.features.push(getFeature(item, settings, propFunc)); | ||
}); | ||
addOptionals(geojson, settings); | ||
} else { | ||
geojson = getFeature(objects, settings, propFunc); | ||
} | ||
addOptionals(geojson, settings); | ||
if (callback && typeof callback === 'function') { | ||
@@ -35,3 +39,3 @@ callback(geojson); | ||
// Helper functions | ||
var geoms = ['Point', 'MultiPoint', 'LineString', 'MultiLineString', 'Polygon', 'MultiPolygon'], | ||
var geoms = ['Point', 'MultiPoint', 'LineString', 'MultiLineString', 'Polygon', 'MultiPolygon', 'GeoJSON'], | ||
geomAttrs = []; | ||
@@ -146,4 +150,8 @@ | ||
if(typeof val === 'string' && item.hasOwnProperty(val)) { | ||
geom.type = gtype; | ||
geom.coordinates = item[val]; | ||
if(gtype === 'GeoJSON') { | ||
geom = item[val]; | ||
} else { | ||
geom.type = gtype; | ||
geom.coordinates = item[val]; | ||
} | ||
} | ||
@@ -154,3 +162,3 @@ | ||
geom.type = gtype; | ||
geom.coordinates = [item[val[1]], item[val[0]]]; | ||
geom.coordinates = [Number(item[val[1]]), Number(item[val[0]])]; | ||
} | ||
@@ -157,0 +165,0 @@ } |
@@ -1,3 +0,3 @@ | ||
// geojson.js - v0.3.0 | ||
// geojson.js - v0.4.0 | ||
// (c) 2016 Casey Thomas, MIT License | ||
!function(a){function b(a,b){var c=a||{};for(var d in b)b.hasOwnProperty(d)&&!c[d]&&(c[d]=b[d]);return c}function c(a,b){if(b.crs&&d(b.crs)&&(a.crs=b.crs),b.bbox&&(a.bbox=b.bbox),b.extraGlobal){a.properties={};for(var c in b.extraGlobal)a.properties[c]=b.extraGlobal[c]}}function d(a){if("name"===a.type){if(a.properties&&a.properties.name)return!0;throw new Error('Invalid CRS. Properties must contain "name" key')}if("link"===a.type){if(a.properties&&a.properties.href&&a.properties.type)return!0;throw new Error('Invalid CRS. Properties must contain "href" and "type" key')}throw new Error('Invald CRS. Type attribute must be "name" or "link"')}function e(a){a.geom={};for(var b in a)a.hasOwnProperty(b)&&-1!==k.indexOf(b)&&(a.geom[b]=a[b],delete a[b]);f(a.geom)}function f(a){for(var b in a)a.hasOwnProperty(b)&&("string"==typeof a[b]?l.push(a[b]):"object"==typeof a[b]&&(l.push(a[b][0]),l.push(a[b][1])));if(0===l.length)throw new Error("No geometry attributes specified")}function g(a,b,c){var d={type:"Feature"};return d.geometry=h(a,b),d.properties=c.call(a),d}function h(a,b){var c={};for(var d in b.geom){var e=b.geom[d];"string"==typeof e&&a.hasOwnProperty(e)?(c.type=d,c.coordinates=a[e]):Array.isArray(e)&&a.hasOwnProperty(e[0])&&a.hasOwnProperty(e[1])&&(c.type=d,c.coordinates=[a[e[1]],a[e[0]]])}return c}function i(a){var b;return a.exclude||a.include?a.include?b=function(b){a.include.forEach(function(a){b[a]=this[a]},this)}:a.exclude&&(b=function(b){for(var c in this)this.hasOwnProperty(c)&&-1===l.indexOf(c)&&-1===a.exclude.indexOf(c)&&(b[c]=this[c])}):b=function(a){for(var b in this)this.hasOwnProperty(b)&&-1===l.indexOf(b)&&(a[b]=this[b])},function(){var c={};return b.call(this,c),a.extra&&j(c,a.extra),c}}function j(a,b){for(var c in b)b.hasOwnProperty(c)&&(a[c]=b[c]);return a}a.version="0.3.0",a.defaults={},a.parse=function(a,d,f){var h,j={type:"FeatureCollection",features:[]},k=b(d,this.defaults);return l.length=0,e(k),h=i(k),a.forEach(function(a){j.features.push(g(a,k,h))}),c(j,k),f&&"function"==typeof f?void f(j):j};var k=["Point","MultiPoint","LineString","MultiLineString","Polygon","MultiPolygon"],l=[]}("object"==typeof module?module.exports:window.GeoJSON={}); | ||
!function(a){function b(a,b){var c=a||{};for(var d in b)b.hasOwnProperty(d)&&!c[d]&&(c[d]=b[d]);return c}function c(a,b){if(b.crs&&d(b.crs)&&(a.crs=b.crs),b.bbox&&(a.bbox=b.bbox),b.extraGlobal){a.properties={};for(var c in b.extraGlobal)a.properties[c]=b.extraGlobal[c]}}function d(a){if("name"===a.type){if(a.properties&&a.properties.name)return!0;throw new Error('Invalid CRS. Properties must contain "name" key')}if("link"===a.type){if(a.properties&&a.properties.href&&a.properties.type)return!0;throw new Error('Invalid CRS. Properties must contain "href" and "type" key')}throw new Error('Invald CRS. Type attribute must be "name" or "link"')}function e(a){a.geom={};for(var b in a)a.hasOwnProperty(b)&&-1!==k.indexOf(b)&&(a.geom[b]=a[b],delete a[b]);f(a.geom)}function f(a){for(var b in a)a.hasOwnProperty(b)&&("string"==typeof a[b]?l.push(a[b]):"object"==typeof a[b]&&(l.push(a[b][0]),l.push(a[b][1])));if(0===l.length)throw new Error("No geometry attributes specified")}function g(a,b,c){var d={type:"Feature"};return d.geometry=h(a,b),d.properties=c.call(a),d}function h(a,b){var c={};for(var d in b.geom){var e=b.geom[d];"string"==typeof e&&a.hasOwnProperty(e)?"GeoJSON"===d?c=a[e]:(c.type=d,c.coordinates=a[e]):Array.isArray(e)&&a.hasOwnProperty(e[0])&&a.hasOwnProperty(e[1])&&(c.type=d,c.coordinates=[Number(a[e[1]]),Number(a[e[0]])])}return c}function i(a){var b;return a.exclude||a.include?a.include?b=function(b){a.include.forEach(function(a){b[a]=this[a]},this)}:a.exclude&&(b=function(b){for(var c in this)this.hasOwnProperty(c)&&-1===l.indexOf(c)&&-1===a.exclude.indexOf(c)&&(b[c]=this[c])}):b=function(a){for(var b in this)this.hasOwnProperty(b)&&-1===l.indexOf(b)&&(a[b]=this[b])},function(){var c={};return b.call(this,c),a.extra&&j(c,a.extra),c}}function j(a,b){for(var c in b)b.hasOwnProperty(c)&&(a[c]=b[c]);return a}a.version="0.4.0",a.defaults={},a.parse=function(a,d,f){var h,j,k=b(d,this.defaults);return l.length=0,e(k),j=i(k),Array.isArray(a)?(h={type:"FeatureCollection",features:[]},a.forEach(function(a){h.features.push(g(a,k,j))}),c(h,k)):h=g(a,k,j),f&&"function"==typeof f?void f(h):h};var k=["Point","MultiPoint","LineString","MultiLineString","Polygon","MultiPolygon","GeoJSON"],l=[]}("object"==typeof module?module.exports:window.GeoJSON={}); |
{ | ||
"name": "geojson", | ||
"description": "Convert an array of geographic objects to GeoJSON", | ||
"description": "Turn your geo data into GeoJSON", | ||
"author": "Casey Thomas <caseypthomas@gmail.com>", | ||
"license": "MIT", | ||
"keywords": "geojson", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"main": "./geojson", | ||
@@ -9,0 +9,0 @@ "repository": { |
359
README.md
@@ -20,182 +20,212 @@ [![Build Status](https://travis-ci.org/caseypt/GeoJSON.js.svg?branch=master)](https://travis-ci.org/caseypt/GeoJSON.js) | ||
The library has one method, `parse`, which takes an array of objects with geometry data as the first parameter, an object consisting of settings for the second parameter, and an optional callback function as the third parameter. If a callback is not specified, the `parse` function returns the GeoJSON output. | ||
The library has one method, `parse`, which takes an array of objects (or a single object) with geometry data as the first parameter, an object consisting of settings for the second parameter, and an optional callback function as the third parameter. If a callback is not specified, the `parse` function returns the GeoJSON output. | ||
Take the example data below: | ||
var data = [ | ||
{ name: 'Location A', category: 'Store', street: 'Market', lat: 39.984, lng: -75.343 }, | ||
{ name: 'Location B', category: 'House', street: 'Broad', lat: 39.284, lng: -75.833 }, | ||
{ name: 'Location C', category: 'Office', street: 'South', lat: 39.123, lng: -74.534 } | ||
]; | ||
```javascript | ||
var data = [ | ||
{ name: 'Location A', category: 'Store', street: 'Market', lat: 39.984, lng: -75.343 }, | ||
{ name: 'Location B', category: 'House', street: 'Broad', lat: 39.284, lng: -75.833 }, | ||
{ name: 'Location C', category: 'Office', street: 'South', lat: 39.123, lng: -74.534 } | ||
]; | ||
``` | ||
Convert it to GeoJSON: | ||
GeoJSON.parse(data, {Point: ['lat', 'lng']}); | ||
{ | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ "type": "Feature", | ||
"geometry": {"type": "Point", "coordinates": [-75.343, 39.984]}, | ||
"properties": { | ||
"name": "Location A", | ||
"category": "Store" | ||
} | ||
}, | ||
{ "type": "Feature", | ||
"geometry": {"type": "Point", "coordinates": [-75.833, 39.284]}, | ||
"properties": { | ||
"name": "Location B", | ||
"category": "House" | ||
} | ||
}, | ||
{ "type": "Feature", | ||
"geometry": {"type": "Point", "coordinates": [ -75.534, 39.123]}, | ||
"properties": { | ||
"name": "Location C", | ||
"category": "Office" | ||
} | ||
} | ||
] | ||
```javascript | ||
GeoJSON.parse(data, {Point: ['lat', 'lng']}); | ||
{ | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ "type": "Feature", | ||
"geometry": {"type": "Point", "coordinates": [-75.343, 39.984]}, | ||
"properties": { | ||
"name": "Location A", | ||
"category": "Store" | ||
} | ||
}, | ||
{ "type": "Feature", | ||
"geometry": {"type": "Point", "coordinates": [-75.833, 39.284]}, | ||
"properties": { | ||
"name": "Location B", | ||
"category": "House" | ||
} | ||
}, | ||
{ "type": "Feature", | ||
"geometry": {"type": "Point", "coordinates": [ -75.534, 39.123]}, | ||
"properties": { | ||
"name": "Location C", | ||
"category": "Office" | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
Convert the example data to GeoJSON, and only include the `name` attribute in `properties` for each feature. | ||
GeoJSON.parse(data, {Point: ['lat', 'lng'], include: ['name']}); | ||
{ | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ "type": "Feature", | ||
"geometry": {"type": "Point", "coordinates": [-75.343, 39.984]}, | ||
"properties": { | ||
"name": "Location A" | ||
} | ||
}, | ||
... | ||
{ "type": "Feature", | ||
"geometry": {"type": "Point", "coordinates": [ -75.534, 39.123]}, | ||
"properties": { | ||
"name": "Location C" | ||
} | ||
} | ||
] | ||
```javascript | ||
GeoJSON.parse(data, {Point: ['lat', 'lng'], include: ['name']}); | ||
{ | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ "type": "Feature", | ||
"geometry": {"type": "Point", "coordinates": [-75.343, 39.984]}, | ||
"properties": { | ||
"name": "Location A" | ||
} | ||
}, | ||
... | ||
{ "type": "Feature", | ||
"geometry": {"type": "Point", "coordinates": [ -75.534, 39.123]}, | ||
"properties": { | ||
"name": "Location C" | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
You can also convert a single object to a GeoJSON feature: | ||
```javascript | ||
var singleobject = { name: 'Location A', category: 'Store', street: 'Market', lat: 39.984, lng: -75.343 } | ||
GeoJSON.parse(singleobject, {Point: ['lat', 'lng']}); | ||
{ | ||
"type": "Feature", | ||
"geometry": {"type": "Point", "coordinates": [-75.343, 39.984]}, | ||
"properties": { | ||
"name": "Location A", | ||
"category": "Store" | ||
} | ||
} | ||
``` | ||
The `parse` method can handle data with different geometry types. Consider the following sample data: | ||
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"} | ||
} | ||
]; | ||
```javascript | ||
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 geometric data | ||
GeoJSON.parse(data2, {'Point': ['x', 'y'], 'LineString': 'line', 'Polygon': 'polygon'}); | ||
```javascript | ||
GeoJSON.parse(data2, {'Point': ['x', 'y'], 'LineString': 'line', 'Polygon': 'polygon'}); | ||
{ | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [102,0.5] | ||
}, | ||
"properties": { | ||
"prop0": "value0" | ||
"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" | ||
} | ||
}, | ||
{ | ||
"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" | ||
} | ||
} | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
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 }]; | ||
```javascript | ||
var data1 = [{ name: 'Location A', street: 'Market', x: 34, y: -75 }]; | ||
var data2 = [{ name: 'Location B', date: '11/23/2012', x: 54, y: -98 }]; | ||
var data2 = [{ name: 'Location B', date: '11/23/2012', x: 54, y: -98 }]; | ||
GeoJSON.defaults = {Point: ['x', 'y'], include: ['name']}; | ||
GeoJSON.defaults = {Point: ['x', 'y'], include: ['name']}; | ||
GeoJSON.parse(data1, {}); | ||
GeoJSON.parse(data1, {}); | ||
{ | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [-75, 34] | ||
}, | ||
"properties": { | ||
"name": "Location A" | ||
} | ||
} | ||
] | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [-75, 34] | ||
}, | ||
"properties": { | ||
"name": "Location A" | ||
} | ||
} | ||
] | ||
} | ||
GeoJSON.parse(data2, {}); | ||
GeoJSON.parse(data2, {}); | ||
{ | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "FeatureCollection", | ||
"features": [ | ||
{ | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [-98, 54] | ||
}, | ||
"properties": { | ||
"name": "Location B" | ||
} | ||
} | ||
] | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [-98, 54] | ||
}, | ||
"properties": { | ||
"name": "Location B" | ||
} | ||
} | ||
] | ||
} | ||
``` | ||
You can specify a callback function as an option third parameter. | ||
GeoJSON.parse(data, {Point: ['lat', 'lng']}, function(geojson){ | ||
console.log(JSON.stringify(geojson)); | ||
}); | ||
```javascript | ||
GeoJSON.parse(data, {Point: ['lat', 'lng']}, function(geojson){ | ||
console.log(JSON.stringify(geojson)); | ||
}); | ||
``` | ||
@@ -229,5 +259,5 @@ ## Parameters | ||
The valid geometry types are | ||
The valid geometry types are | ||
- `Point` | ||
- `Point` | ||
- `MultiPoint` | ||
@@ -239,2 +269,23 @@ - `LineString` | ||
To parse already encoded GeoJSON use | ||
`GeoJSON` | ||
var data = [{name: 'Location A', geo: {"type": "Point", "coordinates": [125.6, 10.1]}}]; | ||
GeoJSON.parse(data, {GeoJSON: 'geo'}); | ||
"type": "FeatureCollection", | ||
"features": [{ | ||
"type": "Feature", | ||
"geometry": { | ||
"type": "Point", | ||
"coordinates": [125.6, 10.1] | ||
}, | ||
"properties": { | ||
"name": "Location A" | ||
} | ||
}] | ||
} | ||
#### bbox, crs | ||
@@ -262,3 +313,3 @@ | ||
{ | ||
{ | ||
"type": "FeatureCollection", | ||
@@ -268,3 +319,3 @@ "features": [ | ||
"geometry": {"type": "Point", "coordinates": [-75.343, 39.984]}, | ||
"properties": { | ||
"properties": { | ||
"name": "Location A", | ||
@@ -287,6 +338,6 @@ "category": "Store", | ||
GeoJSON.parse(data, { | ||
Point: ['lat', 'lng'], | ||
Point: ['lat', 'lng'], | ||
extraGlobal: { | ||
'Creator': 'Mr. Example', | ||
'records': data.length, | ||
'Creator': 'Mr. Example', | ||
'records': data.length, | ||
'summary': 'A few example points' | ||
@@ -296,3 +347,3 @@ } | ||
{ | ||
{ | ||
"type": "FeatureCollection", | ||
@@ -302,3 +353,3 @@ "features": [ | ||
"geometry": {"type": "Point", "coordinates": [-75.343, 39.984]}, | ||
"properties": { | ||
"properties": { | ||
"name": "Location A" | ||
@@ -310,3 +361,3 @@ } | ||
"geometry": {"type": "Point", "coordinates": [ -75.534, 39.123]}, | ||
"properties": { | ||
"properties": { | ||
"name": "Location C" | ||
@@ -313,0 +364,0 @@ } |
@@ -101,2 +101,14 @@ if (typeof window === 'undefined') { | ||
it('parses object to single feature', function() { | ||
var output = GeoJSON.parse(data[0], {Point: ['lat', 'lng']}); | ||
expect(output.type).to.be('Feature'); | ||
expect(output.geometry.type).to.be('Point'); | ||
expect(output.geometry.coordinates[1]).to.be(39.984); | ||
expect(output.geometry.coordinates[0]).to.be(-75.343); | ||
expect(output.properties.name).to.be('Location A'); | ||
expect(output.properties.category).to.be('Store'); | ||
expect(output.properties.street).to.be('Market'); | ||
}); | ||
it('parses data with different geometry types', function(){ | ||
@@ -374,3 +386,27 @@ // Based off example spec at http://geojson.org/geojson-spec.html | ||
}); | ||
it("accepts already formatted GeoJSON", function() { | ||
var data = [{name: 'Location A', geo: {"type": "Point", "coordinates": [125.6, 10.1]}}]; | ||
var output = GeoJSON.parse(data, {GeoJSON: 'geo'}); | ||
expect(output.type).to.be('FeatureCollection'); | ||
expect(output.features).to.be.an('array'); | ||
expect(output.features.length).to.be(1); | ||
expect(output.features[0].geometry.coordinates[0]).to.equal(125.6); | ||
expect(output.features[0].geometry.coordinates[1]).to.equal(10.1); | ||
expect(output.features[0].geometry.type).to.equal('Point'); | ||
expect(output.features[0].properties.name).to.equal('Location A'); | ||
}); | ||
it("converts string coordinates into numbers", function() { | ||
var data = [{ lat: '39.343', lng: '-74.454'}]; | ||
var output = GeoJSON.parse(data, {Point: ['lat', 'lng']}); | ||
output.features.forEach(function(feature) { | ||
expect(feature.geometry.coordinates[0]).to.be.a('number'); | ||
expect(feature.geometry.coordinates[1]).to.be.a('number'); | ||
}); | ||
}); | ||
}); | ||
}); |
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
37224
11
557
380