-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrime_data.py
More file actions
63 lines (56 loc) · 2.53 KB
/
crime_data.py
File metadata and controls
63 lines (56 loc) · 2.53 KB
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
"""
Module containing a class to hold Austin crime data
"""
import pandas as pd
from sodapy import Socrata
from gmplot import gmplot
class CrimeData:
def __init__(self, region, api_key):
self.top = region[0][0]
self.bottom = region[1][0]
self.left = region[0][1]
self.right = region[1][1]
self.df = None
self.client = None
self.api_key = api_key
def load(self, dataset_id, domain, socrata_token, start_year, end_year, max_crimes):
self.client = Socrata(domain, socrata_token)
incident_id_start = start_year * (10 ** 7)
incident_id_end = end_year * (10 ** 7)
query = "incident_report_number >= " + str(incident_id_start) + \
" and incident_report_number <= " + str(incident_id_end) + \
" and latitude > " + str(self.bottom) + \
" and latitude < " + str(self.top) + \
" and longitude > " + str(self.left) + \
" and longitude < " + str(self.right)
dataset = self.client.get(dataset_id, where=query, limit=max_crimes)
self.df = pd.DataFrame.from_dict(dataset)
def crime_count(self):
try:
return self.df['incident_report_number'].count()
except:
print('No dataset has been loaded into this instance')
def markermap(self, zoom=15):
center_lat = self.bottom + (self.top - self.bottom) / 2
center_lng = self.right + (self.left - self.right) / 2
gmap = gmplot.GoogleMapPlotter(center_lat, center_lng, zoom, self.api_key)
try:
lat_coordinates = self.df['latitude'].astype('float').tolist()
lng_coordinates = self.df['longitude'].astype('float').tolist()
gmap.scatter(lat_coordinates, lng_coordinates, '#FF0000', size=5, marker=False)
return gmap
except:
print('No dataset has been loaded into this instance')
def heatmap(self, zoom=15, radius=30):
center_lat = self.bottom + (self.top - self.bottom) / 2
center_lng = self.right + (self.left - self.right) / 2
gmap = gmplot.GoogleMapPlotter(center_lat, center_lng, zoom, self.api_key)
try:
lat_coordinates = self.df['latitude'].astype('float').tolist()
lng_coordinates = self.df['longitude'].astype('float').tolist()
gmap.heatmap(lat_coordinates, lng_coordinates, radius=radius)
return gmap
except:
print('No dataset has been loaded into this instance')
def close(self):
self.client.close()