PostGIS – Calculate Extreme Point of Geography (Polygon) Type

polygonpostgispostgresqlsql

I have a table which include geography(polygon) type.

CREATE TABLE place (
  name varchar(100),
  geom GEOMETRY(POLYGON, 4326)
);
insert into place
SELECT 'place1', ST_GeomFromText('POLYGON((-71.1776585052917
 42.3902909739571,-71.1776820268866 42.3903701743239,
-71.1776063012595 42.3903825660754,-71.1775826583081 
42.3903033653531,-71.1776585052917 42.3902909739571))',4326);

enter image description here

I want to know following points of geometry(polygon)

  • most north point
  • most south point
  • most east point
  • most west point

enter image description here

( This seems to be commonly called "extreme point")

For example, in the above image, the coordinates (x, y) of the four corners of the rectangle are the information I want to know.
(But my applications data is not true rectangle.that has more corner.)

I checked document, I couldn't find suitable function.

https://postgis.net/docs/reference.html

Please tell me how to get or calculate extreme points using SQL.

Best Answer

You want ST_ENVELOPE as in

SELECT 'place1', st_asText(ST_Envelope(ST_GeomFromText('POLYGON((-71.1776585052917
42.3902909739571,-71.1776820268866 42.3903701743239,
-71.1776063012595 42.3903825660754,-71.1775826583081 
42.3903033653531,-71.1776585052917 42.3902909739571))',4326)));

Gives

POLYGON((-71.1776820268866 42.3902909739571,-71.1776820268866 42.3903825660754,-71.1775826583081 42.3903825660754,-71.1775826583081 42.3902909739571,-71.1776820268866 42.3902909739571))

enter image description here

UPDATE For those points you could use ST_OrientedEnvelope.

If you are not using Postgis 2.5 and don't have access to ST_ORIENTATEDENVELOPE you could get the points by looking for the intersection of the outer ring of the polygon and the envelope:

SELECT 'place1', st_asText(st_intersection(ST_ExteriorRing(ST_Envelope(ST_GeomFromText('POLYGON((-71.1776585052917
 42.3902909739571,-71.1776820268866 42.3903701743239,
-71.1776063012595 42.3903825660754,-71.1775826583081 
42.3903033653531,-71.1776585052917 42.3902909739571))',4326))),ST_ExteriorRing(ST_GeomFromText('POLYGON((-71.1776585052917
 42.3902909739571,-71.1776820268866 42.3903701743239,
-71.1776063012595 42.3903825660754,-71.1775826583081 
42.3903033653531,-71.1776585052917 42.3902909739571))',4326))));

gives

MULTIPOINT(-71.1776820268866 42.3903701743239,-71.1776585052917 
42.3902909739571,-71.1776063012595 42.3903825660754,-71.1775826583081 
42.3903033653531)

enter image description here

Related Question