在纯 PostgreSQL 多边形数据中交换坐标
Swapping coordinates in plain PostgreSQL polygon data
我有一个 PostgreSQL 11 table,其中有一个普通的 PostgreSQL Polygon 列用于存储地理区域,像这样(简化):
CREATE TABLE areas
(
name TEXT NOT NULL,
bounds POLYGON NOT NULL
);
然后我在这个 table 中插入了 > 100 行手动输入的数据,坐标按纬度、经度的顺序(这是导航中的常见顺序),例如在这个例子中:
INSERT INTO areas (name, bounds) VALUES (
'Hamburg Harbour',
'((53.543420, 9.875492), (53.486682, 9.885482), (53.467172, 9.990250), (53.519363, 9.993678),
(53.521112, 10.011104), (53.533893, 10.018592), (53.544478, 9.965988))'
);
我现在收到了一些其他区域,其坐标为 PostgreSQL Polygon 数据类型的默认顺序 x、y(即经度、纬度)。因此,我决定交换所有现有区域的坐标,以与新接收的区域以及 PostgreSQL Polygon 数据类型的预期顺序保持一致。搜索显示 PostGIS ST_SwapOrdinates() function ,如下所示:
SELECT ST_AsText(ST_SwapOrdinates(bounds::geometry, 'xy'))
FROM areas
WHERE name = 'Hamburg Harbour';
正在输出:
POLYGON((9.875492 53.54342,9.885482 53.486682,9.99025 53.467172,9.993678 53.519363,10.011104 53.521112,10.018592 53.533893,9.965988 53.544478,9.875492 53.54342))
到目前为止一切顺利。但是,我想像上面那样将交换的坐标存储回 table 中,使用普通的 PostgreSQL Polygon 类型列。 ST_SwapOrdinates()函数returns PostGIS几何类型数据,ST_AsText() function simply converts this into a text representation of the same. Neither of these is of PostgreSQL Polygon data type. Having tried various ways to convert the result back into a plain PostgreSQL Polygon type have been unsuccessful. In a somewhat related post,但反方向转换,解决方案建议使用regexp_replace()构造一个字符串,符合到预期的格式。我不敢相信没有更简单的方法可以将 PostGIS 几何图形转换为 PostgreSQL 多边形。我错过了一些明显的东西吗?
已编辑:
感谢@JGH,答案很简单(我很确定@JGH 的建议是我已经尝试过的许多事情之一,但显然不是!),即从 PostGIS 类型几何转换为 PostgreSQL 类型多边形.
以下创建了一个新的 table 并交换了坐标:
CREATE TABLE areas_swapped AS
SELECT name, ST_SwapOrdinates(bounds::GEOMETRY, 'xy')::POLYGON
FROM areas;
您可以转换为 polygon
:
select 'POLYGON((9.875492 53.54342,9.885482 53.486682,9.99025 53.467172,9.993678 53.519363,10.011104 53.521112,10.018592 53.533893,9.965988 53.544478
,9.875492 53.54342))'::geometry::polygon;
polygon
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
((9.875492,53.54342),(9.885482,53.486682),(9.99025,53.467172),(9.993678,53.519363),(10.011104,53.521112),(10.018592,53.533893),(9.965988,53.544478),(9.875492,53.54342))
(1 row)
我有一个 PostgreSQL 11 table,其中有一个普通的 PostgreSQL Polygon 列用于存储地理区域,像这样(简化):
CREATE TABLE areas
(
name TEXT NOT NULL,
bounds POLYGON NOT NULL
);
然后我在这个 table 中插入了 > 100 行手动输入的数据,坐标按纬度、经度的顺序(这是导航中的常见顺序),例如在这个例子中:
INSERT INTO areas (name, bounds) VALUES (
'Hamburg Harbour',
'((53.543420, 9.875492), (53.486682, 9.885482), (53.467172, 9.990250), (53.519363, 9.993678),
(53.521112, 10.011104), (53.533893, 10.018592), (53.544478, 9.965988))'
);
我现在收到了一些其他区域,其坐标为 PostgreSQL Polygon 数据类型的默认顺序 x、y(即经度、纬度)。因此,我决定交换所有现有区域的坐标,以与新接收的区域以及 PostgreSQL Polygon 数据类型的预期顺序保持一致。搜索显示 PostGIS ST_SwapOrdinates() function
SELECT ST_AsText(ST_SwapOrdinates(bounds::geometry, 'xy'))
FROM areas
WHERE name = 'Hamburg Harbour';
正在输出:
POLYGON((9.875492 53.54342,9.885482 53.486682,9.99025 53.467172,9.993678 53.519363,10.011104 53.521112,10.018592 53.533893,9.965988 53.544478,9.875492 53.54342))
到目前为止一切顺利。但是,我想像上面那样将交换的坐标存储回 table 中,使用普通的 PostgreSQL Polygon 类型列。 ST_SwapOrdinates()函数returns PostGIS几何类型数据,ST_AsText() function simply converts this into a text representation of the same. Neither of these is of PostgreSQL Polygon data type. Having tried various ways to convert the result back into a plain PostgreSQL Polygon type have been unsuccessful. In a somewhat related post,但反方向转换,解决方案建议使用regexp_replace()构造一个字符串,符合到预期的格式。我不敢相信没有更简单的方法可以将 PostGIS 几何图形转换为 PostgreSQL 多边形。我错过了一些明显的东西吗?
已编辑:
感谢@JGH,答案很简单(我很确定@JGH 的建议是我已经尝试过的许多事情之一,但显然不是!),即从 PostGIS 类型几何转换为 PostgreSQL 类型多边形.
以下创建了一个新的 table 并交换了坐标:
CREATE TABLE areas_swapped AS
SELECT name, ST_SwapOrdinates(bounds::GEOMETRY, 'xy')::POLYGON
FROM areas;
您可以转换为 polygon
:
select 'POLYGON((9.875492 53.54342,9.885482 53.486682,9.99025 53.467172,9.993678 53.519363,10.011104 53.521112,10.018592 53.533893,9.965988 53.544478
,9.875492 53.54342))'::geometry::polygon;
polygon
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------
((9.875492,53.54342),(9.885482,53.486682),(9.99025,53.467172),(9.993678,53.519363),(10.011104,53.521112),(10.018592,53.533893),(9.965988,53.544478),(9.875492,53.54342))
(1 row)