From 64f04b21ccdb520edf4cbd022bd63195262702fe Mon Sep 17 00:00:00 2001 From: Qiusheng Wu Date: Sat, 11 Nov 2023 01:02:28 -0500 Subject: [PATCH] Improve file handling --- leafmap/common.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/leafmap/common.py b/leafmap/common.py index 2eff5040f7..f2170049b5 100644 --- a/leafmap/common.py +++ b/leafmap/common.py @@ -11605,6 +11605,7 @@ def gdb_to_vector( gdal_driver: str = "GPKG", file_extension: Optional[str] = None, overwrite: bool = False, + quiet=False, **kwargs, ): """Converts layers from a File Geodatabase (GDB) to a vector format. @@ -11617,6 +11618,7 @@ def gdb_to_vector( gdal_driver (str): The GDAL driver name for the output vector format. Default is "GPKG". file_extension (Optional[str]): The file extension for the output files. If None, it will be determined automatically based on the gdal_driver. Default is None. overwrite (bool): Whether to overwrite the existing output files. Default is False. + quiet (bool): If True, suppress the log output. Defaults to False. Returns: None @@ -11640,6 +11642,9 @@ def gdb_to_vector( if len(filenames) != len(layers): raise ValueError("The length of filenames must match the length of layers.") + if not os.path.exists(out_dir): + os.makedirs(out_dir) + ii = 0 # Iterate over the layers for i in range(layer_count): @@ -11665,6 +11670,9 @@ def gdb_to_vector( if os.path.exists(output_file) and not overwrite: print(f"File {output_file} already exists. Skipping...") continue + else: + if not quiet: + print(f"Converting layer {feature_class_name} to {output_file}...") # Create the output driver output_driver = ogr.GetDriverByName(gdal_driver) @@ -11709,7 +11717,9 @@ def gdb_layer_names(gdb_path: str) -> List[str]: return layer_names -def vector_to_parquet(source: str, output: str, crs=None, overwrite=False, **kwargs) -> None: +def vector_to_parquet( + source: str, output: str, crs=None, overwrite=False, **kwargs +) -> None: """ Convert a GeoDataFrame or a file containing vector data to Parquet format. @@ -11738,6 +11748,11 @@ def vector_to_parquet(source: str, output: str, crs=None, overwrite=False, **kwa if crs is not None: gdf = gdf.to_crs(crs) + + out_dir = os.path.dirname(os.path.abspath(output)) + if not os.path.exists(out_dir): + os.makedirs(out_dir) + gdf.to_parquet(output, **kwargs)