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:

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.