I need to check if a specific point belongs to the area defined by a polygon on a map using PostGIS. How can I specify if the area to be considered is either the one "inside" the polygon, or the one "outside" the polygon?

I thought that the order of vertices could matter, depending on the way in which vertices are listed: clockwise or counter-clockwise. But with both expressions of the polygon I get always the same result. To test, I tried to check if the points (15 15) and (50 50) belong to the areas obtained from the square (0 0, 0 30, 30 30, 30 0, 0 0), giving the vertices in two orders, as in this code:

SELECT ST_AsText(point), name
       'CW' as name, 
       ST_GeomFromText('polygon((0 0, 0 30, 30 30, 30 0, 0 0))', 4326) g
      'CCW' as name, 
       ST_GeomFromText('polygon((0 0, 30 0, 30 30, 0 30, 0 0))', 4326) g
  ) shapes
  (SELECT ST_GeomFromText('point(15 15)', 4326) point
   SELECT ST_GeomFromText('point(50 50)', 4326) point
  ) points
WHERE ST_Within(point,G)

Eventually, the point (15 15) belongs to both the squares CW and CCW, while the point (50 50) doesn't belong to any of the two.

I cannot use ST_Intersects or ST_Disjoint depending on the polygon, because I have hundreds of footprints with very irregular shape (i.e., the footprints came from the products of the Sentinel mission S1, S2, ecc. of Copernicus portal of ESA), and I need a general criteria.

Is there a way to let vertices order matter?

  • 1
    If st_within is true the point is inside, if false outside.
    – MrXsquared
    Jan 16, 2022 at 9:58
  • Of course, but I'm searching for a method to specify, given a polygon, if the query should use the area inside or the area outside. The point of the example code is that either the vertices are given clockwise, or counter clockwise, the result is always the same, i.e. the area considered is the one inside the polygon.
    – danilod
    Jan 16, 2022 at 11:01
  • Sorry i dont understand the logic. You cannot invert the vertices order to create a negative of the polygon, which btw would then cover the entire world. If you really want to go this way you need to define a polygon as area of interest and run difference on your actual polygons. The result would then be defined as the area outside of your actual polygons.
    – MrXsquared
    Jan 16, 2022 at 11:25
  • that's exactly the logic. I need for example to define two polygons, one for the footprint that covers the area of the country Italy, and one for the footprint that covers the area of all the world except Italy. Of course, the polygons of these two areas are the same. But I cannot specify during the query phase if I want inside or outside, because I have hundreds of polygons, so I need to codify this info in the polygon itself.
    – danilod
    Jan 16, 2022 at 12:13
  • 1
    Precalculate the area of your footprints. Defne the area of the world (e.g. Wikipedia). Then implement logic around the lines of CASE WHEN ST_Intersects(<point>, <footprint>) THEN <footprint_area> ELSE <world_area> - <footprint_area> END. That is, if the actual area metric is what you are after.
    – geozelot
    Jan 16, 2022 at 15:02


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy