gpt4 book ai didi

postgresql - 如何使用 Postgis 缓冲一个点?

转载 作者:行者123 更新时间:2023-11-29 12:44:03 24 4
gpt4 key购买 nike

我在 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”的新表?

最佳答案

您正在使用 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)

关于postgresql - 如何使用 Postgis 缓冲一个点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34852077/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com