编辑:我已将登录详细信息添加到演示 SQL Server 2017。
我正在使用 FreeTDS 驱动程序从 R 查询 SQL Server(优于 Microsoft odbc 驱动程序,因为它支持 Linux 中的 Windows 身份验证)。
我正在努力用 st_read
解释几何列.
连接是这样启动的:
library(odbc)
library(DBI)
library(sf)
username <- 'SO-user'
sql_server_ip <- '35.214.169.110'
password <- 'SQLaskingfortrouble?!'
con <- dbConnect(odbc(),
Driver = "FreeTDS",
Server = sql_server_ip,
Database = "stackoverflow-example",
UID = username,
PWD = password,
Port = 1433)
# I have also tried Driver = "ODBC Driver 17 for SQL Server"
查询结果如下:
data <- dbGetQuery(con, 'SELECT TOP 2
objectid, geom,
geom.STGeometryType() geom_type,
geom.STSrid STSrid
FROM spatialdata')
objectid geom geom_type stsRID
864 blob[416 B] LineString 25832
865 blob[416 B] LineString 25832
class(data$geom)
"blob" "vctrs_list_of" "vctrs_vctr"
原始/ Blob 几何看起来像十六进制:
rawToHex(data$geom[1])
e86400000104180000004003780b38331f416b4df3ea9ecf5741a04c150c38331f4159ca32f09ecf574160ba...
正在尝试使用
st_read
,所以我可以从中得到一个有用的 sf 数据帧,根据我传递给函数的选项,给我一系列错误消息:st_read(con, geometry_column = "geom",
query = "SELECT TOP 10 objectid, geom FROM \"spatialdata\"")
Error: nanodbc/nanodbc.cpp:1617: 00000: [FreeTDS][SQL Server]Invalid object name 'spatial_ref_sys'. [FreeTDS][SQL Server]Statement(s) could not be prepared.
<SQL> 'select srtext from spatial_ref_sys where srid = 70647808'
In addition: Warning message:
In CPL_crs_from_input(x) :
GDAL Error 6: EPSG PCS/GCS code 70647808 not found in EPSG support files. Is this a valid EPSG coordinate system?
看起来 EPSG (25832) 没有被正确识别。
在我的情况下,st_read 的正确构造是什么?
额外问题:如何将边界框有效地附加到 st_read?
最佳答案
您是否尝试过 WKB 表示?
mytest <- st_read(con, geometry_column = "geom",
query = "
select geometry::STGeomFromText('POLYGON ((0 0, 1.5 0, 1.5 1.5, 0 1.5, 0 0))', 0).STAsBinary() as geom
union all
select geometry::Parse('CIRCULARSTRING(0 0, 1 1.10, 2 2.3246, 0 7, -3 2.3246, -1 2.1082, 0 0)').STAsBinary() as geom;
")
plot(mytest)
关于r - 从 R 查询 SQL Server 地理空间数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60927929/