Skip to content

Commit

Permalink
population done
Browse files Browse the repository at this point in the history
  • Loading branch information
temospena committed Feb 26, 2024
1 parent 60321fd commit 885cdc4
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 13 deletions.
62 changes: 57 additions & 5 deletions R/functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,45 @@ get_centrality_grid = function(centrality_nodes, grid) {





# get_census --------------------------------------------------------------

get_census = function(CITY) {

CENSUSpoint = st_read("https://github.com/U-Shift/SiteSelection/releases/download/0.1/CENSUSpoint.gpkg")
CENSUScity = CENSUSpoint |> filter(Concelho == toupper(CITY))

# saveRDS(CENSUScity, paste0("outputdata/", CITY, "/CENSUScity.Rds"))
}


# density_grid ------------------------------------------------------------

get_density_grid = function(grid, CENSUScity) {

# CENSUScity = readRDS(paste0("outputdata/", CITY, "/CENSUScity.Rds"))

density_grid =
st_join(CENSUScity |> select(BGRI2021, N_INDIVIDUOS, geom),
grid,
join = st_intersects) %>%
st_drop_geometry() %>%
group_by(ID) %>%
summarise(population = sum(N_INDIVIDUOS))

}



# find_candidates ---------------------------------------------------------

find_candidates = function(grid, centrality_grid, CITY) {
find_candidates = function(grid, centrality_grid, density_grid, CITY) {

candidates_centrality = left_join(st_transform(grid, 3857), centrality_grid)
# centrality
candidates_centrality = grid |>
# st_transform(3857) |>
left_join(centrality_grid)
# Filter in thresholds #for Lisbon. Adjust for other places?
candidates_centrality = candidates_centrality %>%
filter(degree >= mean(centrality_grid$degree), #1088 média
Expand All @@ -285,10 +319,28 @@ find_candidates = function(grid, centrality_grid, CITY) {
betweenness = round(betweenness, digits = 3),
closeness = round(closeness, digits = 3)
)

st_write(candidates_centrality, paste0("outputdata/", CITY, "/candidates_centrality.gpkg"), delete_dsn = TRUE)

# map_candidates = mapview::mapview(candidates_centrality)
st_write(candidates_centrality, paste0("outputdata/", CITY, "/candidates_centrality.gpkg"), delete_dsn = TRUE)


# density
candidates_density = grid |>
# st_transform(3857) |>
left_join(density_grid) |>
filter(population >= mean(density_grid$population)) #above mean

st_write(candidates_density, paste0("outputdata/", CITY, "/candidates_density.gpkg"), delete_dsn = TRUE)


# all candidates
candidates_all = grid |>
left_join(candidates_centrality |> st_drop_geometry()) |>
left_join(candidates_density |> st_drop_geometry()) |>
filter(!is.na(degree)) |>
filter(!is.na(population))

# mapview::mapview(candidates_all)
st_write(candidates_all, paste0("outputdata/", CITY, "/candidates_all.gpkg"), delete_dsn = TRUE)

}

10 changes: 8 additions & 2 deletions _targets.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,14 @@ list(
name = centrality_grid,
command = get_centrality_grid(centrality_nodes, grid)),
tar_target(
name = candidates_centrality,
command = find_candidates(grid, centrality_grid, CITY)
name = CENSUScity,
command = get_census(CITY)),
tar_target(
name = density_grid,
command = get_density_grid(grid, CENSUScity)),
tar_target(
name = candidates_all,
command = find_candidates(grid, centrality_grid, density_grid, CITY)
)
)

11 changes: 8 additions & 3 deletions code/population.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ population_grid =
join = st_intersects) %>%
st_drop_geometry() %>%
group_by(ID) %>%
summarise(population = sum(N_INDIVIDUOS)) |>
mutate(population = scales::rescale(population)) #?
summarise(population = sum(N_INDIVIDUOS))
# mutate(population = scales::rescale(population)) # not needed

# table(population_grid$population > 0.5)
# FALSE TRUE
Expand All @@ -46,6 +46,11 @@ population_grid =
population_grid_geo = GRID |> left_join(population_grid)
mapview::mapview(population_grid_geo, zcol="population") + mapview::mapview(CENSUScity)

# selection - above mean (should be above median?)
## selection - above mean (should be above median?)
# summary(population_grid$population)
# hist(population_grid$population)
# abline(v = mean(population_grid$population), col="red")
# abline(v = median(population_grid$population), col="blue")

density_grid = population_grid_geo |> filter(population > mean(population_grid$population)) #above mean
mapview::mapview(density_grid, zcol="population")
5 changes: 2 additions & 3 deletions run.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ targets::tar_make()
# targets::tar_make_clustermq(workers = 2) # nolint
# targets::tar_make_future(workers = 2) # nolint


targets::tar_load(candidates_centrality)
mapview::mapview(candidates_centrality)
targets::tar_load(candidates_all)
mapview::mapview(candidates_all, zcol="population")

0 comments on commit 885cdc4

Please sign in to comment.