查询以了解点是否在 Postgis 2.5 中的多边形内
Query to know if point is inside a polygon in Postgis 2.5
我在地图中有一个定义为 GeoJSON 的多边形。需要知道一个点是否在该多边形内。
多边形:
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))')
积分:
ST_Point(-69.625137, -22.389777)
需要知道向 Postgis 询问此查询的正确方法。
我们测试了不同的备选方案,其中一些 return 是正确答案,但不知道它们有何不同,哪个是 'right way'([=41= 之间差异的解释]、ST_Contains 和 ST_DWithin
将不胜感激)。我是否需要“SetSRID”查询中的点和多边形?
1: && 运算符,return为真,不正确的值
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,return 错误,正确
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,return 错误,正确
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,return 错误,正确
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)
如果您想 copy/paste 在 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
]
]
]
}
}
]
}
&&
是边界框相交运算符。它在多边形周围构建方框 minX,maxX, minY,maxY 并将检查该点是否在该正方形内。
st_intersects
不言自明。
st_contains
类似于多边形操作中的点(尽管多边形边缘上的点不被视为包含在多边形中)。线到多边形或多边形到多边形是非常不同的,因为第一个特征必须完全在另一个特征内部(而对于交叉点,如果 geom 1 的一部分在 geom2 中,则交叉点为真)。
st_dwithin
检查 2 个几何之间的最小距离是否小于或等于指定距离。值为0时,等同于st_intersects
.
我在地图中有一个定义为 GeoJSON 的多边形。需要知道一个点是否在该多边形内。
多边形:
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))')
积分:
ST_Point(-69.625137, -22.389777)
需要知道向 Postgis 询问此查询的正确方法。
我们测试了不同的备选方案,其中一些 return 是正确答案,但不知道它们有何不同,哪个是 'right way'([=41= 之间差异的解释]、ST_Contains 和 ST_DWithin
将不胜感激)。我是否需要“SetSRID”查询中的点和多边形?
1: && 运算符,return为真,不正确的值
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,return 错误,正确
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,return 错误,正确
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,return 错误,正确
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)
如果您想 copy/paste 在 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
]
]
]
}
}
]
}
&&
是边界框相交运算符。它在多边形周围构建方框 minX,maxX, minY,maxY 并将检查该点是否在该正方形内。
st_intersects
不言自明。
st_contains
类似于多边形操作中的点(尽管多边形边缘上的点不被视为包含在多边形中)。线到多边形或多边形到多边形是非常不同的,因为第一个特征必须完全在另一个特征内部(而对于交叉点,如果 geom 1 的一部分在 geom2 中,则交叉点为真)。
st_dwithin
检查 2 个几何之间的最小距离是否小于或等于指定距离。值为0时,等同于st_intersects
.