def test_footprints(): import json import pytest from shapely.geometry import Polygon # download footprints and plot them gdf = ox.footprints_from_place(place='Emeryville, California, USA') gdf = ox.footprints_from_polygon( Polygon([(17.574, -4.145), (17.575, -4.144), (17.576, -4.145)])) gdf = ox.footprints_from_address( address='600 Montgomery St, San Francisco, California, USA', distance=300) fig, ax = ox.plot_footprints(gdf) # new_river_head.json contains a relation with 1 outer closed way and 2 inner closed ways # inner way 665593284 is directly tagged as a building and should create its own polygon with open("tests/input_data/new_river_head.json", "r") as read_file: new_river_head_responses = [json.load(read_file)] new_river_head_gdf = ox.create_footprints_gdf( responses=new_river_head_responses) assert 665593284 in new_river_head_gdf.index assert new_river_head_gdf.loc[9246394]['geometry'].type == 'Polygon' assert len(new_river_head_gdf.loc[9246394, 'geometry'].interiors) == 2 # clapham_common.json contains a relation with 5 outer rings and 1 inner ring. One of the outer rings is a chain of open ways with open("tests/input_data/clapham_common.json", "r") as read_file: clapham_common_responses = [json.load(read_file)] clapham_common_gdf = ox.create_footprints_gdf( footprint_type='leisure', responses=clapham_common_responses) assert clapham_common_gdf.loc[1290065]['geometry'].type == 'MultiPolygon' # relation_no_outer.json contains a relation with 0 outer rings and 1 inner ring with open("tests/input_data/relation_no_outer.json", "r") as read_file: relation_no_outer_responses = [json.load(read_file)] ox.create_footprints_gdf(responses=relation_no_outer_responses) # inner_chain.json contains a relation with 1 outer rings and several inner rings one of which is a chain of open ways with open("tests/input_data/inner_chain.json", "r") as read_file: inner_chain_responses = [json.load(read_file)] ox.create_footprints_gdf(responses=inner_chain_responses) # mis_tagged_bus_route.json contains a relation with out 'inner' or 'inner' rings with open("tests/input_data/mis_tagged_bus_route.json", "r") as read_file: mis_tagged_bus_route_responses = [json.load(read_file)] ox.create_footprints_gdf(responses=mis_tagged_bus_route_responses) # test plotting multipolygon fig, ax = ox.plot_footprints(clapham_common_gdf) # should raise an exception # polygon or -north, south, east, west- should be provided with pytest.raises(ValueError): ox.create_footprints_gdf(polygon=None, north=None, south=None, east=None, west=None) gdf = ox.footprints_from_place(place='kusatsu, shiga, japan', which_result=2)
def test_footprints(): # download footprints and plot them gdf = ox.footprints_from_place(place2) gdf = ox.footprints_from_polygon(polygon) gdf = ox.footprints_from_address(address, dist=300) fig, ax = ox.plot_footprints(gdf)
def test_footprints(): # download footprints and plot them gdf = ox.footprints_from_place(place='Emeryville, California, USA') gdf = ox.footprints_from_address( address='600 Montgomery St, San Francisco, California, USA', distance=300) fig, ax = ox.plot_footprints(gdf)
def test_footprints(): # download footprints and plot them cs = '[out:json][timeout:180][date:"2019-10-28T19:20:00Z"]' gdf = ox.footprints_from_place(place2, custom_settings=cs) gdf = ox.footprints_from_polygon(polygon) gdf = ox.footprints_from_address(address, dist=300) fig, ax = ox.plot_footprints(gdf) # new_river_head.json contains a relation with 1 outer closed way and 2 # inner closed ways inner way 665593284 is directly tagged as a building # and should create its own polygon with open("tests/input_data/new_river_head.json", "r") as read_file: new_river_head_responses = [json.load(read_file)] new_river_head_gdf = ox.footprints._create_footprints_gdf( responses=new_river_head_responses) assert 665593284 in new_river_head_gdf.index assert new_river_head_gdf.loc[9246394]["geometry"].type == "Polygon" assert len(new_river_head_gdf.loc[9246394, "geometry"].interiors) == 2 # clapham_common.json contains a relation with 5 outer rings and 1 # inner ring. One of the outer rings is a chain of open ways with open("tests/input_data/clapham_common.json", "r") as read_file: clapham_common_responses = [json.load(read_file)] clapham_common_gdf = ox.footprints._create_footprints_gdf( footprint_type="leisure", responses=clapham_common_responses) assert clapham_common_gdf.loc[1290065]["geometry"].type == "MultiPolygon" # relation_no_outer.json contains a relation with 0 outer rings and 1 # inner ring with open("tests/input_data/relation_no_outer.json", "r") as read_file: relation_no_outer_responses = [json.load(read_file)] ox.footprints._create_footprints_gdf(responses=relation_no_outer_responses) # inner_chain.json contains a relation with 1 outer rings and several # inner rings one of which is a chain of open ways with open("tests/input_data/inner_chain.json", "r") as read_file: inner_chain_responses = [json.load(read_file)] ox.footprints._create_footprints_gdf(responses=inner_chain_responses) # mis_tagged_bus_route.json contains a relation with out 'inner' or # 'inner' rings with open("tests/input_data/mis_tagged_bus_route.json", "r") as read_file: mis_tagged_bus_route_responses = [json.load(read_file)] ox.footprints._create_footprints_gdf( responses=mis_tagged_bus_route_responses) # test plotting multipolygon fig, ax = ox.plot_footprints(clapham_common_gdf) # should raise an exception # polygon or -north, south, east, west- should be provided with pytest.raises(ValueError): ox.footprints._create_footprints_gdf(polygon=None, north=None, south=None, east=None, west=None)
def map_matching_node( move_data: Union[PandasMoveDataFrame, DaskMoveDataFrame, PandasDiscreteMoveDataFrame], inplace: Optional[bool] = True, bbox: Optional[Tuple[float, float, float, float]] = None, place: Optional[Text] = None, G: Optional[MultiDiGraph] = None ) -> Optional[DataFrame]: """ Generate Map matching using the graph nodes Parameters ---------- move_data : MoveDataFrame The input trajectories data inplace: bool, optional if set to true the original dataframe will be altered, otherwise the alteration will be made in a copy, that will be returned, by default True bbox : tuple, optional The bounding box as (north, east, south, west), by default None place : string, optional The query to geocode to get place boundary polygon, by default None G : MultiDiGraph, optional The input graph, by default None Returns ------- move_data : MoveDataFrame A copy of the original dataframe or None """ if(G is None): if(bbox is None): bbox = move_data.get_bbox() G = ox.graph_from_bbox( bbox[0], bbox[2], bbox[1], bbox[3], network_type='all_private' ) elif(place is not None): G = ox.footprints_from_place(query=place, tags={'network_type': 'all_private'}) if not inplace: move_data = move_data[:] nodes = ox.get_nearest_nodes( G, X=move_data['lon'], Y=move_data['lat'], method='kdtree' ) gdf_nodes = ox.graph_to_gdfs(G, edges=False) df_nodes = gdf_nodes.loc[nodes] move_data['lat'] = list(df_nodes.y) move_data['lon'] = list(df_nodes.x) move_data['geometry'] = list(df_nodes.geometry) if not inplace: return move_data
def test_footprints(): # download footprints and plot them gdf = ox.footprints_from_place(place='Emeryville, California, USA') gdf = ox.footprints_from_address(address='600 Montgomery St, San Francisco, California, USA', distance=300) fig, ax = ox.plot_footprints(gdf) # test multipolygon footprint # point is the location of known multipolygon building, relation id 1767022 point = (51.5276, -0.11) gdf = ox.footprints_from_point(point=point, distance=20, footprint_type='building') assert 1767022 in gdf.index, "relation 1767022 was not returned in the geodataframe" assert gdf.loc[1767022]['geometry'].type=='MultiPolygon', "relation 1767022 is not a multipolygon"
def score_people_count_script(place_name): data = ox.footprints_from_place(place_name) data = data[['building', 'building:levels', 'geometry']] data = data.reset_index(drop=True) data["area"], data["horizontal_len"], data["vertical_len"], data[ "poly_shapes_count"] = None, None, None, None for index, element in data.iterrows(): try: data["area"][index], data["horizontal_len"][index], data[ "vertical_len"][index] = get_data_from_polygon( element.geometry) data["poly_shapes_count"][index], data[ 'building:levels'] = element.geometry.exterior.coords.__len__( ), '1' except Exception: pass data = data.drop(['geometry'], axis=1) data = data.reset_index(drop=True) data.poly_shapes_count = data.poly_shapes_count.astype('float') data = data[np.isfinite(data['poly_shapes_count'])] data = data.fillna(0) data_accomodation_buildings = data.loc[data.building.isin( accommodation_buildings)] data_undefined_buildings = data.loc[data.building.isin( undefined_buildings)] data_undefined_buildings = data_undefined_buildings.drop(['building'], axis=1) data_undefined_buildings['building'] = model.predict( data_undefined_buildings) data_undefined_buildings.building = encoder.inverse_transform( data_undefined_buildings.building.astype('int')) data = data_accomodation_buildings.append(data_undefined_buildings) data = data.loc[data.building.isin(accommodation_buildings)] accommodation_building_variances = dict(data.building.value_counts()) people_count = score_people_count(data) return people_count, accommodation_building_variances
def test_footprints(): # download footprints and plot them gdf = ox.footprints_from_place(place='Emeryville, California, USA') gdf = ox.footprints_from_address( address='600 Montgomery St, San Francisco, California, USA', distance=300) fig, ax = ox.plot_footprints(gdf) # test multipolygon footprint # point is the location of known multipolygon building, relation id 1767022 point = (51.5276, -0.11) gdf = ox.footprints_from_point(point=point, distance=20, footprint_type='building') assert 1767022 in gdf.index, "relation 1767022 was not returned in the geodataframe" assert gdf.loc[1767022][ 'geometry'].type == 'MultiPolygon', "relation 1767022 is not a multipolygon"
def find_amenities(place_name): area = ox.gdf_from_place(place_name) graph = ox.graph_from_place(place_name) nodes, edges = ox.graph_to_gdfs(graph) buildings = ox.footprints_from_place(place_name) amenities = ox.pois_from_place(place_name, amenities=sustenance_amenities) ax = area.plot(facecolor='white') ax = edges.plot(ax=ax, linewidth=1, edgecolor='#BC8F8F', alpha=0.2) ax = buildings.plot(ax=ax, facecolor='blue', alpha=1) ax = amenities.plot(ax=ax, color='green', alpha=1, markersize=10) ax.figure.savefig('amenities.png') sustenance_amenities_variances = dict(amenities.amenity.value_counts()) wrong_dict_keys = [] for key in sustenance_amenities_variances.keys(): if key not in sustenance_amenities: wrong_dict_keys.append(key) for key in wrong_dict_keys: del sustenance_amenities_variances[key] return sustenance_amenities_variances
import networkx as nx def save_json(data, filename): with open(filename, 'w') as out_file: out_file.write(data.to_json()) if __name__ == '__main__': # map uploading place_name = "Nizhny Novgorod, Russia" graph = ox.graph_from_place(place_name) nodes, edges = ox.graph_to_gdfs(graph) area = ox.gdf_from_place(place_name) buildings = ox.footprints_from_place(place_name) # saving nx.write_gpickle(graph, 'nizhny_novgorod_graph.pkl') save_json(edges, 'nn_roads.json') save_json(buildings, 'nn_buildings.json') save_json(nodes, 'nn_nodes.json') save_json(area, 'nn_area.json') fig, ax = plt.subplots(figsize=(20, 17)) area.plot(ax=ax, facecolor='black') edges.plot(ax=ax, linewidth=1, edgecolor='#BC8F8F') buildings.plot(ax=ax, facecolor='khaki', alpha=0.7) plt.tight_layout() plt.title(place_name) plt.xlabel('lat')
source.ImportFromEPSG(4326) target = osr.SpatialReference() target.ImportFromEPSG(5243) transform = osr.CoordinateTransformation(source, target) poly = ogr.CreateGeometryFromJson( str(json).replace('(', '[').replace(')', ']')) poly.Transform(transform) x1, x2, y1, y2 = poly.GetEnvelope() h_len, v_len, area = abs(x2 - x1), abs(y2 - y1), poly.GetArea() return area, h_len, v_len for place in places: print(place) data = ox.footprints_from_place(place) data = data[['building', 'building:levels', 'geometry']] data = data.reset_index(drop=True) idx_for_drop = [] data["area"], data["horizontal_len"], data["vertical_len"], data[ "poly_shapes_count"] = None, None, None, None for index, element in data.iterrows(): try: data["area"][index], data["horizontal_len"][index], data[ "vertical_len"][index] = get_data_from_polygon( element.geometry) data["poly_shapes_count"][ index] = element.geometry.exterior.coords.__len__() except Exception: pass
def simplify_and_mapping(data_source): if data_source == 'lwm': tau = 2 buildings = extract_footprint_from_prism('../data/lwm-prism.gml') else: # Only for Manhattan, New York tau = 0.00003 buildings = ox.footprints_from_place( '{}, Manhattan, New York City'.format(data_source)) douglas_peucker_buildings = GeoDataFrame() simplified_buildings = GeoDataFrame() sum_haus = [0.0, 0.0] total_points = [0, 0] tolerance = tau * 3 / 5 def comparison(footprint, i): new_footprint = prism.simplify(footprint, tau=tau, epsilon=math.pi / 30) if new_footprint is not None: simplified_buildings.loc[i, 'geometry'] = new_footprint haus = footprint.hausdorff_distance(new_footprint) sum_haus[1] += haus total_points[1] += len(new_footprint.exterior.coords) dp_footprint = footprint.simplify(tolerance) douglas_peucker_buildings.loc[i, 'geometry'] = dp_footprint haus = footprint.hausdorff_distance(dp_footprint) sum_haus[0] += haus total_points[0] += len(dp_footprint.exterior.coords) count = 0 for geom in buildings['geometry']: if geom.geom_type == 'Polygon': comparison(geom, count) count += 1 if geom.geom_type == 'MultiPolygon': for poly in geom: comparison(poly, count) count += 1 print("Average Hausdorff Distance (Douglas Peucker):", sum_haus[0] / count) print("Average Hausdorff Distance (Indoor Simplification):", sum_haus[1] / count) print("Total Number of Points (Douglas Peucker):", total_points[0]) print("Total Number of Points (Indoor Simplification):", total_points[1]) cell_text = [[tolerance, tau], [sum_haus[0] / count, sum_haus[1] / count], [total_points[0], total_points[1]]] # mapping minx, miny, maxx, maxy = buildings.total_bounds map_scale = 50 width = maxx - minx height = maxy - miny ratio = width / height mbr = (ratio * map_scale, map_scale) fig, ax = plt.subplots(figsize=mbr) buildings.plot(ax=ax, facecolor='green', edgecolor='grey', linewidth=0.2, alpha=0.1) douglas_peucker_buildings.plot(ax=ax, facecolor='blue', alpha=0.1) simplified_buildings.plot(ax=ax, facecolor='red', alpha=0.1) ax.table(cellText=cell_text, rowLabels=[ "Distance Tolerance", "Average Hausdorff Distance", "Total Number of Points" ], colLabels=["Douglas Peucker", "Indoor Simplification"], colWidths=[0.05 / ratio, 0.05 / ratio], loc='lower right') legend_elements = [ Patch(facecolor='green', edgecolor='grey', linewidth=0.2, alpha=0.1, label='Original'), Patch(facecolor='blue', alpha=0.1, label='Douglas Peucker'), Patch(facecolor='red', alpha=0.1, label='Indoor Simplification') ] ax.legend(handles=legend_elements, loc='upper right', title='Simplification Method', fontsize=map_scale, title_fontsize=map_scale) plt.tight_layout() plt.savefig('../examples/{}.pdf'.format(data_source), format='pdf')
def park_barriers(place, download_method, distance=None, epsg=None, min_area=100000): """ The function downloads parks areas with a certain extent and converts them to LineString features. Parks may break continuity in the urban structure, besides being attractive areas for pedestrians. Parameters ---------- place: string name of cities or areas in OSM: when using "OSMpolygon" please provide the name of a "relation" in OSM as an argument of "place"; when using "distance_from_address" provide an existing OSM address; when using "OSMplace" provide an OSM place name download_method: string, {"polygon", "distance_from_address", "OSMplace"} it indicates the method that should be used for downloading the data. distance: float it is used only if download_method == "distance from address" epsg: int epsg of the area considered; if None OSMNx is used for the projection min_area: double parks with an extension smaller that this parameter are disregarded Returns ------- LineString GeoDataFrame """ crs = {'init': 'epsg:' + str(epsg)} if download_method == 'distance_from_address': parks_polygon = ox.footprints_from_address(place, distance=distance, footprint_type="leisure", retain_invalid=True) elif download_method == 'OSMplace': parks_polygon = ox.footprints_from_place(place, footprint_type="leisure", retain_invalid=True) else: parks_polygon = ox.footprints_from_polygon(place, footprint_type="leisure", retain_invalid=True) parks_polygon = parks_polygon[parks_polygon.leisure == 'park'] ix_geo = parks_polygon.columns.get_loc("geometry") + 1 to_drop = [] for row in parks_polygon.itertuples(): type_geo = None try: type_geo = row[ix_geo].geom_type except: to_drop.append(row.Index) parks_polygon.drop(to_drop, axis=0, inplace=True) parks_polygon = parks_polygon.to_crs(crs) parks_polygon.area = parks_polygon.geometry.area parks_polygon = parks_polygon[parks_polygon.area >= min_area] pp = parks_polygon['geometry'].unary_union pp = polygonize_full(pp) parks = unary_union(pp).buffer(10).boundary # to simpify a bit parks = linemerge(parks) if parks.type != "LineString": features = [i for i in parks] else: features = [parks] features = [i for i in parks] df = pd.DataFrame({'geometry': features, 'type': ['park'] * len(features)}) park_barriers = gpd.GeoDataFrame(df, geometry=df['geometry'], crs=crs) return park_barriers
2: [place_name_2, list_of_n_2], 3: [place_name_3, list_of_n_3], 4: [place_name_4, list_of_n_4], 5: [place_name_5, list_of_n_5], 6: [place_name_6, list_of_n_6], 7: [place_name_7, list_of_n_7], 8: [place_name_8, list_of_n_8], 9: [place_name_9, list_of_n_9], 10: [place_name_10, list_of_n_10], 11: [place_name_11, list_of_n_11], 12: [place_name_12, list_of_n_12], 13: [place_name_13, list_of_n_13], 14: [place_name_14, list_of_n_14] } buildings = ox.footprints_from_place(places[loc][0]) try: # Fetch OSM street network from the location, only once! takes forever graph = ox.graph_from_place(places[loc][0]) area = ox.gdf_from_place(places[loc][0]) # save gdf as geojason objects area.to_file('input_data/geo/Area_' + places[loc][0].split(',')[0].replace(' ', '_') + '_MA_' + str(min_area).replace('.', '_') + '.geojson', driver='GeoJSON') print('generate: input_data/geo/Area_' + places[loc][0].split(',')[0].replace(' ', '_') + '_MA_' + str(min_area).replace('.', '_') + '.geojson') except:
print(edges.columns) try: edges = edges[['osmid', 'name', 'geometry', 'width', 'length']] except Exception: edges = edges[['osmid', 'geometry']] for col in edges.columns: edges[col] = [ x if not isinstance(x, list) else x[0] for x in edges[col] ] edges = geom_check(edges, 'LineString') edges = edges.to_crs('EPSG:32632') edges.to_file(city + '_roads.shp') print('Roads successfully written as shapefile.') # get building footprints print('Working on building footprints...') buildings = ox.footprints_from_place(PLACE) buildings = buildings[['osmid', 'building:levels', 'geometry', 'height']] buildings = geom_check(buildings, 'Polygon') for col in buildings.columns: buildings[col] = [ x if not isinstance(x, list) else x[0] for x in buildings[col] ] print(buildings.columns) buildings = buildings.to_crs('EPSG:32632') buildings.to_file(city + '_buildings.shp') print('Buildings successfully written as shapefile.')