In [1]:
import geopandas as gpd
import pandas as pd
from shapely.geometry import Point
import numpy as np
In [2]:
#change bike_lanes file path to citibike-publicspace
bike_lanes = gpd.read_file('../data/external/bike-lanes/nyc_bike_routes_2016.shp')
stations = pd.read_csv('../data/processed/stations.csv')
In [3]:
bike_lanes.columns
Out[3]:
In [4]:
bike_lanes = bike_lanes.drop(['instdate', 'moddate'], 1)
In [5]:
bike_lanes = bike_lanes.to_crs({'init': 'epsg:4326'})
In [6]:
geometry = gpd.GeoSeries([Point(xy) for xy in zip(stations.Longitude, stations.Latitude)])
geometry = geometry.buffer(.0005)
geo_stations = gpd.GeoDataFrame(stations, geometry=geometry)
geo_stations.crs = {'init' :'epsg:4326'}
geo_stations.head()
Out[6]:
In [19]:
#bike direction
ft_facilit_keys = bike_lanes['ft_facilit'].unique()
tf_facilit_keys = bike_lanes['tf_facilit'].unique()
print(ft_facilit_keys)
print(tf_facilit_keys)
ft_facility = feature facility start point to end point
bikedirR = ride start point to end point
tf_facility = feature facility end point to start point
bikedirL = ride end point to start point
bikedir2 = two-way
protected bike lanes: http://www.nyc.gov/html/dot/downloads/pdf/2014-09-03-bicycle-path-data-analysis.pdf
standard, signed route, sharrows: http://bikingrules.org/biking/laneprimer/
In [20]:
ft_score = []
for row in bike_lanes['ft_facilit']:
if row == 'Greenway':
ft_score.append(5)
elif row == 'Signed Route':
ft_score.append(5)
elif row == 'Ped Plaza':
ft_score.append(5)
elif row == 'Protected Path':
ft_score.append(4)
elif row == 'Bike-Friendly Parking':
ft_score.append(4)
elif row == 'Curbside':
ft_score.append(3)
elif row == 'Standard':
ft_score.append(3)
elif row == 'Sidewalk':
ft_score.append(2)
elif row == 'Opposite Sidewalk':
ft_score.append(2)
elif row == 'Sharrows':
ft_score.append(1)
else:
ft_score.append(0)
bike_lanes['ft_facilit_score'] = ft_score
In [21]:
tf_score = []
for row in bike_lanes['tf_facilit']:
if row == 'Greenway':
tf_score.append(5)
elif row == 'Signed Route':
tf_score.append(5)
elif row == 'Ped Plaza':
tf_score.append(5)
elif row == 'Protected Path':
tf_score.append(4)
elif row == 'Bike-Friendly Parking':
tf_score.append(4)
elif row == 'Curbside':
tf_score.append(3)
elif row == 'Standard':
tf_score.append(3)
elif row == 'Sharrows/Protected Path':
tf_score.append(2.5)
elif row == 'Sidewalk':
tf_score.append(2)
elif row == 'Opposite Sidewalk':
tf_score.append(2)
elif row == 'Sharrows/Standard':
tf_score.append(1.5)
elif row == 'Sharrows':
tf_score.append(1)
else:
tf_score.append(0)
bike_lanes['tf_facilit_score'] = tf_score
In [22]:
stations_bike_quality = gpd.sjoin(geo_stations, bike_lanes, op='intersects')
stations_bike_quality.head()
Out[22]:
In [30]:
# Save for map
forMap = bike_lanes.iloc[stations_bike_quality.index_right.unique(), :].copy()
forMap["score"] = forMap.ft_facilit_score + forMap.tf_facilit_score
forMap.to_csv("../data/map/bike-lanes.csv")
In [23]:
stations_bike_cut = stations_bike_quality.drop(['index_right', 'bikedir', 'OBJECTID', 'allclasses', 'comments', 'fromstreet', 'onoffst', 'street', 'tostreet'], axis=1)
#stations_bike_cut.head()
print(len(stations_bike_cut))
In [24]:
nums = []
nums2 = []
for elem in stations_bike_cut['ft_facilit_score']:
nums.append(elem)
#print(len(nums))
for elem in stations_bike_cut['tf_facilit_score']:
nums2.append(elem)
#print(len(nums2))
nums_added = []
for i in range(0,len(stations_bike_cut)):
if nums[i] > 0 and nums2[i] > 0:
nums_added.append((nums[i] + nums2[i]) / 2)
elif nums[i] > 0 and nums2[i] == 0:
nums_added.append(nums[i])
elif nums[i] == 0 and nums2[i] > 0:
nums_added.append(nums2[i])
#print(len(nums_added))
stations_bike_cut['metric'] = nums_added
stations_bike_cut.head()
Out[24]:
In [19]:
#groupby and take the average of the station
In [25]:
stations_bikes = pd.DataFrame(stations_bike_cut['metric'].groupby([stations_bike_cut['Station_id']]).mean())
stations_bikes = stations_bikes.reset_index()
stations_bikes.head()
Out[25]:
In [16]:
stations_bikes.to_csv('../data/processed/bike-lane-quality.csv')