I’m very new in the R world. Welcome to my journey of learning Geospatial visualisation in R :)
The purpose of this vignette is to visualise how many blackspot mobile sites were awarded in QLD on map, and calculate the distance between each site and Brisbane General Post Office(GPO). As blackspot sites are normally very far from the capital city, we need to know the travel distance in order to determine LAFA(Living away from home allowance) expenses for our field team. This is a very useful tool when it comes to cost tracking/forecasting budget for rural build projects. I downloaded the Mobile Black Spot Program (MBSP)dataset on QLD Gov website(source 1).
We are using ggmap to visaliase the mobile base stations locations on map. We also need to register a free Google API key and enable relevant API tools in the library. I will not go into details of API keys as this is not the main purpose of this Vignette. Please refer to the instruction on how to get API Key https://developers.google.com/maps/documentation/javascript/get-api-key (source 2)
We can usekable from knitrpackage to tidy up the table. Usehead()function to check the first 6 rows of the dataset.
blackspot<-read.csv(file='QLD blackspot sites.csv')
knitr::kable(head(blackspot[1:6, ]), caption ="QLD Mobile blackspot program")| LGA | BASE_STATION_ID | LOCATION_NAME | TITLE | ROAD_SECTION | MOBILE_OPERATOR | BASE_STATION_TYPE | STATE_ELECTORATE | Latitude | Longitude | ROUND |
|---|---|---|---|---|---|---|---|---|---|---|
| Balonne | OPT-007 | Castlereagh Highway | Castlereagh Highway | Between St George and Dirranbandi (Blue Lagoon) | Optus | Macro tower | WARREGO | -28.32323 | 148.4792 | 2 |
| Balonne | TLS-1114 | Bundoran Road | Bundoran Road | N/A | Telstra | Macro tower | WARREGO | -28.11330 | 148.7275 | 2 |
| Barcaldine | OPT-029 | Landsborough Highway | Landsborough Highway | Avoca Station | Optus | Macro tower | GREGORY | -23.53730 | 144.7930 | 2 |
| Blackall-Tambo | TLS-0544 | Landsborough Highway | Landsborough Highway | Telstra | Macro tower | GREGORY | -25.34400 | 146.5064 | 2 | |
| Brisbane | TLS-0492 | Kholo | Kholo | N/A | Telstra | Macro tower | MOGGILL | -27.50506 | 152.7749 | 2 |
| Brisbane | TLS-1723 | Upper Brookfield | Upper Brookfield | N/A | Telstra | Macro tower | MOGGILL | -27.47553 | 152.8692 | 2 |
First, we need to define an object: qldmap.Then, we use gggmap()``geom_point()to plot the sites on the map. x=longitude and y=latitude . As you can see in the map below, we have some outliers(sites located on Cook island) and some overplotting issues.Instead of generating static maps, Can we plot an interactive map with zoom-in and zoom-out functions?
qldmap<-get_map(location = "QLD,Australia", zoom=5, source = "google")
ggmap(qldmap)+
geom_point(aes(Longitude,Latitude,colour=BASE_STATION_TYPE,shape=MOBILE_OPERATOR), data=blackspot)We have already installed the leaflet package and loaded the library. Create map by callingleaflet() and add layers like addTiles() and addMarkers. Tiles are not specified in below code so OpenStreet map will be used by default. Now we can zoom in and zoom out to check every site name.
To calculate LAFA(Living away from Home allowance) expense, we normally will set the General Post office in each Capital city as our starting point, and destinations are the QLD black spots sites.
Now we need to get distance and travel time between Brisbane GPO and mobile sites. Use gmapsdistance(from,to, mode= "driving"...) (Source 3)to get the time, distance and status. For example, if we set the site on Cook Island as our destination, “status” will be invalid as gmapsdistance() cannot calculate distance over ocean. You can also choose “walking”,“bicycling” and “public transportation” as traveling mode.
I also create a dataframe Timedist2 to store distance/travel time for each site.
set.api.key("AIzaSyB1Fccl9sZOEUWC0NISGPdW5pclAp6vyo0") ## enable Google Distance Matrix API and set API key
mobilesites=paste(blackspot$Latitude,blackspot$Longitude,sep =",")
GPO = "-27.467676,153.027908" ## Brisbane General post office Coordinates
Timedist=gmapsdistance(GPO, mobilesites, mode="driving", shape = "long")
Timedist2=data.frame(Timedist) ##create dataframe
names(Timedist2) <- c("GPO","Destination", "Time", "Location_Name","col2","Distance","col3","col4","Status")
Timedist2$Location_Name <-blackspot$LOCATION_NAME ## Add location name
Timedist2$col2 <- NULL ## delete repeated cols
Timedist2$col3<- NULL
Timedist2$col4 <- NULL
Timedist2$Time <- Timedist2$Time/3600 ## hour
Timedist$Distance <- Timedist2$Distance/1000 ## KM
knitr::kable(head(Timedist2[1:6,]),caption ="QLD Mobile Blackspots sites-Time and distance") | GPO | Destination | Time | Location_Name | Distance | Status |
|---|---|---|---|---|---|
| -27.467676,153.027908 | -28.32323337,148.479157 | 5.9661111 | Castlereagh Highway | 539649 | OK |
| -27.467676,153.027908 | -28.113298,148.72747 | 5.6075000 | Bundoran Road | 508413 | OK |
| -27.467676,153.027908 | -23.5373,144.793 | 12.1361111 | Landsborough Highway | 1122969 | OK |
| -27.467676,153.027908 | -25.344001,146.506428 | 8.8200000 | Landsborough Highway | 800815 | OK |
| -27.467676,153.027908 | -27.505065,152.774925 | 0.7763889 | Kholo | 36091 | OK |
| -27.467676,153.027908 | -27.475527,152.869202 | 0.5269444 | Upper Brookfield | 20464 | OK |
I really enjoyed doing my first R assignment. Please feel free to leave any comments :D
Source:
1.https://www.data.qld.gov.au/dataset/qld-base-stations-mobile-black-spot-program
2.https://developers.google.com/maps/documentation/javascript/get-api-key
3.https://cran.r-project.org/web/packages/gmapsdistance/gmapsdistance.pdf