简体   繁体   English

如何使用R将矩形多边形拟合为不规则多边形?

[英]How to fit rectangular polygon into irregular polygon using R?

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.为了进一步分析,我想获得适合所有这些数据集的足迹/轮廓的最大矩形多边形(所有 4 个角均为 90 度角),以屏蔽和裁剪我的数据集。 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. mapview代码生成以下图。

在此处输入图片说明

The green rectangle encompasses all of the rasters which are in red.绿色矩形包含所有红色栅格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM