Find the area, grid cells, or other zone that a building polygon overlaps or is located in. Implements an efficient spatial join by intersection.

zonalIndex(X, zone, zoneField = NULL, method = "centroid", returnObject = TRUE)

# S3 method for sf
zonalIndex(X, zone, zoneField = NULL, method = "centroid", returnObject = TRUE)

# S3 method for sfc
zonalIndex(X, zone, zoneField = NULL, method = "centroid", returnObject = TRUE)

# S3 method for sp
zonalIndex(X, zone, zoneField = NULL, method = "centroid", returnObject = TRUE)

# S3 method for stars
zonalIndex(X, zone, zoneField = NULL, method = "centroid", returnObject = TRUE)

# S3 method for Raster
zonalIndex(X, zone, zoneField = NULL, method = "centroid", returnObject = TRUE)

# S3 method for character
zonalIndex(X, zone, zoneField = NULL, method = "centroid", returnObject = TRUE)

Arguments

X

Spatial data (or path to file) with building footprint polygons

zone

Spatial data (or path to file) with polygon zones or a spatial grid (i.e. "raster")

zoneField

(Optional) Column name of unique identifiers in zone to use. If omitted, the 'zoneID' will be numbered 1:nrow(zone).

method

One of 'centroid', 'intersect', 'clip' to determine how footprints are allocated to zones. See details. Default is "centroid".

returnObject

Logical of whether to return an sf object of X with zonal information. Default is TRUE which is generally preferred.

Value

'sf' object with attributes of X plus the unique zone ID or a data.table with the row number to the record in X matched to the zone IDs.

Details

Zone assignments for building footprints can be done using three possible methods, set by the method= parameter. Defining by 'centroid' allocates and entire building and its characteristics to the zone(s) which its centroid intersects. Centroids are defined by st_centroid which could be outside the polygon shape. This is the default mode. Defining zones by 'intersect' uses the geometric binary predicate from st_intersects. This method will include a whole building and its characteristics into all zones that it intersects. Therefore a building could appear to be "counted" twice. The final approach, 'clip', uses st_intersection to split footprints so that only that area of the polygon intersecting the zone is include. This method is more time consuming because the geometries are modified.

When zone is a multi-layer stars or Raster*, zoneField can be used to select a specific layer to define the areas, or NULL to use each pixel as a unique zone.

Examples

data("kampala", package="foot") buildings <- kampala$buildings clusters <- kampala$clusters # assign zones and return a new 'sf' object zonalIndex(buildings, clusters)
#> Simple feature collection with 348 features and 2 fields #> Geometry type: POLYGON #> Dimension: XY #> Bounding box: xmin: 32.60631 ymin: 0.328119 xmax: 32.63543 ymax: 0.349785 #> Geodetic CRS: WGS 84 #> First 10 features: #> FID_1 zoneID geometry #> 1 6293 1 POLYGON ((32.60739 0.338599... #> 2 17706 1 POLYGON ((32.60662 0.339353... #> 3 29106 1 POLYGON ((32.60735 0.339238... #> 4 53393 1 POLYGON ((32.60778 0.339552... #> 5 57187 1 POLYGON ((32.60827 0.338821... #> 6 59496 1 POLYGON ((32.60698 0.339543... #> 7 68588 1 POLYGON ((32.60787 0.338906... #> 8 68590 1 POLYGON ((32.60774 0.339637... #> 9 72387 1 POLYGON ((32.60769 0.338973... #> 10 78504 1 POLYGON ((32.60703 0.338654...
# assign all intersecting zones zonalIndex(buildings, clusters, method="intersect")
#> Simple feature collection with 350 features and 2 fields #> Geometry type: POLYGON #> Dimension: XY #> Bounding box: xmin: 32.60631 ymin: 0.328119 xmax: 32.63543 ymax: 0.349785 #> Geodetic CRS: WGS 84 #> First 10 features: #> FID_1 zoneID geometry #> 1 6293 1 POLYGON ((32.60739 0.338599... #> 2 17706 1 POLYGON ((32.60662 0.339353... #> 3 29106 1 POLYGON ((32.60735 0.339238... #> 4 53393 1 POLYGON ((32.60778 0.339552... #> 5 57187 1 POLYGON ((32.60827 0.338821... #> 6 59496 1 POLYGON ((32.60698 0.339543... #> 7 68588 1 POLYGON ((32.60787 0.338906... #> 8 68590 1 POLYGON ((32.60774 0.339637... #> 9 72387 1 POLYGON ((32.60769 0.338973... #> 10 78504 1 POLYGON ((32.60703 0.338654...
# return only a table of indices - note column names zonalIndex(buildings, clusters, zoneField="Id", returnObject=FALSE)
#> xID Id #> 1: 97 1 #> 2: 242 1 #> 3: 390 1 #> 4: 712 1 #> 5: 762 1 #> --- #> 344: 3744 11 #> 345: 4127 11 #> 346: 4702 11 #> 347: 4788 11 #> 348: 4834 11