def test_fmask(): os.chdir(os.path.dirname(os.path.abspath(__file__))) try: os.remove("test_outputs/masks/fmask.tif") except FileNotFoundError: pass pyeo.apply_fmask( "test_data/L1/S2A_MSIL1C_20180329T171921_N0206_R012_T13QFB_20180329T221746.SAFE", "test_outputs/masks/fmask.tif") assert gdal.Open("test_outputs/masks/fmask.tif")
def test_mask_combination(): os.chdir(os.path.dirname(os.path.abspath(__file__))) masks = [ r"test_data/" + mask for mask in os.listdir("test_data") if mask.endswith(".msk") ] try: os.remove("test_outputs/union_or_combination.tif") os.remove("test_outputs/intersection_and_combination.tif") except FileNotFoundError: pass pyeo.combine_masks(masks, "test_outputs/union_or_combination.tif", geometry_func="union", combination_func="or") pyeo.combine_masks(masks, "test_outputs/intersection_and_combination.tif", geometry_func="intersect", combination_func="and") mask_1 = gdal.Open("test_outputs/union_or_combination.tif") assert not mask_1.GetVirtualMemArray().all == False mask_2 = gdal.Open("test_outputs/intersection_and_combination.tif") assert not mask_1.GetVirtualMemArray().all == False
def test_reprojection(): os.chdir(os.path.dirname(os.path.abspath(__file__))) try: os.remove(r"test_outputs/reprojection_test.tif") except FileNotFoundError: pass new_projection = r"""PROJCS["WGS 84 / UTM zone 36S",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",0],PARAMETER["central_meridian",33],PARAMETER["scale_factor",0.9996],PARAMETER["false_easting",500000],PARAMETER["false_northing",10000000],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["Easting",EAST],AXIS["Northing",NORTH],AUTHORITY["EPSG","32736"]]""" image = r"test_data/S2B_MSIL2A_20180728T073609_N0206_R092_T37MBV_20180728T114325.tif" out_file = r"test_outputs/reprojection_test.tif" pyeo.reproject_image(image, out_file, new_projection) result = gdal.Open(out_file) assert result # assert result.GetProjection() == new_projection result_array = result.GetVirtualMemArray() assert result_array.max() > 10
def test_classification(): os.chdir(os.path.dirname(os.path.abspath(__file__))) try: os.remove("test_outputs/class_20180103T172709_20180319T172021.tif") except FileNotFoundError: pass pyeo.classify_image( "test_data/T13QFB_20180103T172709_20180329T171921.tif", "test_data/manantlan_v1.pkl", "test_outputs/class_T13QFB_20180103T172709_20180319T172021.tif", num_chunks=4) image = gdal.Open( "test_outputs/class_T13QFB_20180103T172709_20180319T172021.tif") assert image image_array = image.GetVirtualMemArray() assert not np.all(image_array == 0)
def test_composite_images_with_mask(): os.chdir(os.path.dirname(os.path.abspath(__file__))) try: os.remove(r"test_outputs/composite_test.tif") except FileNotFoundError: pass test_data = [ r"test_data/S2A_MSIL2A_20180329T171921_N0206_R012_T13QFB_20180329T221746.tif", r"test_data/S2B_MSIL2A_20180103T172709_N0206_R012_T13QFB_20180103T192359.tif" ] out_file = r"test_outputs/composite_test.tif" pyeo.composite_images_with_mask(test_data, out_file) image = gdal.Open("test_outputs/composite_test.tif") assert image image_array = image.GetVirtualMemArray() assert image_array.max() > 10
def test_stacking(): os.chdir(os.path.dirname(os.path.abspath(__file__))) try: os.remove("test_outputs/T13QFB_20180329T171921_20180103T172709.msk") os.remove("test_outputs/T13QFB_20180329T171921_20180103T172709.tif") except FileNotFoundError: pass pyeo.stack_old_and_new_images( r"test_data/S2B_MSIL2A_20180103T172709_N0206_R012_T13QFB_20180103T192359.tif", r"test_data/S2A_MSIL2A_20180329T171921_N0206_R012_T13QFB_20180329T221746.tif", r"test_outputs") image = gdal.Open( "test_outputs/T13QFB_20180319T172021_20180103T172709.tif") assert image image_array = image.GetVirtualMemArray() for layer in range(image_array.shape(0)): assert image_array[layer, :, :].max > 10
def test_mask_joining(): os.chdir(os.path.dirname(os.path.abspath(__file__))) try: os.remove("test_outputs/masks/joining_test.tif") except FileNotFoundError: pass pyeo.combine_masks([ "test_data/masks/fmask_cloud_and_shadow.tif", "test_data/masks/confidence_mask.tif" ], "test_outputs/masks/joining_test.tif", combination_func="and", geometry_func="union") out_image = gdal.Open("test_outputs/masks/joining_test.tif") assert out_image out_array = out_image.GetVirtualMemArray() assert 1 in out_array assert 0 in out_array
def test_composite_across_projections_meters(): os.chdir(os.path.dirname(os.path.abspath(__file__))) try: os.remove(r"test_outputs/composite_test.tif") except FileNotFoundError: pass try: shutil.rmtree(r"test_outputs/reprojected") except FileNotFoundError: pass os.mkdir(r"test_outputs/reprojected") epsg = 32736 proj = osr.SpatialReference() proj.ImportFromEPSG(epsg) projection = proj.ExportToWkt() # Refactor this terrible nonsense later test_data = [ r"test_data/S2A_MSIL2A_20180703T073611_N0206_R092_T36MZE_20180703T094637.tif", r"test_data/S2B_MSIL2A_20180728T073609_N0206_R092_T37MBV_20180728T114325.tif" ] pyeo.reproject_image(test_data[0], r"test_outputs/reprojected/0.tif", projection) pyeo.reproject_image(test_data[1], r"test_outputs/reprojected/1.tif", projection) pyeo.reproject_image(pyeo.get_mask_path(test_data[0]), r"test_outputs/reprojected/0.msk", projection) pyeo.reproject_image(pyeo.get_mask_path(test_data[1]), r"test_outputs/reprojected/1.msk", projection) out_file = r"test_outputs/composite_test.tif" pyeo.composite_images_with_mask( [r"test_outputs/reprojected/0.tif", r"test_outputs/reprojected/1.tif"], out_file) image = gdal.Open("test_outputs/composite_test.tif") assert image image_array = image.GetVirtualMemArray() assert image_array.max() > 1