-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAmbord_TX_PrecinctsIntersection.R
96 lines (67 loc) · 3.84 KB
/
Ambord_TX_PrecinctsIntersection.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#Setup
rm(list=ls(all=TRUE)) # clear memory
packages<- c("rgdal","sp") # list the packages that you'll need
lapply(packages, require, character.only=T) # load the packages, if they don't load you might need to install them first
setwd("E:\\GISWork_2\\Ambord_TX_Voting")
#The intersection file was created in ArcGIS for ease
intersection.bg.spdf <- readOGR(".", layer = "BlockGroups_VotingPrecintsINTERSECTION")
overlap.bg.data <- intersection.bg.spdf@data
overlap.bg.data <- overlap.bg.data[,c(9,11,13)]
#2016-12-19: Redoing to get percentages
test.sum <- aggregate(Shape_Area ~ bgkey, overlap.bg.data, sum)
names(test.sum) <- c("bgkey","sum")
test.max <- aggregate(Shape_Area ~ bgkey, overlap.bg.data, max)
names(test.max) <- c("bgkey","max")
test.count <-as.data.frame(table(overlap.bg.data$bgkey))
test.count$bgkey <- as.numeric(as.character(test.count$Var1))
test.count$Var1 <- NULL
names(test.count) <- c("count","bgkey")
test.all <- merge(test.sum,test.max, by = "bgkey")
test.all <- merge(test.all,test.count, by = "bgkey")
#OLD AGGREgate
# overlap.bg.agg <- aggregate(Shape_Area ~ bgkey, overlap.bg.data, max)
# overlap.bg.max <- merge(overlap.bg.data,overlap.bg.agg)
# overlap.bg.max <- overlap.bg.max[,c("bgkey","PRECINCT_1")]
#This was done by finding those block groups that overlapped the precincts and manually deleting a few that were just because of edges overlapping slightly.
blockGroup.spdf <- readOGR(".",layer = "Harris_BlockGroups")
blockGroup.spdf.merge <- merge(blockGroup.spdf, overlap.bg.max)
blockGroup.spdf.merge@data$GEO_FIPS <- paste(blockGroup.spdf.merge@data$STATE,blockGroup.spdf.merge@data$CTBGKEY,sep="")
writeOGR(obj=blockGroup.spdf.merge, dsn=".", layer="BlockGroupsWithPrecint2", driver="ESRI Shapefile",overwrite_layer=TRUE)
BG.census.data <- read.csv("BG_Census_mod.csv",stringsAsFactors = F)
write.csv(blockGroup.spdf.merge@data,"BlockGroupsWithPrecintID.csv")
#Tracts instersection
intersection.tracts.spdf <- readOGR(".", layer = "Tracts_VotingPrecintsINTERSECTION")
overlap.tracts.data <- intersection.tracts.spdf@data
overlap.tracts.data <- overlap.tracts.data[,c(3,5,7)]
overlap.tracts.agg <- aggregate(Shape_Area ~ CTTRTKEY, overlap.tracts.data, max)
overlap.tracts.max <- merge(overlap.tracts.data,overlap.tracts.agg)
overlap.tracts.max <- overlap.tracts.max[,c("CTTRTKEY","PRECINCT_1")]
tracts.spdf <- readOGR(".",layer = "Harris_Tracts")
tracts.spdf.merge <- merge(tracts.spdf, overlap.tracts.max)
writeOGR(obj=tracts.spdf.merge, dsn=".", layer="TractsWithPrecint", driver="ESRI Shapefile",overwrite_layer=TRUE)
#11/9/16: More intersections of voting precincts
library(rgdal)
library(sp)
setwd("E:/GISWork_2/Archive/Ambord_TX_Voting/FilesToSampleForPrecincts")
latlong <- "+init=epsg:4326" #This is the espg code for the WGS 1984 geographic projection
# spatial data (political districts of Austria)
precincts <- readOGR(dsn = ".", layer = "HarrisCounty_VoterPrecincts_1069")
precincts.latlong <- spTransform(precincts, CRS(latlong))
# view
# some addresses
points <- read.csv("Paige's List-MatchedFile.csv", stringsAsFactors = F)
points$ID <- row.names(points)
# make pts spatial
working.points <- (points[,c("ID","GE_LATITUDE_2010","GE_LONGITUDE_2010")])
working.points <- working.points[complete.cases(working.points),]
coordinates(working.points) <- ~GE_LONGITUDE_2010 + GE_LATITUDE_2010 #Define the coordinates to convert it to a spatial points data frame
proj4string(working.points) <- CRS(latlong) #Define the projection using the CRS command to convert the string with the EPSG code
plot(precincts.latlong)
plot(working.points, add=T)
over.table <- over(working.points, precincts.latlong)
over.table <-
working.points.table <- working.points@data
hio <- cbind(working.points, a) # I think this needs more work
a1<-read.csv("LGBT Business LatLong.txt")
a2<-read.csv("LGBT Business LatLong_second.txt")
identical(a1,a2)