Geographical Data

Three main categories - Point

  • Example: Weather Stations from last week

Three main categories - Line

  • Example: Railway Lines

Three main categories - Area

  • Example: Irish Counties

Reading these into R

  • A number of different options exist for spatial file format

    • shapefiles started life as an old ESRI (makers of ArcGIS) format
    • Became an adopted standard for many users
    • Splits information over several files
  • Alternative is geojson

    • open standard for storing geographical information
    • Everything in a single file - usually ends in geojson or json
    • Here we are using geojson files - in the usual folder are files rail.geojson, weather_stations.geojson and counties.geojson
library(tidyverse) # sf package uses this
library(sf) # The 'sf' package handles geographical data
rail <- st_read('rail.geojson') # Use 'st_read' from 'sf' to read some data

Looking at geojson files - 1

rainfall <- st_read('weather_stations.geojson')
rainfall
## Simple feature collection with 5 features and 11 fields
## geometry type:  POINT
## dimension:      XY
## bbox:           xmin: 181346.8 ymin: 393612.6 xmax: 181346.8 ymax: 393612.6
## projected CRS:  TM65 / Irish Grid
##   Year Month Rainfall Station Elevation  Easting Northing  County Abbreviation
## 1 1850   Jan    169.0  Ardara        15 180787.7   394679 Donegal           AR
## 2 1851   Jan    236.4  Ardara        15 180787.7   394679 Donegal           AR
## 3 1852   Jan    249.7  Ardara        15 180787.7   394679 Donegal           AR
## 4 1853   Jan    209.1  Ardara        15 180787.7   394679 Donegal           AR
## 5 1854   Jan    188.5  Ardara        15 180787.7   394679 Donegal           AR
##   Source coast_dist                  geometry
## 1 Briffa   17.36709 POINT (181346.8 393612.6)
## 2 Briffa   17.36709 POINT (181346.8 393612.6)
## 3 Briffa   17.36709 POINT (181346.8 393612.6)
## 4 Briffa   17.36709 POINT (181346.8 393612.6)
## 5 Briffa   17.36709 POINT (181346.8 393612.6)
  • Like a data frame, but has a geometry column

Looking at geojson files - 2

counties <- st_read('counties.geojson')
counties
## Simple feature collection with 7 features and 2 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 44201.66 ymin: 19537.25 xmax: 348847.8 ymax: 453198.8
## projected CRS:  TM65 / Irish Grid
##          County mean_rain                       geometry
## 1        Antrim  77.71545 MULTIPOLYGON (((324297.7 42...
## 2        Armagh  77.71545 MULTIPOLYGON (((306664.9 36...
## 3 Carlow County  92.14104 MULTIPOLYGON (((286891.9 18...
## 4  Cavan County  72.24133 MULTIPOLYGON (((205022.8 33...
## 5  Clare County 100.73245 MULTIPOLYGON (((127094 2124...
## 6     Cork City 100.00000 MULTIPOLYGON (((167246.6 75...
## 7   Cork County 107.23233 MULTIPOLYGON (((44246.91 37...
  • geometry column - objects are MULTIPOLYGON

Looking at geojson files - 3

rail
## Simple feature collection with 7 features and 1 field
## geometry type:  LINESTRING
## dimension:      XY
## bbox:           xmin: -8.947182 ymin: 51.84713 xmax: -7.510596 ymax: 53.46931
## geographic CRS: WGS 84
##         LEN                       geometry
## 1 27.832735 LINESTRING (-7.510596 53.46...
## 2  3.601239 LINESTRING (-8.747836 53.25...
## 3 11.241874 LINESTRING (-7.996752 51.93...
## 4 13.993221 LINESTRING (-8.752088 52.57...
## 5 10.890816 LINESTRING (-8.29744 52.548...
## 6 14.080600 LINESTRING (-8.467084 51.91...
## 7  3.868733 LINESTRING (-8.299423 51.84...
  • geometry column - objects are LINESTRING
  • Note map projection info is different WGS84 essentially Longitude and Latitude

Interlude - Map Projections

Map Projections - 1


By Hellerick - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=26737079

  • Lines of Longitude - great circles
  • Lines of Latitude - Vary in length
  • Map projections - try to map parts of globe onto 2D space preserving
    • Length or
    • Areas or
    • Angles
    • or some other criterion
  • The railway lines used the geographical projection
    • x direction is longitude, y is latitude
    • It preserves neither and is particularly bad for regions close to the poles!
    • Usually helpful to have at least a local length preserving projection

