-2

I am trying to convert MultiLineString to LineString with PostGIS:

enter image description here

This is my query:

SELECT (st_astext(ST_LineMerge( geometry ))) AS shape
FROM   public."Cities"
where id = 364087;

but the result still is MultilineString:

enter image description here

Finally, I want to make a polygon :

SELECT ST_MakePolygon(ST_MakeValid(st_astext(ST_LineMerge( geometry)))) AS shape
FROM   public."Cities"
where id = 364087;
8
  • Do not forget about the ST_Dump() function
    – Taras
    May 1, 2023 at 12:08
  • 1
    What's your question? You seem to have two tasks, but no problem description.
    – Vince
    May 1, 2023 at 12:10
  • this is my question: I want to convert MultilineString to lineString. the reason is : after converting I want to make a polygon @Vince May 1, 2023 at 12:12
  • 4
    Wanting to do something isn't a question. Not all multi-part lines resolve to single-part, and not all linestrings can be polygons. You haven't provided the WKT of the geometry or asked a question.
    – Vince
    May 1, 2023 at 12:18
  • 2
    The purpose of StackExchange is to answer common problems. It isn't to solve your problem, but to solve problems like your problem. Answerable questions need to have a description of the problem with a reproducible example. Adding a link to an external site from which the data will be removed does not improve an incomplete problem description. It might help you, but that requires that others violate best security practice and follow your link, and the next person who comes along won't be able to benefit.
    – Vince
    May 1, 2023 at 12:41

1 Answer 1

1

We can see on the shape drawing that the lines are disconnected, so you won't be able to convert it to a single simple linestring.

You can however convert each part of the multiline to a single line using st_dump, and then these simple lines can be converted to a polygon

with src(geom) as (values ('MULTILINESTRING ((0 0, 0 1, 1 1, 1 0, 0 0),(10 10, 10 11, 11 11, 11 10, 10 10))'::geometry))
SELECT st_asText(ST_MAKEPOLYGON(ln.geom))
FROM (
  SELECT (st_dump(geom)).geom
  FROM src) ln;
                st_astext
------------------------------------------
 POLYGON((0 0,0 1,1 1,1 0,0 0))
 POLYGON((10 10,10 11,11 11,11 10,10 10))
(2 rows)
8
  • How can I set mu geometry columnt to this part ? with src(geom) as (values ('MULTILINESTRING ((0 0, 0 1, 1 1, 1 0, 0 0),(10 10, 10 11, 11 11, 11 10, 10 10))'::geometry)) @JGH May 1, 2023 at 12:17
  • this line is for the example. Remove it, and replace from src by the query you are using to get the geometries
    – JGH
    May 1, 2023 at 12:22
  • I am using query like this pastebin.ubuntu.com/p/cxWcN2Jg5q but I got error [XX000] ERROR: lwpoly_from_lwlines: shell must be closed @JGH May 1, 2023 at 12:43
  • so your lines are not ready to be transformed to polygons (must be closed, i.e. the 1st and last points are the same) and have at least 4 points
    – JGH
    May 1, 2023 at 12:58
  • 1
    this would be a new, different question. Explain what you have, what you have tried (you are expected to make some research before asking) and what doesn't work.
    – JGH
    May 1, 2023 at 13:06

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