5

In QGIS, I've tried the following options to convert my GeoJSON file from MultiLineString to one or multiple LineStrings (translating the Danish version):

Vector -> Geometry Tools -> Polygons to Lines

Vector -> Geometry Tools -> Multipart to Single parts

Vector -> Geoprocess Tools -> Dissolve

Each option produces just the same MultiLineString.

How do I convert the MultiLineString to LineString(s) using either QGIS or another GIS software?

http://bl.ocks.org/d/880c32ce5acfd18a289564c39f7b4f16

7
  • 1
    Multipart to single parts should do it. Could you give some test data?
    – user30184
    Commented Feb 11, 2018 at 11:01
  • I've added the GeoJSON-file at the bottom in the question.
    – Saud
    Commented Feb 11, 2018 at 11:55
  • 1
    You seem to have 5 features in your GeoJSON (which was a bit tedious to extract from the web page that you linked). Four of those features are points (shelters) and one is a MultiLinestring. The MultiLinestring has only one component so there should be nothing special in changing it into a LineString. Could you have another try and give a direct download link to some GeoJSON file that makes trouble for you and that contains only MultiLinestrings? QGIS cannot handle both points and lines on the same layer so if you have used this GeoJSON it is hard to say how QGIS is handling it.
    – user30184
    Commented Feb 11, 2018 at 13:35
  • I've added the GeoJSON file in QGIS as vector and only kept the MultiLineString. I've give it another try and send a link to the GroJSON only containing the MultiLineString.
    – Saud
    Commented Feb 11, 2018 at 15:03
  • Sorry, but I do not see such link. I get still an application with a MapBax raster background.
    – user30184
    Commented Feb 11, 2018 at 16:18

3 Answers 3

7

It shouldbe your second option, Multipart to Single Part. Notice, however, that this will only work with Multilinestrings whose individual Linestrings are spatially connected. To be spatially connected means that either the start or the end point of a line shall have the same coordinates as either the start or end point of another line, as such:

# Line A and Line B are connected
Linestring A (0 0, 1 1)
Linestring B (1 1, 2 3)

# Line A and Line B are disconnected
Linestring A (0 0, 1 1)
Linestring B (1 2, 2 3)

What Multipart to Single Part does is transform your one multiline feature into one single line feature, but if the individual linestrings are disconnected, it can't return one single feature, thus it returns a multiline. There are two things you can do from here:

1. Your Multiline really should be only one linestring feature

This means you'll have to edit your data. In QGIS, go to Properties -> Styles, add another line style on top of your current one, and change it to Marker Style. Then set it to last vertex only, and change the shape of it from a ball to a sideways triangle.

With this, you know the start and end of each individual line in your multiline feature. Then check for the arrows along your line, these are the intersections. Edit the layer, with snap enabled, and make sure each point with a triangle properly connects to the first point of the next line. Only the first and last points of the entire multiline should not need be connected to anything. Then try the Mulyipart to Single Part again.

2. I don't care about the number of features, I just want simple Linestrings

This means a conversion from one Multiline feature to many single Line features. QGIS doesn't do this (afaik), but PostGIS does. You should already have it installed together with QGIS, so create a database, upload your layer (easiest done with QGIS' DB Manager plugin), then execute:

SELECT (ST_DUMP(geom).geom) as geom
FROM your_table_name

The result should be a bunch of binary-code features. These are your Linestrings in WKB format. Below the result panel there is an option to load the result back into QGIS. Just choose a layer name and click Load. Notice, however, that this loaded layer is virtual, you'll need to save it as a shapefile or sometging if you want to retain it after you quit QGIS.

1
  • I don't know if newer versions of QGIS changed this, but Multiparts to Singleparts will break the MultiLineString up into multiple LineString features. Your first section implies that if the feature can't be broken up it is just retained as a MultiLineString feature. Commented May 30 at 13:13
3

You can use GDAL command line tools "ogrinfo" and "ogr2ogr" . First check the layer name from your GeoJSON data with ogrinfo

ogrinfo test.json
INFO: Open of `test.json'
      using driver `GeoJSON' successful.
1: test

Then you can check what geometry types are present in data on layer "test"

ogrinfo -dialect sqlite -sql "select distinct ST_GeometryType(geometry) from test" test.json
INFO: Open of `test.json'
      using driver `GeoJSON' successful.

Layer name: SELECT
Geometry: None
Feature Count: 2
Layer SRS WKT:
(unknown)
ST_GeometryType(geometry): String (0.0)
OGRFeature(SELECT):0
  ST_GeometryType(geometry) (String) = POINT Z

OGRFeature(SELECT):1
  ST_GeometryType(geometry) (String) = MULTILINESTRING Z

Finally select the multilinestrings and save them into a new GeoJSON file as single part linestrings

ogr2ogr -f geojson -dialect sqlite -sql "select * from test where  ST_GeometryType(geometry)='MULTILINESTRING Z'" -explodecollections -nln lines new.json test.json

Check the result with ogrinfo:

ogrinfo new.json -al
INFO: Open of `new.json'
      using driver `GeoJSON' successful.

Layer name: lines
Geometry: Line String
Feature Count: 1
Extent: (13.317425, 58.521537) - (13.438575, 58.629173)
Layer SRS WKT:
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0,
        AUTHORITY["EPSG","8901"]],
    UNIT["degree",0.0174532925199433,
        AUTHORITY["EPSG","9122"]],
    AUTHORITY["EPSG","4326"]]
name: String (0.0)
OGRFeature(lines):0
  name (String) = (null)
  LINESTRING Z (13.4147787 58.5979263 280,13.4145641 58.597658 278

Seems to work and what used to be MULTILINESTRING Z is now LINESTRING Z. Ogr2ogr with option -explodecollections creates one linestring for each part of multilinestring and copies the attributes which is probably what you want. In this example the multilinestring had only one linestring member and no explosion really happened.

If your data contained both linestrings and multilinestrings, modify the SQL

where ST_GeometryType(geometry)='MULTILINESTRING Z' OR ST_GeometryType(geometry)='LINESTRING Z'

3

If you prefer to use a software with GUI, you can consider to use OpenJUMP which is a nice little program for editing vectors.

OpenJUMP can handle all sort of geometries on the same layer. You can see 4 points and on multilinestring here. Notice the symbol with two linestrings.

enter image description here

Select the geometry and use the geometry conversion tool.

enter image description here

All done. Notice the linestring symbol and WKT presentation of the converted geometry. OpenJUMP can save the result back to GeoJSON. QGIS can also open the OpenJUMP's native JML format with GDAL through "Add vector layer".

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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