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_get_network_methods():
from shapely import wkt
# graph from bounding box
north, south, east, west = 37.79, 37.78, -122.41, -122.43
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"]'
# 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 get_network(distance = 1000, center = (54.97351, -1.62545)):
network_pickle_filename = "tests/data/test_network_USB_{}.pkl".format(distance)
if os.path.exists(network_pickle_filename):
network = nx.read_gpickle(path = network_pickle_filename)
else:
network = ox.graph_from_point(
center_point = center,
distance = distance, #meters
distance_type='bbox',
network_type="drive_service")
nx.write_gpickle(G = network, path = network_pickle_filename)
return network
def get_network(distance = 1000, center = (54.97351, -1.62545)):
network_pickle_filename = "tests/data/test_network_USB_{}.pkl".format(distance)
if os.path.exists(network_pickle_filename):
network = nx.read_gpickle(path = network_pickle_filename)
else:
network = ox.graph_from_point(
center_point = center,
distance = distance, #meters
distance_type='bbox',
network_type="drive_service")
nx.write_gpickle(G = network, path = network_pickle_filename)
return network
def test_stats():
# create graph, add bearings, project it
location_point = (37.791427, -122.410018)
G = ox.graph_from_point(location_point, distance=500, distance_type='network')
G = ox.add_edge_bearings(G)
G_proj = ox.project_graph(G)
# calculate stats
stats1 = ox.basic_stats(G)
stats2 = ox.basic_stats(G, area=1000)
stats3 = ox.basic_stats(G_proj, area=1000, clean_intersects=True, tolerance=15, circuity_dist='euclidean')
try:
stats4 = ox.extended_stats(G, connectivity=True, anc=True, ecc=True, bc=True, cc=True)
except NetworkXNotImplemented as e:
warnings.warn("Testing coordinates results in a MultiDigraph, and extended stats are not available for it")
warnings.warn(e.args)
# but now we need center point in lat,lon
# points = [Point(lat,lng) for lat,lng in zip(cameras['lat'], cameras['lon'])]
lat = cameras['lat']
lon = cameras['lon']
center_lat = min(lat) + (max(lat) - min(lat))/2
center_lon = min(lon) + (max(lon) - min(lon))/2
log("Center point = {}, distance = {}"\
.format((center_lat, center_lon), length),
level = lg.INFO)
checkpoint = time.time()
G = ox.graph_from_point(
center_point = (center_lat, center_lon),
distance = length,
retain_all = retain_all,
custom_filter = osm_road_filter
)
log("Returned road graph in {:,.3f} sec"\
.format(time.time() - start_time),
level = lg.INFO)
checkpoint = time.time()
# Make sure that every edge has a geometry attribute
for u, v, data in G.edges(keys=False, data=True):
if 'geometry' not in data:
# if it doesn't have a geometry attribute, the edge is a straight
# line from node to node
if index_destin == -1:
index_destin_last = None
else:
index_destin_last = index_destin
tdf1 = tdf[index_origin: index_destin_last]
origin_coords = tuple(tdf1.iloc[index_origin][[constants.LATITUDE, constants.LONGITUDE]].values)
destin_coords = tuple(tdf1.iloc[index_destin][[constants.LATITUDE, constants.LONGITUDE]].values)
if G is None:
mid_point = tuple(np.mean(np.array([origin_coords, destin_coords]), axis=0))
# all distances from mid_point
all_dists = pd.DataFrame(tdf1[[constants.LATITUDE, constants.LONGITUDE]]).apply(
lambda x: distance(mid_point, tuple(x.values)).m, axis=1).values
max_dist = 1.1 * max(all_dists)
G = ox.graph_from_point(mid_point, distance=max_dist)
# nodes, _ = ox.graph_to_gdfs(G)
# closest points to origin and destination on graph
closest_o_i = ox.utils.get_nearest_node(G, origin_coords)
closest_d_i = ox.utils.get_nearest_node(G, destin_coords)
# find shortest path
shortest_route = ox.nx.shortest_path(G, closest_o_i, closest_d_i, weight='length')
return G, shortest_route