Notable Trees in Auckland, NZ

Here’s an interactive map of ‘notable’ trees in the Auckland region I created this using Leaflet, a powerful mapping package in R. Take your time to scroll around the Auckland region and click on the green points to see what kind of special trees you have in your neighbourhood! (datasource)

The inspiration for this post came from Isaac Bain’s tutorial where he used freely available data to map the DOC huts around NZ. You can re-create a similar visualisation with basically any shapefile by following the code below:

#load required libraries, set working directory and unzip the shapefile
library(tidyverse)
library(sp)
library(leaflet)
library(spdplyr)
library(rgdal)
setwd("C:\\Users\\user\\Documents\\trees")
unzip("kx-pup-notable-trees-SHP.zip")

I downloaded my data from Koordinates which holds GIS datasets from all around the world! In the next step I am reading in the data and converting it to the wgs84 standard.

#read in dataset and transform
trees <- readOGR("C:\\Users\\user\\Documents\\trees","pup-notable-trees")
wgs84 = '+proj=longlat +datum=WGS84'
trees <- spTransform(trees, CRS(wgs84))

In this step I am creating the HTML popup when a user clicks on the data point. You can get pretty fancy with these but the shapefile I had did not have much other information about the tree, apart from the name.

#combine html 'bold' and the tree name for pop-up
tree_popup <- paste0("<strong>Name: </strong>", trees$NAME)

And here is where we see the beauty of Leaflet, in just a few lines of code we can created a nice map for users to explore. Notice how you can make use of the pipe operator to chain commands together - very cool!

#create leaflet object
leaflet(trees) %>%

#add markers, change colour, opacity etc    
addCircleMarkers(stroke = FALSE, radius = 3.5, color = "Green", 
fillOpacity = 0.7, popup = tree_popup, label=trees$NAME, 
labelOptions = labelOptions(textOnly = TRUE)) %>%

#centered around Auckland, adjust lng and lat to suit 
setView(lng = 174.8335603 , lat = -36.8818881, zoom = 12) 
%>%  

#changing the map type to make it look a bit nicer 
addTiles() %>%
addProviderTiles("Stamen.Toner", group = "Toner") %>% 
addProviderTiles(providers$Stamen.Terrain, group = "Terrain")