def exportBuildings(settlements, distance): unnamed = 1 places = [] for node in settlements.nodes: locCoord = (float(node.lat), float(node.lon)) placeName = "unnamed" if "name:en" in node.tags: placeName = node.tags["name:en"] elif "name" in node.tags: placeName = node.tags["name"] else: placeName += str(unnamed) unnamed += 1 try: buildings = ox.buildings_from_point(point=locCoord, distance=distance) ox.save_gdf_shapefile(buildings, filename=placeName, folder="SHP/Buildings") places.append(placeName) except KeyError as e: print('error on ' + placeName) except: print('I got another exception') return places
def Save_OSM_gdf_proj(placename=placename, network_type= 'walk', suffix=suffix, folder=OSM_folder): """ save a projected geodataframe from a projected graphml file Parameters ---------- network_type : string what type of street network to get. Default type is pedestrain network placename: string place name suffix: string output data date folder : string where to save the shapefile, specify local folder path for OSM resource Returns ------- none """ G_proj = ox.load_graphml(filename='{studyregion}_proj_{network_type}{suffix}.graphml'.format( studyregion = placename, network_type=network_type, suffix = suffix), folder=folder) #save projected network geodataframe as shapefile (project to UTM so we can use metres as a unit when buffering) edges_proj_gdfs = ox.graph_to_gdfs(G_proj, nodes=False, edges=True, fill_edge_geometry=True) ox.save_gdf_shapefile(edges_proj_gdfs, filename='{studyregion}_proj_{network_type}{suffix}'.format( studyregion = placename, network_type=network_type, suffix = suffix), folder=folder) #show network figure fig, ax = plt.subplots(figsize=(5, 5)) ax = edges_proj_gdfs.plot(ax=ax) ax.set_axis_off() fig.suptitle('{} OSM {} street network'.format(placename, network_type), fontsize=14, fontweight='bold') plt.show()
def getChangchunReign(): city = ox.gdf_from_places( ['南关区,长春,中国', '朝阳区,长春,中国', '二道区,长春,中国', '绿园区,长春,中国', '宽城区,长春,中国']) city = ox.project_gdf(city) ox.save_gdf_shapefile(city, filename='test1') #save ox.plot_shape(city) #show
def test_gdf_shapefiles(): # test loading spatial boundaries, saving as shapefile, and plotting city = ox.gdf_from_place('Manhattan, New York City, New York, USA') city_projected = ox.project_gdf(city, to_crs={'init': 'epsg:3395'}) ox.save_gdf_shapefile(city_projected) city = ox.gdf_from_place('Manhattan, New York City, New York, USA', buffer_dist=100) ox.plot_shape(city)
def test_gdf_shapefiles(): city = ox.gdf_from_place('Manhattan, New York City, New York, USA') city_projected = ox.project_gdf(city, to_crs={'init':'epsg:3395'}) ox.save_gdf_shapefile(city_projected) city = ox.gdf_from_place('Manhattan, New York City, New York, USA', buffer_dist=100) ox.plot_shape(city)
def test_gdf_shapefiles(): # test loading spatial boundaries, saving as shapefile, and plotting city = ox.gdf_from_place('Manhattan, New York City, New York, USA') city_projected = ox.project_gdf(city, to_crs={'init':'epsg:3395'}) ox.save_gdf_shapefile(city_projected) city = ox.gdf_from_place('Manhattan, New York City, New York, USA', buffer_dist=100) ox.plot_shape(city)
def test_gdf_shapefiles(): with httmock.HTTMock(get_mock_response_content()): city = ox.gdf_from_place('Manhattan, New York City, New York, USA') city_projected = ox.project_gdf(city, to_crs={'init':'epsg:3395'}) ox.save_gdf_shapefile(city_projected) with httmock.HTTMock(get_mock_response_content()): city = ox.gdf_from_place('Manhattan, New York City, New York, USA', buffer_dist=100) ox.plot_shape(city)
def save(G, fName): """suggest different file formats""" nx.write_gml(G, fName + ".gml") # G = nx.read_gml(fName+".gml") if False: ox.save_graphml(G, filename=fName, folder=baseDir + "gis/graph/") ox.save_gdf_shapefile(graph, filename=fName + ".gdf", folder=baseDir + "gis/graph/") ox.save_graphml(G, filename=gName, folder=baseDir + "gis/graph/") G = ox.load_graphml(filename=gName, folder=baseDir + "gis/graph/") ox.save_gdf_shapefile(G, filename=fName + ".gdf", folder=baseDir + "gis/graph/") G = ox.load_graphml(filename=fName + ".gdf", folder=baseDir + "gis/graph/") nx.readwrite.write_shp(G, baseDir + "gis/graph/berlin_street") nx.write_graphml_lxml(G, baseDir + "fName" + ".graphml")
def build_gdf(places, gdf_name, location=PATH_GDF, save_gdf=True): places = [places] if isinstance(places, list) else places state = False print(f'Building GDF for {places}') try: gdf = ox.gdf_from_place(places, gdf_name=gdf_name) ox.save_gdf_shapefile(gdf, gdf_name, location) if save_gdf else None state = True except Exception as exc: print(exc) return state
def get_osm_highways_in_bounds(bounds, network_type='all', simplify=True, to_save=False, out_dir=None): """ Args: - bounds (tuple): min_x, min_y, max_x, max_y Returns: - gdf_e (geopandas DataFrame) - (Optionally) saves to a shapefile """ bbox = to_osmbbox(bounds) #north, south, east, west (ie. max_lat, min_lat, max_lon, min_lon) g = ox.graph_from_bbox(*bbox, network_type=network_type, simplify=simplify) gdf_nodes, gdf_edges = ox.graph_to_gdfs(g) # drop unnecessay columns if 'ref' in gdf_nodes.columns: gdf_nodes.drop('ref', axis=1, inplace=True) edge_cols_keep = ['geometry', 'highway','length', 'osmid'] gdf_edges = gdf_edges[edge_cols_keep] if to_save: now = now_str() out_dir = out_dir or f'/tmp/osmnx/highways/{now}' if isinstance(out_dir, str): out_dir = Path(out_dir) if not out_dir.exists(): out_dir.mkdir(parents=True) print(f'{out_dir} is created') ox.save_gdf_shapefile(gdf_edges, 'osm_edges', str(out_dir)) ox.save_gdf_shapefile(gdf_nodes, 'osm_nodes', str(out_dir)) print(f"Saved gdf_edges to {out_dir}/osm_edges") print(f"Saved gdf_nodes to {out_dir}/osm_nodes") return gdf_nodes, gdf_edges
import shapefile from json import dumps import osmnx as ox %matplotlib inline ox.config(log_console=True, use_cache=True) ox.__version__ place = 'Piedmont, California, USA' gdf = ox.gdf_from_place(place) gdf.loc[0, 'geometry'] ox.save_gdf_shapefile(gdf, filename='place-shape2', folder='data') # read the shapefile reader = shapefile.Reader("data/place-shape2/place-shape2.shp") fields = reader.fields[1:] field_names = [field[0] for field in fields] buffer = [] for sr in reader.shapeRecords(): atr = dict(zip(field_names, sr.record)) geom = sr.shape.__geo_interface__ buffer.append(dict(type="Feature", \ geometry=geom, properties=atr)) # write the GeoJSON file geojson = open("pyshp-demo.json", "w") geojson.write(dumps({"type": "FeatureCollection", "features": buffer}, indent=2) + "\n") geojson.close()
start_0 = time.time() # Create and check the path path = 'data/bikes_streets/{}/'.format(name) assure_path_exists(path) path_simple = 'data/bikes_streets/{}/simple/'.format(name) assure_path_exists(path_simple) print('Starting with: {}'.format(name)) # Download the shape city_shape = area(city) city_shape = ox.project_gdf(city_shape) ox.save_gdf_shapefile(city_shape, filename='{}_shape'.format(name), folder=path) print('Saved') ox.plot_shape(city_shape) # Drive ''' G_drive = get_network(city, n_type='drive') ox.save_graphml(G_drive, filename='{}_drive.graphml'.format(name), folder=path) print('{} Drive downloaded and saved. Elapsed time {} s\nSimplifying the network...'.format( name, round(time.time()-start_0, 2))) G_simple = simplify_graph(G_drive) nx.write_edgelist(G_simple, path=path_simple+'{}_drive_simple.txt'.format(name)) print('{} Drive simplified and saved. Elapsed time {} s'.format( name, round(time.time()-start_0, 2)))
import osmnx as ox, geopandas as gpd ox.config(log_file=True, log_console=True, use_cache=True) location_point = (-17.1010286, 145.7753749) gdf = ox.buildings_from_point(point=location_point, distance=5000) gdf_proj = ox.project_gdf(gdf) bbox = ox.bbox_from_point(point=location_point, distance=5000, project_utm=True) fig, ax = ox.plot_buildings(gdf_proj) import osmnx as ox, geopandas as gpd ox.config(log_file=True, log_console=True, use_cache=True) place_names = ['Gordonvale, Queensland, Australia'] east_bay = ox.gdf_from_places(place_names) ox.save_gdf_shapefile(east_bay) east_bay = ox.project_gdf(east_bay) fig, ax = ox.plot_shape(east_bay) import osmnx as ox ox.config(log_file=True, log_console=True, use_cache=True) city = ox.gdf_from_place('Sydney, New South Wales, Australia') city ox.save_gdf_shapefile(city) city = ox.project_gdf(city) fig, ax = ox.plot_shape(city) import osmnx as ox from IPython.display import Image ox.config(log_console=True, use_cache=True)
import osmnx as ox %matplotlib inline ox.config(log_console=True, use_cache=True) ox.__version__ ##Area by lat/long points listed below: north, south, east, west = 40.0680, 40.0135, -111.7046, -111.7771 ''' place = 'Payson, Utah, USA' gdf = ox.gdf_from_place(place) #gdf.loc[0, 'geometry'] ox.save_gdf_shapefile(gdf, filename='place-shape2', folder='data')''' print('hi') B = ox.graph_from_bbox(north, south, east, west, network_type='drive') ox.save_graph_shapefile(B, filename='the-place', folder='data') print('hi hi') '''B = ox.graph_from_bbox(north, south, east, west, network_type='drive') gdf_nodes, gdf_edges = ox.graph_to_gdfs( B, nodes=True, edges=True, node_geometry=True, fill_edge_geometry=True) ox.save_gdf_shapefile(gdf_nodes, filename='the_places_nodes', folder='data') ox.save_gdf_shapefile(gdf_edges, filename='the_places_edges', folder='data')
# coding: utf-8 # In[5]: import osmnx as ox city = ox.gdf_from_place('Berkeley, California') ox.plot_shape(ox.project_gdf(city)) # In[10]: places = ox.gdf_from_places(['Botswana', 'Zambia', 'Zimbabwe']) places = ox.project_gdf(places) ox.save_gdf_shapefile(places) ox.plot_shape(ox.project_gdf(places)) # In[11]: G = ox.graph_from_bbox(37.79, 37.78, -122.41, -122.43, network_type='drive') G_projected = ox.project_graph(G) ox.plot_graph(G_projected) # In[12]: G = ox.graph_from_point((37.79, -122.41), distance=750, network_type='all')
for city_polygon,city_name,e,w,n,s in zip(dongSanSheng['geometry'],dongSanSheng['place_name'],dongSanSheng['bbox_east'],dongSanSheng['bbox_west'],dongSanSheng['bbox_north'],dongSanSheng['bbox_south']): placeName = city_name.split(',')[0] placeName = placeName.split('/') print(placeName) if len(placeName) > 1 and placeName[1].strip() == '白山市': placeName = '白山市' else: placeName = placeName[0].strip() if isinstance(city_polygon, (Polygon, MultiPolygon)): x = (float(e) + float(w))/2 y = (float(n) + float(s))/2 city_point_df = GeoDataFrame([{'geometry' : Point(x, y).buffer(0.6)}]) city_point_polygon = city_point_df['geometry'][0] print(placeName) dongSanSheng_city_byPoint = GeoDataFrame([{'place_name':placeName, 'geometry':city_point_polygon}]) objectName = placeName + '坐标圆001' ox.save_gdf_shapefile(dongSanSheng_city_byPoint,filename=objectName, folder='osmnx_data/city/') filename1 = 'osmnx_data/city/'+objectName+'/'+placeName+'坐标圆001.shp' filename2 = 'osmnx_data/city/shp/'+placeName+'001.shp' elif isinstance(city_polygon, Point): city_point_df = GeoDataFrame([{'geometry' : city_polygon.buffer(0.6)}]) city_point_polygon = city_point_df['geometry'][0] print(placeName) dongSanSheng_city_byPoint = GeoDataFrame([{'place_name':placeName, 'geometry':city_point_polygon}]) objectName = placeName + '坐标圆001' ox.save_gdf_shapefile(dongSanSheng_city_byPoint,filename=objectName, folder='osmnx_data/city/') filename1 = 'osmnx_data/city/'+objectName+'/'+placeName+'坐标圆001.shp' filename2 = 'osmnx_data/city/shp/'+placeName+'001.shp' else: print('wrong datatype!')
nx.write_shp(g, baseDir + "gis/graph/network_de") pg = nx.to_pandas_edgelist(g) pg = gpd.GeoDataFrame(pg) pg.to_file(baseDir + "gis/graph/network_de.geojson") nx.set_edge_attributes(graph, keys) nx.write_shp(graph, baseDir + "gis/graph/") nx.write_gml(graph, baseDir + "gis/graph/network_de") nx.write_gexf(g, baseDir + "gis/graph/network_de.gexf") ox.save_graphml(graph, filename="routes_germany.graphml", folder=baseDir + "gis/graph/") ox.save_gdf_shapefile(graph, filename="routes_germany.gdf", folder=baseDir + "gis/graph/") pg = nx.to_pandas_edgelist(graph) pg = gpd.GeoDataFrame(pg) pg.to_file(baseDir + "gis/graph/network_de.geojson") pg.to_file(baseDir + "gis/graph/network_de.shp", driver='ESRI Shapefile') print(fiona.supported_drivers) zipN = pd.read_csv(baseDir + 'gis/graph/zip_node.csv') nodeG = list(graph.nodes(data=True)) nodeD = pd.DataFrame(nodeG) nodeL = [] for g in nodeG: kDict = g[1] kDict['id'] = g[0]
import networkx as nx import numpy as np import pandas as pd import matplotlib.cm as cm # filter residential and unclassified roads road filter = ( '["highway"!~"residential|unclassified|living_street|track|abandoned|path|footway|service|pedestrian|road|' 'raceway|cycleway|steps|construction"]') Catania = ox.graph_from_address('Catania, Italy', distance=20000, network_type='drive', custom_filter=filter) # check what is inside the edges (type of roads) edge_file = Catania.edges(keys=True, data=True) ox.plot_graph(Catania) Catania_shp = ox.gdf_from_place('Catania, Italy') ox.save_gdf_shapefile(Catania_shp) # save street network as GraphML file # Catania_projected = ox.project_graph(Catania) # ox.save_graphml(Catania_projected, filename='network_Catania_20km_epgs4326.graphml') ox.save_graphml(Catania, filename='network_Catania_20km_epgs4326.graphml') # save street network as ESRI shapefile (includes NODES and EDGES) # ox.save_graph_shapefile(Catania_projected, filename='networkCatania_20km__shape') ox.save_graph_shapefile(Catania, filename='networkCatania_20km__shape')
place_names = ['Ho Chi Minh City, Vietnam', #'Beijing, China', #'Jakarta, Indonesia', 'London, UK', 'Los Angeles, California, USA', 'Manila, Philippines', #'Mexico City, Mexico', 'New Delhi, India', 'Sao Paulo, Brazil', 'New York, New York, USA', 'Seoul', 'Singapore', #'Tokyo, Japan', #'Nairobi, Kenya', #'Bangalore, India' ] # In this for-loop, we save all the shapefiles for the valid cities. for city in place_names: city_admin_20kmbuff = ox.gdf_from_place(city, gdf_name = 'global_cities', buffer_dist = 20000) fig, ax = ox.plot_shape(city_admin_20kmbuff) ox.save_gdf_shapefile(city_admin_20kmbuff, filename = city) # In this for-loop, we save all the street networks for the valid cities. for city in place_names: grid = ox.graph_from_place(city, network_type = 'drive', retain_all = True) grid_projected = ox.project_graph(grid) ox.save_graph_shapefile(grid_projected, filename = city + '_grid') ox.plot_graph(grid_projected)
@file: osmnxTest.py @time: 2018/05/{DAY} 描述: """ import osmnx as ox from folium import folium ox.config(log_file=True, log_console=True, use_cache=True) # from some place name, create a GeoDataFrame containing the geometry of the place city = ox.gdf_from_place('雁塔区, 西安, 中国') #lding = ox.buildings.buildings_from_place('陕西省, 中国') # save the retrieved data as a shapefile ox.save_gdf_shapefile(city, u'雁塔区', r'./data/') #Get building footprints within the boundaries of some place. aaaBuilding = ox.buildings.buildings_from_place(place=u'雁塔区, 西安, 中国') #gdf = ox.buildings_from_place(place='Piedmont, California, USA') ox.save_gdf_shapefile(aaaBuilding, 'test', r'./data/') #Get building footprints within some distance north, south, east, and west of an address. #address_rect = ox.buildings.buildings_from_address('钟楼, 南大街, 南院门街道, 碑林区, 西安市, 陕西省, 710001, 中国', 3000) point_rect = ox.buildings.buildings_from_point((34.374944, 107.129382), 300, retain_invalid=False) #Get building footprints within some distance north, south, east, and west of a lat-long point. ox.plot_shape(point_rect)
# 8. STORE RESULTS TO DISK edges = generate_xtra_conveyal_tags(edges) # project the edges back to lat/lon coordinate system edges = edges.to_crs('EPSG:4326') if save_as == 'shp': out_path = os.path.join(data_dir, out_fname) # turn the edges back to a graph to save as shapefile logger.info('Saving graph as shapefile. This might take a while...') nodes.gdf_name = 'nodes' # ox.save_gdf_shapefile(nodes, 'nodes', out_path) edges.gdf_name = 'edges' ox.save_gdf_shapefile(edges, 'edges', out_path) elif save_as in ['osm', 'pbf']: logger.info('Saving graph as OSM XML. This might take a while...') ox.save_as_osm([nodes, edges], filename=out_fname + '.osm', folder=data_dir, node_tags=ox.settings.osm_xml_node_tags, node_attrs=ox.settings.osm_xml_node_attrs, edge_tags=ox.settings.osm_xml_way_tags, edge_attrs=ox.settings.osm_xml_way_attrs, merge_edges=False) if save_as == 'pbf': logger.info('Converting OSM XML to .pbf') os.system("osmconvert {0}.osm -o={0}.osm.pbf".format(
#Load package #################################################### import osmnx as ox, geopandas as gpd import os, sys #os.chdir('./BRInE/') #Uncomment if running from Atom from IPython.display import Image ox.config(log_file=True,log_console=True,use_cache=True) # Define functions ############################################### def make_plot(place, point, network_type='drive', bldg_color='orange', dpi=250,dist=1000, default_width=4, street_widths=None): gdf=ox.buildings_from_point(point=point, distance=dist) gdf_proj=ox.project_gdf(gdf) fig, ax=ox.plot_figure_ground(point=point, dist=dist, network_type=network_type, default_width=default_width,street_widths=None, save=False, show=False, close=True) fig, ax=ox.plot_buildings(gdf_proj, fig=fig, ax=ax, color=bldg_color, set_bounds=False,save=True, show=False, close=True, filename=place, dpi=dpi) clArguments=sys.argv[1:] # Location ####################################################### locCoord=(float(clArguments[1]),float(clArguments[2])) placeName=clArguments[0] distance=int(clArguments[3]) size=1000 # Buildings ###################################################### buildings=ox.buildings_from_point(point=locCoord,distance=distance) ox.save_gdf_shapefile(buildings,filename=placeName,folder="SHP/Buildings") # Roads ########################################################## #roads=ox.graph_from_point(locCoord,distance=distance,network_type='drive') #ox.save_graphml(roads,filename=placeName,folder="NTW/Roads") #ox.save_graph_shapefile(roads, filename=placeName,folder="SHP/Roads") # Image ########################################################## # make_plot(placeName,locCoord,dpi=500,dist=distance,default_width=1)
place_name = 'Dakar, Senegal' G = ox.graph_from_place(place_name, which_result=3) G_proj = ox.project_graph(G, to_crs=CRS("EPSG:3857")) nodes_proj, edges_proj = ox.graph_to_gdfs(G_proj) ox.save_graph_shapefile(G_proj, filename='graph') #retrieve saved graph and use (if necessary) edges_saved = "data/graph/edges/edges.shp" nodes_saved = "data/graph/nodes/nodes.shp" edges_proj = gpd.read_file(edges_saved) nodes_proj = gpd.read_file(nodes_saved) ###get boundary of study area boundary = ox.gdf_from_place(place_name, which_result=3) boundary_proj = boundary.to_crs(edges_proj.crs) ox.save_gdf_shapefile(boundary_proj, filename='boundary') boundary_saved = "data/boundary/boundary.shp" boundary_proj = gpd.read_file(boundary_saved) ###points-of-interest of study area ### hospitals hospitals = ox.pois_from_place(place_name, which_result=3, amenities=['hospital'])[[ 'osmid', 'geometry', 'amenity', 'name', 'element_type' ]] hospitals = hospitals[hospitals.element_type == 'way']
# walk - get all streets and paths that pedestrians can use (this network type ignores one-way directionality) # bike - get all streets and paths that cyclists can use # all - download all non-private OSM streets and paths # all_private - download all OSM streets and paths, including private-access ones basic_stats = ox.basic_stats(B) print(basic_stats['circuity_avg']) # ans: [1.1167206203103612] # In this street network, the streets are 12% more circuitous than the straight-lines paths would be. extended_stats = ox.extended_stats(B) print(extended_stats['pagerank_max_node']) # Create place boundary shapefiles from OpenStreetMap Bracciano_shp = ox.gdf_from_place('Bracciano, Italy') ox.save_gdf_shapefile(Bracciano_shp) # using NetworkX to calculate the shortest path between two random nodes route = nx.shortest_path(B, np.random.choice(B.nodes), np.random.choice(B.nodes)) ox.plot_graph_route(B, route, fig_height=10, fig_width=10) # save street network as GraphML file B_projected = ox.project_graph(B) ox.save_graphml(B_projected, filename='network_Bracciano_6km_epgs4326.graphml') # save street network as ESRI shapefile (includes NODES and EDGES) ox.save_graph_shapefile(B_projected, filename='networkBracciano-shape') #street network from bounding box #G = ox.graph_from_bbox(42.2511, 41.3860, 11.6586, 13.4578, network_type='drive_service')
ox.plot_graph(ox.graph_from_place('Houston, Texas, USA')) plt.savefig("ss.jpg") plt.plot() # %% import geopandas as gpd os.environ["PROJ_LIB"] = r"C:\Users\User\Anaconda3\Library\share" import matplotlib.pyplot as plt import osmnx as ox ox.config(log_console=True, use_cache=True) print(ox.__version__) city = ox.gdf_from_place('Manhattan Island, New York, New York, USA') ox.save_gdf_shapefile(city) city = ox.project_gdf(city) fig, ax = ox.plot_shape(city, figsize=(3, 3)) plt.plot() # %% http://ericmjl.github.io/Network-Analysis-Made-Simple/2-networkx-basics-instructor.html out = pd.read_csv(os.path.join("two_component", "moreno_seventh", "out.moreno_seventh_seventh"), skiprows=2, sep=" ", names=["from", "to", "w"]) with open( os.path.join("two_component", "moreno_seventh", "ent.moreno_seventh_seventh.student.gender")) as f: gender = f.read() gender = gender.split("\n")