-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBurglaryToPOIsDistance.Rmd
225 lines (188 loc) · 11 KB
/
BurglaryToPOIsDistance.Rmd
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
---
title: "BurglaryToPointOfInterestDistance"
date: "September 9, 2015"
output: html_document
---
```{r load-crimedata,echo=FALSE,warning=FALSE,message=FALSE,cache=TRUE}
library(sp)
library(rgeos)
library(rgdal)
setwd("/Users/xiaomuliu/CrimeProject/SpatioTemporalModeling/ExploratoryAnalysis/Features/")
# source("importCrimeData.R")
# filePath <- "/Users/xiaomuliu/CrimeProject/SpatioTemporalModeling/ExploratoryAnalysis/CPD_DWH/"
# fileName <- "X_BURGLARY_POINTS_08_14.csv"
# BurglaryData <- importCrimeData(filePath,fileName)
# row.names(BurglaryData) <- NULL
#
# shapefilePath.new <- "/Users/xiaomuliu/CrimeProject/SpatioTemporalModeling/CPDShapeFiles/new/"
# beat_new.rg <- readOGR(paste0(shapefilePath.new,"cpd_beats"), "cpd_beats")
# district_new.rg <- readOGR(paste0(shapefilePath.new, "cpd_districts"),"cpd_districts")
# # centroids
# Crd.beat <- coordinates(beat_new.rg)
# Crd.district <- coordinates(district_new.rg)
#
# ## Match old and new beat
# # Matching the old beat records and the new one by finding which new beat polygon the point falls in
# # and then re-assign that beat number to that crime record.
# # Therefore all crime beat records have a unified reference which is the new beat map.
# source("DataPolyMatching.R")
# Match1 <- DataMatching2(BurglaryData,beat_new.rg,area="BEAT")
# BurglaryData <- Match1$CrimeData
# Match2 <- DataMatching2(BurglaryData,district_new.rg,area="DISTRICT")
# BurglaryData <- Match2$CrimeData
#
# ## Aggregated by "beat" and add 'holiday' attribute
# source("HolidayChart.R")
# BurglaryData$HOLIDAY <- sapply(BurglaryData$DATEOCC,FUN=holidays)
# BurglaryData$HOLIDAY <- factor(BurglaryData$HOLIDAY)
load("MatchedBurglaryData_portal.RData")
```
We take a subset of burglary data so that contains only 2008-2014 records to relieve computational burden. The regular grid is made by setting up a 120 $\times$ 160 grid then constraint it by keeping those inside the city boundary, in which 7724 grids are included.
```{r setup, echo=FALSE, message=FALSE, warning=FALSE, fig.align='center',fig.width=6, fig.height=4.5, cache=TRUE}
source("POIFunction.R")
Path.GIS <- "/Users/xiaomuliu/CrimeProject/SpatioTemporalModeling/GISData/"
YearList <- 2008:2014
BurglaryData.sub <- subset(BurglaryData,YEAR>=2008&YEAR<=2014)
Path.city <- "/Users/xiaomuliu/CrimeProject/SpatioTemporalModeling/GISData/City_Boundary"
city.shp <- readOGR(Path.city,"City_Boundary")
ncell.x <- 120
ncell.y <- 160
X_range <- city.shp@bbox[1,]
Y_range <- city.shp@bbox[2,]
grd.full <- expand.grid(list(X_COORD=seq(X_range[1],X_range[2],length.out=ncell.x),
Y_COORD=seq(Y_range[1],Y_range[2],length.out=ncell.y)))
coordinates(grd.full) = ~X_COORD+Y_COORD # convert to SpatialPoints
proj4string(grd.full) <- proj4string(city.shp)
grdInCity <- over(grd.full,as(city.shp,"SpatialPolygons"))
grd.inCity <- grd.full[!is.na(grdInCity)]
RegGrd <- as.data.frame(grd.inCity@coords)
plot(grd.inCity,pch=16,cex=0.1,main="Regular grids within city boundary",cex.main=0.8)
box(which = "plot", lty = "solid")
```
Now we explore burglary in proximity to some spatial points of interest. They are all calculated in the same fasion that for each incident/grid we calculate the Euclidean distance between that point to the closest landmarks.
```{r cpd-station, echo=FALSE, message=FALSE, warning=FALSE, fig.align='center',fig.width=10, fig.height=4.5, cache=TRUE}
Path.cpd_station <- paste0(Path.GIS,"police_stations_poly")
cpd_station.shp <- readOGR(Path.cpd_station,"police_stations")
plot(cpd_station.shp, main="CPD stations")
plot(city.shp,add=TRUE)
box(which = "plot", lty = "solid")
BurglaryData.sub <- Crime2POIdist(BurglaryData.sub,cpd_station.shp,"Dist2CPDstation",YearList=YearList,MonthList=1:12)
RegGrd <- Grd2POIdist(RegGrd,cpd_station.shp,"Dist2CPDstation")
par(mfrow=c(1,2))
hist(BurglaryData.sub$Dist2CPDstation,breaks=100,xlab="Distance (ft)", freq=FALSE,cex.main=0.6,
main="Histogram of distances between burglary incidents and nearest police stations")
hist(RegGrd$Dist2CPDstation,breaks=100,xlab="Distance (ft)", freq=FALSE,cex.main=0.6,
main="Histogram of distances between regular grids and nearest police stations")
```
```{r school, echo=FALSE, message=FALSE, warning=FALSE, fig.align='center',fig.width=10, fig.height=4.5, cache=TRUE}
Path.school <- paste0(Path.GIS,"School_Grounds")
school.shp <- readOGR(Path.school,"School_Grounds")
plot(school.shp, main="Schools")
plot(city.shp,add=TRUE)
box(which = "plot", lty = "solid")
BurglaryData.sub <- Crime2POIdist(BurglaryData.sub,school.shp,"Dist2School",YearList=YearList,MonthList=1:12)
RegGrd <- Grd2POIdist(RegGrd,school.shp,"Dist2School")
par(mfrow=c(1,2))
hist(BurglaryData.sub$Dist2School,breaks=100,xlab="Distance (ft)", freq=FALSE,cex.main=0.6,
main="Histogram of distances between burglary incidents and nearest schools")
hist(RegGrd$Dist2School,breaks=100,xlab="Distance (ft)", freq=FALSE,cex.main=0.6,
main="Histogram of distances between regular grids and nearest schools")
```
```{r park, echo=FALSE, message=FALSE, warning=FALSE, fig.align='center',fig.width=10, fig.height=4.5, cache=TRUE}
Path.park <- paste0(Path.GIS,"Parks_Aug2012")
park.shp <- readOGR(Path.park,"Parks_Aug2012")
plot(park.shp, main="Parks")
plot(city.shp,add=TRUE)
box(which = "plot", lty = "solid")
BurglaryData.sub <- Crime2POIdist(BurglaryData.sub,park.shp,"Dist2Park",YearList=YearList,MonthList=1:12)
RegGrd <- Grd2POIdist(RegGrd,park.shp,"Dist2Park")
par(mfrow=c(1,2))
hist(BurglaryData.sub$Dist2Park,breaks=100,xlab="Distance (ft)", freq=FALSE,cex.main=0.6,
main="Histogram of distances between burglary incidents and nearest parks")
hist(RegGrd$Dist2Park,breaks=100,xlab="Distance (ft)", freq=FALSE,cex.main=0.6,
main="Histogram of distances between regular grids and nearest parks")
```
```{r hospital, echo=FALSE, message=FALSE, warning=FALSE, fig.align='center',fig.width=10, fig.height=4.5, cache=TRUE}
Path.hospital <- paste0(Path.GIS,"Hospitals")
hospital.shp <- readOGR(Path.hospital,"Hospitals")
plot(park.shp, main="Hospitals")
plot(city.shp,add=TRUE)
box(which = "plot", lty = "solid")
BurglaryData.sub <- Crime2POIdist(BurglaryData.sub,hospital.shp,"Dist2Hospital",YearList=YearList,MonthList=1:12)
RegGrd <- Grd2POIdist(RegGrd,hospital.shp,"Dist2Hospital")
par(mfrow=c(1,2))
hist(BurglaryData.sub$Dist2Hospital,breaks=100,xlab="Distance (ft)", freq=FALSE,cex.main=0.6,
main="Histogram of distances between burglary incidents and nearest hospitals")
hist(RegGrd$Dist2Hospital,breaks=100,xlab="Distance (ft)", freq=FALSE,cex.main=0.6,
main="Histogram of distances between regular grids and nearest hospitals")
```
```{r library, echo=FALSE, message=FALSE, warning=FALSE, fig.align='center',fig.width=10, fig.height=4.5, cache=TRUE}
Path.library <- paste0(Path.GIS,"Libraries")
library.shp <- readOGR(Path.library,"Libraries")
plot(park.shp, main="Libraries")
plot(city.shp,add=TRUE)
box(which = "plot", lty = "solid")
BurglaryData.sub <- Crime2POIdist(BurglaryData.sub,library.shp,"Dist2Library",YearList=YearList,MonthList=1:12)
RegGrd <- Grd2POIdist(RegGrd,library.shp,"Dist2Library")
par(mfrow=c(1,2))
hist(BurglaryData.sub$Dist2Library,breaks=100,xlab="Distance (ft)", freq=FALSE,cex.main=0.6,
main="Histogram of distances between burglary incidents and nearest libraries")
hist(RegGrd$Dist2Library,breaks=100,xlab="Distance (ft)", freq=FALSE,cex.main=0.6,
main="Histogram of distances between regular grids and nearest libraries")
```
```{r cta-stop, echo=FALSE, message=FALSE, warning=FALSE, fig.align='center',fig.width=10, fig.height=4.5, cache=TRUE}
Path.CTA_stop <- "/Users/xiaomuliu/CrimeProject/SpatioTemporalModeling/GISData/CTA_BusStops"
CTA_stop.shp <- readOGR(Path.CTA_stop,"CTA_BusStops")
prj <- paste("+proj=tmerc +lat_0=36.66666666666666 +lon_0=-88.33333333333333 +k=0.9999749999999999",
"+x_0=300000 +y_0=0 +datum=NAD83 +units=us-ft +no_defs +ellps=GRS80 +towgs84=0,0,0")
CTA_stop.shp <- spTransform(CTA_stop.shp, CRSobj=prj)
BurglaryData.sub <- Crime2POIdist(BurglaryData.sub,CTA_stop.shp,"Dist2BusStop",YearList=YearList,MonthList=1:12)
RegGrd <- Grd2POIdist(RegGrd,CTA_stop.shp,"Dist2BusStop")
par(mfrow=c(1,2))
hist(BurglaryData.sub$Dist2BusStop,breaks=100,xlab="Distance (ft)", freq=FALSE,cex.main=0.6,
main="Histogram of distances between burglary incidents and nearest bus stops")
hist(RegGrd$Dist2BusStop,breaks=100,xlab="Distance (ft)", freq=FALSE,cex.main=0.6,
main="Histogram of distances between regular grids and nearest bus stops")
```
```{r cta-route, echo=FALSE, message=FALSE, warning=FALSE, fig.align='center',fig.width=10, fig.height=4.5, cache=TRUE}
Path.CTA_route <- "/Users/xiaomuliu/CrimeProject/SpatioTemporalModeling/GISData/CTA_Routes"
CTA_route.shp <- readOGR(Path.CTA_route,"CTA_Routes")
plot(CTA_route.shp, main="CTA routes")
plot(city.shp,add=TRUE)
box(which = "plot", lty = "solid")
BurglaryData.sub <- Crime2POIdist(BurglaryData.sub,CTA_route.shp,"Dist2CTAroute",YearList=YearList,MonthList=1:12)
RegGrd <- Grd2POIdist(RegGrd,CTA_route.shp,"Dist2CTAroute")
par(mfrow=c(1,2))
hist(BurglaryData.sub$Dist2CTAroute,breaks=100,xlab="Distance (ft)", freq=FALSE,cex.main=0.6,
main="Histogram of distances between burglary incidents and nearest CTA routes")
hist(RegGrd$Dist2CTAroute,breaks=100,xlab="Distance (ft)", freq=FALSE,cex.main=0.6,
main="Histogram of distances between regular grids and nearest CTA routes")
```
```{r cta-rail, echo=FALSE, message=FALSE, warning=FALSE, fig.align='center',fig.width=10, fig.height=4.5, cache=TRUE}
Path.CTA_rail <- "/Users/xiaomuliu/CrimeProject/SpatioTemporalModeling/GISData/CTA_RailLines"
CTA_rail.shp <- readOGR(Path.CTA_rail,"CTA_RailLines")
plot(CTA_rail.shp, main="CTA rails")
plot(city.shp,add=TRUE)
box(which = "plot", lty = "solid")
BurglaryData.sub <- Crime2POIdist(BurglaryData.sub,CTA_rail.shp,"Dist2CTArail",YearList=YearList,MonthList=1:12)
RegGrd <- Grd2POIdist(RegGrd,CTA_rail.shp,"Dist2CTArail")
par(mfrow=c(1,2))
hist(BurglaryData.sub$Dist2CTArail,breaks=100,xlab="Distance (ft)", freq=FALSE,cex.main=0.6,
main="Histogram of distances between burglary incidents and nearest CTA rails")
hist(RegGrd$Dist2CTArail,breaks=100,xlab="Distance (ft)", freq=FALSE,cex.main=0.6,
main="Histogram of distances between regular grids and nearest CTA rails")
```
```{r street, echo=FALSE, message=FALSE, warning=FALSE, fig.align='center',fig.width=10, fig.height=4.5, cache=TRUE}
Path.street <- "/Users/xiaomuliu/CrimeProject/SpatioTemporalModeling/GISData//Major_Streets"
street.shp <- readOGR(Path.street,"Major_Streets")
plot(street.shp, main="Major Streets")
plot(city.shp,add=TRUE)
box(which = "plot", lty = "solid")
BurglaryData.sub <- Crime2POIdist(BurglaryData.sub,street.shp,"Dist2Street",YearList=YearList,MonthList=1:12)
RegGrd <- Grd2POIdist(RegGrd,street.shp,"Dist2Street")
par(mfrow=c(1,2))
hist(BurglaryData.sub$Dist2Street,breaks=100,xlab="Distance (ft)", freq=FALSE,cex.main=0.6,
main="Histogram of distances between burglary incidents and nearest major streets")
hist(RegGrd$Dist2Street,breaks=100,xlab="Distance (ft)", freq=FALSE,cex.main=0.6,
main="Histogram of distances between regular grids and nearest major streets")
```