Created
February 18, 2024 16:54
-
-
Save JosiahParry/f5b974a5000b8623b0957feecfb17a74 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(sf) | |
library(sfheaders) | |
# define polygon coords | |
m <- matrix( | |
c(2, -2, 2, 2, -2, 2, -2, -2, 2, -2), | |
ncol = 2, byrow = TRUE | |
) | |
# create polygon from matrix | |
p1 <- sfc_polygon(m) | |
# overlapping area of 4 | |
m2 <- m + 2 | |
p2 <- sfc_polygon(m2) | |
v2 <- 3 # intensive value for polygon | |
# overlapping area of 1 | |
m3 <- matrix( | |
c( | |
-1, -3, | |
-1, -1, | |
-3, -1, | |
-3, -3, | |
-1, -3 | |
), ncol = 2, byrow = TRUE | |
) | |
p3 <- sfc_polygon(m3) | |
v3 <- 12 # intensive value for polygon | |
# view them all together | |
plot(c(p1, p2, p3)) | |
# join candidates | |
cands <- st_sf(v = c(v2, v3), geometry = c(p2, p3)) | |
# target | |
tar <- st_sf(geometry = p1) | |
# intensive interpolation returns 4.8 | |
st_interpolate_aw(cands, tar, FALSE) | |
# create intersection of overlaps: | |
overlap <- st_intersection(cands, tar) | |
# weighted mean where the value is weighted by overlapping area | |
# divided by the area of the target | |
weighted.mean( | |
cands$v, | |
st_area(overlap) / st_area(tar) | |
) | |
# to replicate the extensive value | |
w <- st_area(overlap) / st_area(tar) | |
sum(cands$v * w) | |
st_interpolate_aw(cands, tar, TRUE) | |
w <- st_area(overlap) / st_area(cands) | |
sum( cands$v) | |
weighted.mean( | |
cands$v, | |
st_area(overlap) / st_area(cands) | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment