【问题标题】:How to Reorder Formal Class Spatial Polygons如何重新排序正式类空间多边形
【发布时间】:2016-11-01 13:24:36
【问题描述】:

我正在寻找一种方法来重新排序一组正式类空间多边形,使用

我正在使用美国人口普查数据(仅限于德克萨斯州),并希望从不同的县组合中创建 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))

但这对我不起作用。

【问题讨论】:

    标签: r rgdal sp


    【解决方案1】:

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

    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')
    

    【讨论】:

    • 这会将它放入 SpatialPolygonDataFrame 中,它可以让我将多边形重新排序为我需要的顺序。感谢您的帮助!
    【解决方案2】:
    shapefilename[order(shapefilename$column_name),]
    

    【讨论】:

    • 请解释为什么这是解决 OP 问题的方法。不鼓励在 SO 上仅提供代码答案,因为它们对 OP 或网站的未来访问者没有帮助。
    • 他需要按特定列(column_name)对名为“shapefilename”的多边形 shapefile 重新排序。这个命令做它。明白了吗?
    猜你喜欢
    • 1970-01-01
    • 2011-06-11
    • 1970-01-01
    • 2015-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-18
    • 1970-01-01
    相关资源
    最近更新 更多