Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to merge linestring in the correct order in postGIS, but sometime they are reversed so something like this :

select astext(linemerge(ST_union(
ST_GeomFromText('LINESTRING(3 3, 2 2)'),
ST_GeomFromText('LINESTRING(1 1, 2 2)'))));

returns this :

"LINESTRING(1 1,2 2,3 3)"

But I want this :

"LINESTRING(3 3, 2 2, 1 1)"

I really don't know what to do,


share|improve this question

If you just want to reverse a geometry you can just use ST_Reverse (returns the geometry with vertex order reversed).

If the problem is you want to keep some arbitrary order, I don't think you can do it using ST_Union (the definition for ST_Union being point set union, as in ordering is not guaranteed).

share|improve this answer

Give it to diciu. I just want to add an example:

select ST_AsText(ST_Reverse(ST_GeomFromText('LINESTRING(3 3, 2 2)')));
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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