@@ -210,7 +210,10 @@ def log_pg_details():
210210
211211
212212def prepare_pgosm_db (skip_qgis_style , db_path , import_mode , schema_name ):
213- """Runs through series of steps to prepare database for PgOSM.
213+ """Runs through steps to prepare the target database for PgOSM Flex.
214+
215+ Includes additional preparation for using --replication and --updated=append
216+ modes.
214217
215218 Parameters
216219 --------------------------
@@ -245,6 +248,9 @@ def prepare_pgosm_db(skip_qgis_style, db_path, import_mode, schema_name):
245248 schema_name = schema_name )
246249 run_insert_pgosm_road (db_path = db_path , schema_name = schema_name )
247250
251+ if import_mode .replication_update or import_mode .update == 'append' :
252+ osm2pgsql_replication_start ()
253+
248254
249255def start_import (pgosm_region , pgosm_date , srid , language , layerset , git_info ,
250256 osm2pgsql_version , import_mode , schema_name , input_file ):
@@ -477,7 +483,7 @@ def get_db_conn(conn_string):
477483 return conn
478484
479485
480- def pgosm_after_import (flex_path ) :
486+ def pgosm_after_import (flex_path : str ) -> bool :
481487 """Runs post-processing SQL via Lua script.
482488
483489 Layerset logic is established via environment variable, must happen
@@ -508,17 +514,38 @@ def pgosm_after_import(flex_path):
508514
509515
510516def pgosm_nested_admin_polygons (flex_path : str , schema_name : str ):
511- """Runs stored procedure to calculate nested admin polygons via psql.
517+ """Runs two stored procedures to calculate nested admin polygons via psql.
512518
513519 Parameters
514520 ----------------------
515521 flex_path : str
516522 schema_name : str
517523 """
518- sql_raw = f'CALL { schema_name } .build_nested_admin_polygons();'
524+ # Populate the table
525+ sql_raw_1 = f'CALL { schema_name } .populate_place_polygon_nested();'
519526
520527 conn_string = os .environ ['PGOSM_CONN' ]
521- cmds = ['psql' , '-d' , conn_string , '-c' , sql_raw ]
528+ cmds = ['psql' , '-d' , conn_string , '-c' , sql_raw_1 ]
529+ LOGGER .info ('Populating place_polygon_nested table (osm.populate_place_polygon_nested() )' )
530+ output = subprocess .run (cmds ,
531+ text = True ,
532+ cwd = flex_path ,
533+ check = False ,
534+ stdout = subprocess .PIPE ,
535+ stderr = subprocess .STDOUT )
536+ LOGGER .info (f'Nested polygon output: \n { output .stdout } ' )
537+
538+ if output .returncode != 0 :
539+ err_msg = f'Failed to populate nested polygon data. Return code: { output .returncode } '
540+ LOGGER .error (err_msg )
541+ sys .exit (f'{ err_msg } - Check the log output for details.' )
542+
543+
544+ # Build the data
545+ sql_raw_2 = f' CALL { schema_name } .build_nested_admin_polygons();'
546+
547+ conn_string = os .environ ['PGOSM_CONN' ]
548+ cmds = ['psql' , '-d' , conn_string , '-c' , sql_raw_2 ]
522549 LOGGER .info ('Building nested polygons... (this can take a while)' )
523550 output = subprocess .run (cmds ,
524551 text = True ,
@@ -537,18 +564,23 @@ def pgosm_nested_admin_polygons(flex_path: str, schema_name: str):
537564
538565def osm2pgsql_replication_start ():
539566 """Runs pre-replication step to clean out FKs that would prevent updates.
567+
568+ This function is necessary for using `--replication (osm2pgsql-replication)
569+ and `--update append` mode.
540570 """
541571 LOGGER .info ('Prep database to allow data updates.' )
542- # This use of append applies to both osm2pgsql --append and osm2pgsq-replication, not renaming from "append"
543572 sql_raw = 'CALL osm.append_data_start();'
544573
545574 with get_db_conn (conn_string = connection_string ()) as conn :
546575 cur = conn .cursor ()
547576 cur .execute (sql_raw )
548577
549578
550- def osm2pgsql_replication_finish (skip_nested ):
551- """Runs post-replication step to put FKs back and refresh materialied views.
579+ def osm2pgsql_replication_finish (skip_nested : bool ):
580+ """Runs post-replication step to refresh materialized views and rebuild
581+ nested data when appropriate.
582+
583+ Only needed for `--replication`, not used for `--update append` mode.
552584
553585 Parameters
554586 ---------------------
0 commit comments