From 7e5291a875c0bfcf0356365fe505851936c72300 Mon Sep 17 00:00:00 2001 From: "Julien M." Date: Fri, 31 May 2024 17:25:40 +0200 Subject: [PATCH 1/4] fix(postgis): handle OGR error getting spatial reference from a PostGIS layer --- dicogis/georeaders/base_georeader.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/dicogis/georeaders/base_georeader.py b/dicogis/georeaders/base_georeader.py index 7cadc546..73b08bb3 100644 --- a/dicogis/georeaders/base_georeader.py +++ b/dicogis/georeaders/base_georeader.py @@ -247,7 +247,14 @@ def get_srs_details( tuple[str, str, str]: crs_name, crs_code, crs_type """ # SRS - srs = dataset_or_layer.GetSpatialRef() + srs = None + try: + srs = dataset_or_layer.GetSpatialRef() + except Exception as err: + logger.error( + "Error occurred getting spatiale reference for " + f"'{dataset_or_layer.GetName()}'. Trace: {err}" + ) if not srs: return ( self.localized_strings.get("srs_undefined", ""), From 30c4a538fc2ac181481db5aad8e24039523fbdbd Mon Sep 17 00:00:00 2001 From: "Julien M." Date: Fri, 31 May 2024 17:28:04 +0200 Subject: [PATCH 2/4] fix: missing dataset type for PostGIS table --- dicogis/georeaders/read_postgis.py | 1 + 1 file changed, 1 insertion(+) diff --git a/dicogis/georeaders/read_postgis.py b/dicogis/georeaders/read_postgis.py index e4ff9282..cc91f664 100644 --- a/dicogis/georeaders/read_postgis.py +++ b/dicogis/georeaders/read_postgis.py @@ -158,6 +158,7 @@ def infos_dataset( format_gdal_long_name=self.conn.GetDriver().LongName, format_gdal_short_name=self.conn.GetDriver().ShortName, database_connection=self.db_connection, + dataset_type="sgbd_postgis", ) # check layer type From 4fb2e587939f11e82c20d90c63feaeb6aa40ba8f Mon Sep 17 00:00:00 2001 From: "Julien M." Date: Sat, 1 Jun 2024 00:49:13 +0200 Subject: [PATCH 3/4] fix: postgis processing using cli --- dicogis/cli/cmd_inventory.py | 89 +++++++++++------ dicogis/export/base_serializer.py | 9 +- dicogis/export/to_json.py | 4 +- dicogis/export/to_xlsx.py | 137 ++++++++++++++++----------- dicogis/georeaders/base_georeader.py | 29 +++--- dicogis/ui/main_windows.py | 2 +- dicogis/utils/utils.py | 8 +- 7 files changed, 171 insertions(+), 107 deletions(-) diff --git a/dicogis/cli/cmd_inventory.py b/dicogis/cli/cmd_inventory.py index 2ea71156..626df226 100644 --- a/dicogis/cli/cmd_inventory.py +++ b/dicogis/cli/cmd_inventory.py @@ -42,6 +42,51 @@ # ################################## +def get_serializer_from_parameters( + output_format: str, + output_path: Path, + opt_raw_path: bool, + opt_prettify_size: bool, + localized_strings: dict | None = None, +) -> MetadatasetSerializerJson | MetadatasetSerializerXlsx: + """Initiate the adequat serializer depending on parameters. + + Args: + output_format: output format + output_path: output path + opt_raw_path: option to serialize dataset raw path without any sugar syntax + opt_prettify_size: option to prettify size in octets (typically: 1 ko instead + of 1024 octects) + localized_strings: localized texts. Defaults to None. + + Returns: + _description_ + """ + if output_format == "excel": + # creating the Excel workbook + output_serializer = MetadatasetSerializerXlsx( + opt_raw_path=opt_raw_path, + opt_size_prettify=opt_prettify_size, + output_path=output_path, + localized_strings=localized_strings, + ) + elif output_format == "json": + output_serializer = MetadatasetSerializerJson( + output_path=output_path, + localized_strings=localized_strings, + opt_size_prettify=opt_prettify_size, + ) + else: + logger.error( + NotImplementedError( + f"Specified output format '{output_format}' is not available." + ) + ) + typer.Exit(1) + + return output_serializer + + def determine_output_path( output_path: Path | str | None, output_format: str = "excel", @@ -54,6 +99,7 @@ def determine_output_path( output_path: output path passed to inventory CLI output_format: input output format passed to inventory CLI input_folder: input folder passed to inventory CLI + pg_services: list of ppostgres services names to use Raises: ValueError: if output format is not supported @@ -249,25 +295,6 @@ def inventory( localized_strings = TextsManager().load_texts(language_code=language) # output format - if output_format == "excel": - # creating the Excel workbook - output_serializer = MetadatasetSerializerXlsx( - translated_texts=localized_strings, - opt_raw_path=opt_raw_path, - opt_size_prettify=opt_prettify_size, - output_path=output_path, - ) - elif output_format == "json": - output_serializer = MetadatasetSerializerJson( - translated_texts=localized_strings, output_path=output_path - ) - else: - logger.error( - NotImplementedError( - f"Specified output format '{output_format}' is not available." - ) - ) - typer.Exit(1) # look for geographic data files if input_folder is not None: @@ -277,6 +304,13 @@ def inventory( input_folder=input_folder, ) + output_serializer = get_serializer_from_parameters( + output_format=output_format, + output_path=output_path, + opt_prettify_size=opt_prettify_size, + opt_raw_path=opt_raw_path, + ) + li_vectors = [] ( num_folders, @@ -391,15 +425,22 @@ def inventory( output_path = determine_output_path( output_path=output_path, output_format=output_format, - input_folder=input_folder, pg_services=pg_services, ) + output_serializer = get_serializer_from_parameters( + output_format=output_format, + output_path=output_path, + opt_prettify_size=opt_prettify_size, + opt_raw_path=opt_raw_path, + ) + print("Start looking for geographic table in PostGIS...") # configure output workbook output_serializer.pre_serializing(has_sgbd=True) for pg_service in pg_services: + print(f"Start processing using PostgreSQL service: {pg_service}") # testing connection settings sgbd_reader = ReadPostGIS(service=pg_service) @@ -427,14 +468,6 @@ def inventory( logger.debug("Layer metadata stored into workbook.") output_serializer.post_serializing() - saved = Utilities.safe_save( - output_object=output_serializer, - dest_dir=f"{output_path.parent.resolve()}", - dest_filename=f"{output_path.resolve()}", - ftype="Excel Workbook", - gui=False, - ) - logger.info(f"Workbook saved: {saved[1]}") send_system_notify( notification_title="DicoGIS analysis ended", diff --git a/dicogis/export/base_serializer.py b/dicogis/export/base_serializer.py index 7946332d..81d2700a 100644 --- a/dicogis/export/base_serializer.py +++ b/dicogis/export/base_serializer.py @@ -9,6 +9,9 @@ import logging from pathlib import Path +# project +from dicogis.utils.texts import TextsManager + # ############################################################################ # ######### Globals ############ # ############################## @@ -25,13 +28,15 @@ class MetadatasetSerializerBase: def __init__( self, - translated_texts: dict, + localized_strings: dict | None = None, output_path: Path | None = None, opt_raw_path: bool = False, opt_size_prettify: bool = True, ) -> None: """Initialize object.""" - self.translated_texts = translated_texts + self.localized_strings = localized_strings + if self.localized_strings is None: + self.localized_strings = TextsManager().load_texts() # output path self.output_path = output_path diff --git a/dicogis/export/to_json.py b/dicogis/export/to_json.py index cebc3514..d42ee491 100644 --- a/dicogis/export/to_json.py +++ b/dicogis/export/to_json.py @@ -36,7 +36,7 @@ class MetadatasetSerializerJson(MetadatasetSerializerBase): def __init__( self, - translated_texts: dict, + localized_strings: dict | None = None, output_path: Path | None = None, opt_size_prettify: bool = True, ) -> None: @@ -44,7 +44,7 @@ def __init__( output_path.mkdir(parents=True, exist_ok=True) super().__init__( - translated_texts=translated_texts, + localized_strings=localized_strings, opt_size_prettify=opt_size_prettify, output_path=output_path, ) diff --git a/dicogis/export/to_xlsx.py b/dicogis/export/to_xlsx.py index 66a99208..a658dfa4 100644 --- a/dicogis/export/to_xlsx.py +++ b/dicogis/export/to_xlsx.py @@ -148,7 +148,7 @@ class MetadatasetSerializerXlsx(MetadatasetSerializerBase): def __init__( self, - translated_texts: dict, + localized_strings: dict | None = None, output_path: Path | None = None, opt_raw_path: bool = False, opt_size_prettify: bool = True, @@ -174,7 +174,7 @@ def __init__( # initiate with parent super().__init__( - translated_texts=translated_texts, + localized_strings=localized_strings, output_path=output_path, opt_raw_path=opt_raw_path, opt_size_prettify=opt_size_prettify, @@ -205,15 +205,15 @@ def pre_serializing( # SHEETS & HEADERS if ( has_vector - and self.translated_texts.get("sheet_vectors") + and self.localized_strings.get("sheet_vectors") not in self.workbook.sheetnames ): self.sheet_vector_files = self.workbook.create_sheet( - title=self.translated_texts.get("sheet_vectors") + title=self.localized_strings.get("sheet_vectors") ) # headers self.sheet_vector_files.append( - [self.translated_texts.get(i) for i in self.li_cols_vector] + [self.localized_strings.get(i) for i in self.li_cols_vector] ) # initialize line counter @@ -221,15 +221,15 @@ def pre_serializing( if ( has_raster - and self.translated_texts.get("sheet_rasters") + and self.localized_strings.get("sheet_rasters") not in self.workbook.sheetnames ): self.sheet_raster_files = self.workbook.create_sheet( - title=self.translated_texts.get("sheet_rasters") + title=self.localized_strings.get("sheet_rasters") ) # headers self.sheet_raster_files.append( - [self.translated_texts.get(i) for i in self.li_cols_raster] + [self.localized_strings.get(i) for i in self.li_cols_raster] ) # initialize line counter @@ -237,15 +237,15 @@ def pre_serializing( if ( has_filedb - and self.translated_texts.get("sheet_filedb") + and self.localized_strings.get("sheet_filedb") not in self.workbook.sheetnames ): self.sheet_flat_geodatabases = self.workbook.create_sheet( - title=self.translated_texts.get("sheet_filedb") + title=self.localized_strings.get("sheet_filedb") ) # headers self.sheet_flat_geodatabases.append( - [self.translated_texts.get(i) for i in self.li_cols_filedb] + [self.localized_strings.get(i) for i in self.li_cols_filedb] ) # initialize line counter @@ -253,15 +253,15 @@ def pre_serializing( if ( has_mapdocs - and self.translated_texts.get("sheet_maplans") + and self.localized_strings.get("sheet_maplans") not in self.workbook.sheetnames ): self.sheet_map_workspaces = self.workbook.create_sheet( - title=self.translated_texts.get("sheet_maplans") + title=self.localized_strings.get("sheet_maplans") ) # headers self.sheet_map_workspaces.append( - [self.translated_texts.get(i) for i in self.li_cols_mapdocs] + [self.localized_strings.get(i) for i in self.li_cols_mapdocs] ) # initialize line counter @@ -269,14 +269,14 @@ def pre_serializing( if ( has_cad - and self.translated_texts.get("sheet_cdao") not in self.workbook.sheetnames + and self.localized_strings.get("sheet_cdao") not in self.workbook.sheetnames ): self.sheet_cad_files = self.workbook.create_sheet( - title=self.translated_texts.get("sheet_cdao") + title=self.localized_strings.get("sheet_cdao") ) # headers self.sheet_cad_files.append( - [self.translated_texts.get(i) for i in self.li_cols_caodao] + [self.localized_strings.get(i) for i in self.li_cols_caodao] ) # initialize line counter @@ -286,7 +286,7 @@ def pre_serializing( self.sheet_server_geodatabases = self.workbook.create_sheet(title="PostGIS") # headers self.sheet_server_geodatabases.append( - [self.translated_texts.get(i) for i in self.li_cols_sgbd] + [self.localized_strings.get(i) for i in self.li_cols_sgbd] ) # initialize line counter self.row_index_server_geodatabases = 1 @@ -394,18 +394,21 @@ def format_feature_attributes(self, metadataset: MetaVectorDataset) -> str: """ out_attributes_str = "" + if not isinstance(metadataset.feature_attributes, (list, tuple)): + return out_attributes_str + for feature_attribute in metadataset.feature_attributes: # field type if "integer" in feature_attribute.data_type.lower(): - translated_feature_attribute_type = self.translated_texts.get("entier") + translated_feature_attribute_type = self.localized_strings.get("entier") elif feature_attribute.data_type.lower() == "real": - translated_feature_attribute_type = self.translated_texts.get("reel") + translated_feature_attribute_type = self.localized_strings.get("reel") elif feature_attribute.data_type.lower() == "string": - translated_feature_attribute_type = self.translated_texts.get("string") + translated_feature_attribute_type = self.localized_strings.get("string") elif feature_attribute.data_type.lower() in ("date", "datetime"): - translated_feature_attribute_type = self.translated_texts.get("date") + translated_feature_attribute_type = self.localized_strings.get("date") elif feature_attribute.data_type.lower() == "binary": - translated_feature_attribute_type = self.translated_texts.get("binary") + translated_feature_attribute_type = self.localized_strings.get("binary") else: translated_feature_attribute_type = feature_attribute.data_type logger.warning( @@ -418,9 +421,9 @@ def format_feature_attributes(self, metadataset: MetaVectorDataset) -> str: out_attributes_str, feature_attribute.name, translated_feature_attribute_type, - self.translated_texts.get("longueur"), + self.localized_strings.get("longueur"), feature_attribute.length, - self.translated_texts.get("precision"), + self.localized_strings.get("precision"), feature_attribute.precision, ) @@ -454,16 +457,14 @@ def store_error( worksheet (Worksheet): Excel workbook's sheet where to store row_index (int): worksheet's row index """ - err_mess = self.translated_texts.get(metadataset.processing_error_type) - logger.warning( - f"Problem detected on {metadataset.name} (in {metadataset.path_as_str}). " - f"Error: {err_mess}" + err_mess = self.localized_strings.get( + metadataset.processing_error_type, metadataset.processing_error_msg ) worksheet[f"A{row_index}"] = metadataset.name worksheet[f"A{row_index}"].style = "Warning Text" worksheet[f"B{row_index}"] = self.format_as_hyperlink( target=metadataset.path_as_str, - label=self.translated_texts.get("browse"), + label=self.localized_strings.get("browse"), ) worksheet[f"B{row_index}"].style = "Warning Text" worksheet[f"C{row_index}"] = err_mess @@ -474,6 +475,10 @@ def store_error( f"{metadataset.processing_error_msg}" ) worksheet[f"Q{row_index}"].style = "Warning Text" + logger.debug( + f"Processing error detected on {metadataset.name} (in " + f"{metadataset.path_as_str}) ({err_mess}) has been stored." + ) def get_sheet_and_incremented_row_index_from_type( self, @@ -596,7 +601,7 @@ def store_md_vector_files( else: worksheet[f"B{row_index}"] = self.format_as_hyperlink( target=metadataset.path.parent, - label=self.translated_texts.get("browse"), + label=self.localized_strings.get("browse"), ) worksheet[f"B{row_index}"].style = "Hyperlink" @@ -611,12 +616,18 @@ def store_md_vector_files( worksheet[f"F{row_index}"] = metadataset.geometry_type # Name of srs - worksheet[f"G{row_index}"] = metadataset.crs_name + worksheet[f"G{row_index}"] = self.localized_strings.get( + metadataset.crs_name, "" + ) # Type of SRS - worksheet[f"H{row_index}"] = metadataset.crs_type + worksheet[f"H{row_index}"] = self.localized_strings.get( + metadataset.crs_type, "" + ) # EPSG code - worksheet[f"I{row_index}"] = metadataset.crs_registry_code + worksheet[f"I{row_index}"] = self.localized_strings.get( + metadataset.crs_registry_code, "" + ) # Spatial extent worksheet[f"J{row_index}"].style = "wrap" worksheet[f"J{row_index}"] = self.format_bbox(bbox=metadataset.bbox) @@ -667,7 +678,7 @@ def store_md_raster_files( else: worksheet[f"B{row_index}"] = self.format_as_hyperlink( target=metadataset.path.parent, - label=self.translated_texts.get("browse"), + label=self.localized_strings.get("browse"), ) worksheet[f"B{row_index}"].style = "Hyperlink" @@ -743,7 +754,7 @@ def store_md_flat_geodatabases( else: worksheet[f"B{row_index}"] = self.format_as_hyperlink( target=metadataset.path.parent, - label=self.translated_texts.get("browse"), + label=self.localized_strings.get("browse"), ) worksheet[f"B{row_index}"].style = "Hyperlink" worksheet[f"C{row_index}"] = metadataset.parent_folder_name @@ -769,7 +780,7 @@ def store_md_flat_geodatabases( # in case of a source error if metadataset.processing_succeeded is False: - err_mess = self.translated_texts.get( + err_mess = self.localized_strings.get( layer_metadataset.processing_error_type ) logger.warning( @@ -786,9 +797,15 @@ def store_md_flat_geodatabases( worksheet[f"I{row_index}"] = layer_metadataset.count_feature_attributes worksheet[f"J{row_index}"] = layer_metadataset.features_objects_count worksheet[f"K{row_index}"] = layer_metadataset.geometry_type - worksheet[f"L{row_index}"] = layer_metadataset.crs_name - worksheet[f"M{row_index}"] = layer_metadataset.crs_type - worksheet[f"N{row_index}"] = layer_metadataset.crs_registry_code + worksheet[f"L{row_index}"] = self.localized_strings.get( + metadataset.crs_name, "" + ) + worksheet[f"M{row_index}"] = self.localized_strings.get( + metadataset.crs_type, "" + ) + worksheet[f"N{row_index}"] = self.localized_strings.get( + metadataset.crs_registry_code, "" + ) # Spatial extent worksheet[f"O{row_index}"].style = "wrap" @@ -827,7 +844,7 @@ def store_md_mapdoc( else: worksheet[f"B{row_index}"] = self.format_as_hyperlink( target=metadataset.path.parent, - label=self.translated_texts.get("browse"), + label=self.localized_strings.get("browse"), ) worksheet[f"B{row_index}"].style = "Hyperlink" @@ -841,9 +858,15 @@ def store_md_mapdoc( worksheet[f"K{row_index}"] = metadataset.storage_date_updated worksheet[f"L{row_index}"] = metadataset.get("xOrigin") worksheet[f"M{row_index}"] = metadataset.get("yOrigin") - worksheet[f"N{row_index}"] = metadataset.crs_name - worksheet[f"O{row_index}"] = metadataset.crs_type - worksheet[f"P{row_index}"] = metadataset.crs_registry_code + worksheet[f"N{row_index}"] = self.localized_strings.get( + metadataset.crs_name, "" + ) + worksheet[f"O{row_index}"] = self.localized_strings.get( + metadataset.crs_type, "" + ) + worksheet[f"P{row_index}"] = self.localized_strings.get( + metadataset.crs_registry_code, "" + ) worksheet[f"Q{row_index}"] = metadataset.format_gdal_long_name worksheet[f"R{row_index}"] = metadataset.count_feature_attributes worksheet[f"S{row_index}"] = metadataset.features_objects_count @@ -864,7 +887,7 @@ def store_md_mapdoc( ) # in case of a source error if mdoc_layer.get("error"): - err_mess = self.translated_texts.get(mdoc_layer.get("error")) + err_mess = self.localized_strings.get(mdoc_layer.get("error")) logger.warning( "Problem detected: {} in {}".format( err_mess, mdoc_layer.get("title") @@ -888,13 +911,13 @@ def store_md_mapdoc( for chp in fields.keys(): # field type if "Integer" in fields[chp][0]: - tipo = self.translated_texts.get("entier") + tipo = self.localized_strings.get("entier") elif fields[chp][0] == "Real": - tipo = self.translated_texts.get("reel") + tipo = self.localized_strings.get("reel") elif fields[chp][0] == "String": - tipo = self.translated_texts.get("string") + tipo = self.localized_strings.get("string") elif fields[chp][0] == "Date": - tipo = self.translated_texts.get("date") + tipo = self.localized_strings.get("date") else: tipo = "unknown" logger.warning(chp + " unknown type") @@ -905,9 +928,9 @@ def store_md_mapdoc( champs, chp.decode("utf8", "replace"), tipo, - self.translated_texts.get("longueur"), + self.localized_strings.get("longueur"), fields[chp][1], - self.translated_texts.get("precision"), + self.localized_strings.get("precision"), fields[chp][2], ) except UnicodeDecodeError: @@ -961,9 +984,15 @@ def store_md_geodatabases_server( worksheet[f"F{row_index}"] = metadataset.geometry_type # SRS - worksheet[f"G{row_index}"] = metadataset.crs_name - worksheet[f"H{row_index}"] = metadataset.crs_type - worksheet[f"I{row_index}"] = metadataset.crs_registry_code + worksheet[f"G{row_index}"] = self.localized_strings.get( + metadataset.crs_name, "" + ) + worksheet[f"H{row_index}"] = self.localized_strings.get( + metadataset.crs_type, "" + ) + worksheet[f"I{row_index}"] = self.localized_strings.get( + metadataset.crs_registry_code, "" + ) # Spatial extent worksheet[f"J{row_index}"].style = "wrap" diff --git a/dicogis/georeaders/base_georeader.py b/dicogis/georeaders/base_georeader.py index 73b08bb3..37069aab 100644 --- a/dicogis/georeaders/base_georeader.py +++ b/dicogis/georeaders/base_georeader.py @@ -252,14 +252,14 @@ def get_srs_details( srs = dataset_or_layer.GetSpatialRef() except Exception as err: logger.error( - "Error occurred getting spatiale reference for " + "Error occurred getting spatial reference for " f"'{dataset_or_layer.GetName()}'. Trace: {err}" ) if not srs: return ( - self.localized_strings.get("srs_undefined", ""), - self.localized_strings.get("srs_no_epsg", ""), - self.localized_strings.get("srs_nr", ""), + "srs_undefined", + "srs_no_epsg", + "srs_nr", ) srs.AutoIdentifyEPSG() @@ -268,17 +268,14 @@ def get_srs_details( # srs type srsmetod = [ - (srs.IsDynamic(), self.localized_strings.get("srs_dyna", "Dynamic")), - (srs.IsCompound(), self.localized_strings.get("srs_comp", "Compound")), - ( - srs.IsDerivedGeographic(), - self.localized_strings.get("srs_derg", "Derived geographic"), - ), - (srs.IsGeocentric(), self.localized_strings.get("srs_geoc", "Geocentric")), - (srs.IsGeographic(), self.localized_strings.get("srs_geog", "Geographic")), - (srs.IsLocal(), self.localized_strings.get("srs_loca", "Local")), - (srs.IsProjected(), self.localized_strings.get("srs_proj", "Projected")), - (srs.IsVertical(), self.localized_strings.get("srs_vert", "Vertical")), + (srs.IsDynamic(), "srs_dyna"), + (srs.IsCompound(), "srs_comp"), + (srs.IsDerivedGeographic(), "srs_derg"), + (srs.IsGeocentric(), "srs_geoc"), + (srs.IsGeographic(), "srs_geog"), + (srs.IsLocal(), "srs_loca"), + (srs.IsProjected(), "srs_proj"), + (srs.IsVertical(), "srs_vert"), ] # searching for a match with one of srs types for srsmet in srsmetod: @@ -290,7 +287,7 @@ def get_srs_details( try: srs_type = typsrs except UnboundLocalError: - typsrs = self.localized_strings.get("srs_nr") + typsrs = "srs_nr" srs_type = typsrs # handling exceptions in srs names'encoding diff --git a/dicogis/ui/main_windows.py b/dicogis/ui/main_windows.py index 19b1ea3e..da0756fc 100644 --- a/dicogis/ui/main_windows.py +++ b/dicogis/ui/main_windows.py @@ -505,7 +505,7 @@ def process(self): # creating the Excel workbook self.xl_workbook = MetadatasetSerializerXlsx( - translated_texts=self.localized_strings, + localized_strings=self.localized_strings, opt_raw_path=self.tab_options.opt_export_raw_path.get(), opt_size_prettify=self.tab_options.opt_export_size_prettify.get(), ) diff --git a/dicogis/utils/utils.py b/dicogis/utils/utils.py index 7f870315..73802ff1 100644 --- a/dicogis/utils/utils.py +++ b/dicogis/utils/utils.py @@ -5,6 +5,8 @@ # ######## Libraries ############# # ################################ +from __future__ import annotations + # Standard library import logging import subprocess @@ -15,7 +17,6 @@ from tkinter import ACTIVE, DISABLED from tkinter.filedialog import asksaveasfilename # dialogs from tkinter.messagebox import showerror as avert -from typing import Optional # Imports depending on operating system if opersys == "win32": @@ -25,7 +26,6 @@ # package from dicogis.__about__ import __package_name__ -from dicogis.export.to_xlsx import MetadatasetSerializerXlsx from dicogis.utils.check_path import check_path # ############################################################################## @@ -117,11 +117,11 @@ def open_dir_file(cls, target: str | Path) -> subprocess.Popen | None: @classmethod def safe_save( cls, - output_object: MetadatasetSerializerXlsx, + output_object, dest_dir: str = r".", dest_filename: str = "DicoGIS.xlsx", ftype: str = "Excel Workbook", - dlg_title: Optional[str] = "DicoGIS - Save output Excel Workbook", + dlg_title: str | None = "DicoGIS - Save output Excel Workbook", gui: bool = True, ): """Safe save output file.""" From b0d542600b549d3a43ff8568350221f1e1e2cfc6 Mon Sep 17 00:00:00 2001 From: "Julien M." Date: Tue, 4 Jun 2024 10:59:53 +0200 Subject: [PATCH 4/4] dev: script to parse a postgis db --- tests/dev/dev_infos_postgis.py | 48 ++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/dev/dev_infos_postgis.py diff --git a/tests/dev/dev_infos_postgis.py b/tests/dev/dev_infos_postgis.py new file mode 100644 index 00000000..ba1a35f2 --- /dev/null +++ b/tests/dev/dev_infos_postgis.py @@ -0,0 +1,48 @@ +from osgeo import gdal, ogr + +# gdal.SetConfigOption("PG_LIST_ALL_TABLES", "YES") + +pg_service_name = "my_pg_service_name" + +conn: gdal.Dataset = gdal.OpenEx( + f"postgresql://?service={pg_service_name}", + gdal.OF_READONLY | gdal.OF_VECTOR | gdal.OF_VERBOSE_ERROR, +) + +print(f"{conn.GetLayerCount()} tables found in PostGIS database.") + +for idx_layer in range(conn.GetLayerCount()): + layer: ogr.Layer = conn.GetLayerByIndex(idx_layer) + print(f"\nLayer: {layer.GetName()}") + print(f"{layer.GetFeatureCount()} features") + geom_type = layer.GetGeomType() + print(f"Geometry Type: {ogr.GeometryTypeToName(geom_type)}") + layer_def = layer.GetLayerDefn() + print(f"{layer_def.GetFieldCount()} feature attributes") + spatial_ref = layer.GetSpatialRef() + print(f"Spatial reference from layer: {spatial_ref}") + feature = layer.GetNextFeature() + if not feature: + continue + geometry = feature.GetGeometryRef() + spatial_ref = geometry.GetSpatialReference() + print(f"Spatial reference from geometry: {spatial_ref}") + + # Get the index of the geometry field + geom_field_index = layer_def.GetGeomFieldIndex("geom") + if geom_field_index == -1: + raise Exception(f"Geometry column 'geom' not found in table {table_name}") + + # Get the geometry field definition + geom_field_defn = layer_def.GetGeomFieldDefn(geom_field_index) + # Get the SRID from the geometry field definition + print(geom_field_defn.GetSpatialRef()) + print(f"Geometry column: {layer.GetGeometryColumn()}") + + # sql_command = ( + # "SELECT ST_SRID('the_geom') AS srid FROM " + f'"{layer.GetName()}"' + "LIMIT 1;" + # ) + # result = conn.ExecuteSQL(sql_command) + # print(result) + + del layer, layer_def, geom_field_defn