Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch for geometry support #711

Merged
merged 4 commits into from
May 3, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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](https://geojson.org/geojson-spec.html#feature-objects)<([LineString](https://geojson.org/geojson-spec.html#linestring) \| [MultiLineString](https://geojson.org/geojson-spec.html#multilinestring) \| [Polygon](https://geojson.org/geojson-spec.html#polygon) \| [MultiPolygon](https://geojson.org/geojson-spec.html#multipolygon))>** any LineString or Polygon
- `line2` **[Feature](https://geojson.org/geojson-spec.html#feature-objects)<([LineString](https://geojson.org/geojson-spec.html#linestring) \| [MultiLineString](https://geojson.org/geojson-spec.html#multilinestring) \| [Polygon](https://geojson.org/geojson-spec.html#polygon) \| [MultiPolygon](https://geojson.org/geojson-spec.html#multipolygon))>** any LineString or Polygon
- `line1` **([Geometry](https://geojson.org/geojson-spec.html#geometry) \| [Feature](https://geojson.org/geojson-spec.html#feature-objects)<([LineString](https://geojson.org/geojson-spec.html#linestring) \| [MultiLineString](https://geojson.org/geojson-spec.html#multilinestring) \| [Polygon](https://geojson.org/geojson-spec.html#polygon) \| [MultiPolygon](https://geojson.org/geojson-spec.html#multipolygon))>)** any LineString or Polygon
- `line2` **([Geometry](https://geojson.org/geojson-spec.html#geometry) \| [Feature](https://geojson.org/geojson-spec.html#feature-objects)<([LineString](https://geojson.org/geojson-spec.html#linestring) \| [MultiLineString](https://geojson.org/geojson-spec.html#multilinestring) \| [Polygon](https://geojson.org/geojson-spec.html#polygon) \| [MultiPolygon](https://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](https://geojson.org/geojson-spec.html#feature-collection-objects) of 2-vertex [LineString](https://geojson.org/geojson-spec.html#linestring) segments from a [LineString](https://geojson.org/geojson-spec.html#linestring), [MultiLineString](https://geojson.org/geojson-spec.html#multilinestring), [MultiPolygon](https://geojson.org/geojson-spec.html#multipolygon) or [Polygon](https://geojson.org/geojson-spec.html#polygon).
Creates a [FeatureCollection](https://geojson.org/geojson-spec.html#feature-collection-objects) of 2-vertex [LineString](https://geojson.org/geojson-spec.html#linestring) segments from a [(Multi)LineString](https://geojson.org/geojson-spec.html#linestring) or [(Multi)Polygon](https://geojson.org/geojson-spec.html#polygon).

**Parameters**

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