0

I'm looking for a way to re-order a set of Formal Class Spatial Polygons using

I'm using US Census data (Limited to Texas) and want to create 33 polygons out of different county combinations.

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)

I've tried converting Texas_LMA into a SpatialPolygonsDataFrame with

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

But that hasn't worked for me.

2 Answers2

0

Your question is not very clear. But I think this is what you are after:

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)

You did not provide the LMA data, so guessing a bit from here:

Texas <- merge(Texas, LMAs, by="FIPS")  
Texas_LMA <- aggregate(Texas, by='LMA')
Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • That gets it into a SpatialPolygonDataFrame and it lets me re-order the polygons to be in the order I need. Thanks for the help! – rskinner593 Jun 29 '16 at 18:22
0
shapefilename[order(shapefilename$column_name),]
Arthur
  • 1
  • 1
  • 2
    Please provide an explanation as to why this is a solution to OP's problem. Code only answers are discouraged on SO as they do not help OP or future visitors of the site. – d_kennetz Apr 17 '19 at 20:59
  • He needs to reorder polygon shapefile called "shapefilename" by a specific column (column_name). This command do It. Understood? – Arthur Apr 20 '19 at 01:51