I have a PostGIS polygon which has to be extended using two points like on the picture:

My original polygon is: POLYGON((10 20,10 10,20 10,20 20,10 20))

The points I would like to extend the polygon with: POINT ( 20 30 ) and POINT ( 10 30 )

It's very important that I would like to insert the Point2 as the first and the last point of the polygon. The Points1 should be in the last but one position. So the position of the new points is crucial.

What do you think, what is the best method to do this? Any suggestions?

enter image description here

UPDATE: I've find a solution, it works, but a little bit complicated:

SELECT ST_MakePolygon(ST_AddPoint(ST_AddPoint(ST_AddPoint(ST_RemovePoint(ST_ExteriorRing( ST_GeomFromText('POLYGON((10 20,10 10,20 10,20 20,10 20))')),
ST_NPoints(ST_GeomFromText('POLYGON((10 20,10 10,20 10,20 20,10 20))'))-1),ST_MakePoint(20,30)),ST_MakePoint(10,30),0),
  • For a single polygon only? No updating a whole table, and no extra point data coming from other tables/columns?
    – geozelot
    Jan 27, 2021 at 18:03
  • @geozelot. No, now I have to append two points to the polygon. However, the polygon will be updated very often with new points in the future. It is like a tracklog, but it's a polygon, not a linestring because it's width is not constant value.
    – zsoooc
    Jan 27, 2021 at 18:32
  • 1
    Your solution is probably as good as can be. One alternative is to keep the points as a LineString (which is slightly simpler to extend) and then create the polygon when needed.
    – dr_jts
    Jan 31, 2021 at 19:56


Your Answer

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

Browse other questions tagged or ask your own question.