Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

turf-combine

Package Overview
Dependencies
Maintainers
9
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

turf-combine - npm Package Compare versions

Comparing version 1.1.0 to 3.0.0-canary.2f5f7167

8

bench.js
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

@@ -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"
}
}
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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc