Social Vulnerability Ireland - Copernicus data

Social Vulnerability Ireland - Copernicus data#

Processing of the Copernicus data into census output areas

Environment#

R Libraries#

Any required R libraries are imported into the kernal:

# Load R libraries
if(!require("pacman"))
    install.packages("pacman")

p_load("sf", "terra", "exactextractr")

print("Loaded Packages:")
p_loaded()
Loading required package: pacman
[1] "Loaded Packages:"
  1. 'exactextractr'
  2. 'terra'
  3. 'sf'
  4. 'pacman'
### Output directory
# create the pipeline directory if it does not exist
pipeline_dir <- file.path("../..","2_pipeline","Ireland","1b_Copernicus","2022")
if(!dir.exists(pipeline_dir)){
    dir.create(pipeline_dir, recursive = TRUE)
    print(paste0(pipeline_dir, " created"))
}

# set country variable which is used within the output filename
country <- "Ireland"

Process#

# Copernicus data folder
copernicus_data_path <- "../../0_data/Copernicus/Ireland"

# High resolution layers - IMPERVIOUSNESS, TREE COVER DENSITY
datasets <- c("IMD","TCD")

# census output areas
census_areas <- st_read("../../0_data/boundaries/Ireland/census_small_areas/2022/SMALL_AREA_2022.shp")
Reading layer `SMALL_AREA_2022' from data source 
  `/Cities/0_data/boundaries/Ireland/census_small_areas/2022/SMALL_AREA_2022.shp' 
  using driver `ESRI Shapefile'
Simple feature collection with 18919 features and 28 fields
Geometry type: MULTIPOLYGON
Dimension:     XY
Bounding box:  xmin: 417471.5 ymin: 519663.7 xmax: 734481.1 ymax: 966896.3
Projected CRS: IRENET95 / Irish Transverse Mercator
# need to join copernicus tiles together
for(dataset in datasets){
    print(paste0("Processing: ", dataset))
    flush.console()

    # export mosaic filepath
    mosaic_output_file <- paste0(pipeline_dir, "/", country, "_", dataset, ".tif")

    # have the tiles been joined together already?
    is_mosaic_created <- file.exists(mosaic_output_file)

    if(is_mosaic_created){
        print(paste0(mosaic_output_file, " already created"))
        flush.console()
    } else {
        # Join the tiles together and export it
        # Get the files
        tiles <- list.files(copernicus_data_path, full.names = TRUE, recursive = TRUE, pattern=paste0(".*", dataset, ".*\\.tif$"))  

        # Create an image catalog
        ic <- terra::sprc(lapply(tiles, rast))

        # Mosic the tiles
        icMosaic <- mosaic(ic, filename = mosaic_output_file, fun="max", overwrite=TRUE)
        #icMosaic <- merge(ic, filename = mosaic_output_file, overwrite=TRUE) # quicker

        print(paste0(mosaic_output_file, " created"))
    }   
}
[1] "Processing: IMD"
[1] "../../2_pipeline/Ireland/1b_Copernicus/2022/Ireland_IMD.tif already created"
[1] "Processing: TCD"
[1] "../../2_pipeline/Ireland/1b_Copernicus/2022/Ireland_TCD.tif already created"
Sys.time()
census_areas_impervious <- census_areas

# Impervious surface
copRaster <- rast(paste0(pipeline_dir, "/", country, "_", "IMD", ".tif"))

# calculate the area of the raster cell (m2)
cellArea <- res(copRaster)[1]*res(copRaster)[2]

# calculate the number of cells within each output area
census_areas_impervious$cell_area_m2 <- exact_extract(copRaster, census_areas_impervious, 'count', coverage_area = TRUE, progress = TRUE)

# calculate the area of each output area that is impervious
census_areas_impervious$imp_area_m2 <- exact_extract(copRaster, census_areas_impervious, 'sum', progress = TRUE)

# calculate the percentage of each output area that is impervious
census_areas_impervious$imp_percent <- round((census_areas_impervious$imp_area_m2/census_areas_impervious$cell_area_m2)*100, 3)

# calculate Z score
census_areas_impervious$impervious <- scale(census_areas_impervious$imp_percent)

head(census_areas_impervious)
Sys.time()
[1] "2025-03-13 15:40:39 GMT"
Warning message in .local(x, y, ...):
“Polygons transformed to raster CRS (EPSG:3035)”
Cannot preload entire working area of 1395265641 cells with max_cells_in_memory = 3e+07. Raster values will be read for each feature individually.
 |======================================================================| 100%
Warning message in .local(x, y, ...):
“Polygons transformed to raster CRS (EPSG:3035)”
Cannot preload entire working area of 1395265641 cells with max_cells_in_memory = 3e+07. Raster values will be read for each feature individually.
 |======================================================================| 100%
Registered S3 method overwritten by 'geojsonsf':
  method        from   
  print.geojson geojson
A sf: 6 × 33
OBJECTIDSA_GUID_20SA_GUID__1SA_PUB2011SA_PUB2016SA_PUB2022SA_GEOGID_SA_CHANGE_SA_URBAN_ASA_URBAN_1geometryCOUNTY_ENGCOUNTY_GAECSO_LEASHAPE_LengSHAPE_Areageometrycell_area_m2imp_area_m2imp_percentimpervious
<int><chr><chr><chr><chr><chr><chr><int><int><chr><chr><chr><chr><dbl><dbl><MULTIPOLYGON [m]><dbl><dbl><dbl><dbl[,1]>
114c07d11e-0a4f-851d-e053-ca3ca8c0ca7f4c07d11e-0a4f-851d-e053-ca3ca8c0ca7f017010016017010016017010016A01701001601CarlowMULTIPOLYGON (((673146.5 67...CARLOWCeatharlachCARLOW2123.346204944.2MULTIPOLYGON (((673146.5 67...204991.3 75945.9537.048 0.4175754
224c07d11e-0a3a-851d-e053-ca3ca8c0ca7f4c07d11e-0a3a-851d-e053-ca3ca8c0ca7f017010046017010046017010046A01701004641CarlowMULTIPOLYGON (((673956.3 67...CARLOWCeatharlachCARLOW2891.271288225.4MULTIPOLYGON (((673956.3 67...288290.4 57333.2519.887-0.2955289
334c07d11e-0a4e-851d-e053-ca3ca8c0ca7f4c07d11e-0a4e-851d-e053-ca3ca8c0ca7f017010037017010037017010037A01701003701CarlowMULTIPOLYGON (((673720.6 67...CARLOWCeatharlachCARLOW2436.986208426.9MULTIPOLYGON (((673720.6 67...208474.4 69274.2033.229 0.2588815
444c07d11e-0a25-851d-e053-ca3ca8c0ca7f4c07d11e-0a25-851d-e053-ca3ca8c0ca7f017010005017010005017010005A01701000501CarlowMULTIPOLYGON (((671923.3 67...CARLOWCeatharlachCARLOW2887.091348798.6MULTIPOLYGON (((671923.3 67...348880.3109519.9631.392 0.1825472
554c07d11e-0a57-851d-e053-ca3ca8c0ca7f4c07d11e-0a57-851d-e053-ca3ca8c0ca7f017010036017010036017010036A01701003601CarlowMULTIPOLYGON (((673711.8 67...CARLOWCeatharlachCARLOW2844.410429349.1MULTIPOLYGON (((673711.8 67...429446.9209251.5548.726 0.9028403
664c07d11e-2df2-851d-e053-ca3ca8c0ca7f4c07d11e-2df2-851d-e053-ca3ca8c0ca7f017010009017010009017010009A01701000941CarlowMULTIPOLYGON (((673488.5 67...CARLOWCeatharlachCARLOW1058.290 59299.2MULTIPOLYGON (((673488.5 67... 59312.7 18759.5931.628 0.1923539
[1] "2025-03-13 15:52:17 GMT"
# output the impervious data as a geojson
st_write(census_areas_impervious, file.path(pipeline_dir, "census_areas_IMD.geojson"), delete_dsn = TRUE)
Deleting source `../../2_pipeline/Ireland/1b_Copernicus/2022/census_areas_IMD.geojson' using driver `GeoJSON'
Writing layer `census_areas_IMD' to data source 
  `../../2_pipeline/Ireland/1b_Copernicus/2022/census_areas_IMD.geojson' using driver `GeoJSON'
