Geographic Information Systems Asked by BritishSteel on May 23, 2021
Consider the following line (GeoJSON):
{
"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:
{
"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).
Openlayers is a javascript library that can check for geometry intersections. see here
Answered by neogeomat on May 23, 2021
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:
turf-line-segment
on both, turning them into a bunch of 2-vertex segmentsAnswered by jmcbroom on May 23, 2021
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:
turf.lineOverlap
. A bit of overlap tolerance has to be used here, in this case 1m was enough.turf.length
.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:
Answered by TomazicM on May 23, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP