diff --git a/README.md b/README.md index c3c9bb7..d87d91b 100644 --- a/README.md +++ b/README.md @@ -17,10 +17,10 @@ In the browser, the library is available at `GeoJSON`. ## Example Usage -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: - + ```javascript var data = [ { name: 'Location A', category: 'Store', street: 'Market', lat: 39.984, lng: -75.343 }, @@ -30,30 +30,30 @@ var data = [ ``` Convert it to GeoJSON: - + ```javascript GeoJSON.parse(data, {Point: ['lat', 'lng']}); -{ +{ "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": {"type": "Point", "coordinates": [-75.343, 39.984]}, - "properties": { + "properties": { "name": "Location A", "category": "Store" } }, { "type": "Feature", "geometry": {"type": "Point", "coordinates": [-75.833, 39.284]}, - "properties": { + "properties": { "name": "Location B", "category": "House" } }, { "type": "Feature", "geometry": {"type": "Point", "coordinates": [ -75.534, 39.123]}, - "properties": { + "properties": { "name": "Location C", "category": "Office" } @@ -63,23 +63,23 @@ GeoJSON.parse(data, {Point: ['lat', 'lng']}); ``` Convert the example data to GeoJSON, and only include the `name` attribute in `properties` for each feature. - + ```javascript GeoJSON.parse(data, {Point: ['lat', 'lng'], include: ['name']}); -{ +{ "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": {"type": "Point", "coordinates": [-75.343, 39.984]}, - "properties": { + "properties": { "name": "Location A" } }, ... { "type": "Feature", "geometry": {"type": "Point", "coordinates": [ -75.534, 39.123]}, - "properties": { + "properties": { "name": "Location C" } } @@ -88,27 +88,27 @@ GeoJSON.parse(data, {Point: ['lat', 'lng'], include: ['name']}); ``` 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": { + "properties": { "name": "Location A", "category": "Store" } } ``` - + The `parse` method can handle data with different geometry types. Consider the following sample data: ```javascript var data2 = [ - { + { x: 0.5, y: 102.0, prop0: 'value0' @@ -255,9 +255,9 @@ or GeoJSON.parse(data, {Point: 'coords'}); -The valid geometry types are +The valid geometry types are -- `Point` +- `Point` - `MultiPoint` - `LineString` - `MultiLineString` @@ -307,12 +307,12 @@ You can add arbitrary properties to features using the `extra` param. The value } }); - { + { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": {"type": "Point", "coordinates": [-75.343, 39.984]}, - "properties": { + "properties": { "name": "Location A", "category": "Store", "style": { @@ -330,27 +330,27 @@ You can add arbitrary properties to features using the `extra` param. The value You can also add dataset properties using the `extraGlobal` param. The value for `extraGlobal` must be an object. 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' } }); - { + { "type": "FeatureCollection", "features": [ { "type": "Feature", "geometry": {"type": "Point", "coordinates": [-75.343, 39.984]}, - "properties": { + "properties": { "name": "Location A" } }, ... { "type": "Feature", "geometry": {"type": "Point", "coordinates": [ -75.534, 39.123]}, - "properties": { + "properties": { "name": "Location C" } } diff --git a/geojson.js b/geojson.js index 51e8ab1..0f7e51e 100644 --- a/geojson.js +++ b/geojson.js @@ -1,5 +1,5 @@ (function(GeoJSON) { - GeoJSON.version = '0.3.1'; + GeoJSON.version = '0.4.0'; // Allow user to specify default parameters GeoJSON.defaults = {}; @@ -145,7 +145,7 @@ // Geometry parameter specified as: {Point: 'coords'} if(typeof val === 'string' && item.hasOwnProperty(val)) { if(gtype === 'GeoJSON') { - geom = item[val] + geom = item[val]; } else { geom.type = gtype; geom.coordinates = item[val]; diff --git a/geojson.min.js b/geojson.min.js index 765b90f..b6fb796 100644 --- a/geojson.min.js +++ b/geojson.min.js @@ -1,3 +1,3 @@ -// geojson.js - v0.3.1 -// (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.1",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"],l=[]}("object"==typeof module?module.exports:window.GeoJSON={}); \ No newline at end of file +// 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)?"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={}); \ No newline at end of file diff --git a/package.json b/package.json index be844f4..faafc5b 100644 --- a/package.json +++ b/package.json @@ -1,10 +1,10 @@ { "name": "geojson", - "description": "Convert an array of geographic objects to GeoJSON", + "description": "Turn your geo data into GeoJSON", "author": "Casey Thomas ", "license": "MIT", "keywords": "geojson", - "version": "0.3.1", + "version": "0.4.0", "main": "./geojson", "repository": { "type": "git", diff --git a/test/test.js b/test/test.js index dfd1966..8b5857b 100644 --- a/test/test.js +++ b/test/test.js @@ -395,7 +395,16 @@ describe('GeoJSON', function() { 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'); + }); }); }); });