Skip to content

Commit

Permalink
Merge pull request #26 from caseypt/feature/cpt/new-tests-update-readme
Browse files Browse the repository at this point in the history
Add test, update README, push v0.4.0
  • Loading branch information
Casey Thomas committed Sep 18, 2016
2 parents 22fb0e5 + 83ef67a commit 529583d
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 33 deletions.
52 changes: 26 additions & 26 deletions README.md
Expand Up @@ -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 },
Expand All @@ -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"
}
Expand All @@ -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"
}
}
Expand All @@ -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'
Expand Down Expand Up @@ -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`
Expand Down Expand Up @@ -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": {
Expand All @@ -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"
}
}
Expand Down
4 changes: 2 additions & 2 deletions 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 = {};
Expand Down Expand Up @@ -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];
Expand Down
6 changes: 3 additions & 3 deletions geojson.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions 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 <caseypthomas@gmail.com>",
"license": "MIT",
"keywords": "geojson",
"version": "0.3.1",
"version": "0.4.0",
"main": "./geojson",
"repository": {
"type": "git",
Expand Down
9 changes: 9 additions & 0 deletions test/test.js
Expand Up @@ -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');
});
});
});
});

0 comments on commit 529583d

Please sign in to comment.