Skip to content

Commit

Permalink
Merge pull request #19 from Turfjs/overhaul
Browse files Browse the repository at this point in the history
overhaul tests and handle jsts edge cases
  • Loading branch information
Morgan Herlocker committed Mar 17, 2015
2 parents 8999ba2 + 2a7a395 commit 787d91a
Show file tree
Hide file tree
Showing 23 changed files with 336 additions and 59 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ var result = {
```


**Returns** `FeatureCollection.<Polygon>`, buffered features
**Returns** `FeatureCollection.<Polygon>,FeatureCollection.<MultiPolygon>,Polygon,MultiPolygon`, buffered features

## Installation

Expand Down
12 changes: 6 additions & 6 deletions bench.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ var buffer = require('./');
var Benchmark = require('benchmark');
var fs = require('fs');

var pt = JSON.parse(fs.readFileSync(__dirname+'/geojson/Point.geojson'));
var line = JSON.parse(fs.readFileSync(__dirname+'/geojson/LineString.geojson'));
var polygon = JSON.parse(fs.readFileSync(__dirname+'/geojson/Polygon.geojson'));
var fc = JSON.parse(fs.readFileSync(__dirname+'/geojson/FeatureCollection.geojson'));
var pt = JSON.parse(fs.readFileSync(__dirname+'/test/fixtures/in/Point.geojson'));
var line = JSON.parse(fs.readFileSync(__dirname+'/test/fixtures/in/LineString.geojson'));
var polygon = JSON.parse(fs.readFileSync(__dirname+'/test/fixtures/in/Polygon.geojson'));
var fc = JSON.parse(fs.readFileSync(__dirname+'/test/fixtures/in/FeatureCollection.geojson'));

var suite = new Benchmark.Suite('turf-buffer');
suite
.add('turf-buffer#Point',function () {
buffer(pt, 10, 'miles');
})
.add('turf-buffer#LineString',function () {
buffer(pt, 10, 'miles');
buffer(line, 10, 'miles');
})
.add('turf-buffer#Polygon',function () {
buffer(pt, 10, 'miles');
buffer(polygon, 10, 'miles');
})
.add('turf-buffer#FeatureCollection',function () {
buffer(fc, 10, 'miles');
Expand Down
1 change: 0 additions & 1 deletion geojson/LineString.geojson

This file was deleted.

38 changes: 15 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var featurecollection = require('turf-featurecollection');
var polygon = require('turf-polygon');
var combine = require('turf-combine');
var jsts = require('jsts');
var normalize = require('geojson-normalize');

/**
* Calculates a buffer for input features for a given radius. Units supported are miles, kilometers, and degrees.
Expand All @@ -15,7 +16,7 @@ var jsts = require('jsts');
* @param {(Feature|FeatureCollection)} feature input to be buffered
* @param {Number} distance distance to draw the buffer
* @param {String} unit 'miles', 'feet', 'kilometers', 'meters', or 'degrees'
* @return {FeatureCollection<Polygon>} buffered features
* @return {FeatureCollection<Polygon>|FeatureCollection<MultiPolygon>|Polygon|MultiPolygon} buffered features
*
* @example
* var pt = {
Expand Down Expand Up @@ -59,15 +60,13 @@ module.exports = function(feature, radius, units) {
break;
}

if (feature.type === 'FeatureCollection') {
var multi = combine(feature);
multi.properties = {};
buffered = bufferOp(multi, radius);
return buffered;
} else {
buffered = bufferOp(feature, radius);
return buffered;
}
var fc = normalize(feature);
var buffered = normalize(featurecollection(fc.features.map(function(f){
return bufferOp(f, radius);
})));

if(buffered.features.length > 1) return buffered;
else if(buffered.features.length === 1) return buffered.features[0];
};

var bufferOp = function(feature, radius) {
Expand All @@ -77,16 +76,9 @@ var bufferOp = function(feature, radius) {
var parser = new jsts.io.GeoJSONParser();
buffered = parser.write(buffered);

if (buffered.type === 'MultiPolygon') {
buffered = {
type: 'Feature',
geometry: buffered,
properties: {}
};
buffered = featurecollection([buffered]);
} else {
buffered = featurecollection([polygon(buffered.coordinates)]);
}

return buffered;
};
return {
type: 'Feature',
geometry: buffered,
properties: {}
};
};
17 changes: 11 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "turf buffer module",
"main": "index.js",
"scripts": {
"test": "tape test.js",
"test": "node ./test/test.js",
"doc": "dox -r < index.js | doxme --readme > README.md"
},
"repository": {
Expand All @@ -28,14 +28,19 @@
"homepage": "https://github.com/Turfjs/turf-buffer",
"devDependencies": {
"benchmark": "^1.0.0",
"tape": "^3.5.0",
"dox": "^0.6.1",
"doxme": "^1.4.3"
"doxme": "^1.8.2",
"glob": "^5.0.3",
"tape": "^3.5.0",
"turf-distance": "^1.0.1",
"turf-extent": "^1.0.4",
"turf-point": "^2.0.1"
},
"dependencies": {
"geojson-normalize": "0.0.0",
"jsts": "^0.15.0",
"turf-combine": "^1.0.1",
"turf-featurecollection": "^1.0.0",
"turf-polygon": "^1.0.2"
"turf-combine": "^1.0.2",
"turf-featurecollection": "^1.0.1",
"turf-polygon": "^1.0.3"
}
}
22 changes: 0 additions & 22 deletions test.js

This file was deleted.

File renamed without changes.
75 changes: 75 additions & 0 deletions test/fixtures/in/GeometryCollection.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "GeometryCollection",
"geometries": [
{
"type": "Polygon",
"coordinates": [
[
[
69.2578125,
35.85343961959182
],
[
69.2578125,
38.496593518947556
],
[
72.421875,
38.496593518947556
],
[
72.421875,
35.85343961959182
],
[
69.2578125,
35.85343961959182
]
]
]
},
{
"type": "LineString",
"coordinates": [
[
55.30517578124999,
39.87601941962116
],
[
55.85449218749999,
41.14556973100947
],
[
56.6015625,
40.111688665595956
],
[
57.65624999999999,
41.57436130598913
],
[
58.11767578124999,
40.94671366508002
]
]
},
{
"type": "Point",
"coordinates": [
62.33642578125001,
29.248063243796576
]
},
{
"type": "Point",
"coordinates": [
58.75488281249999,
32.45415593941475
]
}
]
}
}
53 changes: 53 additions & 0 deletions test/fixtures/in/LineString.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[
-92.4884033203125,
44.58655513209543
],
[
-92.2906494140625,
44.52392653654215
],
[
-92.230224609375,
44.429857265397246
],
[
-92.08740234375,
44.429857265397246
],
[
-91.9830322265625,
44.35527821160296
],
[
-91.92260742187499,
44.308126684886126
],
[
-91.8731689453125,
44.24519901522129
],
[
-91.8072509765625,
44.166444664458595
],
[
-91.58203125,
44.06390660801779
],
[
-91.29638671875,
43.874138181474734
],
[
-91.219482421875,
43.37710501700073
]
]
}
}
41 changes: 41 additions & 0 deletions test/fixtures/in/MultiLineString.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "MultiLineString",
"coordinates": [
[
[
65.7421875,
52.482780222078226
],
[
38.3203125,
44.59046718130883
],
[
61.87499999999999,
37.16031654673677
],
[
63.984375,
21.616579336740603
]
],
[
[
96.328125,
34.59704151614417
],
[
75.234375,
7.013667927566642
],
[
98.0859375,
9.44906182688142
]
]
]
}
}
21 changes: 21 additions & 0 deletions test/fixtures/in/MultiPoint.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "MultiPoint",
"coordinates": [
[
84.0234375,
47.040182144806664
],
[
85.2264404296875,
46.464349400461124
],
[
86.396484375,
47.26432008025478
]
]
}
}
Loading

0 comments on commit 787d91a

Please sign in to comment.