查询以了解点是否在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发送此查询的正确方法。

我们已经测试了不同的选择,其中一些返回正确的答案,但不知道它们有什么不同,这是“正确的方法”(将解释ST_Intersects,ST_Contains和ST_DWithin之间的区别是赞赏)。我需要在查询中“设置SRID”点和多边形吗?

1:&&运算符,返回正确的值

select
ST_SetSRID(ST_GeomFromGeoJSON(ST_AsGeoJSON( 
 'POLYGON ((
    -69.62607888946572 -22.389720278609026,-69.62607888946572 -22.389720278609026))')),4326) &&
ST_SetSRID(ST_Point(-69.625137,-22.389777),4326)

2:ST_Intersects,返回false,正确

select 
ST_Intersects(
ST_SetSRID(
 'POLYGON ((
    -69.62607888946572 -22.389720278609026,-69.62607888946572 -22.389720278609026))':: geography,4326),ST_SetSRID(ST_Point(-69.625137,-22.389777):: geography,4326))

3:ST_包含,返回false,正确

select 
ST_Contains(
ST_SetSRID(ST_GeomFromGeoJSON(ST_AsGeoJSON( 
 'POLYGON ((
    -69.62607888946572 -22.389720278609026,-69.62607888946572 -22.389720278609026))':: geography)),4326))

4:ST_DWithin,返回false,正确

select 
ST_DWithin( 
 'POLYGON ((
    -69.62607888946572 -22.389720278609026,ST_Point(-69.625137,0)

如果您要在geojson.io中复制/粘贴:

{
  "type": "FeatureCollection","features": [
    {
      "type": "Feature","properties": {},"geometry": {
        "type": "Point","coordinates": [
          -69.625137,-22.389777
        ]
      }
    },{
      "type": "Feature","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
            ]
          ]
        ]
      }
    }
  ]
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)