I have a trail that is split up into multiple small parts. On some of the parts i have managed to get the traile name as attribute "text". The rest has text as "null". Now I would like to join all lines in the trail that has close enough endpoints and that has either share the "text" or has the text as null.
Example of attribute "text" in lines that all have close endpoints:
- null
- null
- Trail1
- null
- Trail1
- null
- null
- Trail2
- null
- Trail2
In the above list I would like to group:
- 1-7 with a common attribute "text" = Trail1
- 8-10 with a common attribute "text" = Trail2
EDIT - Added illustration(Left part of the long black line should be grouped as Trail1, right part as Trail2:
I have used ST_ClusterDBSCAN(geom, eps := 5, minpoints := 2) to group by distance before but not on attributes like this. If i would run it on the example above it would put all lines in the same entity Wich is an issue.
The queryes i have been using this far.
1.This query joins two tables, one with all of the small trail parts and one with the texts, so im joining the texts that are within 50 meters from a trail and assume that is the trail name. This gives me the data in the illustration above.
DROP TABLE IF EXISTS "lines_with_text";
CREATE TABLE "lines_with_text" AS
SELECT
lines.id,
max(lines.location) as location,
max(texts.text) as text,
lines.geom
FROM
"line_table" as lines
LEFT OUTER JOIN (SELECT * FROM "text_table") AS texts ON ST_DWithin(texts.geom, lines.geom,50)
GROUP BY lines.id, lines.geom
2.Then i tried to merge them with ST_ClusterDBSCAN but this does not give me the desiered result. This will join all lines that are close together an will split them when two lines are not within 20 meters. I would instead split when a new trailname is found rather than when the distance is to long. Usally the lines are between 0-2 meters apart from each other(they are not perfectly aligned but sometimes they are more than 20 meters apart and then i have accepted that it will be a new polyline. Maby with the same Trail name as the prior one.
DROP TABLE IF EXISTS with_text_joined;
CREATE TABLE with_text_joined AS
SELECT
min(location) as location,
ST_Union(geom) as geom,
min(text) as text
FROM (
SELECT
location,
geom,
text,
ST_ClusterDBSCAN(geom, eps := 20, minpoints := 2) OVER() AS cluster_id
FROM "lines_with_text" as lines
) sq
GROUP BY cluster_id