Skip to main content
deleted 1 character in body
Source Link
TomazicM
  • 27k
  • 24
  • 31
  • 42

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

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

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

Source Link
TomazicM
  • 27k
  • 24
  • 31
  • 42

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