@turf/flatten
Advanced tools
Comparing version 4.2.0 to 4.3.0
@@ -16,4 +16,4 @@ /// <reference types="geojson" /> | ||
(geojson: Polygons | Polygons | MultiPolygons | MultiPolygons): Polygons; | ||
(geojson: Feature | Features): Features; | ||
(geojson: GeoJSON.GeometryCollection | GeoJSON.GeometryObject): Features; | ||
(geojson: Feature<any> | Features<any>): Features<any>; | ||
(geojson: GeoJSON.GeometryCollection | GeoJSON.GeometryObject): Features<any>; | ||
} | ||
@@ -20,0 +20,0 @@ |
141
index.js
@@ -1,9 +0,3 @@ | ||
var featureEach = require('@turf/meta').featureEach; | ||
var geomEach = require('@turf/meta').geomEach; | ||
var getCoords = require('@turf/invariant').getCoords; | ||
var helpers = require('@turf/helpers'); | ||
var point = helpers.point; | ||
var lineString = helpers.lineString; | ||
var polygon = helpers.polygon; | ||
var featureCollection = helpers.featureCollection; | ||
var flattenEach = require('@turf/meta').flattenEach; | ||
var featureCollection = require('@turf/helpers').featureCollection; | ||
@@ -14,6 +8,6 @@ /** | ||
* @name flatten | ||
* @param {Feature} geojson any valid {@link GeoJSON} with multi-geometry {@link Feature}s | ||
* @returns {FeatureCollection} a flattened {@link FeatureCollection} | ||
* @param {FeatureCollection|Geometry|Feature<any>} geojson any valid GeoJSON Object | ||
* @returns {FeatureCollection<any>} all Multi-Geometries are flattened into single Features | ||
* @example | ||
* var geometry = { | ||
* var multiGeometry = { | ||
* "type": "MultiPolygon", | ||
@@ -27,3 +21,3 @@ * "coordinates": [ | ||
* | ||
* var flatten = turf.flatten(geometry); | ||
* var flatten = turf.flatten(multiGeometry); | ||
* | ||
@@ -34,121 +28,10 @@ * //addToMap | ||
function flatten(geojson) { | ||
var type = (geojson.geometry) ? geojson.geometry.type : geojson.type; | ||
switch (type) { | ||
case 'MultiPoint': | ||
return flattenMultiPoint(geojson); | ||
case 'MultiPolygon': | ||
return flattenMultiPolygon(geojson); | ||
case 'MultiLineString': | ||
return flattenMultiLineString(geojson); | ||
case 'FeatureCollection': | ||
return flattenFeatureCollection(geojson); | ||
case 'GeometryCollection': | ||
return flattenGeometryCollection(geojson); | ||
case 'Point': | ||
case 'LineString': | ||
case 'Polygon': | ||
return featureCollection([geojson]); | ||
} | ||
} | ||
module.exports = flatten; | ||
if (!geojson) throw new Error('geojson is required'); | ||
/** | ||
* Flatten MultiPoint | ||
* | ||
* @private | ||
* @param {Feature<MultiPoint>} geojson GeoJSON Feature | ||
* @returns {FeatureCollection<Point>} Feature Collection | ||
*/ | ||
function flattenMultiPoint(geojson) { | ||
var points = []; | ||
getCoords(geojson).forEach(function (coords) { | ||
points.push(point(coords, geojson.properties)); | ||
var results = []; | ||
flattenEach(geojson, function (feature) { | ||
results.push(feature); | ||
}); | ||
return featureCollection(points); | ||
return featureCollection(results); | ||
} | ||
/** | ||
* Flatten MultiLineString | ||
* | ||
* @private | ||
* @param {Feature<MultiLineString>} geojson GeoJSON Feature | ||
* @returns {FeatureCollection<LineString>} Feature Collection | ||
*/ | ||
function flattenMultiLineString(geojson) { | ||
var lines = []; | ||
getCoords(geojson).forEach(function (coords) { | ||
lines.push(lineString(coords, geojson.properties)); | ||
}); | ||
return featureCollection(lines); | ||
} | ||
/** | ||
* Flatten MultiPolygon | ||
* | ||
* @private | ||
* @param {Feature<MultiPolygon>} geojson GeoJSON Feature | ||
* @returns {FeatureCollection<Polygon>} Feature Collection | ||
*/ | ||
function flattenMultiPolygon(geojson) { | ||
var polygons = []; | ||
getCoords(geojson).forEach(function (coords) { | ||
polygons.push(polygon(coords, geojson.properties)); | ||
}); | ||
return featureCollection(polygons); | ||
} | ||
/** | ||
* Flatten FeatureCollection | ||
* | ||
* @private | ||
* @param {FeatureCollection<any>} geojson GeoJSON Feature | ||
* @returns {FeatureCollection<any>} Feature Collection | ||
*/ | ||
function flattenFeatureCollection(geojson) { | ||
var features = []; | ||
featureEach(geojson, function (multiFeature) { | ||
switch (multiFeature.geometry.type) { | ||
case 'MultiPoint': | ||
case 'MultiLineString': | ||
case 'MultiPolygon': | ||
featureEach(flatten(multiFeature), function (feature) { | ||
features.push(feature); | ||
}); | ||
break; | ||
default: | ||
features.push(multiFeature); | ||
} | ||
}); | ||
return featureCollection(features); | ||
} | ||
/** | ||
* Flatten GeometryCollection | ||
* | ||
* @private | ||
* @param {GeometryCollection<any>} geojson GeoJSON Geometry Collection | ||
* @param {*} [properties] translate properties to Feature | ||
* @returns {FeatureCollection<any>} Feature Collection | ||
*/ | ||
function flattenGeometryCollection(geojson) { | ||
var features = []; | ||
geomEach(geojson, function (geometry) { | ||
switch (geometry.type) { | ||
case 'MultiPoint': | ||
case 'MultiLineString': | ||
case 'MultiPolygon': | ||
featureEach(flatten(geometry), function (feature) { | ||
features.push(feature); | ||
}); | ||
break; | ||
default: | ||
var feature = { | ||
type: 'Feature', | ||
properties: {}, | ||
geometry: geometry | ||
}; | ||
features.push(feature); | ||
} | ||
}); | ||
return featureCollection(features); | ||
} | ||
module.exports = flatten; |
{ | ||
"name": "@turf/flatten", | ||
"version": "4.2.0", | ||
"version": "4.3.0", | ||
"description": "turf flatten module", | ||
@@ -33,12 +33,12 @@ "main": "index.js", | ||
"devDependencies": { | ||
"benchmark": "^2.1.3", | ||
"benchmark": "^2.1.4", | ||
"load-json-file": "^2.0.0", | ||
"tape": "^4.0.0", | ||
"tape": "^4.6.3", | ||
"write-json-file": "^2.0.0" | ||
}, | ||
"dependencies": { | ||
"@turf/helpers": "^4.2.0", | ||
"@turf/invariant": "^4.2.0", | ||
"@turf/meta": "^4.2.0" | ||
"@turf/helpers": "^4.3.0", | ||
"@turf/invariant": "^4.3.0", | ||
"@turf/meta": "^4.3.0" | ||
} | ||
} |
@@ -9,3 +9,3 @@ # @turf/flatten | ||
- `geojson` **[Feature](http://geojson.org/geojson-spec.html#feature-objects)** any valid [GeoJSON](GeoJSON) with multi-geometry [Feature](http://geojson.org/geojson-spec.html#feature-objects)s | ||
- `geojson` **([FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [Geometry](http://geojson.org/geojson-spec.html#geometry) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<any>)** any valid GeoJSON Object | ||
@@ -15,3 +15,3 @@ **Examples** | ||
```javascript | ||
var geometry = { | ||
var multiGeometry = { | ||
"type": "MultiPolygon", | ||
@@ -25,3 +25,3 @@ "coordinates": [ | ||
var flatten = turf.flatten(geometry); | ||
var flatten = turf.flatten(multiGeometry); | ||
@@ -32,3 +32,3 @@ //addToMap | ||
Returns **[FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)** a flattened [FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) | ||
Returns **[FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects)<any>** all Multi-Geometries are flattened into single Features | ||
@@ -35,0 +35,0 @@ <!-- This file is automatically generated. Please don't edit it directly: |
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
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
5639
51
Updated@turf/helpers@^4.3.0
Updated@turf/invariant@^4.3.0
Updated@turf/meta@^4.3.0