From 7729405fd9bad333f3f288a2997c590e4467c2d1 Mon Sep 17 00:00:00 2001 From: Chris Schnaufer Date: Fri, 9 Jul 2021 14:35:06 -0700 Subject: [PATCH] Adding plain tiff support --- README.md | 6 ++--- tests/test_soilmask_ratio.py | 43 ++++++++++++++++++++++++++++++++++++ transformer.py | 25 +++++++++++---------- 3 files changed, 59 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 4a891e5..bab7b6b 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ First build the Docker image, using the Dockerfile, and tag it agdrone/transform Read about the [docker build](https://docs.docker.com/engine/reference/commandline/build/) command if needed. ```bash -docker build -t agdrone/transformer-soilmask-by-ratio:1.0 ./ +docker build -t agdrone/transformer-soilmask-by-ratio:1.2 ./ ``` There is one file needed for running the Docker image. @@ -50,11 +50,11 @@ An explanation of the command line options used follows. Be sure to read up on the [docker run](https://docs.docker.com/engine/reference/run/) command line for more information. ```bash -docker run --rm --mount "src=${PWD}/test_data,target=/mnt,type=bind" agdrone/transformer-soilmask-by-ratio:1.0 --ratio 1.25 --working_space "/mnt" "/mnt/orthomosaic.tif" +docker run --rm --mount "src=${PWD}/test_data,target=/mnt,type=bind" agdrone/transformer-soilmask-by-ratio:1.2 --ratio 1.25 --working_space "/mnt" "/mnt/orthomosaic.tif" ``` This example command line assumes the source files are located in the `test_data` folder off the current folder. -The name of the image to run is `agdrone/transformer-soilmask-by-ratio:1.0`. +The name of the image to run is `agdrone/transformer-soilmask-by-ratio:1.2`. We are using the same folder for the source files and the output files. By using multiple `--mount` options, the source and output files can be separated. diff --git a/tests/test_soilmask_ratio.py b/tests/test_soilmask_ratio.py index ee0876a..733baa2 100644 --- a/tests/test_soilmask_ratio.py +++ b/tests/test_soilmask_ratio.py @@ -6,6 +6,7 @@ import json import subprocess import numpy as np +import PIL.Image from osgeo import gdal # The name of the source file to test and it's path @@ -155,3 +156,45 @@ def test_ratio_command_line(): img = gdal.Open(os.path.join(working_space, orthomosaic_mask_name)).ReadAsArray() assert img is not None assert isinstance(img, np.ndarray) + + +def test_ratio_plain_tiff(): + """Runs the command line for a non-GeoTiff file and tests the result""" + orthomosaic_mask_name = 'plain_mask.tif' + result_name = 'result.json' + source_image = os.path.join(TESTING_FILE_PATH, 'orthomosaic.tif') + assert os.path.exists(source_image) + + # Create a non-georeferenced tiff image from the source image + plain_tiff_image = os.path.join(TESTING_FILE_PATH, 'plain.tif') + if os.path.exists(plain_tiff_image): + os.unlink(plain_tiff_image) + img = PIL.Image.open(source_image) + img_array = np.array(img) + result = PIL.Image.fromarray(img_array) + result.save(plain_tiff_image) + + # Setup parameters for running the test + working_space = os.path.realpath('./test_results') + os.makedirs(working_space, exist_ok=True) + for expected_file in [result_name, orthomosaic_mask_name]: + cur_path = os.path.join(working_space, expected_file) + if os.path.exists(cur_path): + os.unlink(cur_path) + + command_line = [SOURCE_PATH, '--working_space', working_space, plain_tiff_image] + subprocess.run(command_line, check=True) + + # Check that the expected files were created + for expected_file in [result_name, orthomosaic_mask_name]: + assert os.path.exists(os.path.join(working_space, expected_file)) + + # Inspect the created files + with open(os.path.join(working_space, result_name)) as in_file: + res = json.load(in_file) + assert 'code' in res + assert res['code'] == 0 + + img = gdal.Open(os.path.join(working_space, orthomosaic_mask_name)).ReadAsArray() + assert img is not None + assert isinstance(img, np.ndarray) diff --git a/transformer.py b/transformer.py index 9046d7f..4313da6 100755 --- a/transformer.py +++ b/transformer.py @@ -204,7 +204,8 @@ def perform_process(self, environment: Environment, check_md: CheckMD, transform Return: Returns a dictionary with the results of processing """ - # pylint: disable=unused-argument, no-self-use + # Disable pylint warnings that reduce readability + # pylint: disable=unused-argument, no-self-use, too-many-branches result = {} file_md = [] @@ -221,17 +222,14 @@ def perform_process(self, environment: Environment, check_md: CheckMD, transform # Get the image's EPSG code epsg = geoimage.get_epsg(one_file) - if epsg is None: - logging.debug("Skipping image that is not georeferenced: '%s'", one_file) - continue + if epsg is not None: + # Get the bounds of the image to see if we can process it. + bounds = geoimage.image_get_geobounds(one_file) - # Get the bounds of the image to see if we can process it. - bounds = geoimage.image_get_geobounds(one_file) - - if bounds is None: - logging.warning("Unable to get bounds of georeferenced image: '%s'", - os.path.basename(one_file)) - continue + if bounds is None: + logging.warning("Unable to get bounds of georeferenced image: '%s'", + os.path.basename(one_file)) + continue # Get the mask name if environment.args.out_file: @@ -255,7 +253,10 @@ def perform_process(self, environment: Environment, check_md: CheckMD, transform transformer_info = environment.generate_transformer_md() image_md = __internal__.prepare_metadata_for_geotiff(transformer_info) - geoimage.create_geotiff(mask_rgb, bounds, rgb_mask_tif, epsg, None, False, image_md, compress=True) + if epsg: + geoimage.create_geotiff(mask_rgb, bounds, rgb_mask_tif, epsg, None, False, image_md, compress=True) + else: + geoimage.create_tiff(mask_rgb, rgb_mask_tif, None, False, image_md, compress=True) transformer_md = { 'name': transformer_info['name'],