7

I have a road centerline shapefile that has roads split at every intersection. However, I would like to join LINESTRINGs that have a common set of attributes into a single LINESTRING. So long as the name, speed limit, etc, do not change, I want to merge them together. This is for eventual conflation into OpenStreetMap.

Esri's ArcGIS Desktop software seems to allow this with the Dissolve command and the UNSPLIT_LINES option.

I also found a Dissolve command in QGIS but it seems to only work on polygons and single fields.

Assuming nothing exists for QGIS, does anyone know how to do it in PostGIS or GRASS?

0

3 Answers 3

5

Interesting question. I don't consider myself a PostGIS guru, but I toyed with your problem and came up with the following query that dissolves highway LINESTRING records into MULTILINESTRING records when they have common values in multiple fields (in my dataset, I matched on the name and state fields). I used OGR to push a highways shapefile (renamed to ushwys) into PostgreSQL; so my geometry field is called wkb_geometry. Try hacking this query to accomodate your data and field conditions:

SELECT 
u.name, 
u.state, 
ST_AsText( ST_Multi( ST_Collect( u.wkb_geometry ) ) ) as multilines 
FROM ushwys u
GROUP BY u.name, u.state 
ORDER BY u.state 
LIMIT 100; --########### DROP THE LIMIT WHEN YOU'RE DONE EXPERIMENTING

If this works, you could use ogr2ogr to perform this query and export the results most any vector format you prefer, such as shapefile, GML, CSV or otherwise. For info on calling SQL queries from ogr2ogr, check out the OGR SQL documentation.

As a matter of reference, I reviewed the PostGIS ST_Collect instruction, as well as as this site that demonstrates grouping on multiple fields.

If you find redactions of this query do not work for you, please let me know and I'll strike my answer so it doesn't linger around and confuse people.


If you're not familiar with using ogr2ogr to push geodata into PostGIS, I used the following ogr2ogr script to perform my data import (beware copying from the web and pasteing directly into your ogr2ogr command window, as I've found the page formatting introduces linebreaks and font/character substitutions for double-quotes that break the ogr script):

ogr2ogr -f "PostGreSQL" PG:"host=127.0.0.1 user=postgres dbname=gisdb password=my_password" 
"E:\GISData\UnitedStates\highways.shp" -nln ushwys -nlt geometry

I wanted to see how this "looked" in QGIS after running that query, so I modded the query to apply this WHERE clause (which gives me all variants of US Highway 65 in the state of Missouri):

where u.state = 'MO' and u.name LIKE '%US%65%'

Next I used QGIS and the QuickWKT plugin to visualize all eight (8) of my resulting highway MULTILINESTRING records. As you can see in the screenshot, the final results do take Hwy 65 from Missoui's northern border with Iowa all the way to her southern border with Arkansas:

enter image description here

To me, this visualization demonstates that my query didn't produce any unexpected duplication or elimination of features. So next I wondered "ok, the dissolve returned eight features, but how many features in the original table actually represent Hwy 65 in Missouri?" My next query answered that question. It seems the original dataset uses thirty-eight (38) features to represent Hwy 65 in Missouri:

SELECT count(*) FROM ushwys WHERE state = 'MO' AND name LIKE '%US%65%';
-- returned 38

On final analysis, the query reduced thirty-eight (38) features to eight (8) features that shared values in the name and state fields. At this point, I'm relatively confident the this query is useful and appropriate for dissolving single-part geometries into multipart-geometries when the dissolve task needs to consider multiple fields.

3

In PostGIS, there is a dedicated function for merging lines

ST_LineMerge - Returns a (set of) LineString(s) formed by sewing together a MULTILINESTRING.

http://www.postgis.org/docs/ST_LineMerge.html

1

In GRASS GIS, take a look at v.build.polylines

v.build.polylines can be used to rebuild broken polylines

Not the answer you're looking for? Browse other questions tagged or ask your own question.