turf-combine
Advanced tools
Comparing version 1.1.0 to 3.0.0-canary.2f5f7167
var combine = require('./'); | ||
var Benchmark = require('benchmark'); | ||
var fs = require('fs'); | ||
var point = require('turf-point'); | ||
var linestring = require('turf-linestring'); | ||
var polygon = require('turf-polygon'); | ||
var featurecollection = require('turf-featurecollection'); | ||
var point = require('turf-helpers').point; | ||
var linestring = require('turf-helpers').lineString; | ||
var polygon = require('turf-helpers').polygon; | ||
var featurecollection = require('turf-helpers').featureCollection; | ||
@@ -9,0 +9,0 @@ // MultiPoint |
102
index.js
@@ -0,5 +1,10 @@ | ||
var meta = require('turf-meta'); | ||
/** | ||
* Combines a {@link FeatureCollection} of {@link Point}, {@link LineString}, or {@link Polygon} features into {@link MultiPoint}, {@link MultiLineString}, or {@link MultiPolygon} features. | ||
* Combines a {@link FeatureCollection} of {@link Point}, | ||
* {@link LineString}, or {@link Polygon} features | ||
* into {@link MultiPoint}, {@link MultiLineString}, or | ||
* {@link MultiPolygon} features. | ||
* | ||
* @module turf/combine | ||
* @name combine | ||
* @category misc | ||
@@ -35,51 +40,52 @@ * @param {FeatureCollection<(Point|LineString|Polygon)>} fc a FeatureCollection of any type | ||
module.exports = function(fc) { | ||
var type = fc.features[0].geometry.type; | ||
var geometries = fc.features.map(function(f) { | ||
if (f.geometry.type === 'Point' || | ||
f.geometry.type === 'LineString' || | ||
f.geometry.type === 'Polygon') return [f.geometry.coordinates]; | ||
return f.geometry.coordinates; | ||
}); | ||
module.exports = function (fc) { | ||
var groups = { | ||
MultiPoint: {coordinates: [], properties: []}, | ||
MultiLineString: {coordinates: [], properties: []}, | ||
MultiPolygon: {coordinates: [], properties: []} | ||
}; | ||
switch (type) { | ||
case 'Point': | ||
case 'MultiPoint': | ||
return { | ||
type: 'Feature', | ||
properties: {}, | ||
geometry: { | ||
type: 'MultiPoint', | ||
coordinates: pluckCoords(geometries) | ||
var multiMapping = Object.keys(groups).reduce(function (memo, item) { | ||
memo[item.replace('Multi', '')] = item; | ||
return memo; | ||
}, {}); | ||
function addToGroup(feature, key, multi) { | ||
if (!multi) { | ||
groups[key].coordinates.push(feature.geometry.coordinates); | ||
} else { | ||
groups[key].coordinates = groups[key].coordinates.concat(feature.geometry.coordinates); | ||
} | ||
}; | ||
case 'LineString': | ||
case 'MultiLineString': | ||
return { | ||
type: 'Feature', | ||
properties: {}, | ||
geometry: { | ||
type: 'MultiLineString', | ||
coordinates: pluckCoords(geometries) | ||
groups[key].properties.push(feature.properties); | ||
} | ||
meta.featureEach(fc, function (feature) { | ||
if (!feature.geometry) return; | ||
if (groups[feature.geometry.type]) { | ||
addToGroup(feature, feature.geometry.type, true); | ||
} else if (multiMapping[feature.geometry.type]) { | ||
addToGroup(feature, multiMapping[feature.geometry.type], false); | ||
} | ||
}; | ||
case 'Polygon': | ||
case 'MultiPolygon': | ||
return { | ||
type: 'Feature', | ||
properties: {}, | ||
geometry: { | ||
type: 'MultiPolygon', | ||
coordinates: pluckCoords(geometries) | ||
} | ||
}; | ||
default: | ||
return fc; | ||
} | ||
}); | ||
return { | ||
type: 'FeatureCollection', | ||
features: Object.keys(groups) | ||
.filter(function (key) { | ||
return groups[key].coordinates.length; | ||
}) | ||
.sort() | ||
.map(function (key) { | ||
return { | ||
type: 'Feature', | ||
properties: { | ||
collectedProperties: groups[key].properties | ||
}, | ||
geometry: { | ||
type: key, | ||
coordinates: groups[key].coordinates | ||
} | ||
}; | ||
}) | ||
}; | ||
}; | ||
function pluckCoords(multi) { | ||
return multi.reduce(function(memo, coords) { | ||
return memo.concat(coords); | ||
}, []); | ||
} |
{ | ||
"name": "turf-combine", | ||
"version": "1.1.0", | ||
"version": "3.0.0-canary.2f5f7167", | ||
"description": "turf combine module", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "tape test.js", | ||
"doc": "dox -r < index.js | doxme --readme > README.md" | ||
"test": "tape test.js" | ||
}, | ||
@@ -21,2 +20,5 @@ "repository": { | ||
], | ||
"dependencies": { | ||
"turf-meta": "^3.0.0-canary.2f5f7167" | ||
}, | ||
"author": "morganherlocker", | ||
@@ -30,13 +32,5 @@ "license": "MIT", | ||
"benchmark": "^1.0.0", | ||
"dox": "^0.6.1", | ||
"doxme": "^1.4.3", | ||
"tape": "^3.5.0", | ||
"turf-featurecollection": "^1.0.0", | ||
"turf-linestring": "^1.0.1", | ||
"turf-multilinestring": "^1.0.2", | ||
"turf-multipoint": "^1.0.0", | ||
"turf-multipolygon": "^1.0.1", | ||
"turf-point": "^2.0.0", | ||
"turf-polygon": "^1.0.2" | ||
"turf-helpers": "^3.0.0-canary.2f5f7167" | ||
} | ||
} |
85
test.js
var test = require('tape'), | ||
point = require('turf-point'), | ||
multipoint = require('turf-multipoint'), | ||
linestring = require('turf-linestring'), | ||
multilinestring = require('turf-multilinestring'), | ||
polygon = require('turf-polygon'), | ||
multipolygon = require('turf-multipolygon'), | ||
featurecollection = require('turf-featurecollection'); | ||
point = require('turf-helpers').point, | ||
multipoint = require('turf-helpers').multiPoint, | ||
linestring = require('turf-helpers').lineString, | ||
multilinestring = require('turf-helpers').multiLineString, | ||
polygon = require('turf-helpers').polygon, | ||
multipolygon = require('turf-helpers').multiPolygon, | ||
featurecollection = require('turf-helpers').featureCollection; | ||
@@ -20,3 +20,3 @@ var combine = require('./') | ||
t.ok(multiPt, 'should combine two Points into a MultiPoint') | ||
t.deepEqual(multiPt.geometry.coordinates, [[50, 51], [100, 101]]) | ||
t.deepEqual(multiPt.features[0].geometry.coordinates, [[50, 51], [100, 101]]) | ||
t.end(); | ||
@@ -33,3 +33,3 @@ }); | ||
t.ok(multiPt, 'should combine Points + MultiPoint into a MultiPoint') | ||
t.deepEqual(multiPt.geometry.coordinates, [[50, 51], [100, 101], [101, 102]]) | ||
t.deepEqual(multiPt.features[0].geometry.coordinates, [[50, 51], [100, 101], [101, 102]]) | ||
t.end(); | ||
@@ -54,4 +54,4 @@ }); | ||
t.ok(multiLine, 'should combine two LineStrings into a MultiLineString') | ||
t.equal(multiLine.geometry.type, 'MultiLineString') | ||
t.deepEqual(multiLine.geometry.coordinates, [[[102, -10], [130, 4]], [[40, -20], [150, 18]]]) | ||
t.equal(multiLine.features[0].geometry.type, 'MultiLineString') | ||
t.deepEqual(multiLine.features[0].geometry.coordinates, [[[102, -10], [130, 4]], [[40, -20], [150, 18]]]) | ||
t.end(); | ||
@@ -80,4 +80,4 @@ }); | ||
t.ok(multiLine, 'should combine LineString + MultiLineString into a MultiLineString') | ||
t.equal(multiLine.geometry.type, 'MultiLineString') | ||
t.deepEqual(multiLine.geometry.coordinates, [[[102, -10], [130, 4]], [[40, -20], [150, 18]], [[50, -10], [160, 28]]]) | ||
t.equal(multiLine.features[0].geometry.type, 'MultiLineString') | ||
t.deepEqual(multiLine.features[0].geometry.coordinates, [[[102, -10], [130, 4]], [[40, -20], [150, 18]], [[50, -10], [160, 28]]]) | ||
t.end(); | ||
@@ -109,4 +109,4 @@ }); | ||
t.ok(multiPoly, 'should combine two Polygons into a MultiPolygon') | ||
t.equal(multiPoly.geometry.type, 'MultiPolygon') | ||
t.deepEqual(multiPoly.geometry.coordinates, | ||
t.equal(multiPoly.features[0].geometry.type, 'MultiPolygon') | ||
t.deepEqual(multiPoly.features[0].geometry.coordinates, | ||
[[[[20,0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0],[20,0]]], | ||
@@ -155,4 +155,4 @@ [[[30.0,0.0],[102.0,0.0],[103.0,1.0],[30.0,0.0]]]]) | ||
t.ok(multiPoly, 'should combine two Polygon + MultiPolygon into a MultiPolygon') | ||
t.equal(multiPoly.geometry.type, 'MultiPolygon') | ||
t.deepEqual(multiPoly.geometry.coordinates, | ||
t.equal(multiPoly.features[0].geometry.type, 'MultiPolygon') | ||
t.deepEqual(multiPoly.features[0].geometry.coordinates, | ||
[[[[20,0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0],[20,0]]], | ||
@@ -166,1 +166,52 @@ [[[30.0,0.0],[102.0,0.0],[103.0,1.0],[30.0,0.0]]], | ||
}); | ||
test('combine -- heterogenous', function(t) { | ||
// MultiPolygon | ||
var p1 = polygon( [ | ||
[ | ||
[20.0,0.0], | ||
[101.0,0.0], | ||
[101.0,1.0], | ||
[100.0,1.0], | ||
[100.0,0.0], | ||
[20.0,0.0] | ||
] | ||
]); | ||
var p2 = multipolygon([ | ||
[[ | ||
[30.0,0.0], | ||
[102.0,0.0], | ||
[103.0,1.0], | ||
[30.0,0.0] | ||
]], | ||
[ | ||
[ | ||
[20.0,5.0], | ||
[92.0,5.0], | ||
[93.0,6.0], | ||
[20.0,5.0] | ||
], | ||
[ | ||
[25, 5], | ||
[30, 5], | ||
[30, 5.5], | ||
[25, 5] | ||
] | ||
] | ||
]); | ||
var pt1 = point([50, 51]) | ||
var multiPoly = combine(featurecollection([p1, p2, pt1])) | ||
t.ok(multiPoly, 'should combine two Polygon + MultiPolygon into a MultiPolygon') | ||
t.equal(multiPoly.features[0].geometry.type, 'MultiPoint') | ||
t.equal(multiPoly.features[1].geometry.type, 'MultiPolygon') | ||
t.deepEqual(multiPoly.features[1].geometry.coordinates, | ||
[[[[20,0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0],[20,0]]], | ||
[[[30.0,0.0],[102.0,0.0],[103.0,1.0],[30.0,0.0]]], | ||
[[[20.0,5.0],[92.0,5.0],[93.0,6.0],[20.0,5.0]], | ||
[[25, 5],[30, 5],[30, 5.5],[25, 5]]] | ||
]) | ||
t.end() | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
12273
3
325
0
1
7
1
+ Addedturf-meta@3.0.12(transitive)