I have a polygon and a line that goes through it. That line might exit my polygon and enter it again at a later point.
So now I'd like to have a relative position inside my line, where those segments are.
As an example. I have the following polygon and a line. My result should be TWO segments, where segment one is located at about 0.1 - 0.4% of my line and segment two is located at about 0.6 - 0.9% of my line.
How can I do that using postgis?
What I've tried so far:
ST_Intersections
-> I do get the segments, but not the indexes.
ST_LineLocatePoint
-> I would get the index of a point (e.g. the start and end-points of my segments)
Problem with that is that if my line crosses itself, I get wrong results, as those start/end points would exist at least twice.
So what I can imagine would be nice is a function (I haven't found it and don't know if it exists) that would give me indexes of relative positions of matches as well. Am I just blind? Does such a function exist?
EDIT:
So this here is a sample of a polygon I want to cut with a line. It gives me all parts of my line. Inside and outside, which would help me in my further analysis. But the order of ST_Split
in this case is just not correct. All sub-segments I get from it are in correct order, expect the last two in that example. With other examples the order is reversed. But I NEED to know the proper order to tell where in my original line that intersection happened.
"type": "LineString",
"coordinates": [
[
14.965792894363402,
47.526699241115935
],
[
14.970245361328125,
47.52540968472776
],
[
14.967606067657472,
47.52504019924539
],
[
14.968990087509153,
47.52488081254758
],
[
14.966779947280882,
47.52460550711063
],
[
14.972037076950071,
47.52419254624608
],
[
14.97059941291809,
47.523286918933046
],
[
14.973410367965698,
47.52304783071382
]
]
}'), st_geomfromgeojson('{
"type": "Polygon",
"coordinates": [
[
[
14.96858239173889,
47.52632976471708
],
[
14.965996742248535,
47.52501121988183
],
[
14.96784210205078,
47.52302609536715
],
[
14.971328973770142,
47.52361294656684
],
[
14.96858239173889,
47.52632976471708
]
]
]
}')));
ST_Intersection
will return a collection (aMultiLineString
in the natural case of multi-overlay between areal and lineal features), and the underlying engine should keep order when creating the intersecting segments:ST_DumpPoints
on that returned geometry returns ageometry_dump
, having apath
field of typeNUMERIC[]
, wherepath[1]
is the geometry sequence number within the collection, andpath[2]
the vertex sequence number of the given member. The same goes forST_Dump
, having onlypath[1]
- with these, you should be able to identify the resulting segments.ST_DumpPoints
gives me all the points each and thatST_Dump
will give me the whole sequences as linestrings. My main problem: So if my input is a number of points or a linestring (the results of the dump, so basically a sub-line of my original line) how can I get the relative start/end inside the originating line? So how can I say, given that input line, and this sub-line-string -> where inside that input line is that sub-line-string? Which are the relative start/end positions?ST_Split
(but i've seen the same forST_Intersection
) where the order of the result is just not correct. But without that proper order, I can't find the exact places where inside my original line, my intersection lies. Would be very very glad for any further advice!