wpgp / foot

An R package for processing building footprints
https://wpgp.github.io/foot/
GNU General Public License v3.0
29 stars 2 forks source link

zonalIndex does not handle building footprints specified as character (path to file) #44

Open edarin opened 2 years ago

edarin commented 2 years ago

image

In the documentation it is stated we can use zonalIndex with X specified as character but the zonalIndex function is not ready for it yet. https://github.com/wpgp/foot/blob/0b857796d21d10cc5d10fbd84634fc9721de3485/R/get_zonal_index.R#L169

edarin commented 2 years ago

What would actually be the best process to handle very large building footprints layer, not for computing gridded layers but zonal stats?

wcjochem commented 2 years ago

In your example giving the error what is se, the second argument you are passing?

wcjochem commented 2 years ago

Hi @edarin, on your second question about 'zonal stats' do you want to calculate summary measures of building footprints within polygons?

That can be with calculate_footstats(). However, that wasn't designed to be as efficient as the gridded processing. Here's my example script of how to split up polygons and process and calculate summary metrics in parallel:

library(foot)     
library(sf) 
library(future)
library(furrr)

# load data 
data("kampala")
bldgs <- st_make_valid(kampala$buildings)
zones <- kampala$adminZones

# create local copies
# this is just to demonstrate how to read in partial dataset
sf::st_write(bldgs, file.path(tempdir(), "buildings.shp"), append = F)
sf::st_write(zones, file.path(tempdir(), "zones.shp"), append = F)

# need to save the path
path <- tempdir()
# clean up
rm(kampala, bldgs, zones)

# set up for parallel processing
# see ?plan for details
future::plan(multisession)

# loop through each of the zones (or groups of zones)
# need to know an attribute of the zones to split and control the loop
results <- furrr::future_map(1:34, function(i) {
  # Read in a subset of zones based on attribute query
  z <- sf::st_read(file.path(path, "zones.shp"), 
                   query = paste0("SELECT * FROM zones WHERE Id = ", i), 
                   quiet = T)

  # Read subset the buildings to process
  b <- sf::st_read(file.path(path, "buildings.shp"),
                   wkt_filter = sf::st_as_text(sf::st_as_sfc(sf::st_bbox(z))),
                   quiet = T)

  # calculate metrics
  return(foot::calculate_footstats(b, z, # buildings and zones
                                   what = 'area', # choose one or more metrics
                                   how = 'mean', 
                                   controlZone = list(zoneName = 'Id'), # match column in data
                                   verbose = F))
}, .options = furrr_options(seed = NULL))

results <- do.call(rbind, results)
results
edarin commented 2 years ago

wowowwww many thanks Chris for the wkt_filter = sf::st_as_text(sf::st_as_sfc(sf::st_bbox(z))). I didn't know about that. powerful!

se was a sf object loaded in my session.