-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
722 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"python.testing.pytestArgs": [ | ||
"." | ||
], | ||
"python.testing.unittestEnabled": false, | ||
"python.testing.pytestEnabled": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import os | ||
import pytest | ||
from contextlib import suppress as do_not_raise | ||
from cropmaps.sts import sentimeseries | ||
import rasterio | ||
|
||
for directory, _, _ in os.walk("./data"): | ||
for file in os.listdir(directory): | ||
if file.endswith(".tif"): | ||
os.remove(os.path.join(directory, file)) | ||
if file.endswith(".tif.aux.xml"): | ||
os.remove(os.path.join(directory, file)) | ||
|
||
|
||
if not os.path.exists("./data/eodata_local"): | ||
os.makedirs("./data/eodata_local") | ||
|
||
landcover_data = "./data/LandCover" | ||
|
||
search_params = [(landcover_data, None, "LC_mosaic.tif", do_not_raise()), | ||
(None, None, "LC_mosaic.tif", pytest.raises(TypeError)), | ||
(landcover_data, "./data/AOI/AOI.geojson", "LC_mosaic.tif", do_not_raise()), | ||
] | ||
|
||
@pytest.mark.parametrize("store, aoi, outname, exception", search_params) | ||
def test_LandCover(store, aoi, outname, exception): | ||
with exception: | ||
eodata = sentimeseries("S2-timeseries") | ||
eodata.find("./data/eodata") | ||
landcover_image = eodata.LandCover(store, aoi, outname) | ||
src = rasterio.open(landcover_image) | ||
assert src.meta["dtype"] == "uint8" | ||
assert src.meta["driver"] == "GTiff" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import os | ||
import pytest | ||
from contextlib import suppress as do_not_raise | ||
from cropmaps.sts import sentimeseries | ||
from cropmaps.exceptions import BandNotFound | ||
import rasterio | ||
|
||
for directory, _, _ in os.walk("./data"): | ||
for file in os.listdir(directory): | ||
if file.endswith(".tif"): | ||
os.remove(os.path.join(directory, file)) | ||
if file.endswith(".tif.aux.xml"): | ||
os.remove(os.path.join(directory, file)) | ||
|
||
|
||
if not os.path.exists("./data/eodata_local"): | ||
os.makedirs("./data/eodata_local") | ||
|
||
search_params = [(None, "NDWI", "./data/eodata_local", "AOI", None, do_not_raise()), | ||
(None, "NDBI", "./data/eodata_local", "AOI", None, do_not_raise()), | ||
(None, "NDVI", "./data/eodata_local", "AOI", None, do_not_raise()), | ||
(None, None, "./data/eodata_local", "AOI", None, do_not_raise()), | ||
(None, None, None, None, None, do_not_raise()), | ||
("./data/eodata/Sentinel-2/L2A/2018/06/27/S2A_MSIL2A_20180627T104021_N0208_R008_T31TEJ_20180627T143337.SAFE/GRANULE/L2A_T31TEJ_A015735_20180627T104837/IMG_DATA/T31TEJ_20180627T104021_B04_10m.jp2", None, None, None, None, pytest.raises(TypeError)), | ||
(None, "NDBI", "./data/eodata_local", "AOI", "highest", pytest.raises(BandNotFound)), | ||
] | ||
|
||
@pytest.mark.parametrize("image, band, store, subregion, resolution, exception", search_params) | ||
def test_apply_SCL(image, band, store, subregion, resolution, exception): | ||
with exception: | ||
eodata = sentimeseries("S2-timeseries") | ||
eodata.find("./data/eodata") | ||
eodata.clipbyMask("./data/AOI/AOI.geojson", store = "./data/eodata_local") | ||
eodata.getVI("NDVI", store = "./data/eodata_local", subregion = "AOI") # Subregion is the name of the mask shapefile | ||
eodata.getVI("NDWI", store = "./data/eodata_local", subregion = "AOI") # This is in 10m | ||
eodata.getVI("NDBI", store = "./data/eodata_local", subregion = "AOI") # This is in 20m | ||
eodata.apply_SCL(image, band, store, subregion, resolution) | ||
|
||
if subregion is None: | ||
level = "raw" | ||
else: | ||
level = subregion | ||
|
||
if image is None: | ||
for im in eodata.data: | ||
if band is None: | ||
bands = ['B02', 'B03', 'B04', 'B08', 'B05', 'B06', 'B07', 'B8A', 'B11', 'B12'] | ||
|
||
for b in bands: | ||
if resolution == "highest": | ||
res = "10" | ||
else: | ||
res = im.setResolution(b) | ||
# Check if path exists | ||
assert getattr(im, b)[res][level] | ||
# Open image to check | ||
src = rasterio.open(getattr(im, b)[res][level]) | ||
# Check datatype | ||
if b in ["NDVI", "NDBI", "NDWI"]: | ||
dtype = ["float32"] | ||
else: | ||
dtype = ["uint8", "uint16"] | ||
|
||
assert src.meta["dtype"] in dtype | ||
|
||
output_format = ["GTiff", "JP2OpenJPEG"] | ||
assert src.meta["driver"] in output_format | ||
src.close() | ||
else: | ||
if resolution == "highest": | ||
res = "10" | ||
else: | ||
res = im.setResolution(band) | ||
# Check if path exists | ||
assert getattr(im, band)[res][level] | ||
# Open image to check | ||
src = rasterio.open(getattr(im, band)[res][level]) | ||
# Check datatype | ||
if band in ["NDVI", "NDBI", "NDWI"]: | ||
dtype = ["float32"] | ||
else: | ||
dtype = ["uint8", "uint16"] | ||
|
||
assert src.meta["dtype"] in dtype | ||
|
||
output_format = ["GTiff", "JP2OpenJPEG"] | ||
assert src.meta["driver"] in output_format | ||
src.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import os | ||
import pytest | ||
import numpy as np | ||
import rasterio | ||
from contextlib import suppress as do_not_raise | ||
from cropmaps.sts import sentimeseries | ||
from cropmaps.prepare_vector import burn | ||
|
||
for directory, _, _ in os.walk("./data"): | ||
for file in os.listdir(directory): | ||
if file.endswith(".tif"): | ||
os.remove(os.path.join(directory, file)) | ||
if file.endswith(".tif.aux.xml"): | ||
os.remove(os.path.join(directory, file)) | ||
|
||
|
||
if not os.path.exists("./data/eodata_local"): | ||
os.makedirs("./data/eodata_local") | ||
|
||
search_params = [("./data/reference_data/france_data_2018.shp", "EC_hcat_n", None, True, None, "gt.tif", do_not_raise()), | ||
] | ||
|
||
@pytest.mark.parametrize("shapefile, classes, classes_id, save_nomenclature, save_to, outfname, exception", search_params) | ||
def test_burn(shapefile, classes, classes_id, save_nomenclature, save_to, outfname, exception): | ||
with exception: | ||
eodata = sentimeseries("S2-timeseries") | ||
eodata.find("./data/eodata") | ||
# To AOI | ||
eodata.clipbyMask("./data/AOI/AOI.geojson", store = "./data/eodata_local") | ||
base = eodata.data[0].B04["10"]["AOI"] | ||
metadata = rasterio.open(base).meta.copy() | ||
|
||
gt_data_raster = burn(shapefile, classes, metadata, classes_id, save_nomenclature, save_to, outfname) | ||
|
||
with rasterio.open(gt_data_raster) as src: | ||
assert src.meta == metadata |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import os | ||
import rasterio | ||
import pytest | ||
from contextlib import suppress as do_not_raise | ||
from cropmaps.sts import sentimeseries | ||
|
||
for directory, _, _ in os.walk("./data"): | ||
for file in os.listdir(directory): | ||
if file.endswith(".tif"): | ||
os.remove(os.path.join(directory, file)) | ||
if file.endswith(".tif.aux.xml"): | ||
os.remove(os.path.join(directory, file)) | ||
|
||
|
||
if not os.path.exists("./data/eodata_local"): | ||
os.makedirs("./data/eodata_local") | ||
|
||
eodata = sentimeseries("S2-timeseries") | ||
eodata.find("./data/eodata") | ||
image = eodata.data[0] | ||
|
||
search_params = [("./data/AOI/AOI.geojson", False, "B08", False, None, None, "./data/eodata_local", True, do_not_raise()), | ||
("./data/AOI/AOI.geojson", False, "B08", False, None, None, None, True, do_not_raise()), | ||
("./data/AOI/AOI.geojson", False, "B8A", True, None, None, None, True, do_not_raise()), | ||
("./data/AOI/AOI.geojson", True, "B8A", True, None, None, None, True, do_not_raise()), | ||
("./data/AOI/AOI.geojson", False, None, True, None, None, None, True, do_not_raise()), | ||
("./data/AOI/AOI.geojson", False, "B8A", False, None, None, None, True, do_not_raise()), | ||
("./data/AOI/AOI.geojson", False, None, True, None, None, "./data/eodata_local", True, do_not_raise()), | ||
] | ||
|
||
@pytest.mark.parametrize("shapefile, series, band, resize, method, new, store, force_update, exception", search_params) | ||
def test_clip_by_mask(shapefile, series, band, resize, method, new, store, force_update, exception): | ||
with exception: | ||
eodata = sentimeseries("S2-timeseries") | ||
eodata.find("./data/eodata") | ||
if series: | ||
eodata.clipbyMask(shapefile, None, band, resize, method, new, store, force_update) | ||
for im in eodata.data: | ||
if band is None: | ||
bands = ['B02', 'B03', 'B04', 'B08', 'B05', 'B06', 'B07', 'B8A', 'B11', 'B12', "SCL"] | ||
for b in bands: | ||
if resize: | ||
resolution = "10" | ||
else: | ||
resolution = im.setResolution(b) | ||
|
||
assert getattr(im, b)[resolution][os.path.splitext(os.path.basename(shapefile))[0]] | ||
# Check the correct datatype | ||
dtype = ["uint8", "uint16"] | ||
src = rasterio.open(getattr(im, b)[resolution][os.path.splitext(os.path.basename(shapefile))[0]]) | ||
assert src.meta["dtype"] in dtype | ||
|
||
# Check the correct datatype | ||
output_format = "GTiff" | ||
assert output_format == src.meta["driver"] | ||
src.close() | ||
else: | ||
if resize: | ||
resolution = "10" | ||
else: | ||
resolution = im.setResolution(band) | ||
|
||
# Check if attribute exists | ||
assert getattr(im, band)[resolution][os.path.splitext(os.path.basename(shapefile))[0]] | ||
|
||
# Check the correct datatype | ||
dtype = ["uint8", "uint16"] | ||
src = rasterio.open(getattr(im, band)[resolution][os.path.splitext(os.path.basename(shapefile))[0]]) | ||
assert src.meta["dtype"] in dtype | ||
|
||
# Check the correct driver | ||
output_format = "GTiff" | ||
assert output_format == src.meta["driver"] | ||
src.close() | ||
else: | ||
eodata.clipbyMask(shapefile, eodata.data[0], band, resize, method, new, store, force_update) | ||
image = eodata.data[0] | ||
if band is None: | ||
bands = ['B02', 'B03', 'B04', 'B08', 'B05', 'B06', 'B07', 'B8A', 'B11', 'B12', "SCL"] | ||
for b in bands: | ||
if resize: | ||
resolution = "10" | ||
else: | ||
resolution = image.setResolution(b) | ||
|
||
assert getattr(image, b)[resolution][os.path.splitext(os.path.basename(shapefile))[0]] | ||
# Check the correct datatype | ||
dtype = ["uint8", "uint16"] | ||
src = rasterio.open(getattr(image, b)[resolution][os.path.splitext(os.path.basename(shapefile))[0]]) | ||
assert src.meta["dtype"] in dtype | ||
|
||
# Check the correct driver | ||
output_format = "GTiff" | ||
assert output_format == src.meta["driver"] | ||
src.close() | ||
|
||
else: | ||
if resize: | ||
resolution = "10" | ||
else: | ||
resolution = image.setResolution(band) | ||
|
||
# Check if attribute exists | ||
assert getattr(image, band)[resolution][os.path.splitext(os.path.basename(shapefile))[0]] | ||
|
||
# Check the correct datatype | ||
dtype = ["uint8", "uint16"] | ||
src = rasterio.open(getattr(image, band)[resolution][os.path.splitext(os.path.basename(shapefile))[0]]) | ||
assert src.meta["dtype"] in dtype | ||
|
||
# Check the correct driver | ||
output_format = "GTiff" | ||
assert output_format == src.meta["driver"] | ||
src.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import os | ||
import pytest | ||
from contextlib import suppress as do_not_raise | ||
from cropmaps.sts import sentimeseries | ||
from cropmaps.exceptions import NoDataError | ||
|
||
if not os.path.exists("./data/eodata_local"): | ||
os.makedirs("./data/eodata_local") | ||
|
||
# Test cropmaps.sts.sentimeseries.find() | ||
|
||
search_params = [("./data/eodata", "L2A", do_not_raise()), | ||
("./data/eodata", "L1C", pytest.raises(NoDataError)) | ||
] | ||
|
||
@pytest.mark.parametrize("search, level, exception", search_params) | ||
def test_find(search, level, exception): | ||
with exception: | ||
eodata = sentimeseries("S2-timeseries") | ||
eodata.find(search, level = level) | ||
assert isinstance(eodata, sentimeseries) | ||
assert eodata.total == 10. | ||
|
||
# Test cropmaps.sts.sentimeseries.find_DIAS() | ||
|
||
def local_DIAS_path_creator(image): | ||
if (image.satellite == "Sentinel-2A") or (image.satellite == "Sentinel-2B"): | ||
satellite = "Sentinel-2" | ||
else: | ||
raise ValueError("Satellite name unkwown!") | ||
|
||
if image.processing_level == "Level-2A" or image.processing_level == "Level-2Ap": | ||
level = "L2A" | ||
elif image.processing_level == "Level-1C": | ||
level = "L1C" | ||
else: | ||
raise ValueError("Processing level unkwown!") | ||
|
||
date = image.datetime | ||
year = str(date.year) | ||
month = str(date.month).zfill(2) | ||
day = str(date.day).zfill(2) | ||
|
||
return os.path.join(satellite, level, year, month, day) | ||
|
||
# Get data | ||
eodata = sentimeseries("S2-timeseries") | ||
eodata.find("./data/eodata") | ||
eodata.sort_images(date=True) | ||
|
||
creodias_paths = [] | ||
for image in eodata.data: | ||
src = os.path.join(image.path, image.name) | ||
DIAS_path = local_DIAS_path_creator(image) | ||
creodias_paths.append(os.path.join("./data/eodata", DIAS_path, image.name)) | ||
|
||
search_params = [(creodias_paths, do_not_raise()), | ||
([], pytest.raises(NoDataError)) | ||
] | ||
|
||
@pytest.mark.parametrize("paths, exception", search_params) | ||
def test_find(paths, exception): | ||
with exception: | ||
eodata = sentimeseries("S2-timeseries") | ||
eodata.find_DIAS(paths) | ||
assert isinstance(eodata, sentimeseries) | ||
assert eodata.total == 10. |
Oops, something went wrong.