def get_subway_data(city): """ Function seacrches for subway data for cities on either local files or queries data from OSM. city: city name (Ideally in (city, country) format) Returns: Geodataframe with subway stations as point features or None if OSM query contains no data """ string = city.split(",")[0] str_ = string.replace(' ','') sub_path = os.getcwd()+"\Subway and growth\subway_census_v1\station_points" sub_data = gpd.read_file(sub_path+"\\subway_stations2.shp") if str_ in sub_data.CITY1.unique(): city_data = sub_data[sub_data.CITY1 == str_] return pois else: bbox = geocoder.arcgis(city).geojson['features'][0]['bbox'] amenities = ['subway', 'light_rail', 'metro', 'underground', 'monorail', 'tram'] osm_tags = '"railway"~"{}"'.format('|'.join(amenities)) try: pois = osm.node_query(bbox[1], bbox[0], bbox[3], bbox[2],tags=osm_tags) ##lat_min, lng_min, lat_max, lng_max return pois except RuntimeError as e: if e.args[0] == "OSM query results contain no data.": return None
def get_nodes(b, tags): nodes = osm.node_query(b[1], b[0], b[3], b[2], tags=tags) nodes_geom = [Point(x) for x in zip(nodes['lon'], nodes['lat'])] nodes_df = gpd.GeoDataFrame(nodes[['amenity', 'lat', 'lon']], geometry=nodes_geom, crs={'init': 'epgs:4326'}) return (nodes_df)
def test_node_query(bbox2): tags = '"amenity"="restaurant"' cafes = osm.node_query(*bbox2, tags=tags) assert len(cafes) == 4 assert 'lat' in cafes.columns assert 'lon' in cafes.columns assert cafes['name'][2965338413] == 'Koja Kitchen'
def test_node_query(bbox2): tags = '"amenity"="restaurant"' cafes = osm.node_query(*bbox2, tags=tags) assert len(cafes) == 2 assert 'lat' in cafes.columns assert 'lon' in cafes.columns assert cafes['name'][1419597327] == 'Cream'
def add_feature(outputfile, bbox, network, tags, colname, mode='a'): all_nodes = [] for tag in tags: nodes = osm.node_query(*bbox, tags=tag) all_nodes = all_nodes + [nodes.loc[:,['lat', 'lon']]] all_nodes = pd.concat(all_nodes) all_nodes_unique = all_nodes.drop_duplicates() node_ids = network.get_node_ids(all_nodes_unique['lon'], all_nodes_unique['lat']) all_nodes_unique["node_id"] = node_ids.values with pd.HDFStore(outputfile, mode=mode) as store: store[colname] = all_nodes_unique
def get_nodes(b, tags): ''' Extract nodes from OSM based on tag query using pandana loaders INPUTS b [list of numbers] - boundary list from shapely.bounds tags [string] - filter to be send to pandana.osm.node_query RETURNS [geopandas dataframe] ''' nodes = osm.node_query(b[1], b[0], b[3], b[2], tags=tags) nodes_geom = [Point(x) for x in zip(nodes['lon'], nodes['lat'])] nodes_df = gpd.GeoDataFrame(nodes[['amenity', 'lat', 'lon']], geometry=nodes_geom, crs={'init': 'epgs:4326'}) return (nodes_df)
def get_distance_hwy(gdf, city): print("Populating distance to highways!!") if not gdf.crs: gdf.crs = {'init':'epsg:4326'} gdf_copy = gdf.copy() gdf_copy.to_crs(epsg=out_proj, inplace=True) bbox = geocoder.arcgis("{}".format(city)).geojson['features'][0]['properties']['raw']['extent'] highway = ['motorway', 'trunk', 'primary', 'secondary', 'tertiary', 'residential'] osm_tags = '"highway"~"{}"'.format('|'.join(highway)) try: highway_pois = osm.node_query(bbox['ymin'],bbox['xmin'],bbox['ymax'],bbox['xmax'],tags=osm_tags) ##lat_min, lng_min, lat_max, lng_max highway_pois = highway_pois[['lat', 'lon', 'highway', 'name']] ## Adding geometry to the dataset highway_pois['geometry'] = (list(zip(highway_pois.lon,highway_pois.lat))) highway_pois['geometry'] = highway_pois.geometry.apply(lambda x: Point(x)) highway_pois = gpd.GeoDataFrame(highway_pois, geometry='geometry') highway_pois.crs = {'init':'epsg:4326'} highway_pois.to_crs(epsg=out_proj, inplace=True) hwy_dist = [] dest_hwy = MultiPoint([i for i in highway_pois.geometry]) for i in gdf_copy.index: if i % 1000 == 0: print("{0} of {1} rows processed" .format(i, len(gdf_copy))) temp_cent = gdf_copy.geometry.iloc[i].centroid nearest_geoms = nearest_points(temp_cent, dest_hwy) hwy_dist.append(nearest_geoms[0].distance(nearest_geoms[1])) except RuntimeError as e: if e.args[0] == "OSM query results contain no data.": hwy_dist = [0 for i in range(len(gdf))] else: print(e) gdf['dis_to_hwy'] = hwy_dist return gdf
cbar_kwargs = {} # keyword arguments to pass to basemap bmap_kwargs = {} # color to make the background of the axis bgcolor = 'k' start_time = time.time() if os.path.isfile(poi_filename): # if a points-of-interest file already exists, just load the dataset from that pois = pd.read_csv(poi_filename) method = 'loaded from CSV' else: # otherwise, query the OSM API for the specified amenities within the bounding box osm_tags = '"amenity"~"{}"'.format('|'.join(amenities)) pois = osm.node_query(bbox[0], bbox[1], bbox[2], bbox[3], tags=osm_tags) # using the '"amenity"~"school"' returns preschools etc, so drop any that aren't just 'school' then save to CSV pois = pois[pois['amenity'].isin(amenities)] pois.to_csv(poi_filename, index=False, encoding='utf-8') method = 'downloaded from OSM' print('{:,} POIs {} in {:,.2f} seconds'.format(len(pois), method, time.time() - start_time)) start_time = time.time() if os.path.isfile(net_filename): # if a street network file already exists, just load the dataset from that network = pandana.network.Network.from_hdf5(net_filename) method = 'loaded from HDF5' else: # otherwise, query the OSM API for the street network within the specified bounding box
def test_node_query_raises(): with pytest.raises(RuntimeError): osm.node_query(37.8, -122.282, 37.8, -122.252)
# finding boundary of New York City NYC = ox.gdf_from_place('New York City, New York, USA') boundary = NYC.bounds boundary # In[2]: # importing amenities import pandana as pnda from pandana.loaders import osm # DINING NYC_restaurants = osm.node_query(40.477399, -74.25909, 40.916179, -73.700181, tags='"amenity"="restaurant"') NYC_restaurants = NYC_restaurants[['lat', 'lon', 'amenity', 'name']] NYC_cafes = osm.node_query(40.477399, -74.25909, 40.916179, -73.700181, tags='"amenity"="cafe"') NYC_cafes = NYC_cafes[['lat', 'lon', 'amenity', 'name']] NYC_dining = NYC_restaurants.append(NYC_cafes) NYC_dining # In[134]:
""" reference https://github.com/gboeing/urban-data-science/blob/master/20-Accessibility-Walkability/pandana-accessibility-demo-simple.ipynb""" import pandana import pandas import matplotlib.pyplot as plt from pandana.loaders import osm bbox = [30.08, -98.02, 30.52, -97.56] #lat-long bounding box for austin amenity = 'supermarket' #accessibility to this type of amenity distance = 10000 #max distance in meters # first download the points of interest corresponding to the specified amenity type pois = osm.node_query(bbox[0], bbox[1], bbox[2], bbox[3], tags='"shop"="{}"'.format(amenity)) pois[['shop', 'name', 'lat', 'lon']].tail() # query the OSM API for the street network within the specified bounding box network = osm.network_from_bbox(bbox[0], bbox[1], bbox[2], bbox[3]) # identify nodes that are connected to fewer than some threshold of other nodes within a given distance # do nothing with this for now, but see full example in other notebook for more lcn = network.low_connectivity_nodes(impedance=1000, count=10, imp_name='distance') # precomputes the range queries (the reachable nodes within this maximum distance) # so, as long as you use a smaller distance, cached results will be used network.precompute(distance + 1)