[英]Can you combine polygons within a SpatialPolygonsDataFrame by values in the dataframe?
I am trying to use the Australian Bureau of Statistics shapefile for Remoteness 2016 - downloading the ESRI shapefile.我正在尝试将澳大利亚统计局 shapefile 用于 Remoteness 2016 - 下载 ESRI shapefile。
I want to combine all of the polygons that are no Major Cities of Australia.我想组合所有不是澳大利亚主要城市的多边形。
library(rgdal)
library(dplyr)
RA_2016 <- readOGR(".", layer = "RA_2016_AUST")
###to simplify only using NSW
RA_2016 <- RA_2016[RA_2016$STE_CODE16 == 1, ]
The data frame has 5 columns.数据框有 5 列。 I don't need any of this data once I have created a variable for whether it is a Major City or not.一旦我为它是否是主要城市创建了一个变量,我就不需要任何这些数据。
MajorCity <- data.frame(10:14, c("Major City", "Regional", "Regional", "Regional", "Regional"))
names(MajorCity) <- c("RA_CODE16", "Bigsmoke")
RA_2016@data <- left_join(RA_2016@data, MajorCity)
What I want to do now is to merge the polygons that have MajorCity == "Regional".我现在要做的是合并具有 MajorCity == "Regional" 的多边形。 I do not need any of the original data from RA_2016.我不需要来自 RA_2016 的任何原始数据。 But I want it to remain a SpatialPolygonsDataFrame with the column "Bigsmoke".但我希望它保持为带有“Bigsmoke”列的 SpatialPolygonsDataFrame。
In the next part of the code I am going to combine it with the Australian Bureau of Statistics' LGA data (basically so I can split LGAs into regional parts and major city parts - where they are split).在代码的下一部分中,我将把它与澳大利亚统计局的 LGA 数据结合起来(基本上这样我就可以将 LGA 拆分为区域部分和主要城市部分——它们被拆分的地方)。 So I think I need to keep that minimal amount of data.所以我认为我需要保留最少量的数据。
Is there a good way to do this?有没有好的方法来做到这一点? Is there another post I have failed to find that will show me the way?是否有另一个我找不到的帖子可以告诉我方法?
Here is how I would do that:这是我将如何做到的:
library(raster)
RA_2016 <- shapefile("RA_2016_AUST.shp")
RA_2016 <- RA_2016[RA_2016$STE_CODE16 == 1, ]
MajorCity <- data.frame(RA_CODE16=10:14, Bigsmoke=c("Major City", "Regional", "Regional", "Regional", "Regional"))
m <- merge(RA_2016, MajorCity)
x <- aggregate(m, "Bigsmoke")
x
#class : SpatialPolygonsDataFrame
#features : 2
#extent : 140.9993, 159.1092, -37.50508, -28.15702 (xmin, xmax, ymin, ymax)
#crs : +proj=longlat +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +no_defs
#variables : 1
#names : Bigsmoke
#min values : Major City
#max values : Regional
Avoid writing to slots "@", like you do here: left_join(RA_2016@data, MajorCity)
That could change the order and the number of records such that the attributes ould no longer match the geometries避免写入槽“@”,就像你在这里做的那样: left_join(RA_2016@data, MajorCity)
这可能会改变记录的顺序和数量,使得属性不再与几何匹配
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.