Skip to content

Commit

Permalink
Use flattenEach with @turf/flatten
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed May 8, 2017
1 parent b49ce34 commit e798e07
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 133 deletions.
8 changes: 4 additions & 4 deletions packages/turf-flatten/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ Flattens any [GeoJSON](GeoJSON) to a [FeatureCollection](http://geojson.org/geoj

**Parameters**

- `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

**Examples**

```javascript
var geometry = {
var multiGeometry = {
"type": "MultiPolygon",
"coordinates": [
[[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]],
Expand All @@ -20,13 +20,13 @@ var geometry = {
]
};

var flatten = turf.flatten(geometry);
var flatten = turf.flatten(multiGeometry);

//addToMap
var addToMap = [flatten]
```

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

<!-- This file is automatically generated. Please don't edit it directly:
if you find an error, edit the source file (likely index.js), and re-run
Expand Down
141 changes: 12 additions & 129 deletions packages/turf-flatten/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
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;

/**
* Flattens any {@link GeoJSON} to a {@link FeatureCollection} inspired by [geojson-flatten](https://github.com/tmcw/geojson-flatten).
*
* @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",
* "coordinates": [
* [[[102.0, 2.0], [103.0, 2.0], [103.0, 3.0], [102.0, 3.0], [102.0, 2.0]]],
Expand All @@ -23,129 +17,18 @@ var featureCollection = helpers.featureCollection;
* ]
* };
*
* var flatten = turf.flatten(geometry);
* var flatten = turf.flatten(multiGeometry);
*
* //addToMap
* var addToMap = [flatten]
*/
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;

/**
* 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));
});
return featureCollection(points);
}

/**
* 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);
}
if (!geojson) throw new Error('geojson is required');

/**
* 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);
}
var results = [];
flattenEach(geojson, function (feature) {
results.push(feature);
});
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);
return featureCollection(results);
}
module.exports = flatten;

0 comments on commit e798e07

Please sign in to comment.