I have troubles handling with MultiLineStrings in QGIS.

I added the table where I defined a column as geometry(MultiLineString, 31468) as a postgis layer in QGIS. Now I want to create features with multiple linestrings. When I finish to create the feature (with right click), then I have to insert an id (which is a value for another column in the table). So I have to create the next linestring feature and to assign the same id, to have a kind of a multiple linestring.

id  |  geom
1   |  "linestring1"
1   |  "linestring2" 

I think this is not a proper way to work with multilinestrings!?

I want to have this:

  id  |  geom
  1   |  "linestring1" and "linestring2"

How can I create a MultiLineString in QGIS/postgis like this?

enter image description here


Select features in QGIS and click Merge Selected Features button on Advanced Digitizing toolbar.


You can make an SQL query into a layer. This means that, as your source data changes, your MultiLineString layer will change too.

  1. Open the DB Manager (Database -> DB Manager) and navigate to your database under PostGIS. My table is called linetest in this example.
  2. Open the SQL Window, and enter the following query:

    SELECT id, ST_Union(geom) AS newgeom FROM linetest GROUP BY id;

  3. Check the "Load as new layer" option, with:

    Column with unique integer values: id (which will be unique, because the GROUP BY clause will have grouped all like IDs).

    Geometry column: newgeom

    Layer name (prefix): (whatever you want to call the new layer)

  4. Click "Load now!", and the calculated layer should appear in the Layers pane, and render in the viewport. You may need to click "Retrieve columns" to get the column names for unique values and geometry to fill correctly.

The result layer will have MultiLineString geometry, except for those lines which had a single ID entry, which will be plain LineString geometry.

I don't have access to PostGIS, but I managed to get this to work under SpatiaLite. I used SpatiaLite's GUnion function instead of ST_Union, but that would have worked too.

DB Manager SQL query - QGIS 1.8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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