Writing 18919 features with 32 fields and geometry type Multi Polygon.
Sys.time()
census_areas_tree_cover <- census_areas

# TREE COVER DENSITY
# cell size = 100 m2, therefore a percentage value directly equals m2, i.e. 100% coverage = 100 m2 area of impervious surface
copRaster <- rast(paste0(pipeline_dir, "/", country, "_", "TCD", ".tif"))

# calculate the area of the raster cell (m2)
cellArea <- res(copRaster)[1]*res(copRaster)[2]

# calculate the number of cells within each output area
census_areas_tree_cover$cell_area_m2 <- exact_extract(copRaster, census_areas_tree_cover, 'count', coverage_area = TRUE, progress = TRUE)

# calculate the area of each output area that has tree cover
census_areas_tree_cover$tcd_area_m2 <- exact_extract(copRaster, census_areas_tree_cover, 'sum', progress = TRUE)

# calculate the percentage of each output area that has tree cover
census_areas_tree_cover$tcd_percent <- round((census_areas_tree_cover$tcd_area_m2/census_areas_tree_cover$cell_area_m2)*100, 3)

# calculate Z score
census_areas_tree_cover$tree_cover_density <- -scale(census_areas_tree_cover$tcd_percent)

head(census_areas_tree_cover)
Sys.time()
[1] "2025-03-13 15:53:31 GMT"
Warning message in .local(x, y, ...):
“Polygons transformed to raster CRS (EPSG:3035)”
Cannot preload entire working area of 1395265641 cells with max_cells_in_memory = 3e+07. Raster values will be read for each feature individually.
 |======================================================================| 100%