Map Projections - 2

EPSG Codes

  • EPSG: Codes introduced by the International Association of Oil and Gas Producers
    • Formerly European Petroleum Survey Group (EPSG)
    • A reference number for map projections
    • ISO 19111:2007 Geographic information—Spatial referencing by coordinates
    • A suggested projection for Ireland is 29902 (Irish Transverse Mercator)
  • See https://epsg.io/29902
  • Sometimes called crs (Coordinate Reference System)
    • Especially in R

Drawing Geographical Data in R

  • The tmap package is very useful
  • tm_shape sets working object
  • tm_borders says what to do with it
  • You may need to install it first
    • install.package(tmap)
library(tmap)
tm_shape(counties) + tm_borders() 

Further Parameters

  • lwd \(\rightarrow\) Line width
  • col \(\rightarrow\) Line colour
tm_shape(counties) + 
  tm_borders(col='red',lwd=2)

Multiple layers

  • A further tm_shape gives another overlaid object
  • This time add a POINT object and draw with tm_dots
# Pick out jan 1980
rain_jan80 <- filter(rainfall,
                     Month=='Jan',
                     Year==1980) 

# Now draw the counties and 
# the rain station locations.

tm_shape(counties) + 
  tm_borders() + 
  tm_shape(rain_jan80) + 
  tm_dots(col='darkblue',size=0.5)

Multiple layers - link to variables - 1

Multiple layers - link to variables - 2

Choropleth Maps - tm_polygons

  • legend is continuous - style='cont'
  • tm_polygons draws borders and fills them in
tm_shape(counties) + 
  tm_polygons(col='mean_rain',style='cont')

Choropleth Maps - More parameters (1)

  • Change the palette with palette=...
tm_shape(counties) + 
  tm_polygons(col='mean_rain',
              style='cont',
              palette='Blues')

Choropleth Maps - More parameters (2)

  • Change the format of the scale with style
tm_shape(counties) +
  tm_polygons(col='mean_rain',
              style='jenks',
              palette='Blues')

Choropleth Maps - More parameters…

  • Change the format of the scale with style
tm_shape(counties) + 
  tm_polygons(col='mean_rain',
              style='quantile',
              palette='OrRd')

Explore Palettes

  • enter tmaptools::palette_explorer() - it shows possibilities
  • you may need to install the package tmaptools
  • You can see the palette you need
  • Close the window before going back to R

What map projection is used?

  • counties \(\rightarrow\) Irish National Grid
  • rail \(\rightarrow\) Lat + Long
  • So which projection is used when the map is drawn?
    • The projection is set by the first tm_shape object
    • remainder are transformed to this ‘on the fly’
tm_shape(counties) + 
  tm_polygons(col='mean_rain') +
  tm_shape(rail) + 
  tm_lines(col='darkgreen',lwd=2)

Interactive, zoomable maps

  • enter tmap_mode('view') to start interactive mode
  • commands same as before but maps now zoomable
tmap_mode('view')
tm_shape(counties) + 
  tm_polygons(col='mean_rain',alpha=0.4) 

OK, so what map projection is this?

Web Mercator, Google Web Mercator, Spherical Mercator, WGS 84 Web Mercator or WGS 84/Pseudo-Mercator is a variant of the Mercator projection and is the de facto standard for Web mapping applications. It rose to prominence when Google Maps adopted it in 2005. It is used by virtually all major online map providers, including Google Maps, Mapbox,Bing Maps, OpenStreetMap, Mapquest, Esri, and many others. Its official EPSG identifier is EPSG:3857, although others have been used historically.

See https://en.wikipedia.org/wiki/Web_Mercator_projection

Transforming Map Projections with st_transform

  • new_object <- st_transform(object,<epsg>)
  • useful if you want non-interactive tmap plots
  • also note tmap_mode('plot') puts tmap back in interactive mode
  • Finally, rainfall converted to Web Mercator on the fly
tmap_mode('plot')
counties_wm <- st_transform(counties,3857)
tm_shape(counties_wm) + tm_borders() + 
  tm_shape(rainfall) + tm_dots(size=0.5)

Conclusion

💡 New ideas

  • New general ideas

    • Types of geographical data objects
    • Map shading classes
    • Map projections
  • New techniques

    • Drawing maps
    • Changing map projections
    • Interactive maps with web-based backdrops
  • Practical issues

    • R commands for above
    • The tmap package
    • How tmap chooses projections
  • Next lecture - Analysing Geographical Data

  • This link may be useful for some of the ideas.