Skip to content

Commit 5dc9dea

Browse files
Merge pull request #46 from AgPipeline/develop
Merging non-GeoTiff (plain tiff) support
2 parents f9f8de0 + d75b76c commit 5dc9dea

3 files changed

Lines changed: 62 additions & 15 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ Li, Zongyang, Abby Stylianou, and Robert Pless. "Learning to Correct for Bad Cam
6767

6868
### Sample Docker Command line
6969

70-
First build the Docker image, using the Dockerfile, and tag it agdrone/transformer-soilmask:2.1.
70+
First build the Docker image, using the Dockerfile, and tag it agdrone/transformer-soilmask:2.4.
7171
Read about the [docker build](https://docs.docker.com/engine/reference/commandline/build/) command if needed.
7272

7373
```bash
74-
docker build -t agdrone/transformer-soilmask:2.1 ./
74+
docker build -t agdrone/transformer-soilmask:2.4 ./
7575
```
7676

7777
There are two files needed for running the Docker image.
@@ -89,11 +89,11 @@ An explanation of the command line options used follows.
8989
Be sure to read up on the [docker run](https://docs.docker.com/engine/reference/run/) command line for more information.
9090

9191
```bash
92-
docker run --rm --mount "src=${PWD}/test_data,target=/mnt,type=bind" agdrone/transformer-soilmask:2.1 --working_space "/mnt" --metadata "/mnt/experiment.yaml" "/mnt/orthomosaic.tif"
92+
docker run --rm --mount "src=${PWD}/test_data,target=/mnt,type=bind" agdrone/transformer-soilmask:2.4 --working_space "/mnt" --metadata "/mnt/experiment.yaml" "/mnt/orthomosaic.tif"
9393
```
9494

9595
This example command line assumes the source files are located in the `test_data` folder off the current folder.
96-
The name of the image to run is `agdrone/transformer-soilmask:2.1`.
96+
The name of the image to run is `agdrone/transformer-soilmask:2.4`.
9797

9898
We are using the same folder for the source files and the output files.
9999
By using multiple `--mount` options, the source and output files can be separated.

soilmask.py

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,8 @@ def perform_process(self, environment: Environment, check_md: CheckMD, transform
385385
Return:
386386
Returns a dictionary with the results of processing
387387
"""
388-
# pylint: disable=unused-argument
388+
# Disable pylint checks that negatively affect the code use and readability
389+
# pylint: disable=unused-argument, too-many-branches
389390
result = {}
390391
file_md = []
391392

@@ -401,17 +402,15 @@ def perform_process(self, environment: Environment, check_md: CheckMD, transform
401402
continue
402403
# Get the image's EPSG code
403404
epsg = geoimage.get_epsg(one_file)
404-
if epsg is None:
405-
logging.debug("Skipping image that is not georeferenced: '%s'", one_file)
406-
continue
405+
if epsg is not None:
406+
# Get the bounds of the image to see if we can process it.
407+
bounds = geoimage.image_get_geobounds(one_file)
407408

408-
# Get the bounds of the image to see if we can process it.
409-
bounds = geoimage.image_get_geobounds(one_file)
409+
if bounds is None:
410+
logging.warning("Unable to get bounds of georeferenced image: '%s'",
411+
os.path.basename(one_file))
412+
continue
410413

411-
if bounds is None:
412-
logging.warning("Unable to get bounds of georeferenced image: '%s'",
413-
os.path.basename(one_file))
414-
continue
415414
# Get the mask name
416415
if environment.args.out_file:
417416
rgb_mask_tif = environment.args.out_file
@@ -433,7 +432,10 @@ def perform_process(self, environment: Environment, check_md: CheckMD, transform
433432
transformer_info = environment.generate_transformer_md()
434433

435434
image_md = __internal__.prepare_metadata_for_geotiff(transformer_info)
436-
geoimage.create_geotiff(mask_rgb, bounds, rgb_mask_tif, epsg, None, False, image_md, compress=True)
435+
if epsg:
436+
geoimage.create_geotiff(mask_rgb, bounds, rgb_mask_tif, epsg, None, False, image_md, compress=True)
437+
else:
438+
geoimage.create_tiff(mask_rgb, rgb_mask_tif, None, False, image_md, compress=True)
437439

438440
transformer_md = {
439441
'name': transformer_info['name'],

tests/test_soilmask.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import json
77
import subprocess
88
import numpy as np
9+
import PIL.Image
910
from osgeo import gdal
1011

1112
# The name of the source file to test and it's path
@@ -129,3 +130,47 @@ def test_outputfile_command_line():
129130
img = gdal.Open(os.path.join(working_space, orthomosaic_mask_name)).ReadAsArray()
130131
assert img is not None
131132
assert isinstance(img, np.ndarray)
133+
134+
135+
def test_plain_tiff():
136+
"""Runs the command line for a non-GeoTiff file and tests the result"""
137+
orthomosaic_mask_name = 'plain_mask.tif'
138+
result_name = 'result.json'
139+
source_image = os.path.join(TESTING_FILE_PATH, 'orthomosaic.tif')
140+
source_metadata = os.path.join(TESTING_FILE_PATH, 'experiment.yaml')
141+
assert os.path.exists(source_image)
142+
assert os.path.exists(source_metadata)
143+
144+
# Create a non-georeferenced tiff image from the source image
145+
plain_tiff_image = os.path.join(TESTING_FILE_PATH, 'plain.tif')
146+
if os.path.exists(plain_tiff_image):
147+
os.unlink(plain_tiff_image)
148+
img = PIL.Image.open(source_image)
149+
img_array = np.array(img)
150+
result = PIL.Image.fromarray(img_array)
151+
result.save(plain_tiff_image)
152+
153+
# Setup parameters for running the test
154+
working_space = os.path.realpath('./test_results')
155+
os.makedirs(working_space, exist_ok=True)
156+
for expected_file in [result_name, orthomosaic_mask_name]:
157+
cur_path = os.path.join(working_space, expected_file)
158+
if os.path.exists(cur_path):
159+
os.unlink(cur_path)
160+
161+
command_line = [SOURCE_PATH, '--metadata', source_metadata, '--working_space', working_space, plain_tiff_image]
162+
subprocess.run(command_line, check=True)
163+
164+
# Check that the expected files were created
165+
for expected_file in [result_name, orthomosaic_mask_name]:
166+
assert os.path.exists(os.path.join(working_space, expected_file))
167+
168+
# Inspect the created files
169+
with open(os.path.join(working_space, result_name)) as in_file:
170+
res = json.load(in_file)
171+
assert 'code' in res
172+
assert res['code'] == 0
173+
174+
img = gdal.Open(os.path.join(working_space, orthomosaic_mask_name)).ReadAsArray()
175+
assert img is not None
176+
assert isinstance(img, np.ndarray)

0 commit comments

Comments
 (0)