简体   繁体   English

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

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

I have a database that has "location" attribute and it is saved as geom object and looks like this: 0101000020E6100000000000603D1D5EC0000000A06D424740我有一个具有“位置”属性的数据库,它被保存为 geom 对象,如下所示: 0101000020E6100000000000603D1D5EC0000000A06D424740

Is there a way to extract coordinates from it in R?有没有办法在R中从中提取坐标? These values are loaded in R as strings.这些值作为字符串加载到 R 中。 Also it gave a warning:它还给出了警告:

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

Any help appreciated.任何帮助表示赞赏。

Have you tried using the rgeos functions?您是否尝试过使用rgeos函数? One option would be reading WKT directly from the database (using ST_AsText )...一种选择是直接从数据库中读取 WKT(使用ST_AsText )...

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

In R, using readWKT you can then extract the coordinate pair:在 R 中,使用readWKT然后可以提取坐标对:

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

Another option would be to read x,y directly from the database using ST_X and ST_Y :另一种选择是使用ST_XST_Y直接从数据库读取x,y

 SELECT ST_X('0101000020E6100000000000603D1D5EC0000000A06D424740'),
        ST_Y('0101000020E6100000000000603D1D5EC0000000A06D424740');
       st_x        |      st_y       
-------------------+-----------------
 -120.456871032715 | 46.518970489502
(1 Zeile)

I see that Jim Jones answer used mainly postgis and not R. The SF package allows for the conversion of Well Known Binary (WKB) to a simple features column (sfc), which in turn can be converted to coordinates.我看到Jim Jones 的回答主要使用 postgis 而不是 R。SF 包允许将众所周知的二进制 (WKB) 转换为简单的特征列 (sfc),然后可以将其转换为坐标。
st_as_sfc documentation st_as_sfc 文档
code:代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM