-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated ndvi. Moved control of layer-writing test into conftest file.
- Loading branch information
1 parent
71b4efd
commit 4a4b4ad
Showing
18 changed files
with
328 additions
and
5,363 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
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
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
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,65 @@ | ||
import ee | ||
from tools.xarray_tools import convert_ratio_to_percentage | ||
from .layer import Layer, get_image_collection | ||
|
||
class NdviSentinel2(Layer): | ||
"""" | ||
NDVI = Sentinel-2 Normalized Difference Vegetation Index | ||
param: year: The satellite imaging year. | ||
return: a rioxarray-format DataArray | ||
Author of associated Jupyter notebook: [email protected] | ||
Notebook: https://github.com/wri/cities-cities4forests-indicators/blob/dev-eric/scripts/extract-VegetationCover.ipynb | ||
Reference: https://en.wikipedia.org/wiki/Normalized_difference_vegetation_index | ||
""" | ||
def __init__(self, year=None, **kwargs): | ||
super().__init__(**kwargs) | ||
self.year = year | ||
|
||
def get_data(self, bbox): | ||
if self.year is None: | ||
raise Exception('NdviSentinel2.get_data() requires a year value') | ||
|
||
start_date = "%s-01-01" % self.year | ||
end_date = "%s-12-31" % self.year | ||
|
||
# Compute NDVI for each image | ||
def calculate_ndvi(image): | ||
ndvi = (image | ||
.normalizedDifference(['B8', 'B4']) | ||
.rename('NDVI')) | ||
return image.addBands(ndvi) | ||
|
||
s2 = ee.ImageCollection("COPERNICUS/S2_HARMONIZED") | ||
ndvi = (s2 | ||
.filterBounds(ee.Geometry.BBox(*bbox)) | ||
.filterDate(start_date, end_date) | ||
.map(calculate_ndvi) | ||
.select('NDVI') | ||
) | ||
|
||
ndvi_mosaic = ndvi.qualityMosaic('NDVI') | ||
|
||
ic = ee.ImageCollection(ndvi_mosaic) | ||
ndvi_data = get_image_collection(ic, bbox, 10, "NDVI") | ||
|
||
xdata = ndvi_data.to_dataarray() | ||
|
||
return xdata | ||
|
||
def post_processing_adjustment(self, data, ndvi_threshold=0.4, convert_to_percentage=True, **kwargs): | ||
""" | ||
Applies the standard post-processing adjustment used for rendering of NDVI including masking | ||
to a threshold and conversion to percentage values. | ||
:param ndvi_threshold: (float) minimum threshold for keeping values | ||
:param convert_to_percentage: (bool) controls whether NDVI values are converted to a percentage | ||
:return: A rioxarray-format DataArray | ||
""" | ||
# Remove values less than the specified threshold | ||
if ndvi_threshold is not None: | ||
data = data.where(data >= ndvi_threshold) | ||
|
||
# Convert to percentage in byte data_type | ||
if convert_to_percentage is True: | ||
data = convert_ratio_to_percentage(data) | ||
|
||
return data |
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
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
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 |
---|---|---|
@@ -1,9 +1,22 @@ | ||
# File defines bboxes using in the test code | ||
|
||
|
||
BBOX_BR_LAURO_DE_FREITAS_1 = ( | ||
BBOX_BRA_LAURO_DE_FREITAS_1 = ( | ||
-38.35530428121955, | ||
-12.821710300686393, | ||
-38.33813814352424, | ||
-12.80363249765361, | ||
) | ||
) | ||
|
||
BBOX_BRA_SALVADOR_ADM4 = ( | ||
-38.647320153390055, | ||
-13.01748678217598787, | ||
-38.3041637148564007, | ||
-12.75607703449720631 | ||
) | ||
|
||
BBOX_SMALL_TEST = ( | ||
-38.43864,-12.97987, | ||
-38.39993,-12.93239 | ||
) | ||
|
67 changes: 67 additions & 0 deletions
67
tests/resources/layer_dumps_for_br_lauro_de_freitas/conftest.py
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 tempfile | ||
import pytest | ||
import os | ||
import shutil | ||
from collections import namedtuple | ||
|
||
from tests.resources.bbox_constants import BBOX_BRA_LAURO_DE_FREITAS_1 | ||
from tools.general_tools import create_target_folder, is_valid_path | ||
|
||
# RUN_DUMPS is the master control for whether the writes and tests are executed | ||
# Setting RUN_DUMPS to True turns on code execution. | ||
# Values should normally be set to False in order to avoid unnecessary execution. | ||
RUN_DUMPS = True | ||
|
||
# Specify None to write to a temporary default folder otherwise specify a valid custom target path. | ||
CUSTOM_DUMP_DIRECTORY = None | ||
|
||
# Both the tests and QGIS file are implemented for the same bounding box in Brazil. | ||
COUNTRY_CODE_FOR_BBOX = 'BRA' | ||
BBOX = BBOX_BRA_LAURO_DE_FREITAS_1 | ||
|
||
def pytest_configure(config): | ||
qgis_project_file = 'layers_for_br_lauro_de_freitas.qgz' | ||
|
||
source_folder = os.path.dirname(__file__) | ||
target_folder = get_target_folder_path() | ||
create_target_folder(target_folder, True) | ||
|
||
source_qgis_file = os.path.join(source_folder, qgis_project_file) | ||
target_qgis_file = os.path.join(target_folder, qgis_project_file) | ||
shutil.copyfile(source_qgis_file, target_qgis_file) | ||
|
||
print("\n\033[93m QGIS project file and layer files written to folder %s.\033[0m\n" % target_folder) | ||
|
||
@pytest.fixture | ||
def target_folder(): | ||
return get_target_folder_path() | ||
|
||
@pytest.fixture | ||
def bbox_info(): | ||
bbox = namedtuple('bbox', ['bounds', 'country']) | ||
bbox_instance = bbox(bounds=BBOX, country=COUNTRY_CODE_FOR_BBOX) | ||
return bbox_instance | ||
|
||
def get_target_folder_path(): | ||
if CUSTOM_DUMP_DIRECTORY is not None: | ||
if is_valid_path(CUSTOM_DUMP_DIRECTORY) is False: | ||
raise ValueError(f"The custom path '%s' is not valid. Stopping." % CUSTOM_DUMP_DIRECTORY) | ||
else: | ||
output_dir = CUSTOM_DUMP_DIRECTORY | ||
else: | ||
sub_directory_name = 'test_result_tif_files' | ||
scratch_dir_name = tempfile.TemporaryDirectory(ignore_cleanup_errors=True).name | ||
dir_path = os.path.dirname(scratch_dir_name) | ||
output_dir = os.path.join(dir_path, sub_directory_name) | ||
|
||
return output_dir | ||
|
||
def prep_output_path(output_folder, file_name): | ||
file_path = os.path.join(output_folder, file_name) | ||
if os.path.isfile(file_path): | ||
os.remove(file_path) | ||
return file_path | ||
|
||
def verify_file_is_populated(file_path): | ||
is_populated = True if os.path.getsize(file_path) > 0 else False | ||
return is_populated |
Oops, something went wrong.