From 9f4df6fa7d02abab15443d3e36320b0959ca2b47 Mon Sep 17 00:00:00 2001 From: Rafa de la Torre Date: Mon, 11 Jul 2016 14:59:02 +0200 Subject: [PATCH] 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. --- server/extension/sql/80_isolines_helper.sql | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/server/extension/sql/80_isolines_helper.sql b/server/extension/sql/80_isolines_helper.sql index f04e62c..2757369 100644 --- a/server/extension/sql/80_isolines_helper.sql +++ b/server/extension/sql/80_isolines_helper.sql @@ -93,20 +93,23 @@ RETURNS SETOF cdb_dataservices_server.isoline AS $$ if isotype == 'isodistance': for r in data_range: isoline = mapzen_isolines.calculate_isodistance(origin, mode, r) - isolines[r] = (isoline) + isolines[r] = isoline elif isotype == 'isochrone': for r in data_range: isoline = mapzen_isolines.calculate_isochrone(origin, mode, r) - isolines[r] = (isoline) + isolines[r] = isoline result = [] for r in data_range: - # -- TODO encapsulate this block into a func/method - locations = isolines[r] + [ isolines[r][0] ] # close the polygon repeating the first point - wkt_coordinates = ','.join(["%f %f" % (l['lon'], l['lat']) for l in locations]) - sql = "SELECT ST_MPolyFromText('MULTIPOLYGON((({0})))', 4326) as geom".format(wkt_coordinates) - multipolygon = plpy.execute(sql, 1)[0]['geom'] + if len(isolines[r]) >= 3: + # -- TODO encapsulate this block into a func/method + locations = isolines[r] + [ isolines[r][0] ] # close the polygon repeating the first point + wkt_coordinates = ','.join(["%f %f" % (l['lon'], l['lat']) for l in locations]) + 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])