In this homework it is aimed to demonstrate how an interactive map can be created and how it can be beneficial for viewing desired variable changes in designated locations. This approach can be applied to create an interactive map for cities with olive production in Aegean Region where also it can be used for many other purposes to create a user-friendly medium in climate change presentations especially for audience who are not familiar with R interface.
Data used in this homework were prepared using CDO (Climate Data Operators) in Linux. CDO operations could be done in R Studio’s interface too (by using the ClimateOperators package) however, because of dealing with large amount of Data and to decrease processing time, operations shown below were done separately in a Linux environment.
Now that the data is ready, let’s start coding in R by first setting the working directory and then load necesseary libraries.
setwd("C:/Users/spell/OneDrive/Documents/Homeworks/MTO533E_HW_EMRESALKIM")
library(ncdf4)
library(leaflet)
taymean <- nc_open("ta_ymean.nc")
prymean <- nc_open("pr_ymean.nc")
ts <- ncvar_get(taymean, "ta")
pr <- ncvar_get(prymean, "pr")
lat <- ncvar_get(taymean, "lat")
lon <- ncvar_get(taymean, "lon")
tc = ts-273.15
prMMD = pr*84600
Warning: While obtaining the mean temperatures there has been a miscalculation which is yet to be resolved. When resolved, this webpage will be updated.
istanbulmeanT=mean(tc[59,263,])
istanbulmeanP=mean(prMMD[59,263,])
ankarameanT=mean(tc[67,261,])
ankarameanP=mean(prMMD[67,261,])
izmirmeanT=mean(tc[55,258,])
izmirmeanP=mean(prMMD[55,258,])
antalyameanT=mean(tc[63,255,])
antalyameanP=mean(prMMD[63,255,])
adanameanT=mean(tc[72,255,])
adanameanP=mean(prMMD[72,255,])
vanmeanT=mean(tc[88,258,])
vanmeanP=mean(prMMD[88,258,])
samsunmeanT=mean(tc[74,264,])
samsunmeanP=mean(prMMD[74,264,])
urfameanT=mean(tc[79,255,])
urfameanP=mean(prMMD[79,255,])
elazigmeanT=mean(tc[79,258,])
elazigmeanP=mean(prMMD[79,258,])
ardahanmeanT=mean(tc[87,263,])
ardahanmeanP=mean(prMMD[87,263,])
In order to provide the information given in pop up windows when hoovering over the cities functions below were used.
MeanTemperatures=c(istanbulmeanT,ankarameanT,izmirmeanT,antalyameanT,adanameanT,vanmeanT,samsunmeanT,urfameanT,elazigmeanT,ardahanmeanT)
MeanPrecipitation=c(istanbulmeanP,ankarameanP,izmirmeanP,antalyameanP,adanameanP,vanmeanP,samsunmeanP,urfameanP,elazigmeanP,ardahanmeanP)
location=c("İstanbul","Ankara","İzmir","Antalya","Adana","Van","Samsun","Urfa","Elazığ","Ardahan")
longitude=c(28.9784,32.8597,27.1428,30.7133,35.3308,43.3730,36.3361,38.7955,39.2225,42.7023)
latitude=c(41.0082,39.9334,38.4237,36.8969,36.9914,38.5012,41.2797,37.1674,38.6748,41.1130)
Data<-data.frame(location,MeanTemperatures,MeanPrecipitation,longitude,latitude)
Finally by formatting the information in pop windows and setting a color palette, the interactive map for demo cities were created.
popup_info=paste(location,"<br/>","Mean Temperature Between 2022-2049",MeanTemperatures,"Celsius","<br/>","Mean Daily Precipitation Between 2022-2049",MeanPrecipitation,"mm/day","<br/>")
colors<-c("blue","red")
palette<-colorFactor(colors,Data$MeanTemperatures)
leaflet(data=Data, options = leafletOptions(attributionControl=FALSE,zoomControl=FALSE))%>%
addTiles()%>%
setView(34,39,5)%>%
addScaleBar(position="bottomleft")%>%
addCircleMarkers(data=Data, lat=latitude,lng=longitude, radius = 5, popup = popup_info, color=palette(MeanTemperatures))