get intersection area between two polygons when using `terra::intersect`

Viewed 15

I have a water basin shapefile

library(terra)
f <- system.file("ex/lux.shp", package="terra")
basins <- vect(f)

I also have a single municipality

muni <- vect("POLYGON ((6.3 49.9, 6.2 49.7, 6.3 49.6, 6.5 49.8, 6.3 49.9))", crs=crs(basins))

I want to extract which basins intersect with the municipality and take average of basin values but using intersected area of each basin with the municipality as a weight. When I do terra::intersect, I only get which basins intersect with municipality but no intersection area

y <- terra::intersect(basins, muni)
y

class       : SpatVector 
geometry    : polygons 
dimensions  : 6, 6  (geometries, attributes)
extent      : 6.2, 6.5, 49.6, 49.87085  (xmin, xmax, ymin, ymax)
coord. ref. : lon/lat WGS 84 (EPSG:4326) 
names       :  ID_1       NAME_1  ID_2     NAME_2  AREA   POP
type        : <num>        <chr> <num>      <chr> <num> <int>
values      :     1     Diekirch     2   Diekirch   218 32543
                  2 Grevenmacher     6 Echternach   188 18899
                  2 Grevenmacher     7     Remich   129 22366

It gives me all the basins but how can I get for each basin, the intersected area that I can use as weights to do the average.

1 Answers

You can use expanse and do something like

y$area <- expanse(y)
y$area
#[1]   8147181 162708218   2924191 120387559  20226870  12054660
Related