2017-07-29 3 views
1

私はLAT、LON、Temperatureのデータを持つ3列のデータセットを持っています 24データロガーのデータポイントに基づいてランドスケープの温度を予測するラスタイメージを生成したいと思います。データセットは、ここにアクセスすることができます。DATAラスタに補間する

これは私がここまで試みられてきたものである:

#Lets try to interpolate the data onto a raster 
library (raster) 
library (gstat) 
library (sp) 
#Temp and XY data 
temp<-read.csv ('test_temp.csv') 
#create a blank raster to the extent of the system 
r<- raster (nrows=300, ncols=100, xmn=-84.95, xmx=-84.936, ymn=45.7, ymx=45.74) 

#build a prediction model temp ~ LAT*LON 
loc<-temp [,c(3,5,6)] 
loc<-na.omit (loc) 
#TPS model 
tps<-Tps(loc, loc$TemperatureC) 
#gstat model 
mod1<-gstat (data=temp, formula=TemperatureC ~ 1, locations=loc) 
summary (mod1) 

r2<-interpolate(r, model=tps) 
    Error in scale.default(x, xc, xs) : 
    length of 'center' must equal the number of columns of 'x' 

r2<-interpolate(r, model=mod1) 
Error in bbox(dataLst[[1]]$data) : object not a >= 2-column array 

Ultimatleyが、私は一日の異なる時間での温度変化を示すための補間データのラスタのシリーズを作成したいです。これを行う方法に関するアイデア?コードと例の融合を使用して

答えて

0

は、私は私には正しいような何かを思い付いた:

library(ggplot2) # start needed libraries 
library(gstat) 
library(sp) 
library(maptools) 

my_location<-c(lon= -84.94, lat = 45.72) 
basemap <- get_map(location = my_location, zoom = 14, maptype = "hybrid") 
plot (basemap) 
#load data 
field_data<-read.csv ("test_temp.csv") 
test_temp <- field_data # duplicate air temp. data file 
test_temp$x <- test_temp$LON # define x & y as longitude and latitude 
test_temp$y <- test_temp$LAT 
test_temp<-na.omit(test_temp) 
coordinates(test_temp) = ~x + y #set spatial coordinates to create a Spatial object 

plot(test_temp) 
x.range <- as.numeric(c(-84.94, -84.935)) # map extent 
y.range <- as.numeric(c(45.705, 45.735)) 
grd <- expand.grid(x = seq(from = x.range[1], to = x.range[2], by = 0.001), 
        y = seq(from = y.range[1], to = y.range[2], by = 0.001)) # expand points to grid 
coordinates(grd) <- ~x + y 
gridded(grd) <- TRUE 
plot(grd, cex = 1.5) 
points(test_temp, pch = 1, col = "red", cex = 1) 
idw <- idw(formula = TemperatureC ~ 1, locations = test_temp, newdata = grd) # apply idw model for the data 
idw.output = as.data.frame(idw) 
names(idw.output)[1:3] <- c("long", "lat", "var1.pred") # give names to the modelled variables 
# plot results 
ggplot() + 
    geom_tile(data = idw.output, alpha=0.5, aes(x = long, y = lat, fill=var1.pred, 0)) + 
    geom_point(data=field_data, aes(x=LON, y=LAT), shape=21, colour="red") + 
    scale_fill_gradient(low="cyan", high="orange") + theme_bw() 

ggmap(basemap, extent = "device") + 
    geom_tile(data = idw.output, alpha=0.5, aes(x = long, y = lat, fill=var1.pred, 0)) + 
    geom_point(data=field_data, aes(x=LON, y=LAT), shape=16, size=.5, colour="red") + 
    scale_fill_gradient(low="cyan", high="red") + theme_bw() 

enter image description here