3

Consider the following line (GeoJSON):

enter image description here

{
"type": "FeatureCollection",
"name": "longline",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { }, "geometry": { "type": "LineString", "coordinates": [ [ 12.999790298013979, 51.989991736063956 ], [ 13.00086345470789, 51.989982154307761 ], [ 13.000786800658325, 51.989560557035155 ], [ 13.001649158715933, 51.989541393522764 ], [ 13.001725812765498, 51.989982154307761 ], [ 13.002703151897455, 51.989924663770587 ], [ 13.002559425554519, 51.98948390298559 ], [ 13.003623000492237, 51.989742610402871 ], [ 13.003402620099736, 51.990432496848953 ], [ 13.002894787021367, 51.990604968460474 ], [ 13.003479274149301, 51.991016983976891 ] ] } }
]
}

I have a second line that coincides exactly with a part of the longer line:

enter image description here

{
"type": "FeatureCollection",
"name": "shortline",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { }, "geometry": { "type": "LineString", "coordinates": [ [ 12.999790298013979, 51.989991736063956 ], [ 13.000461224116039, 51.989985745652334 ] ] } }
]
}

What I'd like to do is erasing that shorter part from the longer line.

This needs to be done in a web application, so I cannot use desktop software or server-side scripts.

I am open to a pure JavaScript solution or to a solution that involves Turf.js.

Keep in mind that the shorter line might have multiple vetices (not just start and end).

3 Answers 3

1

Really old question, but it somehow surfaced up, and for some turf.js exercise here is one possible way of doing it with turf.js library.

Logic is the following:

  1. Find overlapping line segment with turf.lineOverlap. A bit of overlap tolerance has to be used here, in this case 1m was enough.
  2. Calculate length of the overlapping segment and length of the long line with turf.length.
  3. Get the desired shortened long line with turf.lineSliceAlong, which gets a segment of a line from starting to ending point, where points are specified by distance from start of the line. In this case start point offset is overlapping line length, and end point offset is long line length.

JS code could then look something like this:

  var geoJSON1 = {
  "type": "FeatureCollection",
  "name": "longline",
  "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
  "features": [
  { "type": "Feature", "properties": { }, "geometry": { "type": "LineString", "coordinates": [ [ 12.999790298013979, 51.989991736063956 ], [ 13.00086345470789, 51.989982154307761 ], [ 13.000786800658325, 51.989560557035155 ], [ 13.001649158715933, 51.989541393522764 ], [ 13.001725812765498, 51.989982154307761 ], [ 13.002703151897455, 51.989924663770587 ], [ 13.002559425554519, 51.98948390298559 ], [ 13.003623000492237, 51.989742610402871 ], [ 13.003402620099736, 51.990432496848953 ], [ 13.002894787021367, 51.990604968460474 ], [ 13.003479274149301, 51.991016983976891 ] ] } }
  ]
  };
  
  var geoJSON2 = {
  "type": "FeatureCollection",
  "name": "shortline",
  "crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
  "features": [
  { "type": "Feature", "properties": { }, "geometry": { "type": "LineString", "coordinates": [ [ 12.999790298013979, 51.989991736063956 ], [ 13.000461224116039, 51.989985745652334 ] ] } }
  ]
  };


  var longLine = geoJSON1.features[0];
  var shortLine = geoJSON2.features[0];
  
  var overlapLine = turf.lineOverlap(longLine, shortLine, {tolerance: 0.001});
  var overlapLineLength = turf.length(overlapLine, {units: 'kilometers'});
  var longLineLength = turf.length(longLine, {units: 'kilometers'});
  var shortenedLongLine = turf.lineSliceAlong(longLine, overlapLineLength, longLineLength, {units: 'kilometers'});

This is then the result, red line being final shortened line:

enter image description here

0

Openlayers is a javascript library that can check for geometry intersections. see here

1
  • 1
    I am working with OL3. Finding the intersection is not the issue, the issue is getting rid of the intersecting part. Commented Oct 11, 2017 at 14:26
0

I would try to solve this with Turf's turf-line-segment: https://github.com/Turfjs/turf/tree/master/packages/turf-line-segment

If A is your longer feature and B your shorter:

  1. use turf-line-segment on both, turning them into a bunch of 2-vertex segments
  2. remove from A's segments if that segment appears in B
  3. glue the A segments back together

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.