From 097e158d693d965f5b137ec3ba17d6757325f177 Mon Sep 17 00:00:00 2001 From: Alexey Zakharenkov <35913079+alexey-zakharenkov@users.noreply.github.com> Date: Fri, 23 Oct 2020 12:51:19 +0300 Subject: [PATCH] Handle case when postgis returns NaN for a region area; passing warnings from backend to frontend --- web/app/borders_api.py | 29 ++++++++++++++++++++--------- web/app/countries_structure.py | 15 ++++++++++++--- web/app/static/borders.js | 5 ++++- web/app/subregions.py | 10 +++++++--- 4 files changed, 43 insertions(+), 16 deletions(-) diff --git a/web/app/borders_api.py b/web/app/borders_api.py index d5d5150..87511e9 100755 --- a/web/app/borders_api.py +++ b/web/app/borders_api.py @@ -298,11 +298,14 @@ def split(): new_ids.append(free_id) counter += 1 free_id -= 1 + warnings = [] for border_id in new_ids: - update_border_mwm_size_estimation(g.conn, border_id) + try: + update_border_mwm_size_estimation(g.conn, border_id) + except Exception as e: + warnings.append(str(e)) g.conn.commit() - - return jsonify(status='ok') + return jsonify(status='ok', warnings=warnings) @app.route('/join') def join_borders(): @@ -442,9 +445,13 @@ def copy_from_osm(): """, (osm_id,) ) assign_region_to_lowerst_parent(osm_id) - update_border_mwm_size_estimation(g.conn, osm_id) + warnings = [] + try: + update_border_mwm_size_estimation(g.conn, osm_id) + except Exception as e: + warnings.append(str(e)) g.conn.commit() - return jsonify(status='ok') + return jsonify(status='ok', warnings=warnings) @app.route('/rename') def set_name(): @@ -864,10 +871,14 @@ def chop_largest_or_farthest(): GROUP BY name, disabled) ) x""" ) + warnings = [] for border_id in (free_id1, free_id2): - update_border_mwm_size_estimation(g.conn, border_id) + try: + update_border_mwm_size_estimation(g.conn, border_id) + except Exception as e: + warnings.append(str(e)) g.conn.commit() - return jsonify(status='ok') + return jsonify(status='ok', warnings=warnings) @app.route('/hull') def draw_hull(): @@ -1608,7 +1619,7 @@ def border(): @app.route('/start_over') def start_over(): try: - create_countries_initial_structure(g.conn) + warnings = create_countries_initial_structure(g.conn) except CountryStructureException as e: return jsonify(status=str(e)) @@ -1616,7 +1627,7 @@ def start_over(): cursor = g.conn.cursor() cursor.execute(f"DELETE FROM {autosplit_table}") g.conn.commit() - return jsonify(status='ok') + return jsonify(status='ok', warnings=warnings[:10]) if __name__ == '__main__': diff --git a/web/app/countries_structure.py b/web/app/countries_structure.py index 170229c..166f47a 100644 --- a/web/app/countries_structure.py +++ b/web/app/countries_structure.py @@ -136,7 +136,7 @@ unilevel_countries = { 'Panama', 'Papua New Guinea', 'Peru', # need split-merge - 'Philippines', # split at level 3 and merge or not merte + 'Philippines', # split at level 3 and merge or not merge 'Qatar', 'Romania', # need split-merge 'Rwanda', @@ -331,8 +331,13 @@ def _make_country_structure(conn, country_osm_id): admin_level, regions) _create_regions(conn, subregion_ids, regions) prev_region_ids = subregion_ids + warning = None if len(regions) == 1: - update_border_mwm_size_estimation(conn, country_osm_id) + try: + update_border_mwm_size_estimation(conn, country_osm_id) + except Exception as e: + warning = str(e) + return warning def create_countries_initial_structure(conn): @@ -348,9 +353,13 @@ def create_countries_initial_structure(conn): # ({','.join(f"'{c}'" for c in country_initial_levels.keys())}) #""" ) + warnings = [] for rec in cursor: - _make_country_structure(conn, rec[0]) + warning = _make_country_structure(conn, rec[0]) + if warning: + warnings.append(warning) conn.commit() + return warnings def get_osm_border_name_by_osm_id(conn, osm_id): cursor = conn.cursor() diff --git a/web/app/static/borders.js b/web/app/static/borders.js index 611f3e5..63da700 100644 --- a/web/app/static/borders.js +++ b/web/app/static/borders.js @@ -136,8 +136,11 @@ function makeAnswerHandler(on_ok_func) { return function(answer) { if (answer.status !== 'ok') alert(answer.status); - else + else { + if (answer.warnings && answer.warnings.length) + alert(answer.warnings.join('\n')); on_ok_func(answer); + } }; } diff --git a/web/app/subregions.py b/web/app/subregions.py index d359b23..349dc30 100644 --- a/web/app/subregions.py +++ b/web/app/subregions.py @@ -1,3 +1,5 @@ +import math + import config from mwm_size_predictor import MwmSizePredictor @@ -105,12 +107,14 @@ def update_border_mwm_size_estimation(conn, border_id): table = config.TABLE cursor = conn.cursor() cursor.execute(f""" - SELECT ST_Area(geography(geom))/1.0E+6 area + SELECT name, ST_Area(geography(geom))/1.0E+6 area FROM {table} WHERE id = %s""", (border_id, )) - rec = cursor.fetchone() + name, area = cursor.fetchone() + if math.isnan(area): + raise Exception(f"Area is NaN for border '{name}' ({border_id})") border_data = { - 'area': rec[0], + 'area': area, 'urban_pop': 0, 'city_cnt': 0, 'hamlet_cnt': 0