library(sf)
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 7.0.0
# Sample data
url = "https://geobgu.xyz/data/examples2/nafot.gpkg"
pol = st_read(url)
## Reading layer `nafot' from data source `https://geobgu.xyz/data/examples2/nafot.gpkg' using driver `GPKG'
## Simple feature collection with 15 features and 4 fields
## geometry type: POLYGON
## dimension: XY
## bbox: xmin: 620662.1 ymin: 3263494 xmax: 770624.4 ymax: 3691834
## projected CRS: WGS 84 / UTM zone 36N
pol = pol[pol$name_eng == "Jerusalem", ]
pol = st_geometry(pol)
# Optimization function
f = function(d, prop) {
# Target area
area0 = st_area(pol) * prop
area0 = as.numeric(area0)
# Current area
pol1 = st_buffer(pol, d)
area1 = st_area(pol1)
area1 = as.numeric(area1)
# Difference
return(abs(area0 - area1))
}
# Example
d = optimize(f, prop = 0.5, interval = c(-100000, 0))
d
## $minimum
## [1] -2625.665
##
## $objective
## [1] 3.099185
st_area(pol) * 0.5
## 326873415 [m^2]
st_area(st_buffer(pol, d$minimum))
## 326873412 [m^2]
# Plot
plot(pol)
plot(st_buffer(pol, d$minimum), border = "red", add = TRUE)
