How to fit a rectangular polygon into an irregular polygon? Or better; how to get the maximum-rectangular-polygon fitting into an irregular polygon?
Background : I have got a bunch of overlapping raster files which represent different datatakes of a spaceborne sensorsystem. For further analysis, I want to get the maximum-rectangular-polygon (all 4 corners with 90degree angle) which fits into the footprints/outline of all these datasets, to mask and crop my datasets. In other words; How to fit a rectangular polygon of maximum size into the green outline (see image below)?
Idea : First get the outline or footprint for every dataset, then dissolve all the footprints to get the outline and finally fit a rectangular polygon of maximum extent into that irregular polygon
Does the following do what you need?
library(raster)
library(rgeos)
library(mapview)
raster1 <- raster(xmn=-2,xmx=0.1,ymn=50.3,ymx=51.5,vals=1)
raster2 <- raster(xmn=-2.1,xmx=0.5,ymn=50.4,ymx=51.4,vals=1)
raster3 <- raster(xmn=-2.2,xmx=-0.3,ymn=50.2,ymx=51.6,vals=1)
r1 <- extent(raster1)
r1p <- as(r1, 'SpatialPolygons')
r2 <- extent(raster2)
r2p <- as(r2, 'SpatialPolygons')
r3 <- extent(raster3)
r3p <- as(r3, 'SpatialPolygons')
r1p$data <- 1
r2p$data <- 2
r3p$data <- 3
rr <- bind(r1p, r2p, r3p)
rrsp <- as(extent(rr), 'SpatialPolygons')
rrsp$data <- 1
crs(rrsp) <- crs(r1p) <- crs(r2p) <- crs(r3p) <- crs(raster1)
mm <-
mapview(rrsp, col.regions = 'green', col='green', alpha.regions = 0.1) +
mapview(r1p, col.regions = 'red', col='red', alpha.regions = 0.1) +
mapview(r2p, col.regions = 'red', col='red', alpha.regions = 0.1) +
mapview(r3p, col.regions = 'red', col='red', alpha.regions = 0.1)
mm
It finds the extents of each raster, makes spatial polygons from them, and then binds them together. The extent of this is then found, again as a spatial polygon.
The mapview
code makes the following plot.
The green rectangle encompasses all of the rasters which are in red.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.