Mapping geospatial data using the R programming language

I recently had a requirement to visualise some geospatial radiation data exported from an R400 radiation detector (don't ask) by plotting the data on an interactive map.

This simple R script will plot the data from a raw Data Logger file downloaed from an FLIR identiFINDER R400  device onto an interactive heatmap for quck and easy visualisation. It takes a couple of minutes to get this working on a personal computer using RSuidio, even if you have never used R before, alternatively the same script  could be run on a web server as an easy way to display the map online. Everything you need to get this script working from scratch is included in the comments in the code below. Feel free to try it. Enjoy!

The same code can easily be modified to map any geolocated data, for instance annua sales figures at various store locations -- just change the column names, that's all there is to it.

# R programming language (version R-3.5.1) - install this 1st
# http://www.stats.bris.ac.uk/R/

# RStudio IDE (version 1.1.453) - install this 2nd:
# https://www.rstudio.com/


# Install packages

# Import extended open functions -- to support reading Excel files.
# https://www.rdocumentation.org/packages/openxlsx/versions/4.1.0
# https://www.rdocumentation.org/packages/openxlsx/versions/4.1.0/topics/read.xlsx
if (!require(xlsx)) install.packages("xlsx", repos = "http://cran.us.r-project.org")
library("xlsx", lib.loc="C:/Program Files/R/R-3.5.1/library")

# Import extended filter() function.
if (!require(dplyr)) install.packages("dplyr", repos = "http://cran.us.r-project.org")
library("dplyr", lib.loc="C:/Program Files/R/R-3.5.1/library")

# Import mapping functions - setView() etc.
if (!require(maps)) install.packages("maps", repos = "http://cran.us.r-project.org")
library("maps", lib.loc="C:/Program Files/R/R-3.5.1/library")

# Import map layering functions - leaflet(), colorNumeric(), etc.
if (!require(leaflet)) install.packages("leaflet", repos = "http://cran.us.r-project.org")
library("leaflet", lib.loc="C:/Program Files/R/R-3.5.1/library")


# Load the raw data from an Excel workbook.

r400LogRaw = read.xlsx("C:/Users/tacheson/Downloads/R400/Data Logging 112.slk.xlsx", 1)

# Exclude records lacking mappable/geospatial/GIS data.
r400LogFiltered = filter(r400LogRaw, is.null(r400LogRaw$Longitude) == FALSE & is.na(r400LogRaw$Longitude) == FALSE)

# Enforce numeric data type where needed.
r400LogFiltered$Latitude <- as.numeric(as.character(r400LogFiltered$Latitude))
r400LogFiltered$Longitude <- as.numeric(as.character(r400LogFiltered$Longitude))


# Draw the map.

# Define the colour gradient used to visualise radiation dose rate on a heatmap.
colourPallette <- colorNumeric(
  palette = c("Yellow", "Red"),
  domain = r400LogFiltered$Average.Dose.Rate)

# Render the map.
# providerTiles e.g, CartoDB.Positron: http://leaflet-extras.github.io/leaflet-providers/preview/
# radius = ~Maximal.Dose.Rate
leaflet(r400LogFiltered) %>% 
  setView(lng = mean(r400LogFiltered$Longitude), lat = mean(r400LogFiltered$Latitude), zoom = 13) %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addCircleMarkers(lng = ~Longitude, lat = ~Latitude, popup = ~paste0("Dose: ", Average.Dose.Rate), radius = 3, color = ~colourPallette(Average.Dose.Rate), stroke = FALSE, fillOpacity = 0.9)

Examples of heatmaps generated by this script (screenshots):

(Apologies, I currently am unavle to share most of my maps or the interactive HTML-based versions, but i will make some that can be shared soon and upload them here, so please do watch this space.)

I've been dabbling in functional programing languages for a while - particularly F#, the excellent functional language available within the NET Framework and CLI -- Microsoft's thriving mainstream software development platform. Functional programming is, of course, naturally suited to processing datesets and crunching statistics.

This quick easy mapping application was the perfect excuse to venture deeper than ever before into R and the powerful packages available to its developers. The R programming language might have been called "S" for "statistical" (after its most poular application or even after the Scheme langage that inspired its lexical scoping semantics) -- had that name had not already been taken by its forerunner. I'd like to add a new function read.slk() to open raw SLK files (a proprietary file format from Microsoft used by the R400 device) using COM Interop, or do this by combining F# and R, because R can also be harnessed by the .NET CLR. If you find an R package that supports the SLK specification, or if you beat me to it and write one, please let me know!

I want to say a lot more about the R400... Perhaps I will in due course, so watch this space... It's an exceptionally useful and interesting piece of kit, capable of very sensitive radiation detection as well as radionuclide identification by gamma-ray spectroscopy. Its principal use is in military and border threat detection, but it's equally useful in the lab -- and I've found it useful in a wide range of applications from domestic radiation risk assessment, to radiation surveys and environmental monitoring at atomic weapons storage facilities, and even recently in research into the radiation associated with UAP/UFOs!.

06 July 2018

Share the love:

Comments: 0

Add Comment

Tags:


  • Twitter
  • LinkedIn
  • Facebook
  • Windows Live / Messenger
  • Xbox Live
  • RSS
  • Email