如何在纬度和经度上从 R 中的 PostGis 转换 Geom 对象?

How to convert Geom object from PostGis in R in latitude and longitude?

我有一个具有 "location" 属性的数据库,它被保存为 geom 对象,如下所示:0101000020E6100000000000603D1D5EC0000000A06D424740

有没有办法在 R 中从中提取坐标?这些值作为字符串加载到 R 中。它还给出了警告:

Warning message:
In postgresqlExecStatement(conn, statement, ...) :
  RS-DBI driver warning: (unrecognized PostgreSQL field type geometry (id:18832) in column 2)

感谢任何帮助。

您是否尝试过使用 rgeos functions? One option would be reading WKT directly from the database (using ST_AsText)...

SELECT ST_AsText('0101000020E6100000000000603D1D5EC0000000A06D424740');
                st_astext                 
------------------------------------------
 POINT(-120.456871032715 46.518970489502)
(1 Zeile)

在 R 中,使用 readWKT 然后您可以提取坐标对:

> readWKT("POINT(-120.456871032715 46.518970489502)")
SpatialPoints:
          x        y
1 -120.4569 46.51897
Coordinate Reference System (CRS) arguments: NA 

另一种选择是使用 ST_X and ST_Y:

直接从数据库中读取 x,y
 SELECT ST_X('0101000020E6100000000000603D1D5EC0000000A06D424740'),
        ST_Y('0101000020E6100000000000603D1D5EC0000000A06D424740');
       st_x        |      st_y       
-------------------+-----------------
 -120.456871032715 | 46.518970489502
(1 Zeile)

我看到 Jim Jones 回答主要使用 postgis 而不是 R。 SF 包允许将 Well Known Binary (WKB) 转换为简单的特征列 (sfc),后者又可以转换为坐标。
st_as_sfc documentation
代码:

library(sf)
#> Warning: package 'sf' was built under R version 4.0.3
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
wkb = structure(list("0101000020E6100000000000603D1D5EC0000000A06D424740"), class = "WKB")
st_as_sfc(wkb,EWKB = TRUE) %>% st_coordinates()
#>           X        Y
#> 1 -120.4569 46.51897