-
Notifications
You must be signed in to change notification settings - Fork 20
QGIS styles - set db and schema names #308
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
5b0cc0e
Moving and restructuring QGIS style code. Fix broken tests for replic…
rustprooflabs ce7e5c6
Finish restructuring original code, add update to handle different DB…
rustprooflabs 10a38ac
Various documentation improvements, QGIS, troubleshooting, load times.
rustprooflabs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| Documentation moved to https://pgosm-flex.com | ||
| Documentation moved to https://pgosm-flex.com/qgis-styles.html |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| """PgOSM Flex module to handle loading QGIS styles to Postgres. | ||
| """ | ||
| import logging | ||
| import os | ||
| import subprocess | ||
|
|
||
| import db | ||
|
|
||
|
|
||
| LOGGER = logging.getLogger('pgosm-flex') | ||
|
|
||
|
|
||
| def load_qgis_styles(db_path, db_name, schema_name): | ||
| """Loads QGIS style data for easy formatting of most common layers. | ||
| Parameters | ||
| ------------------------- | ||
| db_path : str | ||
| Base path to pgosm-flex/db directory | ||
| db_name : str | ||
| schema_name : str | ||
| """ | ||
| LOGGER.info(f'Load QGIS styles to database {db_name}...') | ||
Check failureCode scanning / CodeQL Clear-text logging of sensitive information
This expression logs [sensitive data (password)](1) as clear text.
|
||
| conn_string = os.environ['PGOSM_CONN'] | ||
|
|
||
| create_layer_style_table(db_path=db_path, conn_string=conn_string) | ||
| populate_layer_style_staging(db_path=db_path, conn_string=conn_string) | ||
| update_styles_db_name(db_name=db_name, schema_name=schema_name, | ||
| conn_string=conn_string) | ||
| load_staging_to_prod(db_path=db_path, conn_string=conn_string) | ||
|
|
||
|
|
||
| def create_layer_style_table(db_path, conn_string): | ||
| """Ensures QGIS layer styles table exists. | ||
| Parameters | ||
| -------------------- | ||
| db_path : str | ||
| conn_string : path | ||
| """ | ||
| create_path = os.path.join(db_path, | ||
| 'qgis-style', | ||
| 'create_layer_styles.sql') | ||
|
|
||
| with open(create_path, 'r') as file_in: | ||
| create_sql = file_in.read() | ||
|
|
||
| with db.get_db_conn(conn_string=conn_string) as conn: | ||
| cur = conn.cursor() | ||
| cur.execute(create_sql) | ||
| LOGGER.debug('QGIS Style table created') | ||
|
|
||
|
|
||
| def populate_layer_style_staging(db_path, conn_string): | ||
| """Loads data to public.layer_styles_staging using psql | ||
| Parameters | ||
| -------------------- | ||
| db_path : str | ||
| conn_string : path | ||
| """ | ||
| # Loading layer_styles data is done from files created by pg_dump, using | ||
| # psql to reload is easiest | ||
| cmds_populate = ['psql', '-d', conn_string, | ||
| '-f', 'qgis-style/layer_styles.sql'] | ||
|
|
||
| output = subprocess.run(cmds_populate, | ||
| text=True, | ||
| capture_output=True, | ||
| cwd=db_path, | ||
| check=False) | ||
|
|
||
| LOGGER.debug(f'Output from loading QGIS style data: {output.stdout}') | ||
|
|
||
|
|
||
| def load_staging_to_prod(db_path, conn_string): | ||
| """Loads data from public.layer_styles_staging to public.layer_styles. | ||
| Parameters | ||
| -------------------- | ||
| db_path : str | ||
| conn_string : path | ||
| """ | ||
| load_path = os.path.join(db_path, | ||
| 'qgis-style', | ||
| '_load_layer_styles.sql') | ||
|
|
||
| with open(load_path, 'r') as file_in: | ||
| load_sql = file_in.read() | ||
|
|
||
| with db.get_db_conn(conn_string=conn_string) as conn: | ||
| cur = conn.cursor() | ||
| cur.execute(load_sql) | ||
|
|
||
| LOGGER.info('QGIS Style table populated') | ||
|
|
||
| with db.get_db_conn(conn_string=conn_string) as conn: | ||
| sql_clean = 'DELETE FROM public.layer_styles_staging;' | ||
| cur = conn.cursor() | ||
| cur.execute(sql_clean) | ||
|
|
||
| LOGGER.debug('QGIS Style staging table cleaned') | ||
|
|
||
|
|
||
| def update_styles_db_name(db_name, schema_name, conn_string): | ||
| """ | ||
| Parameters | ||
| ---------------------- | ||
| db_name : str | ||
| schema_name : str | ||
| conn_string : str | ||
| """ | ||
| if db_name == 'pgosm' and schema_name == 'osm': | ||
| LOGGER.debug('Database name and schema name set to defaults. Update to layer styles not necessary') | ||
| return | ||
|
|
||
| sql_raw = """ | ||
| UPDATE public.layer_styles_staging | ||
| SET f_table_catalog = %(db_name)s , | ||
| f_table_schema = %(schema_name)s | ||
| ; | ||
| """ | ||
| params = {'db_name': db_name, 'schema_name': schema_name} | ||
| with db.get_db_conn(conn_string=conn_string) as conn: | ||
| cur = conn.cursor() | ||
| cur.execute(sql_raw, params=params) | ||
| conn.commit() | ||
|
|
||
| LOGGER.info(f'Updated QGIS layer styles for {db_name}.{schema_name}') | ||
Check failureCode scanning / CodeQL Clear-text logging of sensitive information
This expression logs [sensitive data (password)](1) as clear text.
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check failure
Code scanning / CodeQL
Clear-text logging of sensitive information