如何通过纬度和经度 (Postgis) 从 table 中提取多边形 select

How to select polygon from table by Latitude and Longitude (Postgis)

有多边形table。我需要从 table 中找到一条记录,其中某个点 (Lat/Long) 位于区域内。

坐标示例:149.14668176,-35.32202098

你能帮我写 select 字符串来找到包含我的点的区域吗?

 SELECT PostGIS_full_version();
                                                                                             postgis_full_version                                                                                               
 POSTGIS="2.5.2 r17328" [EXTENSION] PGSQL="96" GEOS="3.5.1-CAPI-1.9.1 r4246" PROJ="Rel. 4.9.3, 15 August 2016" GDAL="GDAL 2.1.2, released 2016/10/24" LIBXML="2.9.4" LIBJSON="0.12.1" LIBPROTOBUF="1.2.1" RASTER

类似的东西:

SELECT id,name FROM area_polygon WHERE ST_Within('149.14668176, -35.32202098', geog);
bounds=# \d bounds.area_polygon;
id           | integer                 |           | not null |
geog         | geography(Polygon,4283) |           |          |
name         | text                    |           |          |

我预计:

  id  | name
------+--------
    1 | Alabama

st_within 只支持几何类型,这就是为什么你在前面的答案中得到错误,因为你有一个地理列类型。

您可以转换为几何:

SELECT id,name 
FROM area_polygon 
WHERE ST_Within(ST_SetSRID(ST_POINT(149.14668176,-35.32202098),4283), geog::geometry);

或者您可以使用 st_dwithin,距离设置为零:

SELECT id,name 
    FROM area_polygon 
    WHERE ST_DWithin(ST_SetSRID(ST_POINT(149.14668176,-35.32202098),4283)::geography, geog,0);

请注意,坐标的顺序必须是 lon/lat(而不是 lat/lon),我假设这些坐标在您的 SRID 4283 中。它们必须与 geog SRID 匹配或者是转换成它...

有关哪些函数支持哪些参数的列表,请参阅 here