Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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 for 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?

share|improve this question
For PostGIS check gis.stackexchange.com/questions/1387/… –  underdark Dec 2 '11 at 17:44
If this Question were asked today I think it would be closed as too broad. Rather than try to close it now, I am going to edit it to retrofit the existing Answers and prevent broader Answers. –  PolyGeo Jun 3 at 6:53
add comment

3 Answers 3

up vote 3 down vote accepted

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:

ST_AsText( ST_Multi( ST_Collect( u.wkb_geometry ) ) ) as multilines 
FROM ushwys u
GROUP BY u.name, u.state 
ORDER BY u.state 

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.

P.S. 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= 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 geometires into multipart-geometries when the dissolve task needs to consider multiple fields.

Cheers. :)

share|improve this answer
add comment

In PostGIS, there is a dedicated function for merging lines

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


share|improve this answer
add comment

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

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

share|improve this answer
add comment

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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