Skip to content

Commit

Permalink
Patch for geometry support (#711)
Browse files Browse the repository at this point in the history
* Quick patch for geometry support
Will be easier to fix once `@turf/flatten` is updated

* Update JSDocs

* Fix test spelling mistake

* Update JSDocs
  • Loading branch information
DenisCarriere authored May 3, 2017
1 parent 054b95b commit edb53a5
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 37 deletions.
4 changes: 2 additions & 2 deletions packages/turf-line-overlap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ Takes any LineString or Polygon and returns the overlapping lines between both f

**Parameters**

- `line1` **[Feature](http://geojson.org/geojson-spec.html#feature-objects)<([LineString](http://geojson.org/geojson-spec.html#linestring) \| [MultiLineString](http://geojson.org/geojson-spec.html#multilinestring) \| [Polygon](http://geojson.org/geojson-spec.html#polygon) \| [MultiPolygon](http://geojson.org/geojson-spec.html#multipolygon))>** any LineString or Polygon
- `line2` **[Feature](http://geojson.org/geojson-spec.html#feature-objects)<([LineString](http://geojson.org/geojson-spec.html#linestring) \| [MultiLineString](http://geojson.org/geojson-spec.html#multilinestring) \| [Polygon](http://geojson.org/geojson-spec.html#polygon) \| [MultiPolygon](http://geojson.org/geojson-spec.html#multipolygon))>** any LineString or Polygon
- `line1` **([Geometry](http://geojson.org/geojson-spec.html#geometry) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<([LineString](http://geojson.org/geojson-spec.html#linestring) \| [MultiLineString](http://geojson.org/geojson-spec.html#multilinestring) \| [Polygon](http://geojson.org/geojson-spec.html#polygon) \| [MultiPolygon](http://geojson.org/geojson-spec.html#multipolygon))>)** any LineString or Polygon
- `line2` **([Geometry](http://geojson.org/geojson-spec.html#geometry) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)<([LineString](http://geojson.org/geojson-spec.html#linestring) \| [MultiLineString](http://geojson.org/geojson-spec.html#multilinestring) \| [Polygon](http://geojson.org/geojson-spec.html#polygon) \| [MultiPolygon](http://geojson.org/geojson-spec.html#multipolygon))>)** any LineString or Polygon

**Examples**

Expand Down
4 changes: 2 additions & 2 deletions packages/turf-line-overlap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ var featureEach = require('@turf/meta').featureEach;
* Takes any LineString or Polygon and returns the overlapping lines between both features.
*
* @name lineOverlap
* @param {Feature<LineString|MultiLineString|Polygon|MultiPolygon>} line1 any LineString or Polygon
* @param {Feature<LineString|MultiLineString|Polygon|MultiPolygon>} line2 any LineString or Polygon
* @param {Geometry|Feature<LineString|MultiLineString|Polygon|MultiPolygon>} line1 any LineString or Polygon
* @param {Geometry|Feature<LineString|MultiLineString|Polygon|MultiPolygon>} line2 any LineString or Polygon
* @returns {FeatureCollection<LineString>} lines(s) that are overlapping between both features
* @example
* var line1 = {
Expand Down
12 changes: 10 additions & 2 deletions packages/turf-line-overlap/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ const fs = require('fs');
const path = require('path');
const load = require('load-json-file');
const write = require('write-json-file');
const featureEach = require('@turf/meta').featureEach;
const featureCollection = require('@turf/helpers').featureCollection;
const {featureEach} = require('@turf/meta');
const {featureCollection, lineString} = require('@turf/helpers');
const lineOverlap = require('./');

const directories = {
Expand Down Expand Up @@ -34,6 +34,14 @@ test('turf-line-overlap', t => {
t.end();
});

test('turf-line-overlap - Geometry Object', t => {
const line1 = lineString([[115, -35], [125, -30], [135, -30], [145, -35]]);
const line2 = lineString([[135, -30], [145, -35]]);

t.true(lineOverlap(line1.geometry, line2.geometry).features.length > 0, 'support geometry object');
t.end();
});

function colorize(features, color = '#F00', width = 6) {
const results = [];
featureEach(features, feature => {
Expand Down
4 changes: 2 additions & 2 deletions packages/turf-line-segment/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

# lineSegment

Creates a [FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) of 2-vertex [LineString](http://geojson.org/geojson-spec.html#linestring) segments from a [LineString](http://geojson.org/geojson-spec.html#linestring), [MultiLineString](http://geojson.org/geojson-spec.html#multilinestring), [MultiPolygon](http://geojson.org/geojson-spec.html#multipolygon) or [Polygon](http://geojson.org/geojson-spec.html#polygon).
Creates a [FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) of 2-vertex [LineString](http://geojson.org/geojson-spec.html#linestring) segments from a [(Multi)LineString](http://geojson.org/geojson-spec.html#linestring) or [(Multi)Polygon](http://geojson.org/geojson-spec.html#polygon).

**Parameters**

- `geojson` **([FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)&lt;([LineString](http://geojson.org/geojson-spec.html#linestring) \| [MultiLineString](http://geojson.org/geojson-spec.html#multilinestring) \| [MultiPolygon](http://geojson.org/geojson-spec.html#multipolygon) \| [Polygon](http://geojson.org/geojson-spec.html#polygon))>)** GeoJSON Polygon or LineString
- `geojson` **([Geometry](http://geojson.org/geojson-spec.html#geometry) \| [FeatureCollection](http://geojson.org/geojson-spec.html#feature-collection-objects) \| [Feature](http://geojson.org/geojson-spec.html#feature-objects)&lt;([LineString](http://geojson.org/geojson-spec.html#linestring) \| [MultiLineString](http://geojson.org/geojson-spec.html#multilinestring) \| [MultiPolygon](http://geojson.org/geojson-spec.html#multipolygon) \| [Polygon](http://geojson.org/geojson-spec.html#polygon))>)** GeoJSON Polygon or LineString

**Examples**

Expand Down
87 changes: 61 additions & 26 deletions packages/turf-line-segment/index.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
var flatten = require('@turf/flatten');
var featureEach = require('@turf/meta').featureEach;
var lineString = require('@turf/helpers').lineString;
var featureCollection = require('@turf/helpers').featureCollection;
var meta = require('@turf/meta');
var geomEach = meta.geomEach;
var featureEach = meta.featureEach;
var getCoords = require('@turf/invariant').getCoords;
var helpers = require('@turf/helpers');
var feature = helpers.feature;
var lineString = helpers.lineString;
var featureCollection = helpers.featureCollection;

/**
* Creates a {@link FeatureCollection} of 2-vertex {@link LineString} segments from a {@link LineString}, {@link MultiLineString}, {@link MultiPolygon} or {@link Polygon}.
* Creates a {@link FeatureCollection} of 2-vertex {@link LineString} segments from a {@link LineString|(Multi)LineString} or {@link Polygon|(Multi)Polygon}.
*
* @name lineSegment
* @param {FeatureCollection|Feature<LineString|MultiLineString|MultiPolygon|Polygon>} geojson GeoJSON Polygon or LineString
* @param {Geometry|FeatureCollection|Feature<LineString|MultiLineString|MultiPolygon|Polygon>} geojson GeoJSON Polygon or LineString
* @returns {FeatureCollection<LineString>} 2-vertex line segments
* @example
* var polygon = {
Expand All @@ -25,32 +29,63 @@ var getCoords = require('@turf/invariant').getCoords;
* var addToMap = [polygon, segments]
*/
module.exports = function (geojson) {
if (!geojson) throw new Error('geojson is required');

var results = [];
var index = 0;
featureEach(geojson, function (multiFeature) {
featureEach(flatten(multiFeature), function (feature) {
var coords = [];
var type = (feature.geometry) ? feature.geometry.type : feature.type;
switch (type) {
case 'Polygon':
coords = getCoords(feature);
break;
case 'LineString':
coords = [getCoords(feature)];
}
coords.forEach(function (coord) {
var segments = createSegments(coord, feature.properties);
segments.forEach(function (segment) {
segment.id = index;
results.push(segment);
index++;
});
});
switch (geojson.type) {
case 'FeatureCollection':
featureEach(geojson, function (feature) {
lineSegment(feature, results);
});
});
break;
case 'GeometryCollection':
geomEach(geojson, function (geometry) {
lineSegment(geometry, results);
});
break;
default:
lineSegment(geojson, results);
}
return featureCollection(results);
};

/**
* Line Segment
*
* @private
* @param {Geomtry|Feature<any>} geojson GeoJSON Feature or Geometry
* @param {Array} results push to results
* @returns {void}
*/
function lineSegment(geojson, results) {
switch (geojson.type) {
case 'Point':
case 'LineString':
case 'Polygon':
case 'MultiPoint':
case 'MultiLineString':
case 'MultiPolygon':
geojson = feature(geojson);
}
geomEach(flatten(geojson), function (geometry) {
var coords = [];
switch (geometry.type) {
case 'Polygon':
coords = getCoords(geometry);
break;
case 'LineString':
coords = [getCoords(geometry)];
}
coords.forEach(function (coord) {
var segments = createSegments(coord, geojson.properties);
segments.forEach(function (segment) {
segment.id = results.length;
results.push(segment);
});
});
});
}

/**
* Create Segments from LineString coordinates
*
Expand Down
12 changes: 9 additions & 3 deletions packages/turf-line-segment/test.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const test = require('tape');
const fs = require('fs');
const test = require('tape');
const path = require('path');
const load = require('load-json-file');
const write = require('write-json-file');
const featureEach = require('@turf/meta').featureEach;
const featureCollection = require('@turf/helpers').featureCollection;
const {featureEach} = require('@turf/meta');
const {featureCollection, lineString} = require('@turf/helpers');
const lineSegment = require('./');

const directories = {
Expand Down Expand Up @@ -38,6 +38,12 @@ test('turf-line-segment', t => {
t.end();
});

test('turf-line-segment - Geometry Object', t => {
const line = lineString([[115, -35], [125, -30], [135, -30], [145, -35]]);
t.true(lineSegment(line.geometry).features.length > 0, 'geometry object');
t.end();
});

// Preview 2-vertex LineStrings with colors
function colorSegments(segments) {
const results = featureCollection([]);
Expand Down

0 comments on commit edb53a5

Please sign in to comment.