简体   繁体   English

在PostGIS中查询地图视图边界内的多边形

[英]Query polygons within a map-view boundary in PostGIS

I am new to PostGIS. 我是PostGIS的新手。 I've got a table which contains boundaries of area in Polygon type. 我有一个包含Polygon类型区域边界的表。 I would like to display the polygon on Google map. 我想在谷歌地图上显示多边形。 Each time, when google map view boundary is changed, the application should query PostGIS for any polygons in that map view. 每次更改谷歌地图视图边界时,应用程序都应该在PostGIS中查询该地图视图中的任何多边形。 Can someone guide me to start? 有人可以指导我开始吗?

Select * from Polygons where ST_Intersects(
    ST_MakeBox2D(ST_MakePoint(lon1, lat1), ST_MakePoint(lon2, lat2)),
    geom);

assuming that you have a table called Polygons and that your geometry field is called geom. 假设您有一个名为Polygons的表,并且您的几何字段称为geom。 You also need to make sure that you have a spatial index on the geometry field with: 您还需要确保在几何字段上具有空间索引:

Create index ix_spatial_geom on Polygons using gist (geom);

If you are planning on loading data from Postgis into google maps, have a look at the AS_GeoJSON function in Postgis, which allows you to create GeoJSON directly from Postgis, which you can then load into Google Maps directly, see this example: load GeoJSON in Google Maps 如果您打算将Postgis中的数据加载到谷歌地图中,​​请查看Postgis中的AS_GeoJSON函数,它允许您直接从Postgis创建GeoJSON,然后您可以直接加载到Google地图中,请参阅此示例: 加载GeoJSON in谷歌地图

EDIT: 编辑:

You can set the SRID when you import the data using the -s switch. 使用-s开关导入数据时,可以设置SRID。 It is highly advisable to explicitly set the SRID on a column either when you create the column or by running an update with UpdateGeometrySRID afterwards, as this helps with both enforcing integrity, and enable conversion from one coordinate system to another. 非常明智的做法是在创建列时或在之后运行UpdateGeometrySRID更新时在列上显式设置SRID,因为这有助于强制执行完整性,并启用从一个坐标系到另一个坐标系的转换。

Once you have you data in a specified coordinate system, you can also use ST_Transfrom to convert from one coordinate system to another, on the fly, such as from 4326 to 3857 (the projected metres of Google Maps tiles). 在指定的坐标系中获得数据后,您还可以使用ST_Transfrom从一个坐标系转换到另一个坐标系,例如从4326到3857(Google地图图块的投影米)。

ST_Intersects is probably the best solution for what you want. ST_Intersects可能是您想要的最佳解决方案。

If you are new to Postgis I recommend you to use their Postgis Cheat Sheet . 如果您是Postgis的新手,我建议您使用他们的Postgis备忘单 This document has all Postgis's basic and most used commands that you will probably use for now and it helps you to get it quickly without having to read the whole manual. 本文档包含了您现在可能会使用的所有Postgis基本和最常用的命令,它可以帮助您快速获取它而无需阅读整本手册。

How to use ST_Intersect and all most used alternatives is there. 如何使用ST_Intersect和所有最常用的替代品。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM