如何使用 Postgis 缓冲一个点?

How to buffer a point using Postgis?

我在 Ubuntu Linux 上使用 Postgres/Postgis。如图所示,我希望将一个(或多个)点缓冲一定距离。

我已经能够生成一个点作为 postgres table pnt:

CREATE TABLE pnt ( 
  p_id INTEGER PRIMARY KEY

);

SELECT AddGeometryColumn('pnt','the_geom','4326','POINT',2);

INSERT INTO pnt(p_id, the_geom)
VALUES(2, ST_GeomFromText('POINT(-71.060316 48.432044)', 4326));

Postgis 有一个 ST_Buffer 函数,大概可以完成缓冲区操作,尽管我不确定如何将语法应用于从上面的代码创建的点。

如何将点缓冲 100 米以生成一个名为 "buffered_points" 的新 table?

您正在使用 PostGIS 的 "old" 语法。现在,您只需像这样创建 table:

CREATE TABLE pnt ( 
  p_id INTEGER PRIMARY KEY,
  the_geom geography(POINT, 4326)
);

INSERT INTO pnt(p_id, the_geom)
VALUES(2, ST_GeogFromText('POINT(-71.060316 48.432044)'));

你必须首先创建新的 table,很像 pnt table,但现在用 POLYGONs 而不是点:

CREATE TABLE buffered_points ( 
  p_id INTEGER PRIMARY KEY,
  the_geom geography(POLYGON, 4326)
);

然后从 pnt table:

插入缓冲区
INSERT INTO buffered_points(p_id, the_geom)
  SELECT p.p_id, ST_Buffer(p.the_geom, 100)
  FROM pnt p;

请注意,我在这里使用 geography 类型(long/lat 坐标),因为在 GPS 坐标(EPSG:4326)上缓冲不会产生合理的结果。

结果

patrick@puny:~$ psql -d test
psql (9.5.0, server 9.4.5)
Type "help" for help.

test=# \dx
                                      List of installed extensions
   Name    | Version |   Schema   |                             Description
-----------+---------+------------+---------------------------------------------------------------------
 ...
 postgis   | 2.1.8   | public     | PostGIS geometry, geography, and raster spatial types and functions
 ...
(6 rows)
test=# CREATE TABLE pnt (
test(#   p_id INTEGER PRIMARY KEY,
test(#   the_geom geography(POINT, 4326)
test(# );
CREATE TABLE
test=# INSERT INTO pnt(p_id, the_geom)
test-# VALUES(2, ST_GeogFromText('POINT(-71.060316 48.432044)'));
INSERT 0 1
test=# select * from pnt;
 p_id |                      the_geom
------+----------------------------------------------------
    2 | 0101000020E61000003CDBA337DCC351C06D37C1374D374840
(1 row)

test=# CREATE TABLE buffered_points (
test(#   p_id INTEGER PRIMARY KEY,
test(#   the_geom geography(POLYGON, 4326)
test(# );
CREATE TABLE
test=# INSERT INTO buffered_points(p_id, the_geom)
  SELECT p.p_id, ST_Buffer(p.the_geom, 100)
  FROM pnt p;
INSERT 0 1
test=# SELECT * FROM buffered_points;
 p_id |                      the_geom
------+----------------------------------------------------
    2 | 0103000020E610000001000000210 ...
(1 row)