Return None/null instead of crashing

When there's no enough information to produce an isoline (less than 3
points) return a NULL multipolygon to the upper layer. This usually
happens when the matrix API returns null cost for most points in the
request.
This commit is contained in:
Rafa de la Torre
2016-07-11 14:59:02 +02:00
parent aafacc3af7
commit 9f4df6fa7d

View File

@@ -93,20 +93,23 @@ RETURNS SETOF cdb_dataservices_server.isoline AS $$
if isotype == 'isodistance': if isotype == 'isodistance':
for r in data_range: for r in data_range:
isoline = mapzen_isolines.calculate_isodistance(origin, mode, r) isoline = mapzen_isolines.calculate_isodistance(origin, mode, r)
isolines[r] = (isoline) isolines[r] = isoline
elif isotype == 'isochrone': elif isotype == 'isochrone':
for r in data_range: for r in data_range:
isoline = mapzen_isolines.calculate_isochrone(origin, mode, r) isoline = mapzen_isolines.calculate_isochrone(origin, mode, r)
isolines[r] = (isoline) isolines[r] = isoline
result = [] result = []
for r in data_range: for r in data_range:
# -- TODO encapsulate this block into a func/method if len(isolines[r]) >= 3:
locations = isolines[r] + [ isolines[r][0] ] # close the polygon repeating the first point # -- TODO encapsulate this block into a func/method
wkt_coordinates = ','.join(["%f %f" % (l['lon'], l['lat']) for l in locations]) locations = isolines[r] + [ isolines[r][0] ] # close the polygon repeating the first point
sql = "SELECT ST_MPolyFromText('MULTIPOLYGON((({0})))', 4326) as geom".format(wkt_coordinates) wkt_coordinates = ','.join(["%f %f" % (l['lon'], l['lat']) for l in locations])
multipolygon = plpy.execute(sql, 1)[0]['geom'] sql = "SELECT ST_MPolyFromText('MULTIPOLYGON((({0})))', 4326) as geom".format(wkt_coordinates)
multipolygon = plpy.execute(sql, 1)[0]['geom']
else:
multipolygon = None
result.append([source, r, multipolygon]) result.append([source, r, multipolygon])