In [3]:
import csv
import numpy as np
import matplotlib.pyplot as pl
from matplotlib import colors
import matplotlib.cm as cm
import matplotlib.colors as mcolors
import seaborn as sns
%matplotlib inline
import numpy as np
import geopandas as gpd
import pandas as pd
from fiona.crs import from_epsg
import pysal as ps
import shapely
#from sklearn import naive_bayes
#from sklearn import svm
#from sklearn.model_selection import train_test_split
#from sklearn import cluster
import ast
from cStringIO import StringIO
import requests
import os
import json
import urllib
import urllib2
from time import sleep
from IPython.display import clear_output
In [4]:
subStations = gpd.GeoDataFrame.from_file('Subway Stations.geojson')
subStations.crs = from_epsg(4326)
In [5]:
print (len(subStations))
print (len(subStations)*2.3)/60
sleep(2.3)
In [6]:
from shapely.geometry import Point
from shapely.geometry import Polygon
from shapely.ops import cascaded_union
In [7]:
def getXY(pt):
return pt.x, pt.y
In [8]:
subStations['polygon'] = 0
In [9]:
walkingdistance='15'
url = 'http://matrix.mapzen.com/isochrone?json={"locations":[{"lat":'+str(getXY(subStations.geometry.iloc[0])[1])+',"lon":'+str(getXY(subStations.geometry.iloc[0])[0])+'}],"costing":"pedestrian","contours":[{"time":' + walkingdistance + ',"color":"ff0000"}]}&id=Walk_From_L&api_key='+(os.getenv("MAPZENKEY"))
response = urllib2.urlopen(url)
data = response.read().decode('utf-8')
dataDict = json.loads(data)
subStations.loc[0,'polygon'] = Polygon(dataDict['features'][0]['geometry']['coordinates'])
sleep(2.3) ### avoid being blocked by the MapzenAPI services
Polygon(dataDict['features'][0]['geometry']['coordinates'])
Out[9]:
In [10]:
i=0
for station in subStations.geometry[1:]:
i+=1
clear_output()
print float(i)/float(len(subStations))
url='http://matrix.mapzen.com/isochrone?json={"locations":[{"lat":'+str(getXY(station)[1])+',"lon":'+str(getXY(station)[0])+'}],"costing":"pedestrian","contours":[{"time":' + walkingdistance + ',"color":"ff0000"}]}&id=Walk_From_L&api_key='+(os.getenv("MAPZENKEY"))
response = urllib2.urlopen(url)
data = response.read().decode('utf-8')
dataDict = json.loads(data)
subStations.loc[i,'polygon'] = Polygon(dataDict['features'][0]['geometry']['coordinates'])
#mainPolygon = cascaded_union([mainPolygon,secPolygon])
sleep(2.3)
In [11]:
subStations.head()
Out[11]:
In [12]:
WDsubStations= subStations.copy(deep=True)
In [13]:
WDsubStations.drop('geometry',inplace=True, axis=1)
In [14]:
WDsubStations = gpd.GeoDataFrame(WDsubStations, crs=WDsubStations.crs, geometry=WDsubStations.polygon)
WDsubStations.drop('polygon',inplace=True, axis=1)
WDsubStations.head()
#WDsubStations = WDsubStations.rename(columns={'polygon': 'geometry'})
Out[14]:
In [15]:
f, ax = pl.subplots(figsize=(55,55))
WDsubStations.plot(linewidth =1, ax = ax, color='b', label = 'tracks')
#pl.axis('off')
#ticks = ax.set_xticklabels(ax.get_xticklabels(), rotation = 90)
pl.title("NYC Areas within 5 Walkingdistance to Subway", size=20)
Out[15]:
In [16]:
with open('15min_Walkingdist_tosubway.geojson', 'w') as f:
f.write(WDsubStations.to_json())
In [21]:
WDsubStations.crs
Out[21]:
In [27]:
prj = 'GEOGCS["WGS84(DD)", DATUM["WGS84", SPHEROID["WGS84", 6378137.0, 298.257223563]], PRIMEM["Greenwich", 0.0], UNIT["degree", 0.017453292519943295], AXIS["Geodetic longitude", EAST], AXIS["Geodetic latitude", NORTH]]'
In [23]:
WDsubStations.to_file(filename='bufferstations',driver='ESRI Shapefile',crs_wkt=prj)
In [25]:
boro = gpd.GeoDataFrame.from_file('../Portafolio/Map5/boro/boro.shp')
boro.crs
Out[25]:
In [26]:
boro.head()
Out[26]:
In [28]:
boro.to_file(filename='boro',driver='ESRI Shapefile',crs_wkt=prj)
In [15]:
subStationstest = gpd.GeoDataFrame.from_file('Data/5min_Walkingdist_tosubway.geojson')
subStationstest.crs = from_epsg(4326)
subStationstest.head()
Out[15]:
In [16]:
f, ax = pl.subplots(figsize=(55,55))
subStationstest.plot(linewidth =1, ax = ax, color='b', label = 'tracks')
#pl.axis('off')
#ticks = ax.set_xticklabels(ax.get_xticklabels(), rotation = 90)
pl.title("NYC Areas within 5 Walkingdistance to Subway", size=20)
Out[16]:
In [ ]: