6

I am using postgres with postgis and I have a set of points which I would like to interpolate with a set of lines.

Now my problem is that the set of lines are of type multilinestring.

Is there a way to convert them to linestring?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Ryan
  • 329
  • 1
  • 3
  • 10

2 Answers2

14

This answer shows a great way to convert multi-part to single-part.

Example for converting multilinestring to linestring with SRID 3857:

  • Check your table if there is any feature with more than 1 part:

    SELECT COUNT(CASE WHEN ST_NumGeometries(geom) > 1 THEN 1 END) AS multi_geom,
    COUNT(geom) AS total_geom
    FROM my_table;
    

If you see "multi_geom" in the result of the above statement is 0, it's safe to proceed to the next step. Otherwise, when multi_geom is larger than 0, you have features in my_table that can not convert safely to single part, you'll have to resolve those problem by hand before proceeding.

  • Convert:

    ALTER TABLE my_table
    ALTER COLUMN geom TYPE geometry(linestring,3857) USING ST_GeometryN(geom, 1);
    
Cao Minh Tu
  • 1,994
  • 4
  • 19
  • 33
3

" ST_Dump comes in very handy for expanding a MULTI geometry into single geometries. Below is an example. That expands a set of MULTIPOLYGONs into single POLYGONs

SELECT somefield1, somefield2, (ST_Dump(the_geom)).geom As the_geom FROM sometable "

http://www.bostongis.com/postgis_dump.snippet

user12711
  • 1,620
  • 3
  • 16
  • 26