如何使用Postgis缓冲一个点?

时间:2016-01-18 10:10:38

标签: postgresql geometry postgis point

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

enter image description here

我已经能够生成一个点作为postgres表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"的新表?

1 个答案:

答案 0 :(得分:2)

你正在使用" old" PostGIS的语法。现在您只需创建表格如下:

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

您必须先创建新表,与pnt表非常相似,但现在使用POLYGON而不是点:

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

然后从pnt表中插入缓冲区:

INSERT INTO buffered_points(p_id, the_geom)
  SELECT p.p_id, ST_Buffer(p.the_geom, 100)
  FROM pnt p;

请注意,我在这里使用geography类型(长/纬度坐标),因为在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)
相关问题