Turf: New module @turf/line-intersect

Created on 1 Mar 2017  路  15Comments  路  Source: Turfjs/turf

Proposing to create a new core module @turf/line-intersect

Description: Takes two GeoJSON LineStrings and returns the intersecting point(s).

Future use: Need to use this functionality in @turf/polygon-slice.

Branch: line-intersect

Geometry Support

  • LineString & MultiLineString & Polygon & MultiPolygon
  • Geometry & Feature & FeatureCollection

Dependencies

  • RBush (to quickly detect intersecting lines divided into pairs of 2 coordinates)
  • @turf/helpers
  • @turf/meta (using coordReduce & featureEach)
  • @turf/bbox-polygon (debugging purposes)

image

Benchmark

double-intersect x 76,313 ops/sec 卤0.86% (87 runs sampled)
feature-collection x 34,139 ops/sec 卤1.80% (89 runs sampled)
multi-linestring x 23,185 ops/sec 卤2.06% (84 runs sampled)
simple x 237,005 ops/sec 卤1.41% (86 runs sampled)

JSDocs

/**
 * Takes two GeoJSON LineStrings and returns the intersecting point(s).
 *
 * @name lineIntersect
 * @param {Feature<LineString>} line1 GeoJSON LineString Feature
 * @param {Feature<LineString>} line2 GeoJSON LineString Feature
 * @returns {FeatureCollection<Point>} point(s) that intersect both lines
 * @example
 * var line1 = {
 *   "type": "Feature",
 *   "properties": {},
 *   "geometry": {
 *     "type": "LineString",
 *     "coordinates": [
 *       [126, -11],
 *       [129, -21]
 *     ]
 *   }
 * };
 * var line2 = {
 *   "type": "Feature",
 *   "properties": {},
 *   "geometry": {
 *     "type": "LineString",
 *     "coordinates": [
 *       [123, -18],
 *       [131, -14]
 *     ]
 *   }
 * };
 * var points = turf.lineIntersect(line1, line2);
 * //= points
 */
export default function (line1, line2) {
new-module

Most helpful comment

You're on fire Denis, keep it up!

All 15 comments

Line Intersect - New Module! 馃帀

@turf/line-intersect module is now published under Turf 3.11.x.

More details on PR https://github.com/Turfjs/turf/pull/590

CC: @Turfjs/ownership

You're on fire Denis, keep it up!

Hi, thank you @DenisCarriere for this new module, but I'm trying to use it and I got error: "turf.lineIntersect is not a function". I'm using turf 3.5.1.

@marie15, you have to upgrade to the latest version.

Yep I forgot that, thank you :+1:

Hi, I'm trying to use this module, it's working good when I try that:
var line1 = { "type": "Feature", "properties": {}, "geometry": { "type": "LineString", "coordinates": [ [126, -11], [129, -21] ] } }; var line2 = { "type": "Feature", "properties": {}, "geometry": { "type": "LineString", "coordinates": [ [123, -18], [131, -14] ] } }; var point = turf.lineIntersect(line1, line2);
But when I tried with my real data, the intersection point is empty. Here is the code:
path1 = new L.Polyline(line1); path2 = new L.Polyline(line2); var interLines = turf.lineIntersect(path1, path1); if (interLines { console.log(interLines); }
In the console I got this result:
Objectfeatures: Array[0]
length: 0
__proto__: Array[0]
type: "FeatureCollection"
__proto__: Object

@marie15 I don't think you're using the proper way to load a GeoJSON into a Leaflet layer, try using this: http://leafletjs.com/examples/geojson/

@DenisCarriere in the Geometry support above, you mention that @turf/line-intersect supports:

  • listLineString & MultiLine
  • Feature & FeatureCollection

My understanding of the @turf/line-intersect documentation is that the geojson geometry needs either be:

  • FeatureCollection or Feature
  • LineString or MultiLineString or Polygon or MultiPolygon

I was planning that it could be used for selecting polygons of a multipolygon feature that intersect with a single polygon feature. Is that valid or is converting to lines or something else necessary to achieve?

@shawnmgoulet Thanks for pointing this out, ~I believe we decided to drop the Polygon support since it would create a bit of confusion on how the Polygons are being handle.~

~Easy solution that you can do right now is use the @turf/polygon-to-linestring module which will do exactly what the name of the module will do.~

~Note: Handling Multi-Polygons with holes will return a FeatureCollection of MultiLineStrings.~

Edit: @turf/line-intersect does work with Polygons & Multi-Polygons.

image

@shawnmgoulet Let me know if you have any issues when using Polygons, works for me.

Thanks @DenisCarriere - this is great.

My use case includes a polygon & multipolygon geojson.
CORRECTION: My use case includes 2 FeatureCollection :

(1) A FeatureCollection with a single polygon feature &
(2) a FeatureCollection with thousands of polygon features.

What I'm having to do is using (1) to select the intersecting polygons of (2) to then manipulate properties of those intersecting polygons of (2) based on properties of (1).

Still should work with these 2 FeatureCollection ?

polygon-select-polygons-multipolygon

@DenisCarriere looking more into this, it might have something to do with the multipolygon issues of JSTS mentioned in #702 by @rowanwins. I am getting the below error when running:

var polyIntersects = turf.lineIntersect(thePolygon, hexPolygons)
where thePolygon is the polygon feature and hexPolygons is the multipolygon feature collection

I'm going to have to continue testing alternative FeatureCollection approaches.

lineintersect-error-polygon-multipolygon

@shawnmgoulet interesting findings, feel free to send a PR with your failing test data.

Only thing you need to do is drop a GeoJSON with 2 features:
https://github.com/Turfjs/turf/tree/master/packages/turf-line-intersect/test/in

If it's the FeatureCollection handling, you could look into iterating over each feature using featureEach in @turf/meta, it's not the most efficient way to do things, but at least you won't have errors.

@shawnmgoulet another option would be to use @turf/combine to merge your entire feature Collection into a MultiPolygon/MultiLineString... let me know if that works or not!

Thank you for the suggestions @DenisCarriere. I'll update once I get an approach to do to solve this case.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

andrewharvey picture andrewharvey  路  3Comments

valeriik picture valeriik  路  4Comments

morganherlocker picture morganherlocker  路  5Comments

stebogit picture stebogit  路  5Comments

psi-gh picture psi-gh  路  4Comments