In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from geopandas import GeoSeries, GeoDataFrame
from shapely.geometry import Point, Polygon, MultiPoint, MultiPolygon
from shapely.prepared import prep
from bokeh.models import ColumnDataSource, Patches, HoverTool, TapTool, Plot, Range1d
from bokeh.plotting import figure, show, output_notebook
output_notebook()
In [2]:
## Read from json and convert to np arrays
map_data = GeoDataFrame.from_file('Neighborhood_Clusters.geojson')
map_data.head()
Out[2]:
Load in NFIRS and CAD data for community.
In [3]:
data = pd.read_csv('DC_CAD_FY1415.csv') # read in CAD data
nfirs = pd.read_csv('DC_NFIRS_FY1415.csv') # read in NFIRS data
merged_data = pd.merge(left=data,right=nfirs, left_on='num_1', right_on='num_1') # merge nfirs and cad based on incident number
data = merged_data[pd.to_numeric(merged_data['incidenttype'],errors='coerce')<130] # select only structure fires
data = data[data['num_1'].str.contains("F14")] #sliced year data
Take geospatial fire data and place it appropriate polygon.
In [4]:
mapped_points = GeoSeries([Point(mapped_x, mapped_y) for mapped_x, mapped_y in zip(data['longitude'],data['latitude'])])
all_points = MultiPoint(mapped_points)
all_polygons = prep(MultiPolygon(list(map_data['geometry'].values)))
city_points = list(filter(all_polygons.contains, all_points))
def num_of_contained_points(apolygon, city_points):
return int(len(list(filter(prep(apolygon).contains, city_points))))
map_data['hood_count'] = map_data['geometry'].apply(num_of_contained_points, args=(city_points,)) #counts number of fires per neighborhood
Color bins based on number of fires in each bin.
In [5]:
color_split = int(max(map_data['hood_count'])/5)
colors = ["#F1EEF6", "#D4B9DA", "#C994C7", "#DF65B0", "#DD1C77", "#980043"]
hood_colors =[]
for i in range(len(map_data['NBH_NAMES'])):
idx = min(int(np.around(map_data.iloc[i]['hood_count']/color_split)),5)
hood_colors.append(colors[idx])
map_data['hood_colors']=hood_colors
Write appended data file out to new geojson file.
In [6]:
map_data.to_file('DC_Fires.geojson', driver="GeoJSON")
Check to make sure new file loads and looks correct.
In [7]:
map_data2 = GeoDataFrame.from_file('DC_Fires.geojson')
map_data2.head()
Out[7]:
In [ ]: