【问题标题】:POSTGRESQL - Polygon function for geolocationsPOSTGRESQL - 地理定位的多边形函数
【发布时间】:2016-04-22 07:38:42
【问题描述】:

我有 2 个表 User_places,我可以在其中看到每个用户的家庭位置,这些位置由 2 个单独的属性定义:经度和纬度。 我有第二个表 Neighborhoods 属性为'Area',它将每个社区定义为多边形 - jsonb 格式 - "[{"latitude":XXXXX,"longitude":YYYYY},{"latitude":ZZZZZ,"longitude":AAAAA },{"latitude":BBBBBB,"longitude":CCCCC},{"latitude":DDDDD,"longitude":EEEEE}]".

有人知道如何在 Postgresql 中检查特定用户是否居住在给定社区吗?

【问题讨论】:

    标签: sql postgresql geolocation polygon


    【解决方案1】:
    SELECT
    user_id,
    ST_Contains(
    ST_GeomFromText(
       (select 
       replace(replace(replace(replace(replace(area::text,']','))'),'[','POLYGON(('),'}',  ''),',"longitude":',' '),'{"latitude":','')
       from neighbourhoods
       limit 1), 4326)
       (ST_SetSRID(ST_MakePoint(latitude, longitude),4326))
    ) as polygon_check
    from user_places
    

    【讨论】:

      【解决方案2】:

      我假设您在您的 postgresql 副本上安装了 Postgis?因为它具有对 postgresql 的所有几何和地理功能扩展。

      您可以使用ST_Contains 来检查一个点是否包含在给定的多边形中,这将返回一个布尔值。

      select ST_Contains(neigbourhood.polygon,User_place.geom) 
      

      如果您在 User_places 表中没有几何点值,则可以使用 ST_SetSRID 函数和 ST_MakePoint 函数创建。

      例如 - SELECT ST_SetSRID(ST_MakePoint(LAT, LON),SRID);

      其中 SRID 是您正在使用的投影的参考,例如 WGS 84 的 4326 `

      我建议在 postgresql 中确保所有地理表中都包含 geoms,因为它使比较更容易。

      https://gis.stackexchange.com/https://gis.stackexchange.com/是一个获取地理和几何数据库问题答案的好地方供参考

      【讨论】:

      • 我已经尝试过这段代码,但它不起作用。你能看出问题吗? 'SELECT ST_Contains((SELECT area FROM neighbors limit 1), (SELECT ST_SetSRID(ST_MakePoint(latitude, longitude),4326) from user_places where user_id = 30840)) from user_places'
      • 错误:函数 st_contains(json, geometry) 不存在 - 可能是因为多边形属性“区域”的 json 格式?
      • 你需要把json转成几何,我用的是ST_GeomFromGeoJSON函数——postgis.net/docs/ST_GeomFromGeoJSON.html
      • 是的,彼得,我用丑陋的方式来获取正确格式的数据,但它确实有效。最后“目的证明手段是正确的”。非常感谢您的帮助!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-05
      • 2015-03-23
      • 2016-06-11
      • 1970-01-01
      • 2014-07-10
      • 2013-08-31
      相关资源
      最近更新 更多