Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LA programming #170

Merged
merged 4 commits into from
Apr 27, 2021
Merged

LA programming #170

merged 4 commits into from
Apr 27, 2021

Conversation

natesheehan
Copy link
Contributor

@natesheehan natesheehan commented Apr 26, 2021

I have updated all sites (except great-kneighton & trumpington meadows, something funky going on there, continuing to investigate) to programmatically add main_local_authority to all-sites.geojson. This was achieved through the following reproducible example:

# Aim: Programtically add LA's to site data
# Inputs: ActDev site data & ONS local authority boundaries
# Method: Loop through site and ONS sf_intersections
# Libraries:
library(sf)
#> Linking to GEOS 3.9.0, GDAL 3.2.1, PROJ 7.2.1

if (!file.exists("la_boundaries.geojson")) {
  # Download latest ONS "Local Authority Districts in the United Kingdom" data
  download.file(url = 'https://opendata.arcgis.com/datasets/b7fc294e5c8643f5b506acc2122c6880_0.geojson',
                destfile = 'la_boundaries.geojson',
                method = 'wininet')
  # Read input data
  la_ew = sf::read_sf("la_boundaries.geojson") %>% sf::as_Spatial() %>% sf::st_as_sf()
  sites = sf::read_sf("data-small/all-sites.geojson")
  # Transform Data (and drop great-kneighton & Trumpington Meadows something funky going on there)
  sites_geo = sites[1] %>% sf::st_as_sf()
  sites_geo = sites_geo[-c(18, 36), ]
  # Loop through sites and calculate main_local_authority
  for (i in 1:nrow(sites_geo)) {
    row <- sites_geo[i, ]
    row_name = row$site_name
    la_site = sf::st_intersection(la_ew, row)
    sites$main_local_authority[sites$site_name == row_name] = la_site$LAD19NM
  }
  # Write out sf
  file.remove("data-small/all-sites.geojson")
  sf::write_sf(sites, "data-small/all-sites.geojson")
  # Remove geojson file
  file.remove("la_boundaries.geojson")
}

I have slightly refactored the add_site.r and build.r file in order to automate the LA column.

Geo-process main_local_authority for existing sites #165
- Removed the la.r file and moved code to add_site.r
- add_site.r script being called from build.r
- arrange sites by name
-uncomment mapview
Copy link
Contributor

@Robinlovelace Robinlovelace left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good. Minor comment about adding the la geojson file made.

code/add_site.R Outdated
# Create intersection between site and ONS data
la_site = sf::st_intersection(la_ew, site)
site$main_local_authority = la_site$LAD19NM
file.remove("la_boundaries.geojson")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to remove it: I suggest adding that file to the project, e.g. to the geojsons folder. That also increases resilience: the arcgis endpoint will likely change at some point in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Suggested changes are in the latest push

@@ -29,6 +29,7 @@ if (new_site) {
# [7] "geometry"
site = sf::read_sf("map.geojson")
sf::st_crs(site) = 4326
site$main_local_authority = add_la(site)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 nice and simple.

@@ -38,7 +39,7 @@ if (new_site) {
new_cols[] = NA
sites = rbind(sites,
sf::st_sf(cbind(sf::st_drop_geometry(site), new_cols),
geometry = site$geometry))
geometry = site$geometry)) %>% arrange(site_name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Save la districts to geojson folder
Copy link
Contributor

@Robinlovelace Robinlovelace left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine, pending the question about

la_ew = sf::read_sf("geojsons/la_boundaries.geojson")

This is good to go!

code/add_site.R Outdated
method = 'wininet')
# Read input data
la_ew = sf::read_sf("la_boundaries.geojson") %>% sf::as_Spatial() %>% sf::st_as_sf()
la_ew = sf::read_sf("geojsons/la_boundaries.geojson") %>% sf::as_Spatial() %>% sf::st_as_sf()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's wrong with just:

la_ew = sf::read_sf("geojsons/la_boundaries.geojson")

?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have pushed this change with an edit to the add site tutorial too 🚀

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Going to merge and delete

@natesheehan natesheehan merged commit 5d71720 into main Apr 27, 2021
@natesheehan natesheehan deleted the la-programming branch April 27, 2021 11:31
@mvl22 mvl22 changed the title La programming LA programming Apr 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants