PostGIS ST_Contains无法返回数据

时间:2015-07-02 12:23:53

标签: postgresql postgis

我使用PostGIS 2.1.7和PostgreSQL 9.4遇到了一个奇怪的案例。

我尝试使用以下查询匹配加利福尼亚州内的一个点(纬度:36.671112,经度:-121.662636,我们可以在此地图上看到:https://www.google.com/maps/d/viewer?mid=zHggIvE-1G0A.kW1YFh5cNOJU&usp=sharing):

select name
from zones
where ST_Contains(area, ST_SetSRID(ST_Point(-121.662636, 36.671112), 4326))

其中zones的定义如下:

create table zones (
  name text,
  kml text,
  area geometry
)

insert into zones (name, kml)
values (
  'California',
  '<Polygon><outerBoundaryIs><LinearRing><coordinates>-118.603375,33.478098,0 -118.60403,33.47654,0 -118.601185,33.469853,0 -118.593969,33.467198,0 -118.575901,33.448261,0 -118.570927,33.439351,0 -118.563442,33.434381,0 -118.558715,33.433419,0 -118.53738,33.434608,0 -118.52323,33.430733,0 -118.516267,33.425075,0 -118.515914,33.422417,0 -118.503952,33.424234,0 -118.48875,33.419826,0 -118.484949,33.412131,0 -118.478465,33.38632,0 -118.482609,33.369914,0 -118.48877,33.356649,0 -118.481886,33.344123,0 -118.465368,33.326056,0 -118.456309,33.32182,0 -118.440047,33.318638,0 -118.402941,33.320901,0 -118.374768,33.320065,0 -118.360332,33.31533,0 -118.343249,33.305234,0 -118.325244,33.299075,0 -118.316787,33.301137,0 -118.305084,33.310323,0 -118.303174,33.320264,0 -118.310213,33.335795,0 -118.316083,33.342928,0 -118.32446,33.348782,0 -118.365094,33.388374,0 -118.368301,33.40711,0 -118.370323,33.409285,0 -118.382037,33.409883,0 -118.423576,33.427258,0 -118.445812,33.428907,0 -118.477646,33.448392,0 -118.48557,33.446213,0 -118.499669,33.447879,0 -118.500212,33.449592,0 -118.530702,33.468071,0 -118.54453,33.474119,0 -118.585936,33.473819,0 -118.598783,33.477939,0 -118.603375,33.478098,0 </coordinates></LinearRing></outerBoundaryIs></Polygon>'
)

update zones
set area = ST_GeomFromKML(kml)

这里有什么回复:

 name 
------
(0 row)

虽然它本应该返回这个:

   name 
-----------
California
(1 row)

您是否看到查询或表创建脚本有任何问题?

(我已经说过,这不是一个与点本身相关的边缘案例,因为我遇到了超过6万个不同点的相同问题,所有这些点都位于加利福尼亚州内)

1 个答案:

答案 0 :(得分:1)

它没有返回匹配,因为该点不在该区域内。

尝试此查询:

select st_xmax(area), st_ymax(area), st_xmin(area), st_ymin(area) from zones;

您会看到该点远远超出最大和最小点数:

"st_xmax","st_ymax","st_xmin","st_ymin"
-118.303174,33.478098,-118.60403,33.299075

你所在的地区似乎是洛杉矶海岸外的一个岛屿(这个岛上有一个名为“阿瓦隆”的小镇),而不是整个州。

(小小的提示。你可以使用像QGIS这样的GIS来显示你的图层 - 你很快就会看到问题。这就是我做的!)。