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
FROM
(SELECT
'CW' as name,
ST_GeomFromText('polygon((0 0, 0 30, 30 30, 30 0, 0 0))', 4326) g
UNION ALL
SELECT
'CCW' as name,
ST_GeomFromText('polygon((0 0, 30 0, 30 30, 0 30, 0 0))', 4326) g
) shapes
CROSS JOIN
(SELECT ST_GeomFromText('point(15 15)', 4326) point
UNION ALL
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?
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.