Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# caller passed in
if memory is None:
maxsize = ''
else:
maxsize = '[maxsize:{}]'.format(memory)
# define the query to send the API
if by_bbox:
# turn bbox into a polygon and project to local UTM
polygon = Polygon([(west, south), (east, south), (east, north), (west, north)])
geometry_proj, crs_proj = project_geometry(polygon)
# subdivide it if it exceeds the max area size (in meters), then project
# back to lat-long
geometry_proj_consolidated_subdivided = consolidate_subdivide_geometry(geometry_proj, max_query_area_size=max_query_area_size)
geometry, _ = project_geometry(geometry_proj_consolidated_subdivided, crs=crs_proj, to_latlong=True)
log('Requesting building footprints data within bounding box from API in {:,} request(s)'.format(len(geometry)))
start_time = time.time()
# loop through each polygon rectangle in the geometry (there will only
# be one if original bbox didn't exceed max area size)
for poly in geometry:
# represent bbox as south,west,north,east and round lat-longs to 8
# decimal places (ie, within 1 mm) so URL strings aren't different
# due to float rounding issues (for consistent caching)
west, south, east, north = poly.bounds
query_template = ('[out:json][timeout:{timeout}]{maxsize};((way["building"]({south:.8f},'
'{west:.8f},{north:.8f},{east:.8f});(._;>;););(relation["building"]'
'({south:.8f},{west:.8f},{north:.8f},{east:.8f});(._;>;);););out;')
query_str = query_template.format(north=north, south=south, east=east, west=west, timeout=timeout, maxsize=maxsize)
response_json = overpass_request(data={'data':query_str}, timeout=timeout)
response_jsons.append(response_json)
# verify that the geometry is valid and is a shapely Polygon/MultiPolygon
# before proceeding
if not polygon.is_valid:
raise TypeError('Shape does not have a valid geometry')
if not isinstance(polygon, (Polygon, MultiPolygon)):
raise TypeError('Geometry must be a shapely Polygon or MultiPolygon. If you requested '
'graph from place name or address, make sure your query resolves to a '
'Polygon or MultiPolygon, and not some other geometry, like a Point. '
'See OSMnx documentation for details.')
if clean_periphery and simplify:
# create a new buffered polygon 0.5km around the desired one
buffer_dist = 500
polygon_utm, crs_utm = project_geometry(geometry=polygon)
polygon_proj_buff = polygon_utm.buffer(buffer_dist)
polygon_buffered, _ = project_geometry(geometry=polygon_proj_buff, crs=crs_utm, to_latlong=True)
# get the network data from OSM, create the buffered graph, then
# truncate it to the buffered polygon
response_jsons = osm_net_download(polygon=polygon_buffered, network_type=network_type,
timeout=timeout, memory=memory,
max_query_area_size=max_query_area_size,
infrastructure=infrastructure, custom_filter=custom_filter)
G_buffered = create_graph(response_jsons, name=name, retain_all=True,
bidirectional=network_type in settings.bidirectional_network_types)
G_buffered = truncate_graph_polygon(G_buffered, polygon_buffered, retain_all=True, truncate_by_edge=truncate_by_edge)
# simplify the graph topology
G_buffered = simplify_graph(G_buffered)
# truncate graph by polygon to return the graph within the polygon that
# caller wants. don't simplify again - this allows us to retain
north, south, east, west, crs_proj : tuple, if return_crs=True
"""
# reverse the order of the (lat,lng) point so it is (x,y) for shapely, then
# project to UTM and buffer in meters
lat, lng = point
point_proj, crs_proj = project_geometry(Point((lng, lat)))
buffer_proj = point_proj.buffer(distance)
if project_utm:
west, south, east, north = buffer_proj.bounds
log('Created bounding box {} meters in each direction from {} and projected it: {},{},{},{}'.format(distance, point, north, south, east, west))
else:
# if project_utm is False, project back to lat-long then get the
# bounding coordinates
buffer_latlong, _ = project_geometry(buffer_proj, crs=crs_proj, to_latlong=True)
west, south, east, north = buffer_latlong.bounds
log('Created bounding box {} meters in each direction from {}: {},{},{},{}'.format(distance, point, north, south, east, west))
if return_crs:
return north, south, east, west, crs_proj
else:
return north, south, east, west
response_jsons = []
# pass server memory allocation in bytes for the query to the API
# if None, pass nothing so the server will use its default allocation size
# otherwise, define the query's maxsize parameter value as whatever the
# caller passed in
if memory is None:
maxsize = ''
else:
maxsize = '[maxsize:{}]'.format(memory)
# define the query to send the API
if by_bbox:
# turn bbox into a polygon and project to local UTM
polygon = Polygon([(west, south), (east, south), (east, north), (west, north)])
geometry_proj, crs_proj = project_geometry(polygon)
# subdivide it if it exceeds the max area size (in meters), then project
# back to lat-long
geometry_proj_consolidated_subdivided = consolidate_subdivide_geometry(geometry_proj, max_query_area_size=max_query_area_size)
geometry, _ = project_geometry(geometry_proj_consolidated_subdivided, crs=crs_proj, to_latlong=True)
log('Requesting building footprints data within bounding box from API in {:,} request(s)'.format(len(geometry)))
start_time = time.time()
# loop through each polygon rectangle in the geometry (there will only
# be one if original bbox didn't exceed max area size)
for poly in geometry:
# represent bbox as south,west,north,east and round lat-longs to 8
# decimal places (ie, within 1 mm) so URL strings aren't different
# due to float rounding issues (for consistent caching)
west, south, east, north = poly.bounds
query_template = ('[out:json][timeout:{timeout}]{maxsize};((way["building"]({south:.8f},'
response_jsons = []
# pass server memory allocation in bytes for the query to the API
# if None, pass nothing so the server will use its default allocation size
# otherwise, define the query's maxsize parameter value as whatever the
# caller passed in
if memory is None:
maxsize = ''
else:
maxsize = '[maxsize:{}]'.format(memory)
# define the query to send the API
if by_bbox:
# turn bbox into a polygon and project to local UTM
polygon = Polygon([(west, south), (east, south), (east, north), (west, north)])
geometry_proj, crs_proj = project_geometry(polygon)
# subdivide it if it exceeds the max area size (in meters), then project
# back to lat-long
geometry_proj_consolidated_subdivided = consolidate_subdivide_geometry(geometry_proj, max_query_area_size=max_query_area_size)
geometry, _ = project_geometry(geometry_proj_consolidated_subdivided, crs=crs_proj, to_latlong=True)
log('Requesting footprints data within bounding box from API in {:,} request(s)'.format(len(geometry)))
start_time = time.time()
# loop through each polygon rectangle in the geometry (there will only
# be one if original bbox didn't exceed max area size)
for poly in geometry:
# represent bbox as south,west,north,east and round lat-longs to 8
# decimal places (ie, within 1 mm) so URL strings aren't different
# due to float rounding issues (for consistent caching)
west, south, east, north = poly.bounds
query_template = ('[out:json][timeout:{timeout}]{maxsize};'
each be from the point
project_utm : bool
if True return bbox as UTM coordinates
return_crs : bool
if True and project_utm=True, return the projected CRS
Returns
-------
north, south, east, west : tuple, if return_crs=False
north, south, east, west, crs_proj : tuple, if return_crs=True
"""
# reverse the order of the (lat,lng) point so it is (x,y) for shapely, then
# project to UTM and buffer in meters
lat, lng = point
point_proj, crs_proj = project_geometry(Point((lng, lat)))
buffer_proj = point_proj.buffer(distance)
if project_utm:
west, south, east, north = buffer_proj.bounds
log('Created bounding box {} meters in each direction from {} and projected it: {},{},{},{}'.format(distance, point, north, south, east, west))
else:
# if project_utm is False, project back to lat-long then get the
# bounding coordinates
buffer_latlong, _ = project_geometry(buffer_proj, crs=crs_proj, to_latlong=True)
west, south, east, north = buffer_latlong.bounds
log('Created bounding box {} meters in each direction from {}: {},{},{},{}'.format(distance, point, north, south, east, west))
if return_crs:
return north, south, east, west, crs_proj
else:
return north, south, east, west
"""
# verify that the geometry is valid and is a shapely Polygon/MultiPolygon
# before proceeding
if not polygon.is_valid:
raise TypeError('Shape does not have a valid geometry')
if not isinstance(polygon, (Polygon, MultiPolygon)):
raise TypeError('Geometry must be a shapely Polygon or MultiPolygon. If you requested '
'graph from place name or address, make sure your query resolves to a '
'Polygon or MultiPolygon, and not some other geometry, like a Point. '
'See OSMnx documentation for details.')
if clean_periphery and simplify:
# create a new buffered polygon 0.5km around the desired one
buffer_dist = 500
polygon_utm, crs_utm = project_geometry(geometry=polygon)
polygon_proj_buff = polygon_utm.buffer(buffer_dist)
polygon_buffered, _ = project_geometry(geometry=polygon_proj_buff, crs=crs_utm, to_latlong=True)
# get the network data from OSM, create the buffered graph, then
# truncate it to the buffered polygon
response_jsons = osm_net_download(polygon=polygon_buffered, network_type=network_type,
timeout=timeout, memory=memory,
max_query_area_size=max_query_area_size,
infrastructure=infrastructure, custom_filter=custom_filter)
G_buffered = create_graph(response_jsons, name=name, retain_all=True,
bidirectional=network_type in settings.bidirectional_network_types)
G_buffered = truncate_graph_polygon(G_buffered, polygon_buffered, retain_all=True, truncate_by_edge=truncate_by_edge)
# simplify the graph topology
G_buffered = simplify_graph(G_buffered)
'(relation["{footprint_type}"]({south:.8f},{west:.8f},{north:.8f},{east:.8f});'
'(._;>;);););out;')
query_str = query_template.format(north=north, south=south, east=east, west=west, timeout=timeout,
maxsize=maxsize, footprint_type=footprint_type)
response_json = overpass_request(data={'data':query_str}, timeout=timeout)
response_jsons.append(response_json)
msg = ('Got all footprint data within bounding box from '
'API in {:,} request(s) and {:,.2f} seconds')
log(msg.format(len(geometry), time.time()-start_time))
elif by_poly:
# project to utm, divide polygon up into sub-polygons if area exceeds a
# max size (in meters), project back to lat-long, then get a list of polygon(s) exterior coordinates
geometry_proj, crs_proj = project_geometry(polygon)
geometry_proj_consolidated_subdivided = consolidate_subdivide_geometry(geometry_proj, max_query_area_size=max_query_area_size)
geometry, _ = project_geometry(geometry_proj_consolidated_subdivided, crs=crs_proj, to_latlong=True)
polygon_coord_strs = get_polygons_coordinates(geometry)
log('Requesting footprint data within polygon from API in {:,} request(s)'.format(len(polygon_coord_strs)))
start_time = time.time()
# pass each polygon exterior coordinates in the list to the API, one at
# a time
for polygon_coord_str in polygon_coord_strs:
query_template = ('[out:json][timeout:{timeout}]{maxsize};('
'way(poly:"{polygon}")["{footprint_type}"];(._;>;);'
'relation(poly:"{polygon}")["{footprint_type}"];(._;>;););out;')
query_str = query_template.format(polygon=polygon_coord_str, timeout=timeout, maxsize=maxsize,
footprint_type=footprint_type)
response_json = overpass_request(data={'data':query_str}, timeout=timeout)
response_jsons.append(response_json)
msg = ('Got all footprint data within polygon from API in '
'{:,} request(s) and {:,.2f} seconds')
infrastructure : string
download infrastructure of given type (default is streets (ie, 'way["highway"]') but other
infrastructures may be selected like power grids (ie, 'way["power"~"line"]'))
custom_filter : string
a custom network filter to be used instead of the network_type presets
Returns
-------
networkx multidigraph
"""
if clean_periphery and simplify:
# create a new buffered bbox 0.5km around the desired one
buffer_dist = 500
polygon = Polygon([(west, north), (west, south), (east, south), (east, north)])
polygon_utm, crs_utm = project_geometry(geometry=polygon)
polygon_proj_buff = polygon_utm.buffer(buffer_dist)
polygon_buff, _ = project_geometry(geometry=polygon_proj_buff, crs=crs_utm, to_latlong=True)
west_buffered, south_buffered, east_buffered, north_buffered = polygon_buff.bounds
# get the network data from OSM then create the graph
response_jsons = osm_net_download(north=north_buffered, south=south_buffered,
east=east_buffered, west=west_buffered,
network_type=network_type, timeout=timeout,
memory=memory, max_query_area_size=max_query_area_size,
infrastructure=infrastructure, custom_filter=custom_filter)
G_buffered = create_graph(response_jsons, name=name, retain_all=retain_all,
bidirectional=network_type in settings.bidirectional_network_types)
G = truncate_graph_bbox(G_buffered, north, south, east, west, retain_all=True, truncate_by_edge=truncate_by_edge)
# simplify the graph topology
G_buffered = simplify_graph(G_buffered)