wpgp/foot

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

Opened this issue · 4 comments

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.

zonalIndex.character <- function(X, zone,

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

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

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

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.