osm-road-length
osm-road-length copied to clipboard
Too complicated polygons
I recently widely used your package to calculate the road length of cities and entire countries. Thank you for this module, it was really helpful. However I found that when the geometry of the object is complicated so that query length is too long, Overpass API returns the error 414.
To fix that I modified a couple of functions:
in katana()
I modified a constructor and the first if
statement as follows with adding additional parameter urllen_threshold
:
def katana(geometry, threshold_func, threshold_value, count=0, urllen_threshold=7648):
if (threshold_func(geometry, threshold_value) and (len(to_overpass_coords(geometry)) < urllen_threshold)) or count == 250:
Since it doesn't guarantee that url length will be short enough before the number of recursions can be exceeded, I also modified the beginning of get()
function:
def get(geometry, threshold_value=1000000, urllen_threshold=7648):
geometries = katana(geometry, threshold_func, threshold_value, urllen_threshold=urllen_threshold)
while True:
for geo in geometries:
urllen = len(to_overpass_coords(geo))
if urllen >= urllen_threshold:
geometries.remove(geo)
geometries.extend(katana(geo, threshold_func, threshold_value, urllen_threshold=urllen_threshold))
break
else:
break
responses = []
for geo in geometries:
Note: the value 7648 was found experimentally, perhaps this value can be higher, but definitely less than 8000.