2017-07-29 4 views
1

J'ai un ensemble de données à trois colonnes avec données LAT, LON et température Je voudrais produire une image matricielle qui prédit la température du paysage basée sur 24 points de données de l'enregistreur de données. L'ensemble de données est accessible ici: DATAInterpoler à raster

Voici ce que j'ai essayé jusqu'ici:

#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 Je veux créer une série de trames de données interpolés pour montrer la variation de température à différents moments de la journée. auriez vous des idées pour faire ça?

Répondre

0

En utilisant une fusion de code et des exemples, je suis venu avec quelque chose qui semble correct pour moi:

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