Skip to content

Commit

Permalink
Merge pull request #556 from rowanwins/dissolve
Browse files Browse the repository at this point in the history
Initial commit of turf-dissolve
  • Loading branch information
DenisCarriere committed Jan 25, 2017
2 parents a289fff + 93fd257 commit 35c87dd
Show file tree
Hide file tree
Showing 13 changed files with 459 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/turf-dissolve/.npmignore
@@ -0,0 +1,2 @@
test
coverage
21 changes: 21 additions & 0 deletions packages/turf-dissolve/LICENSE
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Rowan Winsemius

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
7 changes: 7 additions & 0 deletions packages/turf-dissolve/README.md
@@ -0,0 +1,7 @@
# turf-dissolve
Dissolves a featurecollection of polygons based on an attribute

````
var aggregated = turfDissolve(polys, 'combine');
````
10 changes: 10 additions & 0 deletions packages/turf-dissolve/index.d.ts
@@ -0,0 +1,10 @@
/// <reference types="geojson" />

type Polygons = GeoJSON.FeatureCollection<GeoJSON.Polygon>;

/**
* http://turfjs.org/docs.html#dissolve
*/
declare function dissolve(featureCollection: Polygons, propertyName?: string): Polygons;
declare namespace dissolve { }
export = dissolve;
163 changes: 163 additions & 0 deletions packages/turf-dissolve/index.js
@@ -0,0 +1,163 @@
var turfUnion = require('@turf/union');
var turfOverlaps = require('turf-overlaps');
var turfbbox = require('@turf/bbox');
var Rbush = require('rbush');
var gju = require('geojson-utils');
var getClosest = require('get-closest');

/**
* Dissolves a FeatureCollection of polygons based on a property. Note that multipart features within the collection are not supported
*
* @name dissolve
* @param {FeatureCollection<Polygon>} featureCollection input feature collection to be dissolved
* @param {string} [propertyName] property name on which to dissolve features
* @returns {FeatureCollection} a FeatureCollection containing the dissolved polygons
* @example
* var features = {
* "type": "FeatureCollection",
* "features": [
* {
* "type": "Feature",
* "properties": {
* "combine": "yes"
* },
* "geometry": {
* "type": "Polygon",
* "coordinates": [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]]
* }
* },
* {
* "type": "Feature",
* "properties": {
* "combine": "yes"
* },
* "geometry": {
* "type": "Polygon",
* "coordinates": [[[0, -1], [0, 0], [1, 0], [1, -1], [0,-1]]]
* }
* },
* {
* "type": "Feature",
* "properties": {
* "combine": "no"
* },
* "geometry": {
* "type": "Polygon",
* "coordinates": [[[1,-1],[1, 0], [2, 0], [2, -1], [1, -1]]]
* }
* }
* ]
* }
*
* var dissolved = turf.dissolve(features, 'combine');
*
* //=dissolved
*/
module.exports = function (featureCollection, propertyName) {

var originalIndexOfItemsRemoved = [];
var treeItems = [];
var rtree = new Rbush();
for (var polyIndex = 0; polyIndex < featureCollection.features.length; polyIndex++) {
var inputFeatureBbox = turfbbox(featureCollection.features[polyIndex]);
var treeObj = {
minX: inputFeatureBbox[0],
minY: inputFeatureBbox[1],
maxX: inputFeatureBbox[2],
maxY: inputFeatureBbox[3],
origIndexPosition: polyIndex
};
treeItems.push(treeObj);
}
rtree.load(treeItems);

for (var i = 0; i < featureCollection.features.length; i++) {
var polygon = featureCollection.features[i];

var polyBoundingBox = turfbbox(polygon);
var searchObj = {
minX: polyBoundingBox[0],
minY: polyBoundingBox[1],
maxX: polyBoundingBox[2],
maxY: polyBoundingBox[3]
};
var potentialMatchingFeatures = rtree.search(searchObj);

var featureChanged = false;

for (var searchIndex = 0; searchIndex < potentialMatchingFeatures.length; searchIndex++) {
polygon = featureCollection.features[i];

var matchFeaturePosition = potentialMatchingFeatures[searchIndex].origIndexPosition;

if (originalIndexOfItemsRemoved.length > 0 && matchFeaturePosition !== 0) {
if (matchFeaturePosition > originalIndexOfItemsRemoved[originalIndexOfItemsRemoved.length - 1]) {
matchFeaturePosition = matchFeaturePosition - (originalIndexOfItemsRemoved.length);
} else {
var closestNumber = getClosest.greaterNumber(matchFeaturePosition, originalIndexOfItemsRemoved);
if (closestNumber !== 0) {
matchFeaturePosition = matchFeaturePosition - closestNumber;
}
}
}

if (matchFeaturePosition === i) {
continue;
}
var matchFeature = featureCollection.features[matchFeaturePosition];

if (typeof propertyName !== 'undefined') {
if (matchFeature.properties[propertyName] !== polygon.properties[propertyName]) {
continue;
}
}

var overlapCheck = turfOverlaps(polygon, matchFeature);

if (!overlapCheck) {
var polyClone = JSON.stringify(polygon);
var polyBeingCheckedClone = JSON.stringify(matchFeature);
var linestring1 = toLinestring(JSON.parse(polyClone));
var linestring2 = toLinestring(JSON.parse(polyBeingCheckedClone));
overlapCheck = gju.lineStringsIntersect(linestring1.geometry, linestring2.geometry);
}
if (!overlapCheck) {
continue;
}

featureCollection.features[i] = turfUnion(polygon, matchFeature);
originalIndexOfItemsRemoved.push(potentialMatchingFeatures[searchIndex].origIndexPosition);
originalIndexOfItemsRemoved.sort(function (a, b) {
return a - b;
});

rtree.remove(potentialMatchingFeatures[searchIndex]);
featureCollection.features.splice(matchFeaturePosition, 1);
searchObj.origIndexPosition = i;
rtree.remove(searchObj, function (a, b) {
return a.origIndexPosition === b.origIndexPosition;
});
featureChanged = true;
}
if (featureChanged) {
var newBoundingBox = turfbbox(polygon);
rtree.insert({
minX: newBoundingBox[0],
minY: newBoundingBox[1],
maxX: newBoundingBox[2],
maxY: newBoundingBox[3],
origIndexPosition: i
});
i--;
}
}
return featureCollection;
};

