r-转换坐标参考系(CRS)以使用SP包中的SpatialPoints()创建空间数据帧



问题:

我有一个形状文件,我已经将其导入R中,并为正在进行的分析选择了感兴趣的变量。我的最终目标是插值点数据(海豚ID(,以从名为ncin_SST的对象的70+光栅堆栈中的每个单独光栅文件中获得海面温度(SST(值。该对象是使用函数堆栈::raster((从一个文件夹中包含的多个Aqua-Modis netCDF文件创建的,我从NASA的"海洋颜色项目"下载了数据。我的目标是提取2016年至2021年期间所有70多个光栅文件中每个ID的平均SST。

##Stack all the netCDF files and select the variable "sst" from the raster layers
ncin_SST <- raster::stack(filenames, varname = "sst")

我使用rgdal包中的函数shapefile((导入了我的shapefile,并且我想提取感兴趣的三个变量,涉及变量1=ID、变量2=LONGUIDED和变量3=LATITUDE。在使用对象ncin_SST插入IDs之前,我需要使用sp包中的SpatialPoints((函数从坐标生成空间数据帧。

当我试图将CRS转换为WG85/UTM 34N时,我收到以下错误消息。

如果有人能帮忙,那么非常感谢。

R-代码

#使用光栅包中的函数shapefile((读取我们的shapefile

Points_shp <- shapefile(".", point_ID.shp")

检查可变标题

head(Points_shp)

选择感兴趣的变量

coords = cbind(Points_shp$ID, Points_shp$LATITUDE, Points_shp$LONGITUDE)
#Making a spatial data frame from coordinates
#The IDs were documented in WG84/UTM 34N
#Extract the project code for the CRS

CRS("+init=epsg:32634")

结果

Coordinate Reference System:
Deprecated Proj.4 representation:
+proj=utm +zone=34 +datum=WGS84 +units=m +no_defs 
WKT2 2019 representation:
PROJCRS["WGS 84 / UTM zone 34N",
BASEGEOGCRS["WGS 84",
ENSEMBLE["World Geodetic System 1984 ensemble",
MEMBER["World Geodetic System 1984 (Transit)"],
MEMBER["World Geodetic System 1984 (G730)"],
MEMBER["World Geodetic System 1984 (G873)"],
MEMBER["World Geodetic System 1984 (G1150)"],
MEMBER["World Geodetic System 1984 (G1674)"],
MEMBER["World Geodetic System 1984 (G1762)"],
ELLIPSOID["WGS 84",6378137,298.257223563,
LENGTHUNIT["metre",1]],
ENSEMBLEACCURACY[2.0]],
PRIMEM["Greenwich",0,
ANGLEUNIT["degree",0.0174532925199433]],
ID["EPSG",4326]],
CONVERSION["UTM zone 34N",
METHOD["Transverse Mercator",
ID["EPSG",9807]],
PARAMETER["Latitude of natural origin",0,
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8801]],
PARAMETER["Longitude of natural origin",21,
ANGLEUNIT["degree",0.0174532925199433],
ID["EPSG",8802]],
PARAMETER["Scale factor at natural origin",0.9996,
SCALEUNIT["unity",1],
ID["EPSG",8805]],
PARAMETER["False easting",500000,
LENGTHUNIT["metre",1],
ID["EPSG",8806]],
PARAMETER["False northing",0,
LENGTHUNIT["metre",1],
ID["EPSG",8807]],
ID["EPSG",16034]],
CS[Cartesian,2],
AXIS["(E)",east,
ORDER[1],
LENGTHUNIT["metre",1,
ID["EPSG",9001]]],
AXIS["(N)",north,
ORDER[2],
LENGTHUNIT["metre",1,
ID["EPSG",9001]]],
USAGE[
SCOPE["unknown"],
AREA["Between 18°E and 24°E, northern hemisphere between equator and 84°N, onshore and offshore. Albania. Belarus. Bosnia and Herzegovina. Bulgaria. Central African Republic. Chad. Croatia. Democratic Republic of the Congo (Zaire). Estonia. Finland. Greece. Hungary. Italy. Kosovo. Latvia. Libya. Lithuania. Montenegro. North Macedonia. Norway, including Svalbard and Bjornoys. Poland. Romania. Russian Federation. Serbia. Slovakia. Sudan. Sweden. Ukraine."],
BBOX[0,18,84,24]]] 

最终确定空间坐标参考数据帧

points_spdf = SpatialPoints(coords, proj4string = crs("+proj=utm + zone=34 + datum=WGS84 + units=m + no_defs"))

错误消息

Error in .local(obj, ...) : 
cannot derive coordinates from non-numeric matrix

#使用光栅包中的函数shapefile((读取我们的shapefile

Points_shp <- shapefile(".", point_ID.shp")

检查可变标题

head(Points_shp)

从形状文件中选择感兴趣的变量

#Check the number of layers in Point_shp
dim(Points_shp)
#[1] 635  19
#Check the variable names in the Point_shp object
names(Points_shp)
#making a spatial data frame from coordinates
#EPSG = 32634
CRS("+init=epsg:32634")
coords = cbind(Points_shp$ID, Points_shp$LATITUDE, Points_shp$LONGITUDE)
#Check the variables in the coords object
print(coords)
#check header
head(coords)
#Check the structure of the matrix coords
str(coords)
#Change the matrix coords into a dataframe
coords_N<-as.data.frame(coords)
#Rename Column Names of coords_N dataframe
#Cbind transformed the column headings to V1, V2, and V3
#Change the column names
colnames(coords_N)[colnames(coords_N) == "V1"] <- "ID"
colnames(coords_N)[colnames(coords_N) == "V2"] <- "Longitude"
colnames(coords_N)[colnames(coords_N) == "V3"] <- "Latitude"
#Check the structure of the data frame coords
str(coords_N)
#Change the format of the variables from characters to numerica format
coords_N$ID <- as.numeric(as.character(coords_N$ID))
coords_N$Longitude <- as.numeric(as.character(coords_N$Longitude))
coords_N$Latitude <- as.numeric(as.character(coords_N$Latitude))
#Change latitude and longitude into coordinates using the sp package
coordinates(coords_N) <- ~Latitude + Longitude
#Finalise the coordinate reference system dataframe
points_spdf = SpatialPoints(coords_N, proj4string = crs("+proj=utm + zone=34 + datum=WGS84"))

最新更新