如何使用 PostgreSQL 中的字段创建多边形?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1105757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 23:45:58  来源:igfitidea点击:

How can I create a polygon using fields in PostgreSQL?

postgresqlpolygonpostgis

提问by Dan Goldstein

I have 8 real values in a table that I'd like to combine into a polygon. I haven't been able to figure out how to create a polygon using these values though. I keep trying variations of

我在一个表中有 8 个实数值,我想将它们组合成一个多边形。我一直无法弄清楚如何使用这些值创建多边形。我一直在尝试变体

SELECT polygon(lat1,lon1,lat2,lon2,lat3,lon3,lat4,lon4) FROM table;

but keep getting errors about the polygon function not existing or an invalid input syntax for type polygon. Has anyone done this before?

但不断收到有关多边形函数不存在的错误或多边形类型的无效输入语法。以前有人这样做过吗?

回答by unmounted

The syntax for a regular postgres polygon is more like:

常规 postgres 多边形的语法更像是:

insert into geo_table values (1, '((2,2),(3,4),(3,6),(1,1))');

insert into geo_table values (1, '((2,2),(3,4),(3,6),(1,1))');

Where 1 is some id and the quoted entry is the polygon. I would expect the query to be similar, you probably need parentheses etc for the coordinates. Typically for geospatial data you want (Lon Lat) coordinates. Postgis also takes WKTstatements like:

其中 1 是某个 id,引用的条目是多边形。我希望查询是相似的,您可能需要括号等作为坐标。通常对于您想要的地理空间数据 (Lon Lat) 坐标。Postgis 还采用WKT语句,例如:

GeomFromText('POLYGON((long1 lat1, long2 lat2, long3 lat3))')

GeomFromText('POLYGON((long1 lat1, long2 lat2, long3 lat3))')

回答by user569142

As mentioned by bvmou - GeomFromTextwill work fine. I'll just add a small syntax update:

正如 bvmou 所提到的 -GeomFromText会正常工作。我将添加一个小的语法更新:

GeomFromText('POLYGON((long1 lat1, long2 lat2, long3 lat3))')

回答by drpetermolnar

This example takes longitude and latitude coordinates from a table and converts them into a geometry. The dimensions of each box are given as long_high, long_low, lat_high, and lat_low. Here, a box of approximately 500m by 500m.

此示例从表格中获取经度和纬度坐标,并将它们转换为几何图形。每个框的尺寸被指定为 long_high、long_low、lat_high 和 lat_low。在这里,一个大约 500m x 500m 的盒子。

  1. Add a new geometry column 'box' to the table

    SELECT AddGeometryColumn('public', 'predpol_raw', 'box', 2240, 'POLYGON', 2);
  2. Update the new field with those values.

    UPDATE predpol_raw
    SET box =
            ST_Transform(
                ST_GeomFromText(
                    format('POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s))',
                        long_high,lat_high, long_low,lat_high,
                        long_low,lat_low, long_high,lat_low,
                        long_high,lat_high
                    ),
                    4326
                ),
                2240
            );
    
  1. 向表中添加一个新的几何列“框”

    SELECT AddGeometryColumn('public', 'predpol_raw', 'box', 2240, 'POLYGON', 2);
  2. 使用这些值更新新字段。

    UPDATE predpol_raw
    SET box =
            ST_Transform(
                ST_GeomFromText(
                    format('POLYGON((%s %s, %s %s, %s %s, %s %s, %s %s))',
                        long_high,lat_high, long_low,lat_high,
                        long_low,lat_low, long_high,lat_low,
                        long_high,lat_high
                    ),
                    4326
                ),
                2240
            );
    

Note the transformation to a different spatial reference. The POLYGON keyword requires double parentheses '(( ))'.

请注意到不同空间参考的转换。POLYGON 关键字需要双括号“(( ))”。