function toLinestring(polygon) {
if (polygon === null) throw new Error('No polygon was passed');
polygon.geometry.type = 'LineString';
var flat_arr = [].concat.apply([], polygon.geometry.coordinates);
polygon.geometry.coordinates = flat_arr;
return polygon;
}
27 changes: 27 additions & 0 deletions packages/turf-dissolve/package.json
@@ -0,0 +1,27 @@
{
"name": "@turf/dissolve",
"version": "3.7.5",
"description": "Dissolves polygons based on an attribute",
"main": "index.js",
"keywords": [
"turf",
"dissolve",
"gis",
"geojson",
"polygon"
],
"author": "Rowan Winsemius",
"license": "MIT",
"dependencies": {
"@turf/bbox": "^3.7.5",
"@turf/helpers": "^3.7.5",
"@turf/union": "^3.7.5",
"geojson-utils": "^1.1.0",
"rbush": "^2.0.1",
"turf-overlaps": "^1.0.3"
},
"devDependencies": {
"tape": "^4.6.3"
},
"types": "index.d.ts"
}
18 changes: 18 additions & 0 deletions packages/turf-dissolve/test.js
@@ -0,0 +1,18 @@
var turfDissolve = require('./index');
var test = require('tape');
var polys = require('./test/polys.json');
var expectedOutput = require('./test/outputPolysByProperty.json');
var expectedOutput2 = require('./test/outputPolysWithNoProperty.json');

test('turf-dissolve', function (t) {

var dissolved = turfDissolve(polys, 'combine');
t.equal(dissolved.features.length, 3);
t.deepEqual(dissolved, expectedOutput);

var dissolved2 = turfDissolve(polys);
t.equal(dissolved2.features.length, 2);
t.deepEqual(dissolved2, expectedOutput2);

t.end();
});
1 change: 1 addition & 0 deletions packages/turf-dissolve/test/outputPolysByProperty.json
@@ -0,0 +1 @@
{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[0.5,0],[0,1],[1,1],[2,1],[2,0],[1,0],[1,-1],[0,-1],[0,0],[0.5,0]],[[1,0],[1,0.5],[0.7,0],[1,0]]]},"properties":{"combine":"yes"}},{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[1.3458251953125,0],[2,0],[2,-1],[1.3458251953125,-1],[1.3458251953125,-1.6422311515306498],[0.68115234375,-1.6422311515306498],[0.68115234375,1.4555318956783565],[1.3458251953125,1.4555318956783565],[1.3458251953125,0]]]},"properties":{"combine":"no"}},{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-0.252685546875,1.252341676699629],[-0.252685546875,1.653212936926045],[0.28564453125,1.653212936926045],[0.28564453125,1.252341676699629],[-0.252685546875,1.252341676699629]]]}}]}
1 change: 1 addition & 0 deletions packages/turf-dissolve/test/outputPolysWithNoProperty.json
@@ -0,0 +1 @@
{"type":"FeatureCollection","features":[{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[0.5,0],[0,1],[0.68115234375,1],[0.68115234375,1.4555318956783565],[1.3458251953125,1.4555318956783565],[1.3458251953125,1],[2,1],[2,0],[2,-1],[1.3458251953125,-1],[1.3458251953125,-1.6422311515306498],[0.68115234375,-1.6422311515306498],[0.68115234375,-1],[0,-1],[0,0],[0.5,0]]]},"properties":{"combine":"yes"}},{"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[-0.252685546875,1.252341676699629],[-0.252685546875,1.653212936926045],[0.28564453125,1.653212936926045],[0.28564453125,1.252341676699629],[-0.252685546875,1.252341676699629]]]}}]}

0 comments on commit 35c87dd

Please sign in to comment.