按面积过滤shapefile多边形 [英] Filter shapefile polygons by area
问题描述
我具有以下英国的边界数据集,其中显示了所有县:
I have the following boundary dataset for the United Kingdom, which shows all the counties:
library(raster)
library(sp)
library(ggplot)
# Download the data
GB <- getData('GADM', country="gbr", level=2)
使用subset
函数,很容易通过数据中的属性过滤shapefile多边形.例如,如果我要排除北爱尔兰:
Using the subset
function it is really easy to filter the shapefile polygons by an attribute in the data. For example, if I want to exclude Northern Ireland:
GB_sub <- subset(UK, NAME_1 != "Northern Ireland")
但是,有许多小岛扭曲了比例数据范围,如下图所示:
However, there are lots of small islands which distort the scale data range, as shown in the maps below:
关于如何以最小大小优雅地对数据集进行子集化的任何想法?具有与subset参数一致的格式的某些内容将是理想的.例如:
Any thoughts on how to elegantly subset the dataset on a minimum size? It would be ideal to have something in the format consistent with the subset argument. For example:
GB_sub <- subset(UK, Area > 20) # specify minimum area in km^2
推荐答案
这是另一个潜在的解决方案.由于您的数据是经纬度投影,因此直接根据纬度和经度计算面积会产生偏差,因此最好根据geosphere
包中的函数来计算面积.
Here is another potential solution. Because your data is in lat-long projection, directly calculating the area based on latitude and longitude would cause bias, it is better to calculate the area based on functions from the geosphere
package.
install.packages("geosphere")
library(geosphere)
# Calculate the area
GB$poly_area <- areaPolygon(GB) / 10^6
# Filter GB based on area > 20 km2
GB_filter <- subset(GB, poly_area > 20)
poly_area
包含所有多边形的以km2为单位的面积.我们可以按阈值过滤多边形,例如您的示例中的20
. GB_filter
是最终输出.
poly_area
contains the area in km2 for all polygons. We can filter the polygon by a threshold, such as 20
in your example. GB_filter
is the final output.
这篇关于按面积过滤shapefile多边形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!