Upload shapefile to R Shiny app to extract leaflet map data

[This article was first published on rbloggers – SNAP Tech, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In this post I share an R Shiny app which uses the leaftlet package for interactive maps. This app differs from prior apps I’ve made featuring leaflet maps. First, it displays rasterized map data rather than just point layers. Also, longitude and latitude sliders in the browser allow for cropping the map. Additionally, the user can upload a shapefile to crop and mask the rasterized data overlays in the leaflet window to the specific spatial data they wish to work with, and then extract and download that data.

nwtapp1

Explore the app here

Features

  • Crop map extent using latitude and longitude sliders.
  • Upload a custom shapefile in the upload window to mask map layer.
  • Visually confirm your original shapefile and reprojected (if applicable) shapefile on the map look right.
  • The uploader compares your shapefile with the app data to ensure validity.
  • Shapefiles may contain points, lines, or polygons.
  • Combine crop and mask operations.
  • Use the color swatches to select from common color palettes or create your own.
  • Download the displayed map as a geotiff.
  • Use a modal to maintain clear separation between exploration in space at single time points (maps) and in time at single locations (time series charts).
  • Disable tooltips if you don’t need them.

Another new feature in this app, and relatively new to the Shiny package, is the incorporation of a Shiny module. Shiny modules are handy ways to abstract, externalize and reuse Shiny app code. This is useful for keeping apps tidy when they grow large and complex, but also when the same code can be recycled across multiple apps.

In this case I wrote a Shiny module that provides an interactive file uploader which appears in a modal window popup (shinyBS package) when a button is clicked. The file uploader is specifically geared toward accepting shapefiles. It displays previews of the original file and of the final shapefile as projected (if necessary) and overlain on top of a map. The module does some nominal validation of the shapefile to make sure the user has uploaded, for example, something which actually overlaps with the data in the app shown on the leaflet map. Once uploaded, the user can apply their shapefile as a mask to the app data.

The color picker (shinyJS package) allows for creating your own color palette if not using a predefined palette.

This app is restricted to climate data over Northwest Territories (NT), Canada. Some of the vetting of user uploaded shapefiles, like comparing spatial extents of a user-provided shapefile with this political boundary, would not have to occur if the app data had a worldwide extent. If you upload a shapefile of Australia, it will simply inform you that your shapefile does not overlap the available data. So if you want to play around with this feature, I have uploaded three example shapefiles for your convenience. Download and unzip the file. It contains one shapefile each of points, lines, and polygons which all intersect NT. The polygon also contains a hole. …And I at least know that these three shapefiles work! But please do add a Github issue if you find one that causes problems.

Shapefiles of points, lines or polygons are all permitted and handled by the app appropriately. For now I only permit downloading rasters as geotiffs. I can see cases where a non-spatial format may be desired, such as a csv file containing columns of longitude, latitude and data at least in the case of points and perhaps lines, or when data refers to results of an aggregate function that is applied to an entire polygon or to named sub-polygons.

Lastly, in addition to the modal popup window used to display the file uploader, a modal is also used for displaying time series plots pertaining to the currently selected leaflet map marker. Selecting a community on the map gives the user the option to open this modal and see location-specific data through time, which cannot be seen in the leaflet map itself.

While the leaflet maps show single decadal time slices of climate information spatially, a number of select NT communities have annual-resolution climate data contained in the app which can be viewed distinct from the leaflet map side of things. Of course, if a user uploads an arbitrary shapefile of points, they can only extract and download from what is shown in the decadal map; there would be no similar data sets available for every possible map pixel, hence why user-uploaded points which are added to the leaflet map as map markers are not clickable like the points which come included in the app as a special data set.

Full source code is available on Github.

To leave a comment for the author, please follow the link and comment on their blog: rbloggers – SNAP Tech.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)