使用 sf 和 sp 在 R 中的投影差异 [英] Projection differences in R using sf and sp

查看:62
本文介绍了使用 sf 和 sp 在 R 中的投影差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个从 GeoTIFF 转换为 shapefile 的网格.我想将 shapefile 转换和导出为 GeoPackage 并更改投影,以便在 GIS 中打开时使用英国国家网格作为地理坐标系.然而,这似乎只适用于 sp 而不是 sf(它似乎没有保留像数据这样的方面).

I have a grid I have converted from GeoTIFFs to a shapefile. I would like to convert and export the shapefile as a GeoPackage and change the projection so it uses the British National Grid as the geographic coordinate system when opened in a GIS. However this only seems to work using sp and not sf (which does not appear to retain aspects like the datum).

这是一个问题,因为我想导出包含多个图层的 GeoPackages,您目前只能在 sf 而不是 sp.我做错了什么吗?

This is a problem as I would like to export GeoPackages containing multiple layers which you can only currently do in sf and not sp. Am I doing something wrong?

library(rgdal)
library(sf)

download.file("https://drive.google.com/uc?id=1URbux7Sw25KFTySqRFKXk53DV2UK4lsA&export=download" , destfile="Grid_Shapefile.zip")
unzip("Grid_Shapefile.zip")
Grid_sp <- readOGR(".", "Grid_Shapefile")
Grid_sf <- st_as_sf(Grid_sp)

BNG_Grid_sp <- spTransform(Grid_sp, CRS("+init=epsg:27700"))
BNG_Grid_sf_v1 <- st_transform(Grid_sf, crs=27700)
BNG_Grid_sf_v2 <- st_transform(Grid_sf, crs="+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")

BNG_Grid_sf_v1_geom <- st_geometry(BNG_Grid_sf_v1)
BNG_Grid_sf_v2_geom <- st_geometry(BNG_Grid_sf_v2)

proj4string(BNG_Grid_sp)
attributes(BNG_Grid_sf_v1_geom)
attributes(BNG_Grid_sf_v2_geom)

writeOGR(BNG_Grid_sp, dsn = "BNG_Grid_sp.gpkg", layer = "Grid_sp", driver = "GPKG")
st_write(BNG_Grid_sf_v1, "BNG_Grid_sf_v1.gpkg", "Grid_sf_v1")
st_write(BNG_Grid_sf_v2, "BNG_Grid_sf_v2.gpkg", "Grid_sf_v2")

推荐答案

解决方案(感谢 Roger 发布的内容 此处) 使用 lwgeom 包进行转换.Roger 在 sf GitHub 上的帖子提供了更多详细信息.

The solution to this (thanks to what Roger has posted here) is using the lwgeom package to do the transformation.The post by Roger on the sf GitHub gives further details.

library(rgdal)
library(sf)

download.file("https://drive.google.com/uc?id=1URbux7Sw25KFTySqRFKXk53DV2UK4lsA&export=download" , destfile="Grid_Shapefile.zip")
unzip("Grid_Shapefile.zip")
Grid_sp <- readOGR(".", "Grid_Shapefile")
Grid_sf <- st_as_sf(Grid_sp)

library(lwgeom)
BNG_Grid_sf_v4 <- st_transform_proj(Grid_sf, crs="+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")
st_crs(BNG_Grid_sf_v4)
st_write(BNG_Grid_sf_v4, "BNG_Grid_sf_v4.gpkg", "Grid_sf_v4")

这篇关于使用 sf 和 sp 在 R 中的投影差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