-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolving_crimes.py
More file actions
111 lines (82 loc) · 2.93 KB
/
Copy pathsolving_crimes.py
File metadata and controls
111 lines (82 loc) · 2.93 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
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
import pandas
import geopandas
from geopandas.tools import sjoin
from shapely.geometry import Point
import pylab as pl
print("hi five!")
import code; code.interact(local=locals())
#-----------------------------
# loading data as dataframes
#-----------------------------
suspects_df = pandas.read_csv('evidence/suspects_footprints.csv',sep=',')
victim_df = pandas.read_csv('evidence/victims_footprints.csv',sep=',')
print("data loaded")
#-----------------------------
# having fun with data
#-----------------------------
# columns available on dataset
columns = suspects_df.columns
print("dataset columns: ",columns)
# head of dataset
header = suspects_df.head(10)
print("dataset header: ",header)
# dtypes
dtypes = suspects_df.dtypes
print("dataset dtypes: ",dtypes)
#unique elements
print("suspects: ",suspects_df['name'].unique())
#len of dataset
print("len: ",suspects_df.count)
#n rows, n columns
print("len: ",suspects_df.shape)
print("have fun: done!")
#-----------------------------
# loading data as geodataframes
#-----------------------------
crs = {'init': 'epsg:4326'}
#suspects
geometry = [Point(xy) for xy in zip(suspects_df.longitud, suspects_df.latitud)]
suspects_gdf = geopandas.GeoDataFrame(suspects_df, crs=crs, geometry=geometry)
#victim
geometry = [Point(xy) for xy in zip(victim_df.longitud, victim_df.latitud)]
victim_gdf = geopandas.GeoDataFrame(victim_df, crs=crs, geometry=geometry)
#-----------------------------
# manipulating data
#-----------------------------
#slicing usung conditions
beth = suspects_gdf[suspects_gdf['name']=='Beth']
john = suspects_gdf[suspects_gdf['name']=='John']
jacob = suspects_gdf[suspects_gdf['name']=='Jacob']
lorraine = victim_gdf[victim_gdf['name']=='Lorraine']
martha = suspects_gdf[(suspects_gdf['name']=='Martha')|(suspects_gdf['name']=='martha')]
#manipulation row values
suspects_gdf['name']= suspects_gdf['name'].str.title()#st.lower()#str.upper()
martha = suspects_gdf[suspects_gdf['name']=='Martha']
#check
print("suspects: ",suspects_gdf['name'].unique())
#-----------------------------
# visualizing
#-----------------------------
#simple plot
suspects_gdf.plot(marker='*',color='green')
pl.show()
#visualizing using CartoDB
suspects_gdf.to_file('results/suspects_footprints/suspects_footprints.shp')
victim_gdf.to_file('results/victims_footprints/victims_footprints.shp')
#-----------------------------
# spatial JOINS
#-----------------------------
#load settlements
polygons = geopandas.GeoDataFrame.from_file('settlements/settlements_CDMX.shp')
points_in_polys = sjoin(suspects_gdf, polygons, how='left')
#new geodataframe inspection
#this gdf is the result of
#match between polygons and points
#play wit it!
points_in_polys.columns
#reprojecting
other_crs = {'init': 'epsg:3395'}
points_in_polys = points_in_polys.to_crs(other_crs)
#other option
points_in_polys = points_in_polys.to_crs(polygons.crs)
import code; code.interact(local=locals())