Reputation: 13
I have a polygon in the map defined as GeoJSON. Need to know if a point is inside that polygon.
Polygon:
ST_AsGeoJSON(
'POLYGON ((
-69.62607888946572 -22.389720278609026,
-69.62632833490409 -22.39009971947581,
-69.6252903200153 -22.3904196394022,
-69.62501673469583 -22.390032759398007,
-69.62553708324471 -22.389888919122058,
-69.62607888946572 -22.389720278609026))')
Point:
ST_Point(-69.625137, -22.389777)
Need to know the correct way to ask Postgis this query.
We have tested different alternatives, some of them return the correct answer but don't know how they are different and which is the 'right way' (explanation of the difference between ST_Intersects, ST_Contains and ST_DWithin
will be appreciated) . Do I need to "SetSRID" the point and polygon in the queries?
1: && operator, returns true, incorrect value
select
ST_SetSRID(ST_GeomFromGeoJSON(ST_AsGeoJSON(
'POLYGON ((
-69.62607888946572 -22.389720278609026,
-69.62632833490409 -22.39009971947581,
-69.6252903200153 -22.3904196394022,
-69.62501673469583 -22.390032759398007,
-69.62553708324471 -22.389888919122058,
-69.62607888946572 -22.389720278609026))')), 4326) &&
ST_SetSRID(ST_Point(-69.625137, -22.389777), 4326)
2: ST_Intersects, returns false, CORRECT
select
ST_Intersects(
ST_SetSRID(
'POLYGON ((
-69.62607888946572 -22.389720278609026,
-69.62632833490409 -22.39009971947581,
-69.6252903200153 -22.3904196394022,
-69.62501673469583 -22.390032759398007,
-69.62553708324471 -22.389888919122058,
-69.62607888946572 -22.389720278609026))':: geography, 4326),
ST_SetSRID(ST_Point(-69.625137, -22.389777):: geography, 4326))
3: ST_Contains, returns false, CORRECT
select
ST_Contains(
ST_SetSRID(ST_GeomFromGeoJSON(ST_AsGeoJSON(
'POLYGON ((
-69.62607888946572 -22.389720278609026,
-69.62632833490409 -22.39009971947581,
-69.6252903200153 -22.3904196394022,
-69.62501673469583 -22.390032759398007,
-69.62553708324471 -22.389888919122058,
-69.62607888946572 -22.389720278609026))':: geography)), 4326),
ST_SetSRID(ST_Point(-69.625137, -22.389777), 4326))
4: ST_DWithin, returns false, CORRECT
select
ST_DWithin(
'POLYGON ((
-69.62607888946572 -22.389720278609026,
-69.62632833490409 -22.39009971947581,
-69.6252903200153 -22.3904196394022,
-69.62501673469583 -22.390032759398007,
-69.62553708324471 -22.389888919122058,
-69.62607888946572 -22.389720278609026))':: geography,
ST_Point(-69.625137, -22.389777):: geography,0)
In case you want to copy/paste in geojson.io:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
-69.625137,
-22.389777
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-69.62607888946572,
-22.389720278609026
],
[
-69.62632833490409,
-22.39009971947581
],
[
-69.6252903200153,
-22.3904196394022
],
[
-69.62501673469583,
-22.390032759398007
],
[
-69.62553708324471,
-22.389888919122058
],
[
-69.62607888946572,
-22.389720278609026
]
]
]
}
}
]
}
Upvotes: 1
Views: 1316
Reputation: 17906
&&
is a bounding box intersection operator. It builds the squared box minX,maxX, minY,maxY around the polygon and will check if the point is within this square.
st_intersects
is self explanatory.
st_contains
is similar for a point in polygon operation (though a point on the polygon edge is not considered as being contained in the polygon). It is very different for line to polygon or polygon to polygon, as the 1st feature has to be entierly inside the other one (while for an intersection, if part of geom 1 is in geom2, the intersection is true).
st_dwithin
checks if the minimum distance between the 2 geometries is less or equal the specified distance. With a value of 0, it is the same as st_intersects
.
Upvotes: 1