|
| 1 | +# Sites of Special Scientific Interest (SSSI) in Trafford |
| 2 | +# Created 2024-08-13 |
| 3 | +# Data updated: 2024-07-23 |
| 4 | +# Data: https://naturalengland-defra.opendata.arcgis.com/datasets/Defra::sites-of-special-scientific-interest-england/about |
| 5 | +# Metadata: https://www.data.gov.uk/dataset/5b632bd7-9838-4ef2-9101-ea9384421b0d/sites-of-special-scientific-interest-england |
| 6 | +# Licence: Open Government Licence v3 (https://www.data.gov.uk/dataset/5b632bd7-9838-4ef2-9101-ea9384421b0d/sites-of-special-scientific-interest-england#licence-info) |
| 7 | + |
| 8 | +# NOTES: |
| 9 | +# These features are obtained from an ArcGIS API service, similar to that of the ONS Geography Portal. |
| 10 | +# The default API call returns features for the whole county, therefore it's best to use some extra parameters, such as defining a rectangle (spatial envelope) around the LA, to reduce the amount of data being returned and speed up the process. |
| 11 | + |
| 12 | + |
| 13 | +# Required packages --------- |
| 14 | +library(tidyverse) ; library(sf) |
| 15 | + |
| 16 | +# ========= |
| 17 | +# VERY IMPORTANT NOTE REGARDING SF PACKAGE AND COORDINATE WINDING ORDER 2023-12-21: |
| 18 | +# The IETF standard for GeoJSON has made certain changes over the original non-IETF specification. The changes can be viewed here: https://datatracker.ietf.org/doc/html/rfc7946#appendix-B |
| 19 | +# One key change is that polygon rings MUST follow the right-hand rule for orientation (counter-clockwise external rings, clockwise internal rings). |
| 20 | +# This change has caused issues with certain libraries which have historically used the left-hand rule, i.e. clockwise for outer rings and counter-clockwise for interior rings. |
| 21 | +# D3-Geo, Vega-Lite and versions of sf below 1.0.0 (default behaviour) use the GEOS library for performing flat-space calculations, known as R^2 (R-squared) which produce polygons wound to the left-hand rule. |
| 22 | +# The sf package from version 1.0.0 onwards now uses the S2 library by default which performs S^2 (S-squared) spherical calculations and returns polygons wound according to the right-hand rule. |
| 23 | +# At the time of writing, if we want our geography files to work in D3 and Vega-Lite, they must use the left-hand rule and so we need sf to use the GEOS library not S2. |
| 24 | +# More information regarding this can be found at: https://r-spatial.github.io/sf/articles/sf7.html#switching-between-s2-and-geos |
| 25 | +# ========= |
| 26 | +sf_vers <- package_version(packageVersion('sf')) # packageVersion returns a character string, package_version converts that into numeric version numbers (major.minor.patch) e.g. 1.0.0 |
| 27 | + |
| 28 | +# Only run the following if we are using sf version 1.0.0 or above |
| 29 | +if (sf_vers$major >= 1) { |
| 30 | + useS2 <- sf_use_s2() # store boolean to indicating if sf is currently using the s2 spherical geometry package for geographical coordinate operations |
| 31 | + sf_use_s2(FALSE) # force sf to use R^2 flat space calculations using GEOS which returns polygons with left-hand windings |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +# API parameters specifying the spatial rectangular area containing Trafford |
| 36 | +api_geommetry_envelope <- "&geometryType=esriGeometryEnvelope&geometry=%7B%22spatialReference%22%3A%7B%22latestWkid%22%3A3857%2C%22wkid%22%3A102100%7D%2C%22xmin%22%3A-278455.35481123265%2C%22ymin%22%3A7047642.057770884%2C%22xmax%22%3A-244823.0623658004%2C%22ymax%22%3A7073592.428873666%2C%22type%22%3A%22esriGeometryEnvelope%22%7D" |
| 37 | + |
| 38 | + |
| 39 | +# Local authority district ------------------------- |
| 40 | +# Source: ONS Open Geography Portal |
| 41 | +# URL: https://geoportal.statistics.gov.uk/datasets/ons::local-authority-districts-may-2023-boundaries-uk-bgc/about |
| 42 | +# Licence: OGL v3.0 |
| 43 | +# NOTE: we need the LA boundary stored as an sf object for use in st_intersection() calculations for other boundaries/features |
| 44 | +la <- st_read("https://services1.arcgis.com/ESMARspQHYMw9BZ9/arcgis/rest/services/Local_Authority_Districts_May_2023_UK_BGC_V2/FeatureServer/0/query?outFields=*&where=UPPER(lad23cd)%20like%20%27%25E08000009%25%27&f=geojson") %>% |
| 45 | + select(area_code = LAD23CD, area_name = LAD23NM) |
| 46 | + |
| 47 | + |
| 48 | +# Get the SSSI information for areas within Trafford ------------------------- |
| 49 | +df_sssi <- st_read(paste0("https://services.arcgis.com/JJzESW51TqeY9uat/arcgis/rest/services/SSSI_England/FeatureServer/0/query?outFields=*&where=1%3D1&f=geojson", api_geommetry_envelope)) %>% |
| 50 | + st_intersection(la) |
| 51 | + |
| 52 | +# Process the dataset, renaming and creating required variables |
| 53 | +df_sssi <- df_sssi %>% |
| 54 | + rename(site_name = SSSI_NAME, |
| 55 | + site_area_hectares = SSSI_AREA, |
| 56 | + site_area_square_metres = Shape__Area, |
| 57 | + natural_england_site_information_system_id = ENSISID, |
| 58 | + british_national_grid_reference = REFERENCE) %>% |
| 59 | + # Calculate and store the coordinates of each locality's centroid as a "lat" and "lon" property |
| 60 | + mutate(lon = map_dbl(geometry, ~st_centroid(.x)[[1]]), |
| 61 | + lat = map_dbl(geometry, ~st_centroid(.x)[[2]]), |
| 62 | + site_area_hectares = as.numeric(str_trim(format(site_area_hectares, nsmall = 2))), |
| 63 | + site_area_square_metres = as.numeric(str_trim(format(site_area_square_metres, nsmall = 2)))) %>% |
| 64 | + select(site_name, |
| 65 | + site_area_hectares, |
| 66 | + site_area_square_metres, |
| 67 | + natural_england_site_information_system_id, |
| 68 | + british_national_grid_reference, |
| 69 | + lon, |
| 70 | + lat) |
| 71 | + |
| 72 | + |
| 73 | +# Create the SSSI file for Trafford ------------------------- |
| 74 | +st_write(df_sssi, "trafford_sites_of_special_scientific_interest.geojson") |
| 75 | + |
| 76 | + |
| 77 | +# Ensure sf_use_s2() is reset to the state it was in before running the code above, i.e. whether to use the S2 library (for S^2 spherical coordinates) or GEOS (for R^2 flat space coordinates). Only if using v1 or above of the sf package |
| 78 | +if (sf_vers$major >= 1) sf_use_s2(useS2) |
0 commit comments