I have a shapefile with the Linestring elements which looks like this:
Here you see a lot of small lines (about 582 in total). Is there a way to extract pure end-to-end lines? For now, I tried to combine Linestring elements together as the first step by the following code:
def connect_lineparts(geom_list):
def dfs(adj_list, visited, vertex, result, key):
visited.add(vertex)
result[key].append(vertex)
for neighbor in adj_list[vertex]:
if neighbor not in visited:
dfs(adj_list, visited, neighbor, result, key)
adj_list = defaultdict(list)
for x, y in geom_list:
adj_list[x].append(y)
adj_list[y].append(x)
result = defaultdict(list)
visited = set()
for vertex in adj_list:
if vertex not in visited:
dfs(adj_list, visited, vertex, result, vertex)
return result
When I combine the result points together into Linestring I have something like this:
You can see there are some lines which were created by very distant points and absent of some small lines which were presented initially.