-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathcounty-to-csv.r
31 lines (24 loc) · 959 Bytes
/
county-to-csv.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Shapefiles downloaded from http://www.census.gov/geo/www/cob/cs2000.html
# Description of data at http://www.census.gov/geo/www/cob/cs_metadata.html
library(maptools)
library(plyr)
if (!file.exists("shapes.rdata")) {
files <- dir("shape-files", full = T, pattern = "\\.shp")
shapes <- llply(files, readShapeSpatial, .progress = "text")
save(shapes, file = "shapes.rdata")
} else {
load("shapes.rdata")
}
get_attributes <- function(shape) {
attr <- as.data.frame(shape)
names(attr) <- tolower(names(attr))
attr
}
# Extract county attributes
attributes <- ldply(shapes, get_attributes, .progress = "text")
attributes <- unique(attributes[, c("state", "county", "name", "area", "perimeter")])
write.table(attributes, "county-attr.csv", col=T, row=F, sep=",")
# Extract boundaries
raw <- ldply(shapes, fortify, region = c("STATE", "COUNTY"),
.progress = "text")
write.table(raw, "county-boundaries-raw.csv", col=T, row=F, sep=",")