Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_osm_xml_output():
G = ox.graph_from_place('Piedmont, California, USA')
ox.save_graph_osm(G)
G = networkx.MultiGraph()
key = 0
for index, row in self.df_streets.iterrows():
first = row.geometry.coords[0]
last = row.geometry.coords[-1]
data = [row[f] for f in list(self.df_streets.columns)]
attributes = dict(zip(list(self.df_streets.columns), data))
G.add_edge(first, last, key=key, **attributes)
key += 1
nodes, edges = mm.nx_to_gdf(G)
assert len(nodes) == 29
assert len(edges) == 35
# osmnx compatibility
G = ox.graph_from_place("Preborov, Czechia", network_type="drive")
pts, lines = mm.nx_to_gdf(G)
assert len(pts) == 7
assert len(lines) == 16
# LineString Z
line1 = LineString([(0, 0, 0), (1, 1, 1)])
line2 = LineString([(0, 0, 0), (-1, -1, -1)])
gdf = gpd.GeoDataFrame(geometry=[line1, line2])
G = mm.gdf_to_nx(gdf)
pts, lines = mm.nx_to_gdf(G)
assert pts.iloc[0].geometry.wkt == "POINT Z (0 0 0)"
assert lines.iloc[0].geometry.wkt == "LINESTRING Z (0 0 0, 1 1 1)"
def test_plots():
G = ox.graph_from_place('Piedmont, California, USA', network_type='drive', simplify=False)
G2 = ox.simplify_graph(G, strict=False)
# test getting colors
co = ox.get_colors(n=5, return_hex=True)
nc = ox.get_node_colors_by_attr(G2, 'osmid')
ec = ox.get_edge_colors_by_attr(G2, 'length')
# save a plot to disk as png
fig, ax = ox.plot_graph(G, save=True, file_format='png')
# save a plot to disk as svg
G_simplified = ox.simplify_graph(G)
fig, ax = ox.plot_graph(G_simplified, show=False, save=True, close=True, file_format='svg')
G_projected = ox.project_graph(G_simplified)
fig, ax = ox.plot_graph(G_projected)
G1 = ox.graph_from_bbox(north, south, east, west, network_type='drive_service')
G1 = ox.graph_from_bbox(north, south, east, west, network_type='drive_service', truncate_by_edge=True)
# graph from point
location_point = (37.791427, -122.410018)
bbox = ox.bbox_from_point(location_point, project_utm=True)
G2 = ox.graph_from_point(location_point, distance=750, distance_type='bbox', network_type='drive')
G3 = ox.graph_from_point(location_point, distance=500, distance_type='network')
# graph from address
G4 = ox.graph_from_address(address='350 5th Ave, New York, NY', distance=1000, distance_type='network',
network_type='bike')
# graph from list of places
places = ['Los Altos, California, USA', {'city': 'Los Altos Hills', 'state': 'California'}, 'Loyola, California']
G5 = ox.graph_from_place(places, network_type='all', clean_periphery=False)
# graph from polygon
polygon = wkt.loads(
'POLYGON ((-122.418083 37.754154, -122.418082 37.766028, -122.410909 37.766028, -122.410908 37.754154, -122.418083 37.754154))')
G6 = ox.graph_from_polygon(polygon, network_type='walk')
# test custom query filter
filtr = ('["area"!~"yes"]'
'["highway"!~"motor|proposed|construction|abandoned|platform|raceway"]'
'["foot"!~"no"]'
'["service"!~"private"]'
'["access"!~"private"]')
G = ox.graph_from_point(location_point, network_type='walk', custom_filter=filtr)
def test_overpass():
import pytest
# Test changing the endpoint. This should fail because we didn't provide a valid endpoint
ox.config(
overpass_endpoint="http://NOT_A_VALID_ENDPOINT/api/"
)
with pytest.raises(Exception):
G = ox.graph_from_place('Piedmont, California, USA')
ox.config(overpass_endpoint="http://overpass-api.de/api")
def test_network_saving_loading():
# save/load graph as shapefile and graphml file
G = ox.graph_from_place('Piedmont, California, USA')
G_projected = ox.project_graph(G)
ox.save_graph_shapefile(G_projected)
ox.save_graphml(G_projected)
ox.save_graphml(G_projected, filename='gephi.graphml', gephi=True)
G2 = ox.load_graphml('graph.graphml')
G3 = ox.load_graphml('graph.graphml', node_type=str)
# convert graph to node/edge GeoDataFrames and back again
gdf_nodes, gdf_edges = ox.graph_to_gdfs(G, nodes=True, edges=True, node_geometry=True, fill_edge_geometry=True)
G4 = ox.gdfs_to_graph(gdf_nodes, gdf_edges)
# find graph nodes nearest to some set of points
X = gdf_nodes['x'].head()
Y = gdf_nodes['y'].head()
nn1 = ox.get_nearest_nodes(G, X, Y)
nn2 = ox.get_nearest_nodes(G, X, Y, method='kdtree')
import osmnx as ox
G = ox.graph_from_place('Bay Saint Louis, MS , USA', network_type='drive')
stats = ox.basic_stats(G)
print(stats["street_length_avg"])