Warning message in .local(x, y, ...):
“Polygons transformed to raster CRS (EPSG:3035)”
Cannot preload entire working area of 1395265641 cells with max_cells_in_memory = 3e+07. Raster values will be read for each feature individually.
 |======================================================================| 100%
A sf: 6 × 33
OBJECTIDSA_GUID_20SA_GUID__1SA_PUB2011SA_PUB2016SA_PUB2022SA_GEOGID_SA_CHANGE_SA_URBAN_ASA_URBAN_1geometryCOUNTY_ENGCOUNTY_GAECSO_LEASHAPE_LengSHAPE_Areageometrycell_area_m2tcd_area_m2tcd_percenttree_cover_density
<int><chr><chr><chr><chr><chr><chr><int><int><chr><chr><chr><chr><dbl><dbl><MULTIPOLYGON [m]><dbl><dbl><dbl><dbl[,1]>
114c07d11e-0a4f-851d-e053-ca3ca8c0ca7f4c07d11e-0a4f-851d-e053-ca3ca8c0ca7f017010016017010016017010016A01701001601CarlowMULTIPOLYGON (((673146.5 67...CARLOWCeatharlachCARLOW2123.346204944.2MULTIPOLYGON (((673146.5 67...204991.3 202.78300.099 0.7831503
224c07d11e-0a3a-851d-e053-ca3ca8c0ca7f4c07d11e-0a3a-851d-e053-ca3ca8c0ca7f017010046017010046017010046A01701004641CarlowMULTIPOLYGON (((673956.3 67...CARLOWCeatharlachCARLOW2891.271288225.4MULTIPOLYGON (((673956.3 67...288290.4 961.12640.333 0.7459016
334c07d11e-0a4e-851d-e053-ca3ca8c0ca7f4c07d11e-0a4e-851d-e053-ca3ca8c0ca7f017010037017010037017010037A01701003701CarlowMULTIPOLYGON (((673720.6 67...CARLOWCeatharlachCARLOW2436.986208426.9MULTIPOLYGON (((673720.6 67...208474.4 968.99020.465 0.7248895
444c07d11e-0a25-851d-e053-ca3ca8c0ca7f4c07d11e-0a25-851d-e053-ca3ca8c0ca7f017010005017010005017010005A01701000501CarlowMULTIPOLYGON (((671923.3 67...CARLOWCeatharlachCARLOW2887.091348798.6MULTIPOLYGON (((671923.3 67...348880.326616.50397.629-0.4154939
554c07d11e-0a57-851d-e053-ca3ca8c0ca7f4c07d11e-0a57-851d-e053-ca3ca8c0ca7f017010036017010036017010036A01701003601CarlowMULTIPOLYGON (((673711.8 67...CARLOWCeatharlachCARLOW2844.410429349.1MULTIPOLYGON (((673711.8 67...429446.911372.12792.648 0.3773941
664c07d11e-2df2-851d-e053-ca3ca8c0ca7f4c07d11e-2df2-851d-e053-ca3ca8c0ca7f017010009017010009017010009A01701000941CarlowMULTIPOLYGON (((673488.5 67...CARLOWCeatharlachCARLOW1058.290 59299.2MULTIPOLYGON (((673488.5 67... 59312.7 107.75130.182 0.7699382
[1] "2025-03-13 16:07:43 GMT"

Export#

# output the tree cover density data as a geojson
st_write(census_areas_tree_cover, file.path(pipeline_dir, "census_areas_TCD.geojson"), delete_dsn = TRUE)
Deleting source `../../2_pipeline/Ireland/1b_Copernicus/2022/census_areas_TCD.geojson' using driver `GeoJSON'
Writing layer `census_areas_TCD' to data source 
  `../../2_pipeline/Ireland/1b_Copernicus/2022/census_areas_TCD.geojson' using driver `GeoJSON'
Writing 18919 features with 32 fields and geometry type Multi Polygon.

END