问题描述
我正在尝试在 r 中制作地形图。第一步,下载海拔数据:
ex_df <- data.frame(x=c(5.86,7.7,5.86,5.86),y=c(50.59,50.59,49.8,50.59))
crs_obj <- crs("+proj=longlat +datum=wgs84 +no_defs +ellps=wgs84 +towgs84=0,0")
elev <- get_elev_raster(ex_df,z=12,prj=crs_obj,clip = "bBox")
我收到以下错误:
Error in sp::CRS(prj) :
PROJ4 argument-value pairs must begin with +: GEOGCRS["unkNown",DATUM["World Geodetic System 1984",ELLIPSOID["WGS 84",6378137,298.257223563,LENGTHUNIT["metre",1]],ID["epsg",6326]],PRIMEM["Greenwich",ANGLEUNIT["degree",0.0174532925199433],8901]],CS[ellipsoidal,2],AXIS["longitude",east,ORDER[1],0.0174532925199433,9122]]],AXIS["latitude",north,ORDER[2],9122]]]]
搜索 SO 给了我这个解决方案 (Create topographic map in R):
# Generate a data frame of lat/long coordinates.
ex.df <- data.frame(x=seq(from=-73,to=-72.5,length.out=10),y=seq(from=41,to=41.5,length.out=10))
# Specify projection.
prj_dd <- "+proj=longlat +ellps=wgs84 +datum=wgs84 +no_defs"
# Use elevatr package to get elevation data for each point.
elev <- get_elev_raster(ex.df,prj = prj_dd,z = 10,clip = "bBox")
还有……同样的错误。显然 prj 论点有问题,但在线搜索并没有说明应该如何解决这个问题。有什么想法吗?
解决方法
尝试使用 sf
包:
library(sf)
library(magrittr)
ex.df %>%
st_as_sf(coords = c("x","y"),crs = 4326) %>%
elevatr::get_elev_point()
Simple feature collection with 10 features and 2 fields
geometry type: POINT
dimension: XY
bbox: xmin: -73 ymin: 41 xmax: -72.5 ymax: 41.5
geographic CRS: WGS 84 (with axis order normalized for visualization)
elevation elev_units geometry
1 0.00 meters POINT (-73 41)
2 0.00 meters POINT (-72.94444 41.05556)
3 0.00 meters POINT (-72.88889 41.11111)
4 0.00 meters POINT (-72.83333 41.16667)
5 0.00 meters POINT (-72.77778 41.22222)
6 52.59 meters POINT (-72.72222 41.27778)
7 56.54 meters POINT (-72.66667 41.33333)
8 85.23 meters POINT (-72.61111 41.38889)
9 171.73 meters POINT (-72.55556 41.44444)
10 87.20 meters POINT (-72.5 41.5)
,
更新 R 和相关包解决了问题。