|
1 | 1 | ## Risk of Flooding from Rivers and Sea ## |
2 | 2 |
|
3 | 3 | # Source: Environment Agency |
4 | | -# Publisher URL: https://www.data.gov.uk/dataset/bad20199-6d39-4aad-8564-26a46778fd94/risk-of-flooding-from-rivers-and-sea |
| 4 | +# Publisher URL: https://environment.data.gov.uk/dataset/96ab4342-82c1-4095-87f1-0082e8d84ef1 |
| 5 | +# NOTE: previous dataset now retired: https://www.data.gov.uk/dataset/bad20199-6d39-4aad-8564-26a46778fd94/risk-of-flooding-from-rivers-and-sea |
5 | 6 | # Licence: Open Government Licence 3.0 |
6 | | -# Attribution: (C) Environment Agency Copyright and/or Database Rights 2023. All rights reserved. |
7 | | -# Last Updated: 2024-02-09 |
8 | | -# NOTE 1: "This year we are pausing the updates to this dataset after December 2023. This is in advance of publishing the first outputs from our new National Flood Risk Assessment. These outputs will be published by the end of 2024 and will include a new version of this dataset." |
9 | | -# NOTE 2: The initial dataset is downloaded from https://environment.data.gov.uk/explore/8d57464f-d465-11e4-8790-f0def148f590 by creating a bounding polygon around Trafford. This file is large ~14MB. |
| 7 | +# Attribution: (C) Environment Agency copyright and/or database right 2025. All rights reserved. |
| 8 | +# Last Updated: 2025-01-28 |
| 9 | +# Update frequency: Quarterly |
| 10 | +# The dataset is downloaded from https://environment.data.gov.uk/explore/96ab4342-82c1-4095-87f1-0082e8d84ef1?download=true by uploading a bounding polygon around Trafford. This file is large ~40MB. |
| 11 | + |
10 | 12 |
|
11 | 13 | # Load libraries --------------------------- |
12 | 14 | library(tidyverse) ; library(sf) |
13 | 15 |
|
14 | 16 |
|
15 | | -# Set sf geometry calculation method --------------------------- |
| 17 | +# Set sf geometry calculation method if required --------------------------- |
16 | 18 | # Refer to the following for further information: https://github.com/r-spatial/sf/issues/1771 |
17 | | -is_sf_using_s2 <- sf_use_s2() # Store whether sf is using S2 or R2 geometry calculations |
18 | | -sf_use_s2(FALSE) # Set sf to use R2 calculations as we get errors using S2 |
| 19 | +# Uncomment and run the following 2 lines if you encounter issues working with the spatial files |
| 20 | +#is_sf_using_s2 <- sf_use_s2() # Store whether sf is using S2 or R2 geometry calculations |
| 21 | +#sf_use_s2(FALSE) # Set sf to use R2 calculations as we get errors using S2 |
| 22 | + |
| 23 | + |
| 24 | +# Convert the boundary of Trafford and its immediate environs (created in Plotter) from GeoJSON into a ShapeFile --------------------------- |
| 25 | +read_sf("trafford_and_environs_boundary.geojson") %>% |
| 26 | + # This creates 4 files with the extensions .dbf, .prj, .shp and .shx |
| 27 | + write_sf("trafford_and_environs_boundary.shp", driver = "ESRI Shapefile") |
| 28 | + |
| 29 | +# The above process creates 4 files which we need to ZIP up, create a list with their names so that we can reuse it in the following 2 lines below |
| 30 | +shp_files <- c("trafford_and_environs_boundary.dbf", "trafford_and_environs_boundary.prj", "trafford_and_environs_boundary.shp", "trafford_and_environs_boundary.shx") |
19 | 31 |
|
| 32 | +# Add them to a ZIP ready to upload |
| 33 | +zip(zipfile = "trafford_and_environs_boundary", files = shp_files) |
20 | 34 |
|
21 | | -# Load raw data and Trafford's boundary --------------------------- |
22 | | -flood_risk_raw <- st_read("Risk_of_Flooding_from_Rivers_and_Sea.json") %>% # Downloaded file mentioned in NOTE 2 |
23 | | - st_make_valid() # The geometry returned in the data downloaded above needs correcting as there are overlapping vertices/self-intersections etc. |
| 35 | +# Remove the 4 individual files as they are no longer needed |
| 36 | +file.remove(shp_files) |
24 | 37 |
|
25 | | -# Load boundary of Trafford --------------------------- |
26 | | -trafford <- st_read("https://www.trafforddatalab.io/spatial_data/local_authority/2021/trafford_local_authority_full_resolution.geojson") %>% |
27 | | - st_set_crs(4326) %>% |
28 | | - select(-lat, -lon) |
29 | 38 |
|
30 | | -# Intersect data, leaving just flood risk data within Trafford's boundary and tidy up variables --------------------------- |
31 | | -trafford_flood_risk <- st_intersection(flood_risk_raw, trafford) %>% |
32 | | - rename(flood_risk = prob_4band, |
33 | | - suitable_scale = suitability) %>% |
34 | | - mutate(publication_date = as.character(as.POSIXct(pub_date, tz="UTC"))) %>% |
35 | | - filter(st_geometry_type(geometry) != "POINT") %>% # Remove any point data as this will show up as a marker pin on the map which will be confusing. There seem to be 6 within the dataset. |
36 | | - select(flood_risk, suitable_scale, publication_date, RoFRS_id = id) |
| 39 | +# Manually download the flood risk data from the Environment Agency --------------------------- |
| 40 | +# 1. Visit https://environment.data.gov.uk/explore/96ab4342-82c1-4095-87f1-0082e8d84ef1?download=true |
| 41 | +# 2. Change the "Area of interest" drop-down to "Upload sharefile" |
| 42 | +# 3. Use the "Browse..." button to select the "trafford_and_environs_boundary.zip" file created above |
| 43 | +# 4. Change the "Layers" drop-down to "rofs_4band" (NOTE: data is also available for risk of flooding to a depth of 0.2m, 0.3m, 0.6m, 0.9m and 1.2m) |
| 44 | +# 5. Change the "File format" drow-down to "GeoJSON" |
| 45 | +# 6. Select the "Download file" button |
| 46 | +# 7. Copy the downloaded file "rofs_4band.json" from your "Downloads" folder to the "flood_risk" folder containing this script |
| 47 | + |
| 48 | + |
| 49 | +# Load raw flood risk data and tidy up the variables --------------------------- |
| 50 | +trafford_flood_risk <- read_sf("rofrs_4band.json") %>% |
| 51 | + rename(flood_risk = risk_band) %>% |
| 52 | + filter(st_geometry_type(geometry) != "POINT") %>% # Just in case there is any point data, remove it as this will show up as a marker pin on the map which will be confusing. The previous version of this dataset had 6 within Trafford. |
| 53 | + select(object_id = objectid, flood_risk, geometry) |
37 | 54 |
|
38 | 55 |
|
39 | 56 | # write data --------------------------- |
40 | | -st_write(trafford_flood_risk, "trafford_flood_risk.geojson", driver = "GeoJSON") |
| 57 | +file.remove("trafford_flood_risk.geojson") # first remove the previous file |
| 58 | +write_sf(trafford_flood_risk, "trafford_flood_risk.geojson", driver = "GeoJSON") |
| 59 | + |
| 60 | + |
| 61 | +# Clean up the filesystem removing the downloaded flood risk JSON file and the shapefile ZIP --------------------------- |
| 62 | +file.remove(c("rofrs_4band.json", "trafford_and_environs_boundary.zip")) |
41 | 63 |
|
42 | | -sf_use_s2(is_sf_using_s2) # Reset whether sf is using S2 or R2 geometry calculations |
| 64 | +# reset R's spherical geometry if required --------------------------- |
| 65 | +#sf_use_s2(is_sf_using_s2) # Reset whether sf is using S2 or R2 geometry calculations |
0 commit comments