Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support for multilinestrings, polygons, and multipolygons #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 115 additions & 0 deletions fixture/multipolygonIn.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
[
[
-80.09479522705078,
32.84094293282064
],
[
-80.03917694091797,
32.89313750722354
],
[
-79.99248504638672,
32.87036022808352
],
[
-79.91558074951172,
32.916197139908846
],
[
-79.87918853759766,
32.88391197291899
],
[
-79.80915069580078,
32.865169649519416
],
[
-79.79782104492188,
32.817863720254564
],
[
-79.86991882324219,
32.756963536267065
],
[
-79.88948822021484,
32.79102659804327
],
[
-80.0302505493164,
32.74310364571191
],
[
-79.98252868652344,
32.83373132321818
],
[
-80.09479522705078,
32.84094293282064
]
],
[
[
-79.93034362792969,
32.84786552687888
],
[
-79.89017486572266,
32.84786552687888
],
[
-79.89635467529297,
32.81844077366436
],
[
-79.90768432617188,
32.83546216294805
],
[
-79.95094299316406,
32.81353570021499
],
[
-79.93034362792969,
32.84786552687888
]
]
],
[
[
[
-80.0192642211914,
32.95941776109039
],
[
-80.04741668701172,
32.93838636388491
],
[
-80.0079345703125,
32.91245035823873
],
[
-80.01171112060547,
32.93838636388491
],
[
-79.91558074951172,
32.95106342152381
],
[
-80.0192642211914,
32.95941776109039
]
]
]
]
}
}
85 changes: 85 additions & 0 deletions fixture/polygon-with-holeIn.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-80.09479522705078,
32.84094293282064
],
[
-80.03917694091797,
32.89313750722354
],
[
-79.99248504638672,
32.87036022808352
],
[
-79.91558074951172,
32.916197139908846
],
[
-79.87918853759766,
32.88391197291899
],
[
-79.80915069580078,
32.865169649519416
],
[
-79.79782104492188,
32.817863720254564
],
[
-79.86991882324219,
32.756963536267065
],
[
-79.88948822021484,
32.79102659804327
],
[
-80.0302505493164,
32.74310364571191
],
[
-79.98252868652344,
32.83373132321818
],
[
-80.09479522705078,
32.84094293282064
]
],
[
[
-79.93034362792969,
32.84786552687888
],
[
-79.89017486572266,
32.84786552687888
],
[
-79.89635467529297,
32.81844077366436
],
[
-79.90768432617188,
32.83546216294805
],
[
-79.95094299316406,
32.81353570021499
],
[
-79.93034362792969,
32.84786552687888
]
]
]
}
}
59 changes: 59 additions & 0 deletions fixture/polygonIn.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-80.09479522705078,
32.84094293282064
],
[
-80.03917694091797,
32.89313750722354
],
[
-79.99248504638672,
32.87036022808352
],
[
-79.91558074951172,
32.916197139908846
],
[
-79.87918853759766,
32.88391197291899
],
[
-79.80915069580078,
32.865169649519416
],
[
-79.79782104492188,
32.817863720254564
],
[
-79.86991882324219,
32.756963536267065
],
[
-79.88948822021484,
32.79102659804327
],
[
-80.0302505493164,
32.74310364571191
],
[
-79.98252868652344,
32.83373132321818
],
[
-80.09479522705078,
32.84094293282064
]
]
]
}
}
17 changes: 17 additions & 0 deletions geojsonutil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use strict'

exports.isLinearRing = function (points) {
return points.length >= 4 && arrEqual(points[0], points[points.length - 1])
}

function arrEqual (a1, a2) {
if (!a1 || !a2 || a1.length !== a2.length) {
return false
}
for (var i in a1) {
if (a1[i] !== a2[i]) {
return false
}
}
return true
}
56 changes: 48 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
'use strict'

var linestring = require('turf-linestring');
var multilinestring = require('turf-multilinestring');
var polygon = require('turf-polygon');
var multipolygon = require('turf-multipolygon');

var Spline = require('./spline.js');
var isLinearRing = require('./geojsonutil.js').isLinearRing

/**
* Takes a {@link LineString|line} and returns a curved version
Expand Down Expand Up @@ -43,11 +50,41 @@ var Spline = require('./spline.js');
*
* //=result
*/
module.exports = function(line, resolution, sharpness) {
var lineOut = linestring([]);
module.exports = function(feature, resolution, sharpness) {
var type = feature.geometry.type;

if (type === 'LineString') {
return linestring(_bezier(feature.geometry.coordinates, resolution, sharpness), feature.properties);
}

if (type === 'MultiLineString') {
var lines = feature.geometry.coordinates.map(function(line) {
return_bezier(line, resolution, sharpness)
})
return multilinestring(lines, feature.properties)
}

if (type === 'Polygon') {
var rings = feature.geometry.coordinates.map(function(ring) {
return _bezier(ring, resolution, sharpness)
})
return polygon(rings, feature.properties)
}

if (type === 'MultiPolygon') {
let polygons = feature.geometry.coordinates.map(function(polygon) {
return polygon.map(function(ring) {
return _bezier(ring, resolution, sharpness)
})
})
return multipolygon(polygons, feature.properties)
}
}

function _bezier(points, resolution, sharpness) {
var coords = [];

lineOut.properties = line.properties;
var pts = line.geometry.coordinates.map(function(pt) {
var pts = points.map(function(pt) {
return {x: pt[0], y: pt[1]};
});

Expand All @@ -59,9 +96,12 @@ module.exports = function(line, resolution, sharpness) {
for (var i=0; i<spline.duration; i+=10) {
var pos = spline.pos(i);
if (Math.floor(i/100)%2===0) {
lineOut.geometry.coordinates.push([pos.x, pos.y]);
coords.push([pos.x, pos.y]);
}
}

return lineOut;
};

if (isLinearRing(points)) {
coords.push(coords[0])
}
return coords;
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@
},
"homepage": "https://github.com/Turfjs/turf-bezier",
"dependencies": {
"turf-linestring": "^1.0.1"
"turf-linestring": "^1.0.1",
"turf-multilinestring": "^1.0.2",
"turf-multipolygon": "^1.0.1",
"turf-polygon": "^1.0.3"
},
"devDependencies": {
"benchmark": "1.0.0",
Expand Down
Loading