简体   繁体   English

使用 SP Package 中的 SpatialPoints() 转换坐标参考系统 (CRS) 以创建空间 Dataframe

[英]Converting the Coordinate Reference System (CRS) to Create a Spatial Dataframe using SpatialPoints() in the SP Package

Issue:问题:

I have a shapefile that I've imported into R and I selected the variables of interest for the analysis that I'm conducting.我有一个已导入到 R 中的shapefile ,并为我正在进行的分析选择了感兴趣的变量。 My ultimate aim is to interpolate the point data (dolphin IDs) to obtain sea surface temperature (SST) values from each individual raster file within a stack of 70+ rasters from an object called ncin_SST.我的最终目标是对点数据(海豚 ID)进行插值,以从名为 ncin_SST 的 object 的70 多个栅格堆栈中的每个单独的栅格文件中获取海面温度(SST)值。 This object was created using the function stack::raster() from multiple Aqua Modis.netCDF files contained in one folder and I downloaded the data from the 'Ocean Color Project' from NASA .这个 object 是使用 function stack::raster() 从一个文件夹中包含的多个Aqua Modis.netCDF 文件创建的,我从 NASA 的“海洋色彩项目”下载了数据。 My goal is to extract the average SST per ID over the time period across all 70+ raster files from 2016-to 2021.我的目标是提取 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")

I imported my shapefile using the function shapefile() in the rgdal package and I want to extract three variables of interest involving variable 1 = ID, variable 2 = LONGITUDE, and variable 3 = LATITUDE .我在rgdal package中使用 function shapefile() 导入了我的 shapefile,我想提取三个感兴趣的变量,涉及变量 1 = ID、变量 2 = LONGITUDE 和变量 3 = LATITUDE Before I can interpolate the IDs with the object ncin_SST , I need to produce a spatial data frame from coordinates using the SpatialPoints() function from the sp package .在我可以使用object ncin_SST插入 ID之前,我需要使用sp package中的SpatialPoints() function 从坐标生成空间数据框。

When I am attempting to transform the CRS to WG85/UTM 34N, I am getting this error message below.当我尝试将 CRS 转换为 WG85/UTM 34N 时,我收到以下错误消息。

If anyone is able to help, then many thanks.如果有人能够提供帮助,那么非常感谢。

R-code R代码

#Read our shapefile using the function shapefile() from the raster package #使用来自栅格 package 的 function shapefile() 读取我们的 shapefile

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

Check variable headers检查变量头

head(Points_shp)

Select the variables of interest Select 感兴趣的变量

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

Results结果

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]]] 

Finalising the spatial coordinate reference data frame完成空间坐标参考数据框

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

Error Message错误信息

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

#Read our shapefile using the function shapefile() from the raster package #使用来自栅格 package 的 function shapefile() 读取我们的 shapefile

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

Check variable headers检查变量头

head(Points_shp)

Select the variables of interest from the shapefile Select shapefile 中感兴趣的变量

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 70 多个光栅文件堆栈的坐标参考系统 (CRS) 转换为 stack::raster () 格式 - Converting the Coordinate Reference System (CRS) of a Stack of 70+ Raster Files in stack::raster () Format 更改人口普查区域数据的坐标参考系 (CRS) - Changing the Coordinate Reference System (CRS) on Census Tract Data 如何在R中更改.shp文件的坐标参考系统(CRS)? - How to change coordinate reference system (CRS) for a .shp file in R? 是否有可以将 CRS(坐标参考系统)分配给数据集的 R function? 还是只能使用 shapefile? - Is there an R function that can assign a CRS (Coordinate Reference System) to a dataset? Or is it only possible with shapefiles? 从我自己的包中使用sp(空间包)会导致错误:找不到功能 - Using sp (spatial package) from my own package results in error: could not find function 为什么我的空间连接使用sp和sf包返回不同的结果? - Why is my spatial join returning different results using the sp versus the sf package? 在 R 中使用 sf 包重新投影和 CRS 的 Shapefile - Reprojecting & CRS of Shapefile using sf Package in R 由于更新 sp package 我通过调用 sp::CRS 定义收到警告 - Since update of sp package i get a warning by calling a sp::CRS definition 使用Spatial * DataFrame应用 - Using apply with Spatial*DataFrame 在R中转换形状坐标系 - converting shape coordinate system in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM