Background
This is a basic tutorial on how to load shapefiles and geopoints to a leaflet map.
There is a lot more detailed information here [https://rstudio.github.io/leaflet/](https://rstudio.github.io/leaflet/)
The example is hopefully useful as it uses UK postcode and district shapefiles, which are commonly useful for a variety of purposes.
UK Postcode data can be found here
https://www.freemaptools.com/map-tools.htm
UK district shapefiles are available here
https://www.ordnancesurvey.co.uk/opendatadownload/products.html
Libraries
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr 1.1.3 ✔ readr 2.1.4
✔ forcats 1.0.0 ✔ stringr 1.5.0
✔ ggplot2 3.4.4 ✔ tibble 3.2.1
✔ lubridate 1.9.3 ✔ tidyr 1.3.0
✔ purrr 1.0.2
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag() masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library (knitr)
library (leaflet)
Data
Create a folder to house data
if (! dir.exists ("data/ukpostcodes" ))(dir.create ("data/ukpostcodes" ))
system ("rm -rf data/ukpostcodes/*" )
Download a list of UK postcodes and gps locations
download.file (url = "https://data.freemaptools.com/download/full-uk-postcodes/ukpostcodes.zip" ,destfile = paste ("data/ukpostcodes/ukpostcodes.zip" ))
system ("unzip data/ukpostcodes/ukpostcodes.zip -d data/ukpostcodes/" )
Download a list of UK district shapefiles
# set a timeout proportional to the size of the dataset and the speed of the connection
options (timeout= 300 )
download.file (url = "https://api.os.uk/downloads/v1/products/BoundaryLine/downloads?area=GB&format=ESRI%C2%AE+Shapefile&redirect" ,destfile = paste ("data/ukpostcodes/bdline_essh_gb.zip" ))
system ("unzip data/ukpostcodes/bdline_essh_gb.zip -d data/ukpostcodes/" )
Read Postcode data
ukpostcodes <- read_csv ("data/ukpostcodes/ukpostcodes.csv" )
Rows: 1794534 Columns: 4
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (1): postcode
dbl (3): id, latitude, longitude
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Make a small sample of all postcodes
sample<- ukpostcodes[sample (1 : nrow (ukpostcodes),size = 100 ),] %>%
mutate (
latitude = as.numeric (latitude),
longitude = as.numeric (longitude)
)
head (sample)
# A tibble: 6 × 4
id postcode latitude longitude
<dbl> <chr> <dbl> <dbl>
1 1005683 IP7 7DJ 52.1 0.977
2 965023 KY15 7AJ 56.3 -3.20
3 550211 PL20 6JB 50.5 -4.08
4 198258 TN10 4QA 51.2 0.285
5 2579536 NP10 9NT 51.6 -3.06
6 1529418 BS13 7HN 51.4 -2.61
Read in shapefiles to a map
map = read_sf ("data/ukpostcodes/Data/GB/county_region.dbf" )
mapproj <- st_transform (map, st_crs ("+proj=longlat +init=epsg:4326 +ellps=WGS84 +datum=WGS84 +no_defs" ))
Warning in CPL_crs_from_input(x): GDAL Message 1: +init=epsg:XXXX syntax is
deprecated. It might return a CRS with a non-EPSG compliant axis order.
leaflet (mapproj) %>%
addTiles () %>%
addMeasure (primaryLengthUnit= "kilometers" , secondaryLengthUnit= "meters" ) %>%
addScaleBar (options = c (imperial = FALSE )) %>%
addPolygons (color = "green" ,label = mapproj$ NAME) %>%
addCircleMarkers (lng = sample$ longitude,lat = sample$ latitude,label = sample$ postcode,radius = 0.3 )