r - 更改 SF 对象的 CRS

标签 r spatial sp sf

我有一些“道路”作为 sp 对象:

class       : SpatialLinesDataFrame 
features    : 17360 
extent      : 490176.4, 567680.9, 148639.1, 212821  (xmin, xmax, ymin, ymax)
coord. ref. : +init=epsg:27700 +proj=tmerc +lat_0=49 +lon_0=-2     +k=0.9996012717 +x_0=400000 +y_0=-100000 +datum=OSGB36 +units=m +no_defs     +ellps=airy +towgs84=446.448,-125.157,542.060,0.1502,0.2470,0.8421,-20.4894 
variables   : 4
names       :    osm_id, lanes,     type, width 
min values  :        73,     2, motorway,   6.3 
max values  : 587969162,     6,    trunk,  18.9

我将它们转换为 sf 对象,如下所示:
roads                   <- st_as_sf(roads, crs = 27700)

转换后,SRID 为空,proj4string(可能)看起来不正确。为什么是这样?我可以修吗?
Simple feature collection with 17360 features and 4 fields
geometry type:  LINESTRING
dimension:      XY
bbox:           xmin: 490176.4 ymin: 148639.1 xmax: 567680.9 ymax: 212821
epsg (SRID):    NA
proj4string:    +proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +towgs84=446.448,-125.157,542.06,0.1502,0.247,0.8421,-20.4894 +units=m +no_defs
First 10 features:
     osm_id lanes    type width                       geometry
73       73     2 primary   6.3 LINESTRING (526256 191950, ...
74       74     2 primary   6.3 LINESTRING (525236.4 190755...
75       75     2 primary   6.3 LINESTRING (526324.4 192518...
79       79     2 primary   6.3 LINESTRING (524912.4 190235...
482     482     2 primary   6.3 LINESTRING (527283.9 198571...
546     546     2 primary   6.3 LINESTRING (506089.1 205091...
1210   1210     2 primary   6.3 LINESTRING (526023.9 195013...
1213   1213     2 primary   6.3 LINESTRING (524861.1 196243...
1214   1214     2 primary   6.3 LINESTRING (524482.3 196798...
1698   1698     2 primary   6.3 LINESTRING (527335.3 195524...

我想我需要解决这个问题,因为然后我想将我的数据 st_transform() 转换为与栅格相同的 CRS,我必须提取一些值。
crs(raster)
CRS arguments:
 +proj=laea +lat_0=52 +lon_0=10 +x_0=4321000 +y_0=3210000 +ellps=GRS80 +units=m +no_defs

我想我可以通过做这样的事情将我的“道路”转换为正确的 CRS?但它不起作用。我得到....
roads <- st_transform(roads, crs(raster))
'Error: cannot create a crs from an object of class CRS

帮助表示赞赏。谢谢你。

最佳答案

你必须适应 st_transform 像这样的论点:

roads <- st_transform(roads, crs = 4326)

或者
roads <- st_transform(roads, crs = proj4string(raster))

或者正如@TimSalabim所说:
roads <- st_transform(roads, crs = crs(raster)@projargs)

或者正如@R'n'E 指出的那样:
roads <- st_transform(roads, crs = st_crs(raster))

关于r - 更改 SF 对象的 CRS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50372533/

相关文章:

r - 无法使 IRkernel::installspec() 工作,jupyter-client 不可见

r - 将匹配 NA 的 df 的字符行与所有内容进行比较,并根据比较创建新列或 df

java - 组织.hibernate.MappingException : No Dialect mapping for JDBC type: 2002

java.lang.AssertionError : Status expected:<200> but was:<201>

r - 在ggplot上绘制shapefile

r - 根据条件创建序列计数器

R - 如何命名数据框中的对比

r - R中的运动数据分析;航类和时间子采样

r - 使用 R 包 sf 编写带坐标的 CSV

r - 从嵌套的 SpatialPolygonsDataFrame 中提取父多边形或从父多边形中提取 'Dissolving' 孔