Thursday, 29 September 2016

qgis - Merging lines with same name and intersect each other?


I have shp file with 50k lines (roads). Many roads have same name like 'high road' . some of them are the same road (they intersect each other) and some are located in different places of the city.



I want to merge all lines with same name and that intersect to single road. How can I do that? I have available SQL 2012 spatial with geometries and geographic data of roads and QGIS with shp file,


Someone told me in other post to use dissolve, but it looks like dissolve works on polygons only (i don't have selection for line vector layer)



Answer



I can describe how to do this in spatialite. You'll probably be able to adapt it to sql server. First import the shapefile into spatialite


.loadshp "roads" roads (code page) (srid)

Suppose the roads table now has columns: id as primary key, name, and geometry. Create a duplicate, empty table for the merge:


CREATE TABLE roads_merge (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT);
SELECT AddGeometryColumn('roads_merge','geometry',(srid),'LINESTRING','XY');


Now insert into the new table the columns that intersect with identical names:


INSERT INTO roads_merge (name, geometry) 
SELECT r1.name, GUnion(r1.geometry, r2.geometry)
FROM roads AS r1, roads AS r2
WHERE r1.id <> r2.id AND ST_Intersects(r1.geometry, r2.geometry) AND r1.name = r2.name;

And now insert all the single roads:


INSERT INTO roads_merge(name, geometry)
SELECT r1.name, r1.geometry
FROM roads AS r1, roads AS r2

WHERE r1.id <> r2.id AND Disjoint(r1.geometry, r2.geometry) AND r1.name <> r2.name;

I would probably first create a spatial index and use it in above the query.


HTH, Micha


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...