library(sf)
# increment in metres.
# Smaller numbers will give you a more accurate map, but will take longer to calculate.
increment <- 10*1000
# load shapefile and convert to an equal-area projection so we can work in metres
gerShp <- st_read('gadm36_DEU_shp/gadm36_DEU_0.shp') # change this to the correct path
gerShp <- st_transform(gerShp, 3035)
# calculate total area and our 30% value
totalArea <- st_area(gerShp)
thirtyPC <- totalArea * 0.3
# Plot it
plot(gerShp, col = 'lightgrey', border = 'darkgrey', max.plot=1, reset=F)
# Find the bounding box of the feature
bbox <- st_bbox(gerShp)
thisArea <- totalArea - totalArea # zero with correct units
i <- 1
# While our subarea is less than 30%...
while (thisArea < thirtyPC) {
# Starting at bottom, create a bounding box that is smaller than full bounding box
thisBBox <- bbox
thisBBox['ymax'] <- thisBBox$ymin + (increment * i)
# Clip shp to this bounding box
thisSubarea <- st_crop(gerShp, y=thisBBox)
thisArea <- st_area(thisSubarea)
print(thisArea)
i <- i + 1
}
plot(thisSubarea, max.plot=1, add=T, col='red', border=NA)
actualPercentage <- thisArea / totalArea
2条答案
按热度按时间q8l4jmvw1#
这可能是一种非常低效的方法,但这是一个开始:
在本例中,actualPercentage = 0.3011579,非常接近我们的目标值30%。
4ngedf3f2#