Skip to content

Instantly share code, notes, and snippets.

@francisbarton
Created November 6, 2023 00:52
Show Gist options
  • Save francisbarton/40c333b6f494bd23d0f82ed2e58dd8b6 to your computer and use it in GitHub Desktop.
Save francisbarton/40c333b6f494bd23d0f82ed2e58dd8b6 to your computer and use it in GitHub Desktop.
Find longitude and latitude for GP practices in England
# https://digital.nhs.uk/data-and-information/data-collections-and-data-sets/data-collections/gp-data-available-through-sdcs
zip_url <- "https://digital.nhs.uk/binaries/content/assets/website-assets/data-and-information/data-collections/general-practice-data-collections/catchment-area-8.zip"
dl <- tempfile("catchment_files", tempdir(), ".zip")
download.file(zip_url, dl, mode = "wb")
out_dir <- paste0(tempdir(), "\\catchment_files")
if (!dir.exists(out_dir)) dir.create(out_dir)
utils::unzip(dl, junkpaths = TRUE, exdir = out_dir)
gp_data <- dir(out_dir, full.names = TRUE) |>
purrr::map(readr::read_file) |>
purrr::map(jsonlite::fromJSON) |>
purrr::map_df("features") |>
dplyr::select("properties") |>
tidyr::unnest_wider("properties") |>
janitor::clean_names() |>
dplyr::select(all_of(c(
"name",
"ods_code",
"region",
"icb_code",
"sub_icb_code",
"ccg_code",
longitude = "org_longitude",
latitude = "org_latitude"
))) |>
dplyr::mutate(across(all_of(c("longitude", "latitude")), as.numeric))
gp_data_points <- gp_data |>
dplyr::select(all_of(c(
x = "longitude",
y = "latitude"
))) |>
purrr::pmap(\(x, y) sf::st_point(c(x, y)))
gp_data |>
sf::st_sf(geometry = gp_data_points, crs = 4326)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment