def test_multi_layer_save(tmp_path): """ Test multi_layer_save. """ geodataframes, layer_names = transform.load_multilayer( Helpers.test_multilayer_file_path ) assert len(geodataframes) == len(layer_names) == 2 filenames = [] for layer_name in layer_names: filenames.append(tmp_path / f"{layer_name}_test_multi_layer.shp") transform.save_files(geodataframes, layer_names, filenames, transform.SHAPEFILE)
def test_load_filegeodatabase(tmp_path): """ Helpers.tests loading a filegeodatabase. """ geodataframes, layer_names = transform.load_multilayer( Helpers.test_filegeodatabase_file_path ) assert len(geodataframes) == len(layer_names) filenames = [] for layer_name in layer_names: filenames.append(tmp_path / f"{layer_name}_test_filegeodatabase.shp") # Save to multiple shapefiles transform.save_files(geodataframes, layer_names, filenames, transform.SHAPEFILE) # Save same files to a single geopackage filenames = [tmp_path / "saving_filegeodatabase.gpkg"] transform.save_files(geodataframes, layer_names, filenames, transform.GEOPACKAGE)
def test_single_save_file_geojson(tmp_path): """ Test single_save_file_geojson. """ # tmp_path is a temporary Path directory geodataframes, layer_names = transform.load_singlelayer( Helpers.test_singlelayer_file_path, transform.determine_filetype(Helpers.test_singlelayer_file_path), ) filenames = [tmp_path / Helpers.test_single_file_save_path_geojson] try: transform.save_files( geodataframes, layer_names, transform_to_type=transform.GEOJSON, filenames=filenames, ) except fiona.errors.SchemaError: print([gdf.columns for gdf in geodataframes]) raise
def run_transform(inputs: Tuple[str, ...], transform_to_type: str, output: str): """ Run functions in transform.py. """ geodataframes, layer_names = [], [] for input_filename in inputs: gdfs, lnms = transform.load_file(Path(input_filename)) geodataframes.extend(gdfs) layer_names.extend(lnms) error_msg = ("Command line inputs did not match " "currently implemented functionality.\n" f"{inputs, transform_to_type, output}") # Check that output filetype can handle saving. if transform_to_type in transform.SAVE_CAPABLE: # Check if output is a directory -> save to multiple individual files. if Path(output).exists() and Path(output).is_dir(): click.echo(f"Saving to output folder: {output}") # Save individual geopackages, geojsons or shapefiles to a given directory. save_paths = [] assert len(layer_names) != 0 for layer_name in layer_names: save_paths.append( Path(output) / Path(f"{layer_name}.{transform_to_type}")) assert len(save_paths) != 0 transform.save_files(geodataframes, layer_names, save_paths, transform_to_type) finished(output) # Check if output is a single file that is also multilayer capable. elif (not Path(output).exists() and transform_to_type in transform.MULTILAYER_CAPABLE): click.echo(f"Saving all layers to {output}") # Save a single geopackage output file with multiple layers # (if there are multiple layers in inputs). # Make sure file suffix is in the saved file. output = (output if f".{transform_to_type}" in output else f"{output}.{transform_to_type}") transform.save_files(geodataframes, layer_names, [Path(output)], transform_to_type) finished(output) # Check if output is a single file and the output filetype does not # handle multilayer data. elif (not Path(output).exists() and transform_to_type not in transform.MULTILAYER_CAPABLE and len(geodataframes) == 1): click.echo(f"Saving layer to {output}") output = (output if f".{transform_to_type}" in output else f"{output}.{transform_to_type}") transform.save_files(geodataframes, layer_names, [Path(output)], transform_to_type) finished(output) # If file exists, overwriting is not currently not supported. -> error elif Path(output).is_file(): raise FileExistsError( "Output file already exists. Overwriting currently not possible." ) # If multilayer data has been read but output filetype does not support # multiple layers -> error elif (not Path(output).exists() and transform_to_type not in transform.MULTILAYER_CAPABLE and len(geodataframes) != 1): raise TypeError( "Cannot save multilayer data to filetype that does not support" "multiple layers. Please give a directory to save" "files in individual type: {transform_to_type} files or " "use a multilayer capable filetype.") else: raise NotImplementedError(error_msg) else: raise NotImplementedError(error_msg)