Preface

This demo blog post uses a very developmental version of fmi. To run the demo on your own computer, you will have to do the following:

# fmi depends on rwfs
install_github("ropengov/rwfs")
install_github("ropengov/fmi")

Accessing FMI lightning strikes data using package fmi

library(fmi)
library(dplyr)
library(DT)
library(leaflet)
library(raster)
library(sp)
library(tmap)

NOTE! You will need to provide your own apiKey first:

API_KEY <- "ENTER YOUR API KEY HERE"
# Load data from tmap
data(Europe)
finland <- subset(Europe, iso_a3 == "FIN")
# Project to WGS84 (EPSG:4326) from ETRS-LAEA (EPSG:3035)
crs_wgs84 <- CRS("+init=epsg:4326")
finland <- spTransform(finland, crs_wgs84)

Using the function above simplifies querying. For demonstration, let’s get data for April 2015:

# Define the query time period
startDateTime <- "2015-06-24"
endDateTime <- "2015-06-25"

# Setup a bounding box that covers the whole of Finland
bbox <- raster::extent(c(25, 31, 60, 70))

# First, initialize a new fmi session
init_session(API_KEY)

# NOTE: the response SpatialPointsDataFrame will be in WGS84 CRS.
lightning_data <- fmi_lightnings(startDateTime, endDateTime, bbox)
## OGR data source with driver: GML 
## Source: "/tmp/RtmpvGZdSg/file389970e64300", layer: "BsWfsElement"
## with 17324 features
## It has 4 fields

Let’s take a look at the measurement data:

DT::datatable(lightning_data@data)
# Create a palette that maps factor levels to colors
pal <- colorFactor(c("navy", "red"), domain = c(0, 1))

m <- leaflet::leaflet(lightning_data) %>%
  addCircleMarkers(
    radius = 3,
    color = ~pal(cloud_indicator),
    stroke = FALSE, fillOpacity = 0.5
  ) %>% 
  addLegend("bottomright", pal = pal, values = ~cloud_indicator,
    title = "Cloud lightning", labels = c("ground", "cloud"),
    opacity = 1
  )
m %>% addProviderTiles("CartoDB.Positron") 

That’s it, a small demo on how to use fmi package for fetching lightning strike data.

sessionInfo()
## R version 3.3.1 (2016-06-21)
## Platform: x86_64-suse-linux-gnu (64-bit)
## Running under: openSUSE Tumbleweed
## 
## locale:
##  [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
##  [3] LC_TIME=C                  LC_COLLATE=fi_FI.UTF-8    
##  [5] LC_MONETARY=fi_FI.UTF-8    LC_MESSAGES=en_US.UTF-8   
##  [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
##  [9] LC_ADDRESS=C               LC_TELEPHONE=C            
## [11] LC_MEASUREMENT=fi_FI.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
## [1] tmap_1.4-1    raster_2.5-8  sp_1.2-3      leaflet_1.0.1 DT_0.2       
## [6] dplyr_0.5.0   fmi_0.1.15    R6_2.2.0     
## 
## loaded via a namespace (and not attached):
##  [1] Rcpp_0.12.7        spdep_0.6-8        plyr_1.8.4        
##  [4] formatR_1.4        RColorBrewer_1.1-2 LearnBayes_2.15   
##  [7] rwfs_0.1.15.9002   bitops_1.0-6       class_7.3-14      
## [10] tools_3.3.1        boot_1.3-18        digest_0.6.10     
## [13] jsonlite_1.1       evaluate_0.10      tibble_1.2        
## [16] nlme_3.1-128       lattice_0.20-33    Matrix_1.2-6      
## [19] DBI_0.5-1          yaml_2.1.13        rgdal_1.1-10      
## [22] coda_0.18-1        e1071_1.6-7        osmar_1.1-7       
## [25] stringr_1.1.0      knitr_1.14         gtools_3.5.0      
## [28] htmlwidgets_0.7    rgeos_0.3-20       classInt_0.1-23   
## [31] grid_3.3.1         XML_3.98-1.4       rmarkdown_1.1     
## [34] gdata_2.17.0       tidyr_0.6.0        deldir_0.1-12     
## [37] magrittr_1.5       scales_0.4.0       gmodels_2.16.2    
## [40] splines_3.3.1      MASS_7.3-45        htmltools_0.3.5   
## [43] assertthat_0.1     colorspace_1.2-7   geosphere_1.5-5   
## [46] KernSmooth_2.23-15 stringi_1.1.2      munsell_0.4.3     
## [49] lazyeval_0.2.0     RCurl_1.95-4.8