library("sp")
library("ggplot2")
## Could be wind direction values in degrees
values <- c(55, 355)
## Location of sensors. First column x/longitud, second y/latitude
locations <- data.frame(lon = c(1, 2), lat = c(1, 2))
coordinates(locations) <- ~lon+lat
## The grid for which to extrapolate values
grid <- data.frame(lon = c(1, 2, 1, 2), lat = c(1, 2, 2, 1))
coordinates(grid) <- ~lon+lat
## Perform the inverse distance weighted interpolation
res <- idw360(values, locations, grid)
head(res)
if (FALSE) {
df <- cbind(res, as.data.frame(grid))
## The wind direction compass starts where the 90 degree mark is located
ggplot(df, aes(lon, lat)) +
geom_point() +
geom_spoke(aes(angle = ((90 - pred) %% 360) * pi / 180),
radius = 1,
arrow=arrow(length = unit(0.2, "npc")))
library("mapproj")
## Random values in each of the measuring stations
locations <- stations[, c("lon", "lat")]
coordinates(locations) <- ~lon+lat
crs_string <- "+proj=longlat +ellps=WGS84 +no_defs +towgs84=0,0,0"
proj4string(locations) <- CRS(crs_string)
values <- runif(length(locations), 0, 360)
pixels <- 10
grid <- expand.grid(lon = seq((min(coordinates(locations)[, 1]) - .1),
(max(coordinates(locations)[, 1]) + .1),
length.out = pixels),
lat = seq((min(coordinates(locations)[, 2]) - .1),
(max(coordinates(locations)[, 2]) + .1),
length.out = pixels))
grid <- SpatialPoints(grid)
proj4string(grid) <- CRS(crs_string)
## bind the extrapolated values for plotting
df <- cbind(idw360(values, locations, grid), as.data.frame(grid))
ggplot(df, aes(lon, lat)) +
geom_point(size = .1) +
geom_spoke(aes(angle = ((90 - pred) %% 360) * pi / 180),
radius = .07,
arrow=arrow(length = unit(0.2,"cm"))) +
coord_map()
}
Run the code above in your browser using DataLab