[英]Why can I not get worldclim data (avg temp and prec) for the state which I live at all using R?
I'd like to get the average temperature and precipitation for the state of Ceara in Brazil;我想得到巴西 Ceara 的 state 的平均温度和降水量; I made a cropped area on the map as:
我在 map 上做了一个裁剪区域:
and I used the center lat/lon as lat=-5.49839
and lon=-39.32062
I got the caps of latitude and longitude as latinicial=-7.24614
(minimum latitude), latfinal=-3.76140
(maximum latitude), longinicial=-40.38084
(minimum longitude) and longfinal=-38.77385
(maximum longitude) then I've simulated a uniformly distributed temperature for both latitude and longitude which lies in their maxima and minima.我使用中心纬度/经度作为
lat=-5.49839
和lon=-39.32062
我得到纬度和经度的上限为latinicial=-7.24614
(最小纬度), latfinal=-3.76140
(最大纬度), longinicial=-40.38084
(最小经度)和longfinal=-38.77385
(最大经度)然后我模拟了纬度和经度的均匀分布温度,位于它们的最大值和最小值。
My code is given as follows:我的代码如下:
library(raster)
library(sp)
library(rgeos)
library(rgdal)
library(dismo)
library(rgdal)
library(sf)
d=getData('worldclim',lat=-5.49839,lon=-39.32062,res=0.5,var='bio')
latinicial=-7.24614
latfinal=-3.76140
longinicial=-40.38084
longfinal=-38.77385
latitude=runif(100,latinicial,latfinal)
longitude=runif(100,longinicial,longfinal)
coord=data.frame(latitude,longitude)
points = SpatialPoints(coord, proj4string = d@crs)
d <- d[[c(1,12)]]
names(d)=c("Temp","Prec")
extract(d,coord)
But when I run it, I got NA values for all rows even though I'm showing you only 4 rows:但是当我运行它时,我得到了所有行的 NA 值,即使我只显示了 4 行:
So, I'd like to know what happened to it.所以,我想知道它发生了什么。 Why do I get NA values?
为什么我会得到 NA 值?
The problem is with the order of longitude and latitude in coords
.问题在于
coords
中经度和纬度的顺序。 When you put coords
into SpatialPoints
, it expects the order to be longitude
then latitude
, but you have it reversed.当您将
coords
放入SpatialPoints
时,它期望顺序是longitude
然后latitude
,但是您将其颠倒了。 Once you fix that, then it will extract the data correctly.一旦你解决了这个问题,它就会正确地提取数据。 All the code above
coord
works fine. coord
上面的所有代码都可以正常工作。 Also, if you are going to run this code multiple times, then I would recommend using set.seed
.另外,如果您要多次运行此代码,那么我建议您使用
set.seed
。 This will allow you to get the same values every time when you run the runif
statements.这将允许您在每次运行
runif
语句时获得相同的值。
library(raster)
library(sp)
set.seed(243)
d = getData(
'worldclim',
lat = -5.49839,
lon = -39.32062,
res = 0.5,
var = 'bio'
)
latinicial = -7.24614
latfinal = -3.76140
longinicial = -40.38084
longfinal = -38.77385
latitude = runif(100, latinicial, latfinal)
longitude = runif(100, longinicial, longfinal)
coord = data.frame(longitude, latitude)
points = SpatialPoints(coord, proj4string = d@crs)
d <- d[[c(1, 12)]]
names(d) = c("Temp", "Prec")
extract(d, coord)
Output Output
head()
Temp Prec
[1,] 239 655
[2,] 267 832
[3,] 256 541
[4,] 269 740
[5,] 242 784
[6,] 233 981
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.