Merge pull request #57 from sacovo/patch-2

catch empty response instead of failing
This commit is contained in:
Marcelo de Oliveira Rosa Prates 2021-09-13 16:45:46 -03:00 committed by GitHub
commit 18c4dd704c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -6,6 +6,7 @@ from xmlrpc.client import Boolean
import osmnx as ox import osmnx as ox
from osmnx import utils_geo from osmnx import utils_geo
from osmnx._errors import EmptyOverpassResponse
import numpy as np import numpy as np
from shapely.geometry import Point, Polygon, MultiPolygon, LineString, MultiLineString from shapely.geometry import Point, Polygon, MultiPolygon, LineString, MultiLineString
from shapely.ops import unary_union from shapely.ops import unary_union
@ -266,35 +267,38 @@ def get_streets(
# Boundary defined by polygon (perimeter) # Boundary defined by polygon (perimeter)
if perimeter is not None: if perimeter is not None:
# Fetch streets data, project & convert to GDF # Fetch streets data, project & convert to GDF
streets = ox.graph_from_polygon( try:
unary_union(perimeter.to_crs(3174).buffer(buffer).to_crs(4326).geometry) streets = ox.graph_from_polygon(
if buffer > 0 unary_union(perimeter.geometry).buffer(buffer)
else unary_union(perimeter.geometry), if buffer > 0
retain_all=retain_all, else unary_union(perimeter.geometry),
custom_filter=custom_filter, custom_filter=custom_filter,
) )
streets = ox.project_graph(streets) streets = ox.project_graph(streets)
streets = ox.graph_to_gdfs(streets, nodes=False) streets = ox.graph_to_gdfs(streets, nodes=False)
perimeter = unary_union(ox.project_gdf(perimeter).geometry) except EmptyOverpassResponse:
return MultiLineString()
# Boundary defined by polygon (perimeter) # Boundary defined by polygon (perimeter)
elif (point is not None) and (radius is not None): elif (point is not None) and (radius is not None):
# Fetch streets data, save CRS & project # Fetch streets data, save CRS & project
streets = ox.graph_from_point( try:
point, streets = ox.graph_from_point(
dist=radius + dilate + buffer, point,
retain_all=retain_all, dist=radius + dilate + buffer,
custom_filter=custom_filter, retain_all=retain_all,
) custom_filter=custom_filter,
crs = ox.graph_to_gdfs(streets, nodes=False).crs )
streets = ox.project_graph(streets) crs = ox.graph_to_gdfs(streets, nodes=False).crs
# Compute perimeter from point & CRS streets = ox.project_graph(streets)
perimeter = get_boundary(point, radius, crs, circle=circle, dilate=dilate) # Compute perimeter from point & CRS
# Convert to GDF perimeter = get_boundary(point, radius, crs, circle=circle, dilate=dilate)
streets = ox.graph_to_gdfs(streets, nodes=False) # Convert to GDF
streets = ox.graph_to_gdfs(streets, nodes=False)
# Intersect with perimeter & filter empty elements # Intersect with perimeter & filter empty elements
streets.geometry = streets.geometry.intersection(perimeter) streets.geometry = streets.geometry.intersection(perimeter)
streets = streets[~streets.geometry.is_empty] streets = streets[~streets.geometry.is_empty]
except EmptyOverpassResponse:
return MultiLineString()
if type(width) == dict: if type(width) == dict:
streets = unary_union( streets = unary_union(