Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
gliegard committed Jun 24, 2024
1 parent 69f42cb commit 6560188
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 31 deletions.
40 changes: 14 additions & 26 deletions pdaltools/color.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import argparse
import tempfile
import time
from collections import Counter
from math import ceil

import numpy as np
import pdal
import requests
from osgeo import gdal_array
Expand Down Expand Up @@ -60,7 +60,15 @@ def newfn(*args, **kwargs):
return decorator


def download_image_from_geoplateforme(proj, layer, minx, miny, maxx, maxy, pixel_per_meter, outfile, timeout):
def is_image_white(filename: str):
rasterArray = gdal_array.LoadFile(filename)
band_is_white = [np.all(band == 255) for band in rasterArray]
return np.all(band_is_white)


def download_image_from_geoplateforme(
proj, layer, minx, miny, maxx, maxy, pixel_per_meter, outfile, timeout, check_images
):
# Give single-point clouds a width/height of at least one pixel to have valid BBOX and SIZE
if minx == maxx:
maxx = minx + 1 / pixel_per_meter
Expand Down Expand Up @@ -90,14 +98,8 @@ def download_image_from_geoplateforme(proj, layer, minx, miny, maxx, maxy, pixel
print(f"Ecriture du fichier: {outfile}")
open(outfile, "wb").write(req.content)


def is_image_white(filename: str):
rasterArray = gdal_array.LoadFile(filename)
band3 = rasterArray[2]
flatten = band3.flatten()

# return true if that all element have the value 255
return Counter(flatten)[255] == len(flatten)
if check_images and is_image_white(outfile):
raise ValueError(f"Downloaded image is white, with stream: {layer}")


@copy_and_hack_decorator
Expand Down Expand Up @@ -136,12 +138,9 @@ def color(
if color_rvb_enabled:
tmp_ortho = tempfile.NamedTemporaryFile()
download_image_from_geoplateforme_retrying(
proj, stream_RGB, minx, miny, maxx, maxy, pixel_per_meter, tmp_ortho.name, timeout_second
proj, stream_RGB, minx, miny, maxx, maxy, pixel_per_meter, tmp_ortho.name, timeout_second, check_images
)

if check_images and is_image_white(tmp_ortho.name):
raise ValueError(f"Downloaded image is white, for RGB image, with stream: {stream_RGB}")

pipeline |= pdal.Filter.colorization(
raster=tmp_ortho.name, dimensions="Red:1:256.0, Green:2:256.0, Blue:3:256.0"
)
Expand All @@ -150,20 +149,9 @@ def color(
if color_ir_enabled:
tmp_ortho_irc = tempfile.NamedTemporaryFile()
download_image_from_geoplateforme_retrying(
proj,
stream_IRC,
minx,
miny,
maxx,
maxy,
pixel_per_meter,
tmp_ortho_irc.name,
timeout_second,
proj, stream_IRC, minx, miny, maxx, maxy, pixel_per_meter, tmp_ortho_irc.name, timeout_second, check_images
)

if check_images and is_image_white(tmp_ortho_irc.name):
raise ValueError(f"Downloaded image is white, for IRC image, with stream: {stream_IRC}")

pipeline |= pdal.Filter.colorization(raster=tmp_ortho_irc.name, dimensions="Infrared:1:256.0")

pipeline |= pdal.Writer.las(
Expand Down
10 changes: 5 additions & 5 deletions test/test_color.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def test_color_narrow_cloud():
@pytest.mark.geopf
def test_download_image_ok():
tif_output = os.path.join(TMPDIR, "download_image.tif")
color.download_image_from_geoplateforme(epsg, layer, minx, miny, maxx, maxy, pixel_per_meter, tif_output, 15)
color.download_image_from_geoplateforme(epsg, layer, minx, miny, maxx, maxy, pixel_per_meter, tif_output, 15, True)


def assert_las_is_white(filename: str):
Expand Down Expand Up @@ -121,22 +121,22 @@ def test_color_epsg_2975_detected():
def test_download_image_raise1():
retry_download = color.retry(2, 5)(color.download_image_from_geoplateforme)
with pytest.raises(requests.exceptions.HTTPError):
retry_download(epsg, "MAUVAISE_COUCHE", minx, miny, maxx, maxy, pixel_per_meter, OUTPUT_FILE, 15)
retry_download(epsg, "MAUVAISE_COUCHE", minx, miny, maxx, maxy, pixel_per_meter, OUTPUT_FILE, 15, True)


@pytest.mark.geopf
def test_download_image_raise2():
retry_download = color.retry(2, 5)(color.download_image_from_geoplateforme)
with pytest.raises(requests.exceptions.HTTPError):
retry_download("9001", layer, minx, miny, maxx, maxy, pixel_per_meter, OUTPUT_FILE, 15)
retry_download("9001", layer, minx, miny, maxx, maxy, pixel_per_meter, OUTPUT_FILE, 15, True)


def test_retry_on_server_error():
with requests_mock.Mocker() as mock:
mock.get(requests_mock.ANY, status_code=502, reason="Bad Gateway")
with pytest.raises(requests.exceptions.HTTPError):
retry_download = color.retry(2, 1, 2)(color.download_image_from_geoplateforme)
retry_download(epsg, layer, minx, miny, maxx, maxy, pixel_per_meter, OUTPUT_FILE, 15)
retry_download(epsg, layer, minx, miny, maxx, maxy, pixel_per_meter, OUTPUT_FILE, 15, True)
history = mock.request_history
assert len(history) == 3

Expand All @@ -146,7 +146,7 @@ def test_retry_on_connection_error():
mock.get(requests_mock.ANY, exc=requests.exceptions.ConnectionError)
with pytest.raises(requests.exceptions.ConnectionError):
retry_download = color.retry(2, 1)(color.download_image_from_geoplateforme)
retry_download(epsg, layer, minx, miny, maxx, maxy, pixel_per_meter, OUTPUT_FILE, 15)
retry_download(epsg, layer, minx, miny, maxx, maxy, pixel_per_meter, OUTPUT_FILE, 15, True)

history = mock.request_history
assert len(history) == 3
Expand Down

0 comments on commit 6560188

Please sign in to comment.