[postgis-users] Converting Multilinestrings to Linestrings

1,674 views
Skip to first unread message

Leigh Holcombe

unread,
May 7, 2010, 5:57:09 AM5/7/10
to postgi...@postgis.refractions.net

Hello,
I'm pretty new at PostGIS, and this is my first post to the users list. I recently uploaded a street network into my database using the shp2pgsql function, in order to do some georeferencing for a transportation project. The streets network is composed of line segments going from intersection to intersection. I assumed these would be imported as linestrings. However, they have the geometry type multilinestring. Which seems inappropriate, because their format suggests that they are really just linestrings in disguise. To explain, here's a sample geometry:
MULTILINESTRING((1275799.56 245381.12,1275819.04 246040.71))

Ultimately, what I'd like to do is a simple line_interpolate_point - which seems like it would work if I could just get rid of that MULTI. Is there a way to force linestring data to be imported as a linestring instead of as a multilinestring? Is there a way to transform all the multilinestrings in a dataset into linestrings? Is there a way to convert a multilinestring into a linestring easily and quickly on the fly (I'm doing PHP/LAPP programming)? Another thought was that I could do string parsing on the text, and then pass the data to line_interpolate_point as a string, rather than an object - does that even work?

Thanks in advance for any advice you might have.

Leigh Holcombe

_______________________________________________
postgis-users mailing list
postgi...@postgis.refractions.net
http://postgis.refractions.net/mailman/listinfo/postgis-users

strk

unread,
May 7, 2010, 6:05:37 AM5/7/10
to PostGIS Users Discussion
On Fri, May 07, 2010 at 02:57:09AM -0700, Leigh Holcombe wrote:

> Is there a way to force linestring data to be imported as a linestring instead
> of as a multilinestring?

Yes, shp2pgsql -S

> Is there a way to transform all the multilinestrings in a dataset into linestrings?

1. Drop the type constraint (enforce_geotype_xxxx)
2. Update the geometries
3. Re-insert the type constraint
4. Update the geometry_columns record


> Is there a way to convert a multilinestring into a linestring easily and quickly
> on the fly (I'm doing PHP/LAPP programming)?

ST_GeometryN(the_geom, 1) gives you the first LINESTRING of the (pseudo)set.

--strk;

() Free GIS & Flash consultant/developer
/\ http://strk.keybit.net/services.html

Paragon Corporation

unread,
May 8, 2010, 12:33:53 AM5/8/10
to PostGIS Users Discussion
Leigh,

If your multilinestrings contain more than one linestring each, then use
ST_Dump instead of ST_GeometryN. You'll probably want to create a new table
as well to explode the single row into multiple rows.

Then to insert into the new table the insert would be

INSERT INTO newtable(field1,field2, original_gid, the_geom)
SELECT field1, field2, gid, (ST_Dump(the_geom)) .geom
FROM oldtable;

http://www.postgis.org/documentation/manual-svn/ST_Dump.html

Leo and Regina
http://www.postgis.us
Reply all
Reply to author
Forward
0 new messages