|
26 | 26 | streamHandler.setFormatter(formatter) |
27 | 27 | logger.addHandler(streamHandler) |
28 | 28 |
|
| 29 | +complex_geom_datasets = ['flood-risk-zone'] |
29 | 30 | export_tables = { |
30 | 31 | DATABASE_NAME: ["entity", "old_entity"], |
31 | 32 | "digital-land": [ |
@@ -125,6 +126,9 @@ def do_replace_table(table, source, csv_filename, postgress_conn, sqlite_conn): |
125 | 126 |
|
126 | 127 | make_valid_with_handle_geometry_collection(postgress_conn, source) |
127 | 128 |
|
| 129 | + if source in complex_geom_datasets: |
| 130 | + update_entity_subdivided(postgress_conn,source) |
| 131 | + |
128 | 132 |
|
129 | 133 | def do_replace(source, sqlite_conn, tables_to_export=None): |
130 | 134 | if tables_to_export is None: |
@@ -216,6 +220,36 @@ def make_valid_multipolygon(connection, source): |
216 | 220 |
|
217 | 221 | logger.info(f"Updated {rowcount} rows with valid multi polygons") |
218 | 222 |
|
| 223 | +def update_entity_subdivided(connection, source): |
| 224 | + delete_sql = """ |
| 225 | + DELETE FROM entity_subdivided WHERE dataset = %s ; |
| 226 | + """ |
| 227 | + |
| 228 | + update_entity_subdivided = """ |
| 229 | + INSERT INTO entity_subdivided (entity, dataset, geometry_subdivided) |
| 230 | + SELECT e.entity, e.dataset, g.geom |
| 231 | + FROM entity e |
| 232 | + JOIN LATERAL ( |
| 233 | + SELECT (ST_Dump(ST_Subdivide(ST_MakeValid(e.geometry)))).geom |
| 234 | + ) AS g ON true |
| 235 | + WHERE dataset = %s |
| 236 | + AND e.geometry IS NOT NULL |
| 237 | + AND ST_IsValid(e.geometry); |
| 238 | +
|
| 239 | + """.strip() |
| 240 | + |
| 241 | + with connection.cursor() as cursor: |
| 242 | + |
| 243 | + cursor.execute(delete_sql, (source,)) |
| 244 | + deleted_count = cursor.rowcount |
| 245 | + |
| 246 | + cursor.execute(update_entity_subdivided, (source,)) |
| 247 | + rowcount = cursor.rowcount |
| 248 | + |
| 249 | + connection.commit() |
| 250 | + |
| 251 | + logger.info(f"Updated entity_sub_divided table - {deleted_count} rows deleted") |
| 252 | + logger.info(f"Updated entity_sub_divided table - {rowcount} rows with subdivided geometries") |
219 | 253 |
|
220 | 254 | if __name__ == "__main__": |
221 | 255 | do_replace_cli() |
0 commit comments