使用sf和sp

时间:2018-07-27 08:24:06

标签: r projection coordinate-systems sp sf

我有一个从GeoTIFF转换为shapefile的网格。我想将shapefile转换并导出为GeoPackage,并更改投影,以便在GIS中打开时使用英国国家网格作为地理坐标系。但是,这似乎仅在使用sp而不是sf时才有效(似乎并没有保留像基准这样的方面)。

这是一个问题,因为我想导出包含多层的GeoPackage,您目前只能在sf中进行此操作,而不能在sp中进行。我在做错什么吗?

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")

3 个答案:

答案 0 :(得分:1)

针对此问题的解决方案(感谢Roger发布的here)是使用lwgeom包进行转换。Roger在sf GitHub上的发布提供了更多详细信息。

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")

答案 1 :(得分:0)

使用ogrinfo,尤其是命令

ogrinfo BNG*v1.gpkg Grid_sf_v1 > info1
ogrinfo BNG*v2.gpkg Grid_sf_v1 > info2

除了明显的_v1 _v2命名之外,两个info [1 | 2]文件之间的差异还在于:

13c13
<             TOWGS84[446.448,-125.157,542.06,0.15,0.247,0.842,-20.489]],
---
>             TOWGS84[446.448,-125.157,542.06,0.1502,0.247,0.8421,-20.4894]],

_v2中的其他数字是否会在ArcGIS中引起麻烦?

答案 2 :(得分:0)

请继续关注https://github.com/r-spatial/sf/issues/810,而不是此处。