library(urbanR)
#library(openair)
library(ggplot2)
#library(mapview)
library(raster) # to use crs()
library(sf)
#library(sp)

# -----  NO2 data:
output_path <- "/esarchive/scratch/nperez/git/Flor/Jan4Nuria/"
results <- readRDS(paste0(output_path, "test_NonUniformGrid.rds"))

n_receptors <- nrow(results[results$date == results$date[1],])
crs_sim <- "+proj=utm +zone=31 +ellps=intl +units=m +no_defs"

# data.frame:
mean_map <- results[results$date == results$date[1],]
ggplot(data = mean_map, aes(x = X, y = Y, colour = NO2)) + geom_point()


# ----  Districts:
path <- "/esarchive/scratch/jbenavides/data/geom/";
districts <- sf::read_sf(paste0(path, "BCN_Districte_ED50_SHP.shp"))
plot(districts)
ggplot(data = districts, aes(x = Coord_X, y = Coord_Y, colour = Area)) + geom_point()
ggplot(data = districts, aes(x = Coord_X, y = Coord_Y, colour = Area)) + geom_path()
ggplot(data = districts, aes(x = Coord_X, y = Coord_Y, fill = Area)) + geom_polygon(color = 'black')

ggplot() + geom_point(data = districts, aes(x = Coord_X, y = Coord_Y, group = Area))
ggplot() + geom_point(data = districts, aes(x = Coord_X, y = Coord_Y, colour = N_Distri)) + 
  geom_polygon(colour = 'black')

# Districts!!
ggplot() + geom_sf(data = districts) + geom_point(data = districts, aes(x = Coord_X, y = Coord_Y, colour = N_Distri))

ggplot() + geom_sf(data = districts, aes(fill = N_Distri))

# Add points NO2:
ggplot() + geom_sf(data = districts) + 
 geom_point(data = mean_map, aes(x = X, y = Y, colour = NO2))
dev.new()

# This method fails because coordinates names are missed, check colnames: coordinates(dist)
#library(rgdal)
#dist <- readOGR(paste0(path, "BCN_Districte_ED50_SHP.shp"))
#ggplot() + geom_point(data = dist, aes(x = coords[1], y = coords[2], colour = N_Distri))
#ggplot() + geom_polygon(data = dist, aes(x = colour = 'black')
#dist2 <- SpatialPointsDataFrame(coords = test, data = dist@data, proj = dist@proj4string, bbox = dist@bbox)

# ---- Create uniform grid:I
spatial_context <- context_sp(geom_path = '/esarchive/scratch/jbenavides/data/geom/')
config <- NULL
config$crs_sim <- "+proj=utm +zone=31 +ellps=intl +units=m +no_defs"
receptor_grid <- sf::st_sf(geom=sf::st_make_grid(spatial_context, 
                           offset = c(420819, 4574709), square = TRUE, 
#offset = c(420821.5, 4574716),
                     cellsize = 10, what = "centers"), crs=config$crs_sim)
# add coordinates required for plotting
receptor_grid$data <- cbind(receptor_grid$data, Coord_X = st_coordinates(receptor_grid)[,1],
                            Coord_Y = st_coordinates(receptor_grid)[,2])
receptor_grid$Coord_X <- st_coordinates(receptor_grid)[,1]
receptor_grid$Coord_Y <- st_coordinates(receptor_grid)[,2]

ggplot() + geom_point(data = receptor_grid, aes(x = Coord_X, y = Coord_Y, colour = Coord_X))

# points on the are of BCN
ref <- st_intersection(receptor_grid, districts)
ggplot() + geom_point(data = ref, aes(x = Coord_X, y = Coord_Y, colour = Coord_X))

# calculate distance between points
origin <- c(428000, 4582500)
dist <- sqrt((ref$Coord_X - origin[1])^2 + (ref$Coord_Y - origin[2])^2) 
ref$Dist <- dist
ggplot() + geom_point(data = ref, aes(x = Coord_X, y = Coord_Y, colour = Dist))
dev.new()

# change to sf data.frame
mean_map_sf <- mean_map %>% sf::st_as_sf(coords = c("X", "Y"), crs = crs_sim)

bcn_mean_map <- mean_map_sf
bcn_mean_map$Coord_X <- sf::st_coordinates(bcn_mean_map)[,1]
bcn_mean_map$Coord_Y <- sf::st_coordinates(bcn_mean_map)[,2]
ggplot() + geom_point(data = bcn_mean_map, aes(x = Coord_X, y = Coord_Y, colour = NO2))
dev.new()

# select points where there are samples:
target <- st_intersection(receptor_grid, bcn_mean_map)
target
bcn_mean_map[50,]
subset(receptor_grid, Coord_X == 424974 & Coord_Y == 4581814)

ggplot() + geom_point(data = target, aes(x = Coord_X, y = Coord_Y))

# new object can be plotted:
ggplot() + geom_sf(data = districts) + 
  geom_point(data = bcn_mean_map, aes(x = X, y = Y, colour = NO2))

# transform to lat-lon
bcn_mean_map <- sf::st_transform(bcn_mean_map,crs("+proj=longlat"))
bcn_mean_map$X <- sf::st_coordinates(bcn_mean_map)[,1]
bcn_mean_map$Y <- sf::st_coordinates(bcn_mean_map)[,2]

ggplot(data = bcn_mean_map, aes(x = X, y = Y, colour = NO2)) + geom_point() +
 expand_limits(x = 2.125, y = 41.375)


districts$ <- sf::st_coordinates(districts)[,1]
districts$Y <- sf::st_coordinates(districsts)[,2]

points <- sp::SpatialPoints(methods::as(bcn_mean_map[,c('X','Y')], "Spatial"), 
              proj4string = sp::CRS("+proj=longlat"))

# alternative: convert to SpatialPointsDataFrame
mean_map_sp <- SpatialPointsDataFrame(mean_map[,c("X", "Y")], mean_map[,4:5])
plot(mean_map_sp)
plot(world, add = T)
ggplot(data = mean_map_sp, aes(x = X, y = Y, colour = NO2)) + geom_point()
