Skip to content

Commit

Permalink
quick fix for : handle segment
Browse files Browse the repository at this point in the history
  • Loading branch information
stebogit committed Aug 1, 2017
1 parent adb56ea commit 0bb151f
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 4 deletions.
21 changes: 17 additions & 4 deletions packages/turf-clean-coords/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ var getGeomType = invariant.getGeomType;
module.exports = function (geojson, mutate) {
if (!geojson) throw new Error('geojson is required');
var type = getGeomType(geojson);
var coords = getCoords(geojson);

// Store new "clean" points in this Array
var newCoords = [];
Expand All @@ -34,12 +33,12 @@ module.exports = function (geojson, mutate) {
break;
case 'MultiLineString':
case 'Polygon':
coords.forEach(function (line) {
getCoords(geojson).forEach(function (line) {
newCoords.push(cleanCoords(line));
});
break;
case 'MultiPolygon':
coords.forEach(function (polygons) {
getCoords(geojson).forEach(function (polygons) {
var polyPoints = [];
polygons.forEach(function (ring) {
polyPoints.push(cleanCoords(ring));
Expand All @@ -51,7 +50,7 @@ module.exports = function (geojson, mutate) {
return geojson;
case 'MultiPoint':
var existing = {};
coords.forEach(function (coord) {
getCoords(geojson).forEach(function (coord) {
var key = coord.join('-');
if (!existing.hasOwnProperty(key)) {
newCoords.push(coord);
Expand Down Expand Up @@ -120,6 +119,8 @@ function feature(geojson, type, coordinates) {
*/
function cleanCoords(line) {
var points = getCoords(line);
// handle "clean" segment
if (points.length === 2 && !equals(points[0], points[1])) return points;

var prevPoint, point, nextPoint;
var newPoints = [];
Expand All @@ -139,6 +140,18 @@ function cleanCoords(line) {
return newPoints;
}

/**
* Compares two points and returns if they are equals
*
* @private
* @param {Array<number>} pt1 point
* @param {Array<number>} pt2 point
* @returns {boolean} true if they are equals
*/
function equals(pt1, pt2) {
return pt1[0] === pt2[0] && pt1[1] === pt2[1];
}

/**
* Returns if `point` is on the segment between `start` and `end`.
* Borrowed from `@turf/boolean-point-on-line` to speed up the evaluation (instead of using the module as dependency)
Expand Down
17 changes: 17 additions & 0 deletions packages/turf-clean-coords/test/in/clean-segment.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[
0,
0
],
[
2,
2
]
]
}
}
21 changes: 21 additions & 0 deletions packages/turf-clean-coords/test/in/segment.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[
0,
0
],
[
1,
1
],
[
2,
2
]
]
}
}
17 changes: 17 additions & 0 deletions packages/turf-clean-coords/test/out/clean-segment.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[
0,
0
],
[
2,
2
]
]
}
}
17 changes: 17 additions & 0 deletions packages/turf-clean-coords/test/out/segment.geojson
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[
0,
0
],
[
2,
2
]
]
}
}

0 comments on commit 0bb151f

Please sign in to comment.