Skip to content

Commit

Permalink
Merge pull request #12 from AgPipeline/develop
Browse files Browse the repository at this point in the history
Merging support for non-GeoTIFF images (plain TIFF)
  • Loading branch information
Chris-Schnaufer authored Jul 9, 2021
2 parents 937ca40 + cf3d321 commit bcdf280
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 15 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down
43 changes: 43 additions & 0 deletions tests/test_soilmask_ratio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
25 changes: 13 additions & 12 deletions transformer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []

Expand All @@ -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:
Expand All @@ -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'],
Expand Down

0 comments on commit bcdf280

Please sign in to comment.