Postgis-ST_within没有执行我想要的操作.如何在空心区域中找到一个点? [英] Postgis - ST_within didn't do what I want. How to find a point in a hollow area?

查看:387
本文介绍了Postgis-ST_within没有执行我想要的操作.如何在空心区域中找到一个点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看屏幕打印.

我在Postgis中运行了一个空间查询,以返回地图上某个点所在的选区(区域).查询使用函数,该点位于多边形内.

I ran a spatial query in Postgis to return the electoral constituency (area) that a point on the map lies in. The query uses a ST_within function where the point is within a polygon.

从印刷品中可以看到,该点实际上并不是约克外区"的多边形区域内,尽管从技术上讲,您可能会说它在内"内,或者至少Postgis如此认为.关键实际上在于约克中央.

As you can see from the print, the point is not actually 'in' the polygon area of York Outer although technically you might say it's 'within' it, or at least Postgis thinks so. The point would actually lie in York Central.

我确定Postgis实际上会返回两个记录,但是由于我只从游标中获取了第一条记录,所以这就是我所看到的.

I'm sure Postgis actually returns both but since I only fetch the first record from the cursor, this is what I see.

一次只能在一个选区中出现一个点,并且此查询返回了错误的一个,或者我问的是数据库的错误问题.

A point can only be in one electoral constituency at a time and this query has returned the wrong one or rather I asked the wrong question of the database.

我应该使用哪个功能来确保始终返回正确的区域,以确保该区域可能具有空心的内部或奇怪的形状?

Which function should I be using to ensure I always return the correct area for a point where it's possible the area may have a hollow interior or be a strange shape?

谢谢

菲尔

推荐答案

这应该按照您的描述进行.数据有问题吗?您能提供一个包含多边形/点数据的小型复制品吗?

This should work as you described it. Maybe something is wrong with the data? Could you provide a small repro, with polygon / point data?

同样,导致此类问题的一些常见原因是无效的GIS数据.您可以使用PostGIS的ST_IsValid功能检查多边形形状.如果数据无效,则不同的工具可能会以不同的方式对其进行解释,并且GIS数据的绘制方式可能与PostGIS认为该数据所代表的内容不匹配,从而引起更多的混乱.

Also, a somewhat common reason for such problems is not valid GIS data. You can check the polygon shape with PostGIS's ST_IsValid function. If the data is not valid, different tools might interpret it in different ways, and how GIS data is drawn might not match what PostGIS thinks this data represents, causing more confusion.

这是一个简单的复制品,显示了它的工作原理,正如您所预期的那样,外部多边形孔内的点仅是内部多边形st_within,而不是外部多边形:

Here is a simple repro showing it works as you expect it to work, with point inside the outer polygon's hole only st_within the inner polygon, not the outer one:

select st_astext(point), name 
from 
  (select 
       'outer' as name, 
       st_geomfromtext('polygon((0 0, 30 0, 30 30, 0 30, 0 0), (10 10, 20 10, 20 20, 10 20, 10 10))') g
  union all 
  select 
      'inner' as name, 
       st_geomfromtext('polygon((10 10, 20 10, 20 20, 10 20, 10 10))') g
  ) shapes
cross join
  (select st_geomfromtext('point(15 15)') point
   union all
   select st_geomfromtext('point(5 5)') point
  ) points
where st_within(point, g)

我的结果是

1   POINT(5 5)     outer
2   POINT(15 15)   inner

这篇关于Postgis-ST_within没有执行我想要的操作.如何在空心区域中找到一个点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