简体   繁体   English

从光栅文件中提取特定坐标的数据并在R中合并

[英]extracting data from raster file for specific coordinate and merge in R

I have gridded data files ( each file consist of monthly rainfall and is label pYYYYMM) https://www.dropbox.com/sh/63z166tjxyu12s5/AAAs3Ccn1zdVoBYMj8Y1o303a?dl=0我有网格数据文件(每个文件由每月降雨量组成,标签为 pYYYYMM) https://www.dropbox.com/sh/63z166tjxyu12s5/AAAs3Ccn1zdVoBYMj8Y1o303a?dl=0

library(raster)
files= list.files( ,pattern='*.grd',full.names=TRUE)
df = NULL
for(i in seq_along(files)) {
r2 <- raster(files[i])
# setting the missing values to NA
r2[r2 >= 170141000918782798866653488190622531584.00] <- NA_real_
# Setting the projection
crs(r2) <- "+proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +datum=WGS84"
plot(r2)
#Doing this, got me the value for a given point but you will have to manual edit:
d1<- getValuesBlock(r2, row=42, nrows=1, col=26, ncols=1)
# Combine data to get one dataframe 
rbind(df,d1)->df
# then write to a csv file
}

The loop above works for me to an extent but it means that I have to manually change each point and then I will also have manually input the date.上面的循环在某种程度上对我有用,但这意味着我必须手动更改每个点,然后我还将手动输入日期。

I am kindly asking for some guidance on how I can modify my code to the data for the latitude and and longitude of my interest which is in this file https://www.dropbox.com/s/3p4u4pyxkyo2q15/latandlong.csv?dl=0我恳请您提供一些有关如何将代码修改为我感兴趣的经纬度数据的指导,该数据位于此文件中https://www.dropbox.com/s/3p4u4pyxkyo2q15/latandlong.csv?dl =0

This is how you would normally do this, assuming that all your grid files line up假设您的所有网格文件都排成一行,这就是您通常会这样做的方式

library(raster)
library(rgdal)
files <- list.files(pattern='.grd$', full.names=TRUE)
s <- stack(files)
# points <- ...

# if your points are lonlat
points <- SpatialPoints(points[, c('longitude', 'latitude')], 
          proj4string=CRS('+proj=longlat +datum=WGS84'))
# and your raster data are not they need to be matched
pts <- spTransform(points, CRS("+proj=stere +lat_0=90 +lat_ts=70 +lon_0=-45 +datum=WGS84")

df <- extract(s, points)
write.csv(df, 'file.csv')

Your data is in two different coordinate systems or the .grd files are bad.您的数据位于两个不同的坐标系中,或者 .grd 文件不好。 I had a look at your actual data and my guess is that your data is from Canada, the lat and longs are in wgs-1984 and I'm unsure what is going on with your .grd files.我查看了您的实际数据,我猜测您的数据来自加拿大,纬度和经度在 wgs-1984 中,我不确定您的 .grd 文件发生了什么。 There lower left hand corner starts at 0,0.左下角从 0,0 开始。 If you extract correctly you will have all NA values because the data do not overlap.如果您正确提取,您将拥有所有 NA 值,因为数据不重叠。 This is a data issue and not an R question.这是一个数据问题,而不是一个 R 问题。

library(raster)
library(rgdal)
#get extent of grid file
dat.dir<-'filelocations'
a<-raster(paste0(dat.dir,'/p201307.grd')
extent(a)
#class       : Extent 
#xmin        : -0.5 
#xmax        : 124.5 
#ymin        : -0.5 
#ymax        : 94.5 
#get locs
locs<-read.csv(paste0(dat.dir,'/latandlong.csv'))
head(locs)
#longitude latitude
# 1 -116.8736  60.0334

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM