Skip to content

Commit

Permalink
optimize tar duration
Browse files Browse the repository at this point in the history
now it only takes about 4sec to run a city, if it was ever previously run (if folder & network files exist)
  • Loading branch information
temospena committed Feb 16, 2024
1 parent ba8e1ce commit b7293c1
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 9 deletions.
69 changes: 64 additions & 5 deletions R/functions.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,25 @@
# functions all in one


# select_city -------------------------------------------------------------

select_city = function(CITY){
CITY = CITY
}



# get_citylimit -----------------------------------------------------------

get_citylimit = function(CITY) {

if(file.exists(paste0("outputdata/", CITY, "/CITYlimit.geojson"))){

CITYlimit = st_read(paste0("outputdata/", CITY, "/CITYlimit.geojson"), quiet = TRUE)

} else {


MUNICIPIOSgeo = st_read("https://github.com/U-Shift/SiteSelection/releases/download/0.1/CAOP_municipios.gpkg", quiet = TRUE) # Portugal
CITYlimit = MUNICIPIOSgeo %>%
filter(Concelho == CITY) %>%
Expand All @@ -20,8 +34,14 @@ get_citylimit = function(CITY) {
}

st_write(CITYlimit, paste0(output_dir, "/CITYlimit.geojson"), delete_dsn = TRUE)

}
}



# make_grid ---------------------------------------------------------------

make_grid = function(CITYlimit, cellsize_input, square_input) {

CITYlimit_meters = st_transform(CITYlimit, 3857) #projected
Expand All @@ -43,8 +63,17 @@ make_grid = function(CITYlimit, cellsize_input, square_input) {
}



# get_osm -----------------------------------------------------------------

get_osm = function(CITYlimit, CITY) {

if(file.exists(paste0("outputdata/", CITY, "/road_network.gpkg"))){

road_network = st_read(paste0("outputdata/", CITY, "/road_network.gpkg"), quiet = TRUE)

} else {

CITYlimit = st_read(paste0("outputdata/", CITY, "/CITYlimit.geojson"), quiet = TRUE)
BBOX = st_as_sfc(st_bbox(CITYlimit))

Expand Down Expand Up @@ -80,11 +109,21 @@ get_osm = function(CITYlimit, CITY) {

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

}
}



# clean_osm ---------------------------------------------------------------

clean_osm = function(road_network, CITY) {

if(file.exists(paste0("outputdata/", CITY, "/road_network_clean.shp"))){

road_network_clean = sf::st_read(paste0("outputdata/", CITY, "/road_network_clean.shp"), quiet = TRUE)

} else {

options(qgisprocess.path="/usr/bin/qgis_process.bin")
qgis_configure()

Expand Down Expand Up @@ -134,18 +173,30 @@ clean_osm = function(road_network, CITY) {
# remotes::install_github("saferactive/trafficalmr")
# road_network_clean_consolidate = road_network_clean %>% st_transform(3857) %>% trafficalmr::osm_consolidate(200)
# osm_tags missing here, not working!


}
}



# get_centrality ----------------------------------------------------------

get_centrality = function(road_network_clean, CITY) {
# road_network_clean = st_transform(road_network_clean, 3857)

# qgis_show_help("grass7:v.net.centrality")

if (file.exists(paste0("outputdata/", CITY, "/centrality_nodes.gpkg"))){
file.remove(paste0("outputdata/", CITY, "/centrality_nodes.gpkg"))
}
if(file.exists(paste0("outputdata/", CITY, "/centrality_nodes.gpkg"))){

centrality_nodes = sf::st_read(paste0("outputdata/", CITY, "/centrality_nodes.gpkg"), quiet = TRUE)

} else {

# if (file.exists(paste0("outputdata/", CITY, "/centrality_nodes.gpkg"))){
# file.remove(paste0("outputdata/", CITY, "/centrality_nodes.gpkg"))
# }


# qgis_show_help("grass7:v.net.centrality")

# input = st_read(paste0("outputdata/", CITY, "/road_network_clean.shp"))
input = road_network_clean
Expand Down Expand Up @@ -176,8 +227,13 @@ get_centrality = function(road_network_clean, CITY) {

st_write(centrality_nodes, output_path, delete_dsn = TRUE)

}
}



# get_centrality_grid -----------------------------------------------------

get_centrality_grid = function(centrality_nodes, grid) {

centrality_grid =
Expand All @@ -200,6 +256,9 @@ get_centrality_grid = function(centrality_nodes, grid) {
}



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

find_candidates = function(grid, centrality_grid, CITY) {

candidates_centrality = left_join(st_transform(grid, 3857), centrality_grid)
Expand Down
2 changes: 1 addition & 1 deletion _targets.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

# Set defaults HERE ######################
CITY_input = "Almada"
cellsize_input = c(200, 200)
cellsize_input = c(300, 300)
square_input = TRUE #TRUE = squares, FALSE = hexagons


Expand Down
6 changes: 3 additions & 3 deletions run.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
# See https://books.ropensci.org/targets/hpc.html
# to learn about your options.

# targets::tar_visnetwork()
targets::tar_visnetwork()

targets::tar_make()
# targets::tar_make_clustermq(workers = 2) # nolint
# targets::tar_make_future(workers = 2) # nolint


# tar_load(candidates_centrality)
# mapview::mapview(candidates_centrality)
targets::tar_load(candidates_centrality)
mapview::mapview(candidates_centrality)

0 comments on commit b7293c1

Please sign in to comment.