catch empty response instead of failing

If a street is present in the layers with a custom filter that does not
exist in the specified area, osmnx throws an exception and the whole
plotting fails. I suggest catching the exception and just returning an
empty `MultiLineString`. Maybe a warning could also be added, to inform
that the requested street is not present in the result.
This commit is contained in:
Sandro Covo
2021-09-07 09:03:43 +02:00
parent 560ecf95ca
commit ca33f516b9

View File

@@ -2,6 +2,7 @@ from functools import reduce
import osmnx as ox
from osmnx import utils_geo
from osmnx._errors import EmptyOverpassResponse
import numpy as np
from shapely.geometry import Point, Polygon, MultiPolygon, MultiLineString
from shapely.ops import unary_union
@@ -166,6 +167,7 @@ def get_streets(
# Boundary defined by polygon (perimeter)
if perimeter is not None:
# Fetch streets data, project & convert to GDF
try:
streets = ox.graph_from_polygon(
unary_union(perimeter.geometry).buffer(buffer)
if buffer > 0
@@ -174,9 +176,12 @@ def get_streets(
)
streets = ox.project_graph(streets)
streets = ox.graph_to_gdfs(streets, nodes=False)
except EmptyOverpassResponse:
return MultiLineString()
# Boundary defined by polygon (perimeter)
elif (point is not None) and (radius is not None):
# Fetch streets data, save CRS & project
try:
streets = ox.graph_from_point(
point,
dist=radius + dilate + buffer,
@@ -192,6 +197,8 @@ def get_streets(
# Intersect with perimeter & filter empty elements
streets.geometry = streets.geometry.intersection(perimeter)
streets = streets[~streets.geometry.is_empty]
except EmptyOverpassResponse:
return MultiLineString()
if type(width) == dict:
streets = unary_union(