How can I create a polygon using fields in PostgreSQL?

前端 未结 3 1047
心在旅途
心在旅途 2021-01-11 17:40

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

3条回答
  •  清酒与你
    2021-01-11 18:19

    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.

    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
              );
      

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

提交回复
热议问题