1

I am trying to learn about geospatial visualization.

Here is a shapefile I downloaded for the UK:

library(sf)
library(leaflet)

# Set the URL 
url <- "https://data.london.gov.uk/download/statistical-gis-boundary-files-london/b381c92b-9120-45c6-b97e-0f7adc567dd2/London-wards-2014.zip"

# Download the zip file
download.file(url, destfile = "London-wards-2014.zip")

unzip("London-wards-2014.zip", list = TRUE)

shapefile <- st_read("London-wards-2014 (1)/London-wards-2014_ESRI/London_Ward.shp")

It looks like this:

Simple feature collection with 654 features and 7 fields
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 503568.2 ymin: 155850.8 xmax: 561957.5 ymax: 200933.9
Projected CRS: OSGB 1936 / British National Grid

First 10 features:

                         NAME  GSS_CODE HECTARES NONLD_AREA LB_GSS_CD              BOROUGH POLY_ID                       geometry
1           Chessington South E05000405  755.173          0 E09000021 Kingston upon Thames   50840 POLYGON ((516401.6 160201.8...
2      Tolworth and Hook Rise E05000414  259.464          0 E09000021 Kingston upon Thames  117160 POLYGON ((517829.6 165447.1...
3                  Berrylands E05000401  145.390          0 E09000021 Kingston upon Thames   50449 POLYGON ((518107.5 167303.4...
4                   Alexandra E05000400  268.506          0 E09000021 Kingston upon Thames   50456 POLYGON ((520480 166909.8, ...
5                    Beverley E05000402  187.821          0 E09000021 Kingston upon Thames  117161 POLYGON ((522071 168144.9, ...
6                 Coombe Hill E05000406  442.170          0 E09000021 Kingston upon Thames  117159 POLYGON ((522007.6 169297.3...
7  Chessington North and Hook E05000404  192.980          0 E09000021 Kingston upon Thames   50530 POLYGON ((517175.3 164077.3...
8               Surbiton Hill E05000413  166.482          0 E09000021 Kingston upon Thames   50457 POLYGON ((517469.3 166878.5...
9                  Old Malden E05000410  180.016          0 E09000021 Kingston upon Thames   50455 POLYGON ((522231.1 166015, ...
10                 St. Mark's E05000412  137.578          0 E09000021 Kingston upon Thames   50450 POLYGON ((517460.6 167802.9...

I tried to plot this:

# Plot the shapefile using leaflet
leaflet() %>% 
  addTiles() %>% 
  addPolygons(data = shapefile)

But I get nothing. And if I use this code - suddenly I get something that kinda looks ok:

my_shapefile_wgs84 <- st_transform(shapefile, crs = 4326)

# Plot the shapefile using leaflet
leaflet() %>% 
  addTiles() %>% 
  addPolygons(data = my_shapefile_wgs84)

Can someone exaplin this difference?

1

1 Answer 1

1

The shapefile stores its locations using British National Grid coordinates. This is the number of metres from an origin south-west of England. The leaflet package cannot deal with data with these coordinates, so you have to transform them to latitude-longitude degrees.

The crs is the coordinate reference system the data is using. British National Grid is "EPSG code 27700" and the lat-long system that leaflet needs is "EPSG code 4326". R knows that the shapefile is in British National Grid because this is in the ".prj" component of the shapefile files. You then convert the data to EPSG 4326 and leaflet displays it correctly.

You should get some clear warnings if you try leaflet without lat-long data:

Warning messages:
1: sf layer is not long-lat data 
2: sf layer has inconsistent datum (+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.999601272 +x_0=400000 +y_0=-100000 +ellps=airy +units=m +no_defs).
Need '+proj=longlat +datum=WGS84' 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.