如何重新排序正式类空间多边形

时间:2016-06-29 17:23:59

标签: r rgdal sp

我正在寻找一种使用

重新排序一组正式类空间多边形的方法

我正在使用美国人口普查数据(限于德克萨斯州),并希望从不同的县组合中创建33个多边形。

library(tmap)
library(maptools)
library(ggplot2)
library(rgeos)
library(sp)
library(mapdata)
library(rgdal)
library(raster)

# Download the map of texas and get the LMAs boundaries
#   Download shape
f <- tempfile()
download.file("http://www2.census.gov/geo/tiger/GENZ2010/gz_2010_us_050_00_20m.zip", destfile = f)
unzip(f, exdir = ".")
US <- read_shape("gz_2010_us_050_00_20m.shp")

#   Select only Texas
Texas <- US[(US$STATE %in% c("48")),]


#   Load the LMA append data
LMAs = read.table('LMA append data.csv',header=T, sep=',')

#   Append LMA data to Texas shape
Texas$FIPS <- paste0(Texas$STATE, Texas$COUNTY)
Texas <- append_data(Texas, LMAs, key.shp = "FIPS", key.data = "FIPS")
Texas <- Texas[order(Texas$LMA),]

#   Create shape object with LMAs polygons
Texas_LMA <- unionSpatialPolygons(Texas, IDs=Texas$LMA)

我尝试将Texas_LMA转换为带有

的SpatialPolygonsDataFrame
#   Create shape object with LMAs polygons
Texas_LMA <- unionSpatialPolygons(Texas, IDs=Texas$LMA)
spp <- SpatialPolygonsDataFrame(Texas_LMA,data=matrix(1:33,nrow=33,ncol=1))

但这对我没用。

2 个答案:

答案 0 :(得分:0)

你的问题不是很清楚。但我认为这就是你所追求的目标:

library(raster)
f <- tempfile()  download.file("http://www2.census.gov/geo/tiger/GENZ2010/gz_2010_us_050_00_20m.zip", destfile = f)
unzip(f, exdir = ".")
US <- shapefile("gz_2010_us_050_00_20m.shp")
Texas <- US[(US$STATE %in% c("48")),]
LMAs = read.csv('LMA append data.csv')  
Texas$FIPS <- paste0(Texas$STATE, Texas$COUNTY)

你没有提供LMA数据,所以从这里猜测一下:

Texas <- merge(Texas, LMAs, by="FIPS")  
Texas_LMA <- aggregate(Texas, by='LMA')

答案 1 :(得分:0)

shapefilename[order(shapefilename$column_name),]