def test_dissolve_multisinglepolygons(tmpdir): # Init tmp_dir = Path(tmpdir) tmp_dir.mkdir(parents=True, exist_ok=True) # Prepare test data + test for suffix in test_helper.get_test_suffix_list(): for crs_epsg in test_helper.get_test_crs_epsg_list(): # Write test file in the right format + crs print(f"Run test for suffix {suffix} and crs_epsg {crs_epsg}") test_gdf = gpd.GeoDataFrame(geometry=[ test_helper.TestData.polygon_with_island, test_helper.TestData.multipolygon ], crs=test_helper.TestData.crs_epsg) # Reproject if needed if test_helper.TestData.crs_epsg != crs_epsg: test_gdf = test_gdf.to_crs(epsg=crs_epsg) assert isinstance(test_gdf, gpd.GeoDataFrame) input_path = tmp_dir / f"polygons_{crs_epsg}{suffix}" gfo.to_file(test_gdf, input_path) output_path = tmp_dir / f"{input_path.stem}-output{suffix}" # Now run test basetest_dissolve_multisinglepolygons(input_path, output_path)
def test_copy(tmpdir): # Prepare test data + run tests tmp_dir = Path(tmpdir) for suffix in test_helper.get_test_suffix_list(): # If test input file is in wrong format, convert it src = test_helper.prepare_test_file( path=test_helper.TestFiles.polygons_parcels_gpkg, tmp_dir=tmp_dir, suffix=suffix) # Copy to dest file dst = Path(tmpdir) / f"polygons_parcels_output{suffix}" gfo.copy(src, dst) assert src.exists() == True assert dst.exists() == True if suffix == ".shp": assert dst.with_suffix(".shx").exists() == True # Copy to dest dir dst_dir = Path(tmpdir) / "dest_dir" dst_dir.mkdir(parents=True, exist_ok=True) gfo.copy(src, dst_dir) dst = dst_dir / src.name assert src.exists() == True assert dst.exists() == True if suffix == ".shp": assert dst.with_suffix(".shx").exists() == True
def test_export_by_distance(tmpdir): # Prepare test data + run tests tmp_dir = Path(tmpdir) tmp_dir.mkdir(parents=True, exist_ok=True) for suffix in test_helper.get_test_suffix_list(): for crs_epsg in test_helper.get_test_crs_epsg_list(): # If test input file is in wrong format, convert it input_to_select_from_path = test_helper.prepare_test_file( path=test_helper.TestFiles.polygons_parcels_gpkg, tmp_dir=tmp_dir, suffix=suffix, crs_epsg=crs_epsg) # If test input file is in wrong format, convert it input_to_compare_with_path = test_helper.prepare_test_file( path=test_helper.TestFiles.polygons_zones_gpkg, tmp_dir=tmp_dir, suffix=suffix, crs_epsg=crs_epsg) # Now run test output_path = tmp_dir / f"{input_to_select_from_path.stem}-output{suffix}" print(f"Run test for suffix {suffix}, crs_epsg {crs_epsg}") basetest_export_by_distance( input_to_select_from_path, input_to_compare_with_path, output_path)
def test_simplify(tmpdir): # Init tmp_dir = Path(tmpdir) tmp_dir.mkdir(parents=True, exist_ok=True) test_inputs = [] test_inputs.append({ "input_path": test_helper.TestFiles.polygons_parcels_gpkg, "geometrytype": GeometryType.MULTIPOLYGON}) test_inputs.append({ "input_path": test_helper.TestFiles.points_gpkg, "geometrytype": GeometryType.MULTIPOINT}) test_inputs.append({ "input_path": test_helper.TestFiles.linestrings_rows_of_trees_gpkg, "geometrytype": GeometryType.MULTILINESTRING}) # Prepare test data + run tests for suffix in test_helper.get_test_suffix_list(): for crs_epsg in test_helper.get_test_crs_epsg_list(): for test_input in test_inputs: # If test input file is in wrong format, convert it input_path = test_helper.prepare_test_file( path=test_input['input_path'], tmp_dir=tmp_dir, suffix=suffix, crs_epsg=crs_epsg) # Now run test output_path = tmp_dir / f"{input_path.stem}-output{suffix}" print(f"Run test for suffix {suffix}, crs_epsg {crs_epsg}, geometrytype {test_input['geometrytype']}") basetest_simplify(input_path, output_path, test_input['geometrytype'])
def test_rename_column(tmpdir): # Prepare test data + run tests for one layer tmp_dir = Path(tmpdir) for suffix in test_helper.get_test_suffix_list(): # If test input file is in wrong format, convert it test_path = test_helper.prepare_test_file( path=test_helper.TestFiles.polygons_parcels_gpkg, tmp_dir=tmp_dir, suffix=suffix) # Check if input file is ok orig_layerinfo = gfo.get_layerinfo(test_path) assert "OPPERVL" in orig_layerinfo.columns assert "area" not in orig_layerinfo.columns # Rename try: gfo.rename_column(test_path, "OPPERVL", "area") exception_raised = False except: exception_raised = True # Check if the result was expected if test_path.suffix == ".shp": # For shapefiles, columns cannot be renamed assert exception_raised is True else: # For file types that support rename, check if it worked assert exception_raised is False result_layerinfo = gfo.get_layerinfo(test_path) assert "OPPERVL" not in result_layerinfo.columns assert "area" in result_layerinfo.columns
def test_rename_layer(tmpdir): # Prepare test data + run tests for one layer tmp_dir = Path(tmpdir) for suffix in test_helper.get_test_suffix_list(): # If test input file is in wrong format, convert it src = test_helper.prepare_test_file( path=test_helper.TestFiles.polygons_parcels_gpkg, tmp_dir=tmp_dir, suffix=suffix) if suffix == ".gpkg": gfo.rename_layer(src, layer="parcels", new_layer="parcels_renamed") layernames_renamed = gfo.listlayers(path=src) assert layernames_renamed[0] == "parcels_renamed" elif suffix == ".shp": # Now test rename layer try: gfo.rename_layer(src, layer="polygons_parcels", new_layer="polygons_parcels_renamed") layernames_renamed = gfo.listlayers(path=src) assert layernames_renamed[0] == "polygons_parcels_renamed" except Exception as ex: assert "rename_layer is not possible" in str(ex) else: raise Exception(f"test not implemented for suffix {suffix}")
def test_move(tmpdir): # Prepare test data + run tests tmp_dir = Path(tmpdir) for suffix in test_helper.get_test_suffix_list(): # Test move to dest file src = test_helper.prepare_test_file( path=test_helper.TestFiles.polygons_parcels_gpkg, tmp_dir=tmp_dir, suffix=suffix) dst = Path(tmpdir) / f"polygons_parcels_output{suffix}" gfo.move(src, dst) assert src.exists() == False assert dst.exists() == True if suffix == ".shp": assert dst.with_suffix(".shx").exists() == True # Test move to dest dir src = test_helper.prepare_test_file( path=test_helper.TestFiles.polygons_parcels_gpkg, tmp_dir=tmp_dir, suffix=suffix) dst_dir = Path(tmpdir) / "dest_dir" dst_dir.mkdir(parents=True, exist_ok=True) gfo.move(src, dst_dir) dst = dst_dir / src.name assert src.exists() == False assert dst.exists() == True if suffix == ".shp": assert dst.with_suffix(".shx").exists() == True
def test_union_circles(tmpdir): # Prepare test data + run tests tmp_dir = Path(tmpdir) tmp_dir.mkdir(parents=True, exist_ok=True) for suffix in test_helper.get_test_suffix_list(): for crs_epsg in test_helper.get_test_crs_epsg_list(): # If test input file is in wrong format, convert it input1_path = test_helper.prepare_test_file( path=test_helper.TestFiles.polygons_overlappingcircles_one_gpkg, tmp_dir=tmp_dir, suffix=suffix, crs_epsg=crs_epsg) # If test input file is in wrong format, convert it input2_path = test_helper.prepare_test_file( path=test_helper.TestFiles.polygons_overlappingcircles_twothree_gpkg, tmp_dir=tmp_dir, suffix=suffix, crs_epsg=crs_epsg) # Now run test output_path = tmp_dir / f"{input1_path.stem}-output{suffix}" print(f"Run test for suffix {suffix}, crs_epsg {crs_epsg}") basetest_union_circles(tmp_dir, input1_path, input2_path, output_path)
def test_get_only_layer(tmpdir): ### Test file with 1 layer ### # Prepare test data + run tests for one layer tmp_dir = Path(tmpdir) for suffix in test_helper.get_test_suffix_list(): # If test input file is in wrong format, convert it src = test_helper.prepare_test_file( path=test_helper.TestFiles.polygons_parcels_gpkg, tmp_dir=tmp_dir, suffix=suffix) layer = gfo.get_only_layer(src) if suffix == ".gpkg": assert layer == 'parcels' elif suffix == ".shp": assert layer == src.stem else: raise Exception(f"test not implemented for suffix {suffix}") ### Test Geopackage with 2 layers ### srcpath = test_helper.TestFiles.polygons_twolayers_gpkg layers = gfo.listlayers(srcpath) assert len(layers) == 2 error_raised = False try: layer = gfo.get_only_layer(srcpath) except: error_raised = True assert error_raised is True
def test_select(tmpdir): # Prepare test data + run tests tmp_dir = Path(tmpdir) tmp_dir.mkdir(parents=True, exist_ok=True) for input_suffix in test_helper.get_test_suffix_list(): for output_suffix in test_helper.get_test_suffix_list(): for crs_epsg in test_helper.get_test_crs_epsg_list(): # If test input file is in wrong format, convert it input_path = test_helper.prepare_test_file( path=test_helper.TestFiles.polygons_parcels_gpkg, tmp_dir=tmp_dir, suffix=input_suffix, crs_epsg=crs_epsg) # Now run test output_path = tmp_dir / f"{input_path.stem}-{input_suffix.replace('.', '')}-output{output_suffix}" print(f"Run test for input_suffix {input_suffix}, output_suffix {output_suffix}, crs_epsg {crs_epsg}") basetest_select(input_path, output_path)
def test_to_file_gpd_none(tmpdir): # Prepare test data + run tests for one layer tmp_dir = Path(tmpdir) for suffix in test_helper.get_test_suffix_list(): # If test input file is in wrong format, convert it input_path = test_helper.prepare_test_file( path=test_helper.TestFiles.polygons_parcels_gpkg, tmp_dir=tmp_dir, suffix=suffix) basetest_to_file_gpd_none(input_path, tmp_dir, suffix)
def test_get_crs(tmpdir): # Prepare test data + run tests tmp_dir = Path(tmpdir) for suffix in test_helper.get_test_suffix_list(): # If test input file is in wrong format, convert it src = test_helper.prepare_test_file( path=test_helper.TestFiles.polygons_parcels_gpkg, tmp_dir=tmp_dir, suffix=suffix) crs = gfo.get_crs(src) assert crs.to_epsg() == 31370
def test_to_file(tmpdir): # Prepare test data + run tests for one layer tmp_dir = Path(tmpdir) for suffix in test_helper.get_test_suffix_list(): # If test input file is in wrong format, convert it input_path = test_helper.prepare_test_file( path=test_helper.TestFiles.polygons_parcels_gpkg, tmp_dir=tmp_dir, suffix=suffix) output_path = io_util.with_stem(input_path, f"{input_path}-output") basetest_to_file(input_path, output_path)
def test_get_default_layer(tmpdir): # Prepare test data + run tests tmp_dir = Path(tmpdir) for suffix in test_helper.get_test_suffix_list(): # If test input file is in wrong format, convert it src = test_helper.prepare_test_file( path=test_helper.TestFiles.polygons_parcels_gpkg, tmp_dir=tmp_dir, suffix=suffix) layer = gfo.get_default_layer(src) assert layer == 'polygons_parcels-2020'
def test_remove(tmpdir): # Prepare test data + run tests for one layer tmp_dir = Path(tmpdir) for suffix in test_helper.get_test_suffix_list(): # If test input file is in wrong format, convert it input_path = test_helper.prepare_test_file( path=test_helper.TestFiles.polygons_parcels_gpkg, tmp_dir=tmp_dir, suffix=suffix) assert input_path.exists() == True # Remove and check result gfo.remove(input_path) assert input_path.exists() == False
def test_get_layerinfo(tmpdir): # Prepare test data + run tests tmp_dir = Path(tmpdir) for suffix in test_helper.get_test_suffix_list(): # If test input file is in wrong format, convert it src = test_helper.prepare_test_file( path=test_helper.TestFiles.polygons_parcels_gpkg, tmp_dir=tmp_dir, suffix=suffix) basetest_get_layerinfo(src=src) # Test geopackage, two layers basetest_get_layerinfo(src=test_helper.TestFiles.polygons_twolayers_gpkg, layer='parcels')
def test_dissolve_linestrings_nogroupby(tmpdir): # Prepare test data + run tests tmp_dir = Path(tmpdir) tmp_dir.mkdir(parents=True, exist_ok=True) for suffix in test_helper.get_test_suffix_list(): for crs_epsg in test_helper.get_test_crs_epsg_list(): # If test input file is in wrong format, convert it input_path = test_helper.prepare_test_file( path=test_helper.TestFiles.linestrings_watercourses_gpkg, tmp_dir=tmp_dir, suffix=suffix, crs_epsg=crs_epsg) # Now run test output_path = tmp_dir / f"{input_path.stem}-output{suffix}" print(f"Run test for suffix {suffix}, crs_epsg {crs_epsg}") basetest_dissolve_linestrings_nogroupby(input_path, output_path)
def test_makevalid(tmpdir): # Prepare test data + run tests tmp_dir = Path(tmpdir) tmp_dir.mkdir(parents=True, exist_ok=True) for suffix in test_helper.get_test_suffix_list(): for crs_epsg in test_helper.get_test_crs_epsg_list(): # If test input file is in wrong format, convert it input_path = test_helper.prepare_test_file( path=test_helper.TestFiles.polygons_invalid_geometries_gpkg, tmp_dir=tmp_dir, suffix=suffix, crs_epsg=crs_epsg) # Now run test output_path = tmp_dir / f"{input_path.stem}-output{suffix}" print(f"Run test for suffix {suffix}, crs_epsg {crs_epsg}") basetest_makevalid(input_path, output_path)
def test_buffer_ext(tmpdir): # Prepare test data + run tests tmp_dir = Path(tmpdir) tmp_dir.mkdir(parents=True, exist_ok=True) for suffix in test_helper.get_test_suffix_list(): # Buffer on not-projected data is weird, so not tested (at the moment) for crs_epsg in test_helper.get_test_crs_epsg_list(): # If test input file is in wrong format, convert it input_path = test_helper.prepare_test_file( path=test_helper.TestFiles.polygons_parcels_gpkg, tmp_dir=tmp_dir, suffix=suffix, crs_epsg=crs_epsg) # Now run test output_path = tmp_dir / f"{input_path.stem}-output{suffix}" print(f"Run test for suffix {suffix}, crs_epsg {crs_epsg}") basetest_buffer_ext(input_path, output_path)
def test_convert(tmpdir): # Prepare test data + run tests tmp_dir = Path(tmpdir) for suffix in test_helper.get_test_suffix_list(): # If test input file is in wrong format, convert it src = test_helper.prepare_test_file( path=test_helper.TestFiles.polygons_parcels_gpkg, tmp_dir=tmp_dir, suffix=suffix) # Convert dst = Path(tmpdir) / f"polygons_parcels_output{suffix}" gfo.convert(src, dst) # Now compare source and dst file src_layerinfo = gfo.get_layerinfo(src) dst_layerinfo = gfo.get_layerinfo(dst) assert src_layerinfo.featurecount == dst_layerinfo.featurecount assert len(src_layerinfo.columns) == len(dst_layerinfo.columns)
def test_cmp(tmpdir): # Prepare test data + run tests tmp_dir = Path(tmpdir) for suffix in test_helper.get_test_suffix_list(): # If test input file is in wrong format, convert it src = test_helper.prepare_test_file( path=test_helper.TestFiles.polygons_parcels_gpkg, tmp_dir=tmp_dir, suffix=suffix) src2 = test_helper.prepare_test_file( path=test_helper.TestFiles.polygons_invalid_geometries_gpkg, tmp_dir=tmp_dir, suffix=suffix) # Copy test file to tmpdir dst = Path(tmpdir) / f"polygons_parcels_output{suffix}" gfo.copy(src, dst) # Now compare source and dst files assert gfo.cmp(src, dst) == True assert gfo.cmp(src2, dst) == False
def test_listlayers(tmpdir): # Prepare test data + run tests for one layer tmp_dir = Path(tmpdir) for suffix in test_helper.get_test_suffix_list(): # If test input file is in wrong format, convert it src = test_helper.prepare_test_file( path=test_helper.TestFiles.polygons_parcels_gpkg, tmp_dir=tmp_dir, suffix=suffix) layers = gfo.listlayers(src) if suffix == ".gpkg": assert layers[0] == "parcels" elif suffix == ".shp": assert layers[0] == src.stem else: raise Exception(f"test not implemented for suffix {suffix}") # Test geopackage 2 layers layers = gfo.listlayers(test_helper.TestFiles.polygons_twolayers_gpkg) assert 'parcels' in layers assert 'zones' in layers
def test_spatial_index(tmpdir): # Prepare test data + run tests for one layer tmp_dir = Path(tmpdir) for suffix in test_helper.get_test_suffix_list(): # If test input file is in wrong format, convert it src = test_helper.prepare_test_file( path=test_helper.TestFiles.polygons_parcels_gpkg, tmp_dir=tmp_dir, suffix=suffix) # Check if spatial index present: layer = gfo.get_only_layer(src) has_spatial_index = gfo.has_spatial_index(path=src, layer=layer) assert has_spatial_index is True # Remove spatial index gfo.remove_spatial_index(path=src, layer=layer) has_spatial_index = gfo.has_spatial_index(path=src, layer=layer) assert has_spatial_index is False # Create spatial index gfo.create_spatial_index(path=src, layer=layer) has_spatial_index = gfo.has_spatial_index(path=src, layer=layer) assert has_spatial_index is True