Global rename variables table=>borders_table, config.TABLE=>config.BORDERS_TABLE
This commit is contained in:
parent
147188de93
commit
ee5bb44583
6 changed files with 122 additions and 115 deletions
|
@ -137,10 +137,10 @@ def query_bbox():
|
|||
ymax = request.args.get('ymax')
|
||||
simplify_level = request.args.get('simplify')
|
||||
simplify = simplify_level_to_postgis_value(simplify_level)
|
||||
table = request.args.get('table')
|
||||
table = config.OTHER_TABLES.get(table, config.TABLE)
|
||||
borders_table = request.args.get('table')
|
||||
borders_table = config.OTHER_TABLES.get(borders_table, config.BORDERS_TABLE)
|
||||
borders = fetch_borders(
|
||||
table=table,
|
||||
table=borders_table,
|
||||
simplify=simplify,
|
||||
where_clause=geom_inside_bbox_sql(xmin, ymin, xmax, ymax)
|
||||
)
|
||||
|
@ -157,15 +157,15 @@ def query_small_in_bbox():
|
|||
xmax = request.args.get('xmax')
|
||||
ymin = request.args.get('ymin')
|
||||
ymax = request.args.get('ymax')
|
||||
table = request.args.get('table')
|
||||
table = config.OTHER_TABLES.get(table, config.TABLE)
|
||||
borders_table = request.args.get('table')
|
||||
borders_table = config.OTHER_TABLES.get(borders_table, config.BORDERS_TABLE)
|
||||
with g.conn.cursor() as cursor:
|
||||
cursor.execute(f"""
|
||||
SELECT id, name, ST_Area(geography(ring))/1E6 AS area,
|
||||
ST_X(ST_Centroid(ring)), ST_Y(ST_Centroid(ring))
|
||||
FROM (
|
||||
SELECT id, name, (ST_Dump(geom)).geom AS ring
|
||||
FROM {table}
|
||||
FROM {borders_table}
|
||||
WHERE {geom_inside_bbox_sql(xmin, ymin, xmax, ymax)}
|
||||
) g
|
||||
WHERE ST_Area(geography(ring))/1E6 < %s
|
||||
|
@ -224,7 +224,7 @@ def search():
|
|||
with g.conn.cursor() as cursor:
|
||||
cursor.execute(f"""
|
||||
SELECT ST_XMin(geom), ST_YMin(geom), ST_XMax(geom), ST_YMax(geom)
|
||||
FROM {config.TABLE}
|
||||
FROM {config.BORDERS_TABLE}
|
||||
WHERE name ILIKE %s
|
||||
ORDER BY (ST_Area(geography(geom)))
|
||||
LIMIT 1""", (f'%{query}%',)
|
||||
|
@ -242,11 +242,11 @@ def split():
|
|||
region_id = int(request.args.get('id'))
|
||||
line = request.args.get('line')
|
||||
save_region = (request.args.get('save_region') == 'true')
|
||||
table = config.TABLE
|
||||
borders_table = config.BORDERS_TABLE
|
||||
with g.conn.cursor() as cursor:
|
||||
# check that we're splitting a single polygon
|
||||
cursor.execute(f"""
|
||||
SELECT ST_NumGeometries(geom) FROM {table} WHERE id = %s
|
||||
SELECT ST_NumGeometries(geom) FROM {borders_table} WHERE id = %s
|
||||
""", (region_id,)
|
||||
)
|
||||
res = cursor.fetchone()
|
||||
|
@ -255,7 +255,7 @@ def split():
|
|||
cursor.execute(f"""
|
||||
SELECT ST_AsText(
|
||||
(ST_Dump(ST_Split(geom, ST_GeomFromText(%s, 4326)))).geom)
|
||||
FROM {table}
|
||||
FROM {borders_table}
|
||||
WHERE id = %s
|
||||
""", (line, region_id)
|
||||
)
|
||||
|
@ -266,13 +266,13 @@ def split():
|
|||
geometries.append(res[0])
|
||||
# get region properties and delete old border
|
||||
cursor.execute(f"""
|
||||
SELECT name, parent_id, disabled FROM {table} WHERE id = %s
|
||||
SELECT name, parent_id, disabled FROM {borders_table} WHERE id = %s
|
||||
""", (region_id,))
|
||||
name, parent_id, disabled = cursor.fetchone()
|
||||
if save_region:
|
||||
parent_id = region_id
|
||||
else:
|
||||
cursor.execute(f"DELETE FROM {table} WHERE id = %s",
|
||||
cursor.execute(f"DELETE FROM {borders_table} WHERE id = %s",
|
||||
(region_id,))
|
||||
base_name = name
|
||||
# insert new geometries
|
||||
|
@ -281,8 +281,8 @@ def split():
|
|||
free_id = get_free_id()
|
||||
for geom in geometries:
|
||||
cursor.execute(f"""
|
||||
INSERT INTO {table} (id, name, geom, disabled, count_k,
|
||||
modified, parent_id)
|
||||
INSERT INTO {borders_table} (id, name, geom, disabled,
|
||||
count_k, modified, parent_id)
|
||||
VALUES (%s, %s, ST_GeomFromText(%s, 4326), %s, -1,
|
||||
now(), %s)
|
||||
""", (free_id, f'{base_name}_{counter}', geom,
|
||||
|
@ -311,18 +311,18 @@ def join_borders():
|
|||
return jsonify(status='failed to join region with itself')
|
||||
with g.conn.cursor() as cursor:
|
||||
try:
|
||||
table = config.TABLE
|
||||
borders_table = config.BORDERS_TABLE
|
||||
free_id = get_free_id()
|
||||
cursor.execute(f"""
|
||||
UPDATE {table}
|
||||
UPDATE {borders_table}
|
||||
SET id = {free_id},
|
||||
geom = ST_Union({table}.geom, b2.geom),
|
||||
mwm_size_est = {table}.mwm_size_est + b2.mwm_size_est,
|
||||
geom = ST_Union({borders_table}.geom, b2.geom),
|
||||
mwm_size_est = {borders_table}.mwm_size_est + b2.mwm_size_est,
|
||||
count_k = -1
|
||||
FROM (SELECT geom, mwm_size_est FROM {table} WHERE id = %s) AS b2
|
||||
FROM (SELECT geom, mwm_size_est FROM {borders_table} WHERE id = %s) AS b2
|
||||
WHERE id = %s""", (region_id2, region_id1)
|
||||
)
|
||||
cursor.execute(f"DELETE FROM {table} WHERE id = %s", (region_id2,))
|
||||
cursor.execute(f"DELETE FROM {borders_table} WHERE id = %s", (region_id2,))
|
||||
except psycopg2.Error as e:
|
||||
g.conn.rollback()
|
||||
return jsonify(status=str(e))
|
||||
|
@ -354,11 +354,12 @@ def join_to_parent():
|
|||
else:
|
||||
break
|
||||
with g.conn.cursor() as cursor:
|
||||
borders_table = config.BORDERS_TABLE
|
||||
while len(descendants) > 1:
|
||||
lowest_ids = descendants.pop()
|
||||
ids_str = ','.join(str(x) for x in lowest_ids)
|
||||
cursor.execute(f"""
|
||||
DELETE FROM {config.TABLE} WHERE id IN ({ids_str})"""
|
||||
DELETE FROM {borders_table} WHERE id IN ({ids_str})"""
|
||||
)
|
||||
g.conn.commit()
|
||||
return jsonify(status='ok')
|
||||
|
@ -370,10 +371,10 @@ def set_parent():
|
|||
region_id = int(request.args.get('id'))
|
||||
parent_id = request.args.get('parent_id')
|
||||
parent_id = int(parent_id) if parent_id else None
|
||||
table = config.TABLE
|
||||
borders_table = config.BORDERS_TABLE
|
||||
with g.conn.cursor() as cursor:
|
||||
cursor.execute(f"""
|
||||
UPDATE {table} SET parent_id = %s WHERE id = %s
|
||||
UPDATE {borders_table} SET parent_id = %s WHERE id = %s
|
||||
""", (parent_id, region_id)
|
||||
)
|
||||
g.conn.commit()
|
||||
|
@ -413,16 +414,17 @@ def copy_from_osm():
|
|||
osm_id = int(request.args.get('id'))
|
||||
name = request.args.get('name')
|
||||
name_sql = f"'{name}'" if name else "'name'"
|
||||
table = config.TABLE
|
||||
borders_table = config.BORDERS_TABLE
|
||||
osm_table = config.OSM_TABLE
|
||||
with g.conn.cursor() as cursor:
|
||||
# Check if this id already in use
|
||||
cursor.execute(f"SELECT id FROM {table} WHERE id = %s", (osm_id,))
|
||||
cursor.execute(f"SELECT id FROM {borders_table} WHERE id = %s",
|
||||
(osm_id,))
|
||||
rec = cursor.fetchone()
|
||||
if rec and rec[0]:
|
||||
return jsonify(status=f"Region with id={osm_id} already exists")
|
||||
cursor.execute(f"""
|
||||
INSERT INTO {table} (id, geom, name, modified, count_k)
|
||||
INSERT INTO {borders_table} (id, geom, name, modified, count_k)
|
||||
SELECT osm_id, way, {name_sql}, now(), -1
|
||||
FROM {osm_table}
|
||||
WHERE osm_id = %s
|
||||
|
@ -443,10 +445,10 @@ def copy_from_osm():
|
|||
@validate_args_types(id=int)
|
||||
def set_name():
|
||||
region_id = int(request.args.get('id'))
|
||||
table = config.TABLE
|
||||
borders_table = config.BORDERS_TABLE
|
||||
new_name = request.args.get('new_name')
|
||||
with g.conn.cursor() as cursor:
|
||||
cursor.execute(f"UPDATE {table} SET name = %s WHERE id = %s",
|
||||
cursor.execute(f"UPDATE {borders_table} SET name = %s WHERE id = %s",
|
||||
(new_name, region_id))
|
||||
g.conn.commit()
|
||||
return jsonify(status='ok')
|
||||
|
@ -458,7 +460,7 @@ def set_name():
|
|||
def delete_border():
|
||||
region_id = int(request.args.get('id'))
|
||||
with g.conn.cursor() as cursor:
|
||||
cursor.execute(f"DELETE FROM {config.TABLE} WHERE id = %s",
|
||||
cursor.execute(f"DELETE FROM {config.BORDERS_TABLE} WHERE id = %s",
|
||||
(region_id,))
|
||||
g.conn.commit()
|
||||
return jsonify(status='ok')
|
||||
|
@ -471,7 +473,7 @@ def disable_border():
|
|||
region_id = int(request.args.get('id'))
|
||||
with g.conn.cursor() as cursor:
|
||||
cursor.execute(f"""
|
||||
UPDATE {config.TABLE}
|
||||
UPDATE {config.BORDERS_TABLE}
|
||||
SET disabled = true
|
||||
WHERE id = %s""", (region_id,))
|
||||
g.conn.commit()
|
||||
|
@ -485,7 +487,7 @@ def enable_border():
|
|||
region_id = int(request.args.get('id'))
|
||||
with g.conn.cursor() as cursor:
|
||||
cursor.execute(f"""
|
||||
UPDATE {config.TABLE}
|
||||
UPDATE {config.BORDERS_TABLE}
|
||||
SET disabled = false
|
||||
WHERE id = %s""", (region_id,))
|
||||
g.conn.commit()
|
||||
|
@ -498,8 +500,10 @@ def update_comment():
|
|||
region_id = int(request.form['id'])
|
||||
comment = request.form['comment']
|
||||
with g.conn.cursor() as cursor:
|
||||
cursor.execute(f"UPDATE {config.TABLE} SET cmnt = %s WHERE id = %s",
|
||||
(comment, region_id))
|
||||
cursor.execute(f"""
|
||||
UPDATE {config.BORDERS_TABLE}
|
||||
SET cmnt = %s WHERE id = %s
|
||||
""", (comment, region_id))
|
||||
g.conn.commit()
|
||||
return jsonify(status='ok')
|
||||
|
||||
|
@ -565,10 +569,10 @@ def divide(preview=False):
|
|||
@validate_args_types(id=int)
|
||||
def chop_largest_or_farthest():
|
||||
region_id = int(request.args.get('id'))
|
||||
table = config.TABLE
|
||||
borders_table = config.BORDERS_TABLE
|
||||
with g.conn.cursor() as cursor:
|
||||
cursor.execute(f"""SELECT ST_NumGeometries(geom)
|
||||
FROM {table}
|
||||
FROM {borders_table}
|
||||
WHERE id = {region_id}""")
|
||||
res = cursor.fetchone()
|
||||
if not res or res[0] < 2:
|
||||
|
@ -576,11 +580,12 @@ def chop_largest_or_farthest():
|
|||
free_id1 = get_free_id()
|
||||
free_id2 = free_id1 - 1
|
||||
cursor.execute(f"""
|
||||
INSERT INTO {table} (id, parent_id, name, disabled, modified, geom)
|
||||
INSERT INTO {borders_table} (id, parent_id, name, disabled,
|
||||
modified, geom)
|
||||
SELECT id, region_id, name, disabled, modified, geom FROM
|
||||
(
|
||||
(WITH w AS (SELECT name, disabled, (ST_Dump(geom)).geom AS g
|
||||
FROM {table} WHERE id = {region_id})
|
||||
FROM {borders_table} WHERE id = {region_id})
|
||||
(SELECT {free_id1} id, {region_id} region_id,
|
||||
name||'_main' as name, disabled,
|
||||
now() as modified, g as geom, ST_Area(g) as a
|
||||
|
@ -610,16 +615,16 @@ def chop_largest_or_farthest():
|
|||
@validate_args_types(id=int)
|
||||
def draw_hull():
|
||||
border_id = int(request.args.get('id'))
|
||||
table = config.TABLE
|
||||
borders_table = config.BORDERS_TABLE
|
||||
with g.conn.cursor() as cursor:
|
||||
cursor.execute(f"""
|
||||
SELECT ST_NumGeometries(geom) FROM {table} WHERE id = %s
|
||||
SELECT ST_NumGeometries(geom) FROM {borders_table} WHERE id = %s
|
||||
""", (border_id,))
|
||||
res = cursor.fetchone()
|
||||
if not res or res[0] < 2:
|
||||
return jsonify(status='border should have more than one outer ring')
|
||||
cursor.execute(f"""
|
||||
UPDATE {table} SET geom = ST_ConvexHull(geom)
|
||||
UPDATE {borders_table} SET geom = ST_ConvexHull(geom)
|
||||
WHERE id = %s""", (border_id,)
|
||||
)
|
||||
g.conn.commit()
|
||||
|
@ -638,14 +643,14 @@ def backup_do():
|
|||
if timestamp == tsmax:
|
||||
return jsonify(status="please try again later")
|
||||
backup_table = config.BACKUP
|
||||
table = config.TABLE
|
||||
borders_table = config.BORDERS_TABLE
|
||||
cursor.execute(f"""
|
||||
INSERT INTO {backup_table}
|
||||
(backup, id, name, parent_id, geom, disabled, count_k,
|
||||
modified, cmnt, mwm_size_est)
|
||||
SELECT %s, id, name, parent_id, geom, disabled, count_k,
|
||||
modified, cmnt, mwm_size_est
|
||||
FROM {table}
|
||||
FROM {borders_table}
|
||||
""", (timestamp,)
|
||||
)
|
||||
g.conn.commit()
|
||||
|
@ -656,7 +661,7 @@ def backup_do():
|
|||
@check_write_access
|
||||
def backup_restore():
|
||||
ts = request.args.get('timestamp')
|
||||
table = config.TABLE
|
||||
borders_table = config.BORDERS_TABLE
|
||||
backup_table = config.BACKUP
|
||||
with g.conn.cursor() as cursor:
|
||||
cursor.execute(f"SELECT count(1) FROM {backup_table} WHERE backup = %s",
|
||||
|
@ -664,9 +669,9 @@ def backup_restore():
|
|||
(count,) = cursor.fetchone()
|
||||
if count <= 0:
|
||||
return jsonify(status="no such timestamp")
|
||||
cursor.execute(f"DELETE FROM {table}")
|
||||
cursor.execute(f"DELETE FROM {borders_table}")
|
||||
cursor.execute(f"""
|
||||
INSERT INTO {table}
|
||||
INSERT INTO {borders_table}
|
||||
(id, name, parent_id, geom, disabled, count_k,
|
||||
modified, cmnt, mwm_size_est)
|
||||
SELECT id, name, parent_id, geom, disabled, count_k,
|
||||
|
@ -720,10 +725,10 @@ def make_osm():
|
|||
xmax = request.args.get('xmax')
|
||||
ymin = request.args.get('ymin')
|
||||
ymax = request.args.get('ymax')
|
||||
table = request.args.get('table')
|
||||
table = config.OTHER_TABLES.get(table, config.TABLE)
|
||||
borders_table = request.args.get('table')
|
||||
borders_table = config.OTHER_TABLES.get(borders_table, config.BORDERS_TABLE)
|
||||
borders = fetch_borders(
|
||||
table=table,
|
||||
table=borders_table,
|
||||
where_clause=geom_inside_bbox_sql(xmin, ymin, xmax, ymax)
|
||||
)
|
||||
xml = borders_to_xml(borders)
|
||||
|
@ -736,7 +741,7 @@ def josm_borders_along():
|
|||
region_id = int(request.args.get('id'))
|
||||
line = request.args.get('line')
|
||||
# select all outer osm borders inside a buffer of the given line
|
||||
table = config.TABLE
|
||||
borders_table = config.BORDERS_TABLE
|
||||
osm_table = config.OSM_TABLE
|
||||
with g.conn.cursor() as cursor:
|
||||
cursor.execute(f"""
|
||||
|
@ -745,7 +750,7 @@ def josm_borders_along():
|
|||
geom,
|
||||
ST_Buffer(ST_GeomFromText(%s, 4326), 0.2)
|
||||
) as line
|
||||
FROM {table}
|
||||
FROM {borders_table}
|
||||
WHERE id = %s
|
||||
), osmborders AS (
|
||||
SELECT (ST_Dump(way)).geom as g
|
||||
|
@ -841,10 +846,10 @@ def potential_parents():
|
|||
@validate_args_types(xmin=(None, float), xmax=(None, float),
|
||||
ymin=(None, float), ymax=(None, float))
|
||||
def export_poly():
|
||||
table = request.args.get('table')
|
||||
table = config.OTHER_TABLES.get(table, config.TABLE)
|
||||
borders_table = request.args.get('table')
|
||||
borders_table = config.OTHER_TABLES.get(borders_table, config.BORDERS_TABLE)
|
||||
|
||||
fetch_borders_args = {'table': table, 'only_leaves': True}
|
||||
fetch_borders_args = {'table': borders_table, 'only_leaves': True}
|
||||
|
||||
if 'xmin' in request.args:
|
||||
# If one coordinate is given then others are also expected.
|
||||
|
@ -898,11 +903,11 @@ def export_poly():
|
|||
@app.route('/stat')
|
||||
def statistics():
|
||||
group = request.args.get('group')
|
||||
table = request.args.get('table')
|
||||
table = config.OTHER_TABLES.get(table, config.TABLE)
|
||||
borders_table = request.args.get('table')
|
||||
borders_table = config.OTHER_TABLES.get(borders_table, config.BORDERS_TABLE)
|
||||
with g.conn.cursor() as cursor:
|
||||
if group == 'total':
|
||||
cursor.execute(f"SELECT count(1) FROM {table}")
|
||||
cursor.execute(f"SELECT count(1) FROM {borders_table}")
|
||||
return jsonify(total=cursor.fetchone()[0])
|
||||
elif group == 'sizes':
|
||||
cursor.execute(f"""
|
||||
|
@ -921,7 +926,7 @@ def statistics():
|
|||
WHEN coalesce(cmnt, '') = '' THEN false
|
||||
ELSE true
|
||||
END) AS cmnt
|
||||
FROM {table}"""
|
||||
FROM {borders_table}"""
|
||||
)
|
||||
result = []
|
||||
for res in cursor:
|
||||
|
@ -944,7 +949,7 @@ def statistics():
|
|||
) / 1E6,
|
||||
sum(ST_NumInteriorRings(g)),
|
||||
ST_AsGeoJSON(ST_Centroid(ST_Collect(g)))
|
||||
FROM (SELECT name, (ST_Dump(geom)).geom AS g FROM {table}) a
|
||||
FROM (SELECT name, (ST_Dump(geom)).geom AS g FROM {borders_table}) a
|
||||
GROUP BY name"""
|
||||
)
|
||||
result = []
|
||||
|
@ -961,11 +966,11 @@ def statistics():
|
|||
@validate_args_types(id=int)
|
||||
def border():
|
||||
region_id = int(request.args.get('id'))
|
||||
table = config.TABLE
|
||||
borders_table = config.BORDERS_TABLE
|
||||
simplify_level = request.args.get('simplify')
|
||||
simplify = simplify_level_to_postgis_value(simplify_level)
|
||||
borders = fetch_borders(
|
||||
table=table,
|
||||
table=borders_table,
|
||||
simplify=simplify,
|
||||
only_leaves=False,
|
||||
where_clause=f'id = {region_id}'
|
||||
|
|
|
@ -19,14 +19,14 @@ def geom_inside_bbox_sql(xmin, ymin, xmax, ymax):
|
|||
|
||||
|
||||
def fetch_borders(**kwargs):
|
||||
table = kwargs.get('table', config.TABLE)
|
||||
borders_table = kwargs.get('table', config.BORDERS_TABLE)
|
||||
simplify = kwargs.get('simplify', 0)
|
||||
where_clause = kwargs.get('where_clause', '1=1')
|
||||
only_leaves = kwargs.get('only_leaves', True)
|
||||
osm_table = config.OSM_TABLE
|
||||
geom = (f'ST_SimplifyPreserveTopology(geom, {simplify})'
|
||||
if simplify > 0 else 'geom')
|
||||
leaves_filter = (f""" AND id NOT IN (SELECT parent_id FROM {table}
|
||||
leaves_filter = (f""" AND id NOT IN (SELECT parent_id FROM {borders_table}
|
||||
WHERE parent_id IS NOT NULL)"""
|
||||
if only_leaves else '')
|
||||
query = f"""
|
||||
|
@ -48,14 +48,14 @@ def fetch_borders(**kwargs):
|
|||
WHERE osm_id = t.id
|
||||
) AS admin_level,
|
||||
parent_id,
|
||||
( SELECT name FROM {table}
|
||||
( SELECT name FROM {borders_table}
|
||||
WHERE id = t.parent_id
|
||||
) AS parent_name,
|
||||
( SELECT admin_level FROM {osm_table}
|
||||
WHERE osm_id = (SELECT parent_id FROM {table} WHERE id = t.id)
|
||||
WHERE osm_id = (SELECT parent_id FROM {borders_table} WHERE id = t.id)
|
||||
) AS parent_admin_level,
|
||||
mwm_size_est
|
||||
FROM {table} t
|
||||
FROM {borders_table} t
|
||||
WHERE ({where_clause}) {leaves_filter}
|
||||
) q
|
||||
ORDER BY area DESC
|
||||
|
@ -105,7 +105,7 @@ def get_subregions_for_preview(region_ids, next_level):
|
|||
|
||||
def get_subregions_one_for_preview(region_id, next_level):
|
||||
osm_table = config.OSM_TABLE
|
||||
table = config.TABLE
|
||||
borders_table = config.BORDERS_TABLE
|
||||
with g.conn.cursor() as cursor:
|
||||
# We use ST_SimplifyPreserveTopology, since ST_Simplify would give NULL
|
||||
# for very little regions.
|
||||
|
@ -115,7 +115,7 @@ def get_subregions_one_for_preview(region_id, next_level):
|
|||
osm_id
|
||||
FROM {osm_table}
|
||||
WHERE ST_Contains(
|
||||
(SELECT geom FROM {table} WHERE id = %s), way
|
||||
(SELECT geom FROM {borders_table} WHERE id = %s), way
|
||||
)
|
||||
AND admin_level = %s
|
||||
""", (region_id, next_level)
|
||||
|
@ -196,16 +196,16 @@ def divide_into_subregions(region_ids, next_level):
|
|||
|
||||
|
||||
def divide_into_subregions_one(region_id, next_level):
|
||||
table = config.TABLE
|
||||
borders_table = config.BORDERS_TABLE
|
||||
osm_table = config.OSM_TABLE
|
||||
subregions = get_subregions_info(g.conn, region_id, table,
|
||||
subregions = get_subregions_info(g.conn, region_id, borders_table,
|
||||
next_level, need_cities=False)
|
||||
with g.conn.cursor() as cursor:
|
||||
is_admin_region = is_administrative_region(g.conn, region_id)
|
||||
if is_admin_region:
|
||||
for subregion_id, data in subregions.items():
|
||||
cursor.execute(f"""
|
||||
INSERT INTO {table}
|
||||
INSERT INTO {borders_table}
|
||||
(id, geom, name, parent_id, modified, count_k, mwm_size_est)
|
||||
SELECT osm_id, way, name, %s, now(), -1, {data['mwm_size_est']}
|
||||
FROM {osm_table}
|
||||
|
@ -215,26 +215,26 @@ def divide_into_subregions_one(region_id, next_level):
|
|||
else:
|
||||
for subregion_id, data in subregions.items():
|
||||
cursor.execute(f"""
|
||||
INSERT INTO {table}
|
||||
INSERT INTO {borders_table}
|
||||
(id, geom, name, parent_id, modified, count_k, mwm_size_est)
|
||||
SELECT osm_id, way, name,
|
||||
(SELECT parent_id FROM {table} WHERE id = %s),
|
||||
(SELECT parent_id FROM {borders_table} WHERE id = %s),
|
||||
now(), -1, {data['mwm_size_est']}
|
||||
FROM {osm_table}
|
||||
WHERE osm_id = %s
|
||||
""", (region_id, subregion_id)
|
||||
)
|
||||
cursor.execute(f"DELETE FROM {table} WHERE id = %s", (region_id,))
|
||||
cursor.execute(f"DELETE FROM {borders_table} WHERE id = %s", (region_id,))
|
||||
g.conn.commit()
|
||||
|
||||
|
||||
def divide_into_clusters(region_ids, next_level, mwm_size_thr):
|
||||
table = config.TABLE
|
||||
borders_table = config.BORDERS_TABLE
|
||||
autosplit_table = config.AUTOSPLIT_TABLE
|
||||
cursor = g.conn.cursor()
|
||||
insert_cursor = g.conn.cursor()
|
||||
for region_id in region_ids:
|
||||
cursor.execute(f"SELECT name FROM {table} WHERE id = %s", (region_id,))
|
||||
cursor.execute(f"SELECT name FROM {borders_table} WHERE id = %s", (region_id,))
|
||||
base_name = cursor.fetchone()[0]
|
||||
|
||||
where_clause = f"""
|
||||
|
@ -271,7 +271,7 @@ def divide_into_clusters(region_ids, next_level, mwm_size_thr):
|
|||
subregion_id = free_id
|
||||
name = f"{base_name}_{counter}"
|
||||
insert_cursor.execute(f"""
|
||||
INSERT INTO {table} (id, name, parent_id, geom, modified, count_k, mwm_size_est)
|
||||
INSERT INTO {borders_table} (id, name, parent_id, geom, modified, count_k, mwm_size_est)
|
||||
SELECT {subregion_id}, %s, osm_border_id, geom, now(), -1, mwm_size_est
|
||||
FROM {autosplit_table} WHERE subregion_ids[1] = %s AND {where_clause}
|
||||
""", (name, cluster_id,) + splitting_sql_params
|
||||
|
@ -282,8 +282,8 @@ def divide_into_clusters(region_ids, next_level, mwm_size_thr):
|
|||
|
||||
def get_free_id():
|
||||
with g.conn.cursor() as cursor:
|
||||
table = config.TABLE
|
||||
cursor.execute(f"SELECT min(id) FROM {table} WHERE id < -1000000000")
|
||||
borders_table = config.BORDERS_TABLE
|
||||
cursor.execute(f"SELECT min(id) FROM {borders_table} WHERE id < -1000000000")
|
||||
min_id = cursor.fetchone()[0]
|
||||
free_id = min_id - 1 if min_id else -1_000_000_001
|
||||
return free_id
|
||||
|
@ -296,10 +296,10 @@ def assign_region_to_lowest_parent(region_id):
|
|||
if pot_parents:
|
||||
# potential_parents are sorted by area ascending
|
||||
parent_id = pot_parents[0]['properties']['id']
|
||||
table = config.TABLE
|
||||
borders_table = config.BORDERS_TABLE
|
||||
with g.conn.cursor() as cursor:
|
||||
cursor.execute(f"""
|
||||
UPDATE {table}
|
||||
UPDATE {borders_table}
|
||||
SET parent_id = %s
|
||||
WHERE id = %s
|
||||
""", (parent_id, region_id)
|
||||
|
@ -309,7 +309,7 @@ def assign_region_to_lowest_parent(region_id):
|
|||
|
||||
|
||||
def create_or_update_region(region, free_id):
|
||||
table = config.TABLE
|
||||
borders_table = config.BORDERS_TABLE
|
||||
with g.conn.cursor() as cursor:
|
||||
if region['id'] < 0:
|
||||
if not free_id:
|
||||
|
@ -317,7 +317,7 @@ def create_or_update_region(region, free_id):
|
|||
region_id = free_id
|
||||
|
||||
cursor.execute(f"""
|
||||
INSERT INTO {table}
|
||||
INSERT INTO {borders_table}
|
||||
(id, name, disabled, geom, modified, count_k)
|
||||
VALUES (%s, %s, %s, ST_GeomFromText(%s, 4326), now(), -1)
|
||||
""", (region_id, region['name'],
|
||||
|
@ -326,13 +326,13 @@ def create_or_update_region(region, free_id):
|
|||
assign_region_to_lowest_parent(region_id)
|
||||
return region_id
|
||||
else:
|
||||
cursor.execute(f"SELECT count(1) FROM {table} WHERE id = %s",
|
||||
cursor.execute(f"SELECT count(1) FROM {borders_table} WHERE id = %s",
|
||||
(-region['id'],))
|
||||
rec = cursor.fetchone()
|
||||
if rec[0] == 0:
|
||||
raise Exception(f"Can't find border ({region['id']}) for update")
|
||||
cursor.execute(f"""
|
||||
UPDATE {table}
|
||||
UPDATE {borders_table}
|
||||
SET disabled = %s,
|
||||
name = %s,
|
||||
modified = now(),
|
||||
|
@ -346,7 +346,7 @@ def create_or_update_region(region, free_id):
|
|||
|
||||
|
||||
def find_potential_parents(region_id):
|
||||
table = config.TABLE
|
||||
borders_table = config.BORDERS_TABLE
|
||||
osm_table = config.OSM_TABLE
|
||||
p_geogr = "geography(p.geom)"
|
||||
c_geogr = "geography(c.geom)"
|
||||
|
@ -356,7 +356,7 @@ def find_potential_parents(region_id):
|
|||
p.name,
|
||||
(SELECT admin_level FROM {osm_table} WHERE osm_id = p.id) admin_level,
|
||||
ST_AsGeoJSON(ST_SimplifyPreserveTopology(p.geom, 0.01)) geometry
|
||||
FROM {table} p, {table} c
|
||||
FROM {borders_table} p, {borders_table} c
|
||||
WHERE c.id = %s
|
||||
AND ST_Intersects(p.geom, c.geom)
|
||||
AND ST_Area({p_geogr}) > ST_Area({c_geogr})
|
||||
|
|
|
@ -14,7 +14,7 @@ except:
|
|||
HAS_DAEMON = False
|
||||
|
||||
|
||||
table = config.TABLE
|
||||
borders_table = config.BORDERS_TABLE
|
||||
|
||||
CONNECT_WAIT_INTERVAL = 5
|
||||
CHECK_BORDERS_INTERVAL = 10
|
||||
|
@ -32,7 +32,7 @@ no_count_queries = [
|
|||
SELECT id, name,
|
||||
ST_Area(geography(geom))/1000000.0 area,
|
||||
ST_Area(geography(ST_Envelope(geom)))/1000000.0 env_area
|
||||
FROM {table}
|
||||
FROM {borders_table}
|
||||
WHERE {condition}) q
|
||||
WHERE area != 'NaN'::double precision
|
||||
AND area <= env_area
|
||||
|
@ -59,7 +59,7 @@ class App():
|
|||
self.conn.autocommit = True
|
||||
|
||||
with self.conn.cursor() as cur:
|
||||
cur.execute(f"SELECT count_k FROM {config.TABLE} LIMIT 1")
|
||||
cur.execute(f"SELECT count_k FROM {borders_table} LIMIT 1")
|
||||
|
||||
return self.conn
|
||||
except psycopg2.Error:
|
||||
|
@ -82,10 +82,10 @@ class App():
|
|||
|
||||
with self.get_connection().cursor() as cur:
|
||||
cur.execute(f"""
|
||||
UPDATE {table}
|
||||
UPDATE {borders_table}
|
||||
SET count_k = n.count
|
||||
FROM (SELECT coalesce(sum(t.count), 0) AS count
|
||||
FROM {table} b, tiles t
|
||||
FROM {borders_table} b, tiles t
|
||||
WHERE b.id = %s AND ST_Intersects(b.geom, t.tile)
|
||||
) AS n
|
||||
WHERE id = %s
|
||||
|
|
|
@ -5,7 +5,7 @@ DEBUG = True
|
|||
# if the main table is read-only
|
||||
READONLY = False
|
||||
# main table name
|
||||
TABLE = 'borders'
|
||||
BORDERS_TABLE = 'borders'
|
||||
# from where OSM borders are imported
|
||||
OSM_TABLE = 'osm_borders'
|
||||
# All populated places in OSM
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
from config import (
|
||||
TABLE as table,
|
||||
BORDERS_TABLE as borders_table,
|
||||
OSM_TABLE as osm_table
|
||||
)
|
||||
from countries_division import country_initial_levels
|
||||
|
@ -15,7 +15,7 @@ class CountryStructureException(Exception):
|
|||
|
||||
def _clear_borders(conn):
|
||||
with conn.cursor() as cursor:
|
||||
cursor.execute(f"DELETE FROM {table}")
|
||||
cursor.execute(f"DELETE FROM {borders_table}")
|
||||
conn.commit()
|
||||
|
||||
|
||||
|
@ -23,7 +23,7 @@ def _find_subregions(conn, osm_ids, next_level, regions):
|
|||
"""Return subregions of level 'next_level' for regions with osm_ids."""
|
||||
subregion_ids = []
|
||||
for osm_id in osm_ids:
|
||||
more_subregions = get_subregions_info(conn, osm_id, table,
|
||||
more_subregions = get_subregions_info(conn, osm_id, borders_table,
|
||||
next_level, need_cities=False)
|
||||
for subregion_id, subregion_data in more_subregions.items():
|
||||
region_data = regions.setdefault(subregion_id, {})
|
||||
|
@ -49,7 +49,7 @@ def _create_regions(conn, osm_ids, regions):
|
|||
)
|
||||
with conn.cursor() as cursor:
|
||||
cursor.execute(f"""
|
||||
INSERT INTO {table} (id, name, parent_id, mwm_size_est,
|
||||
INSERT INTO {borders_table} (id, name, parent_id, mwm_size_est,
|
||||
geom, modified)
|
||||
VALUES {sql_values}
|
||||
""", tuple(regions[osm_id]['name'] for osm_id in osm_ids)
|
||||
|
|
|
@ -1,15 +1,14 @@
|
|||
import math
|
||||
from queue import Queue
|
||||
|
||||
import config
|
||||
from config import (
|
||||
BORDERS_TABLE as borders_table,
|
||||
OSM_TABLE as osm_table,
|
||||
OSM_PLACES_TABLE as osm_places_table,
|
||||
)
|
||||
from mwm_size_predictor import MwmSizePredictor
|
||||
|
||||
|
||||
table = config.TABLE
|
||||
osm_table = config.OSM_TABLE
|
||||
osm_places_table = config.OSM_PLACES_TABLE
|
||||
|
||||
|
||||
def get_subregions_info(conn, region_id, region_table,
|
||||
next_level, need_cities=False):
|
||||
"""
|
||||
|
@ -35,11 +34,12 @@ def _get_subregions_basic_info(conn, region_id, region_table,
|
|||
next_level, need_cities):
|
||||
cursor = conn.cursor()
|
||||
region_id_column, region_geom_column = (
|
||||
('id', 'geom') if region_table == table else
|
||||
('id', 'geom') if region_table == borders_table else
|
||||
('osm_id', 'way')
|
||||
)
|
||||
cursor.execute(f"""
|
||||
SELECT subreg.osm_id, subreg.name, ST_Area(geography(subreg.way))/1.0E+6 area
|
||||
SELECT subreg.osm_id, subreg.name,
|
||||
ST_Area(geography(subreg.way))/1.0E+6 area
|
||||
FROM {region_table} reg, {osm_table} subreg
|
||||
WHERE reg.{region_id_column} = %s AND subreg.admin_level = %s AND
|
||||
ST_Contains(reg.{region_geom_column}, subreg.way)
|
||||
|
@ -111,7 +111,7 @@ def update_border_mwm_size_estimation(conn, border_id):
|
|||
cursor = conn.cursor()
|
||||
cursor.execute(f"""
|
||||
SELECT name, ST_Area(geography(geom))/1.0E+6 area
|
||||
FROM {table}
|
||||
FROM {borders_table}
|
||||
WHERE id = %s""", (border_id, ))
|
||||
name, area = cursor.fetchone()
|
||||
if math.isnan(area):
|
||||
|
@ -124,7 +124,7 @@ def update_border_mwm_size_estimation(conn, border_id):
|
|||
}
|
||||
cursor.execute(f"""
|
||||
SELECT coalesce(p.population, 0), p.place
|
||||
FROM {table} b, {config.OSM_PLACES_TABLE} p
|
||||
FROM {borders_table} b, {osm_places_table} p
|
||||
WHERE b.id = %s
|
||||
AND ST_Contains(b.geom, p.center)
|
||||
""", (border_id, ))
|
||||
|
@ -140,7 +140,7 @@ def update_border_mwm_size_estimation(conn, border_id):
|
|||
('urban_pop', 'area', 'city_cnt', 'hamlet_cnt')
|
||||
]
|
||||
mwm_size_est = MwmSizePredictor.predict(feature_array)
|
||||
cursor.execute(f"UPDATE {table} SET mwm_size_est = %s WHERE id = %s",
|
||||
cursor.execute(f"UPDATE {borders_table} SET mwm_size_est = %s WHERE id = %s",
|
||||
(mwm_size_est, border_id))
|
||||
conn.commit()
|
||||
|
||||
|
@ -159,7 +159,7 @@ def is_leaf(conn, region_id):
|
|||
cursor = conn.cursor()
|
||||
cursor.execute(f"""
|
||||
SELECT count(1)
|
||||
FROM {table}
|
||||
FROM {borders_table}
|
||||
WHERE parent_id = %s
|
||||
""", (region_id,)
|
||||
)
|
||||
|
@ -184,12 +184,14 @@ def get_predecessors(conn, region_id):
|
|||
while True:
|
||||
cursor.execute(f"""
|
||||
SELECT id, name, parent_id
|
||||
FROM {table} WHERE id = %s
|
||||
FROM {borders_table} WHERE id = %s
|
||||
""", (region_id,)
|
||||
)
|
||||
rec = cursor.fetchone()
|
||||
if not rec:
|
||||
raise Exception(f"No record in '{table}' table with id = {region_id}")
|
||||
raise Exception(
|
||||
f"No record in '{borders_table}' table with id = {region_id}"
|
||||
)
|
||||
predecessors.append(rec[0:2])
|
||||
parent_id = rec[2]
|
||||
if not parent_id:
|
||||
|
@ -206,7 +208,7 @@ def get_region_full_name(conn, region_id):
|
|||
def get_parent_region_id(conn, region_id):
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(f"""
|
||||
SELECT parent_id FROM {table} WHERE id = %s
|
||||
SELECT parent_id FROM {borders_table} WHERE id = %s
|
||||
""", (region_id,))
|
||||
rec = cursor.fetchone()
|
||||
parent_id = int(rec[0]) if rec and rec[0] is not None else None
|
||||
|
@ -216,7 +218,7 @@ def get_parent_region_id(conn, region_id):
|
|||
def get_child_region_ids(conn, region_id):
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(f"""
|
||||
SELECT id FROM {table} WHERE parent_id = %s
|
||||
SELECT id FROM {borders_table} WHERE parent_id = %s
|
||||
""", (region_id,))
|
||||
child_ids = []
|
||||
for rec in cursor:
|
||||
|
@ -257,11 +259,11 @@ def find_osm_child_regions(conn, region_id):
|
|||
with conn.cursor() as cursor:
|
||||
cursor.execute(f"""
|
||||
SELECT c.id, oc.admin_level
|
||||
FROM {table} c, {table} p, {osm_table} oc
|
||||
FROM {borders_table} c, {borders_table} p, {osm_table} oc
|
||||
WHERE p.id = c.parent_id AND c.id = oc.osm_id
|
||||
AND p.id = %s
|
||||
""", (region_id,)
|
||||
)
|
||||
for rec in cursor:
|
||||
children.append({'id': int(rec[0]), 'admin_level': int(rec[1])})
|
||||
for osm_id, admin_level in cursor:
|
||||
children.append({'id': osm_id, 'admin_level': admin_level})
|
||||
return children
|
||||
|
|
Loading…
Add table
Reference in a new issue