Skip to content

Instantly share code, notes, and snippets.

@szechno
Last active November 15, 2025 21:54
Show Gist options
  • Select an option

  • Save szechno/6ecbb3af6ca6746021a378246a9cb79a to your computer and use it in GitHub Desktop.

Select an option

Save szechno/6ecbb3af6ca6746021a378246a9cb79a to your computer and use it in GitHub Desktop.
library(sf)
library(readr)
library(dplyr)
library(stringr)
library(units)
library(mapview)
d <- read_csv("powerstation_locations.csv",
col_types = cols(
`Site Name` = col_character(),
Region = col_character(),
`Commissioned date` = col_character(),
`Closure date or planned closure date` = col_character(),
`Nameplate Capacity (MW)` = col_character(),
`Primary Fuel` = col_character(),
X = col_character(),
Y = col_character()
)) |>
rename(site = `Site Name`,
region = Region,
start = `Commissioned date`,
end = `Closure date or planned closure date`,
capacity = `Nameplate Capacity (MW)`,
fuel = `Primary Fuel`) |>
# don't want unknown capacity or coords
filter(X != "[x]") |>
filter(capacity != "[x]") |>
# replace [x] with NA
mutate(across(site:fuel, \(x) ifelse(x == "[x]", NA, x))) |>
# set years
mutate(across(start:end, \(x) x |> as.integer() |> set_units("year"))) |>
# remove commas in 1,000s
mutate(capacity = str_replace_all(capacity, ",", "") |>
as.integer()) |>
st_as_sf(coords = c("X", "Y"), crs = 27700)
# check geometries correct
# # Beinneun Wind Farm
# mapview(d)
# bob <- d |> filter(site == "Beinneun Wind Farm")
# st_coordinates(bob)
# # reverse coordinates and confirm... nope, still wrong
# st_point(x = c(828648, 1851376)) |> st_sfc(crs = 27700) |> mapview()
# actual coordinates from google
bob_geom <- st_point(x = c(-4.9655, 57.0956)) |>
st_sfc(crs = 4326) |> st_transform(crs = 27700)
# POINT (220440.8 804418.4)
st_geometry(d[d$site == "Beinneun Wind Farm", ]) <- bob_geom
# NOTE: Gunfleet Sands 02 also seems wrong, quick fix, put it with 01...
st_geometry(d[d$site == "Gunfleet Sands 02", ]) <- st_geometry(d[d$site == "Gunfleet Sands 01", ])
write_rds(d, "electricity1920_2020.rds")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment