| import re |
| import datetime |
| import pytest |
| import numpy as np |
| import pandas as pd |
| import rasterio |
|
|
| from pathlib import Path |
| from PIL import Image |
| from PIL.ExifTags import TAGS, GPSTAGS |
| from numbers import Rational |
| from math import isclose |
|
|
| from geopy import distance |
| from sklearn.neighbors import NearestNeighbors |
|
|
| NUM_LOW_ALT_POINTS = 48 |
| NUM_AERIAL_POINTS = 378 |
| FIELD_ALTITUDE = 580 |
| NUMBER_REGEX = re.compile(r'DJI_(\d*)') |
|
|
| @pytest.fixture |
| def expected_low_alt_dirs(): |
| return { |
| "14.05.2025": {}, |
| "19.05.2025": {"3m", "5m"}, |
| "02.06.2025": {"5m"}, |
| "17.06.2025": {"10m"}, |
| } |
|
|
| @pytest.fixture |
| def expected_reconstructed_files(): |
| return [ |
| "dsm.tif", |
| "result.tif", |
| "result_Blue.tif", |
| "result_Green.tif", |
| "result_NIR.tif", |
| "result_Red.tif", |
| "result_RedEdge.tif", |
| "index_map/GNDVI.tif", |
| "index_map/LCI.tif", |
| "index_map/NDRE.tif", |
| "index_map/NDVI.tif", |
| "index_map/OSAVI.tif", |
| "index_map_color/GNDVI_local.tif", |
| "index_map_color/LCI_local.tif", |
| "index_map_color/NDRE_local.tif", |
| "index_map_color/NDVI_local.tif", |
| "index_map_color/OSAVI_local.tif", |
| ] |
|
|
|
|
| @pytest.fixture |
| def dates(): |
| current_dir = Path('.') |
| return [date for date in current_dir.glob("*.2025") if date.is_dir()] |
|
|
|
|
| @pytest.fixture |
| def low_alt_dates(): |
| current_dir = Path('.') / "low_alt_aerial" |
| return [date for date in current_dir.glob("*.2025") if date.is_dir()] |
|
|
|
|
|
|
| @pytest.fixture |
| def expected_coordinates(): |
| points_from_clustering = np.array([ |
| [42.68728947222222, 23.551391194444445], |
| [42.68753313888889, 23.55158558333333], |
| [42.68719561111111, 23.551736694444447], |
| [42.68777580555555, 23.55177588888889], |
| [42.68742836111111, 23.55191866666667], |
| [42.68800580555556, 23.551961305555555], |
| [42.68706686111111, 23.552049944444445], |
| [42.68766652777777, 23.55210197222222], |
| [42.68824475, 23.55214391666667], |
| [42.68733511111111, 23.552177111111117], |
| [42.68790061111111, 23.552279416666668], |
| [42.688478277777776, 23.552312611111116], |
| [42.68757630555555, 23.552349083333333], |
| [42.68693769444444, 23.552375583333333], |
| [42.68813625, 23.55246508333333], |
| [42.688717277777776, 23.552487361111112], |
| [42.68720997222222, 23.55250088888889], |
| [42.68781238888889, 23.55252313888889], |
| [42.68836805555555, 23.552659472222224], |
| [42.68743805555555, 23.55266425], |
| [42.68895875, 23.552668944444445], |
| [42.68679780555555, 23.552695611111112], |
| [42.68804525, 23.55271505555556], |
| [42.68708316666666, 23.55283188888889], |
| [42.68767447222222, 23.55284063888889], |
| [42.68860108333333, 23.55284225], |
| [42.68827408333333, 23.55289391666667], |
| [42.688826166666665, 23.55298816666667], |
| [42.68665552777777, 23.55300711111111], |
| [42.68790877777777, 23.55302052777778], |
| [42.68732319444444, 23.55302491666667], |
| [42.68847563888889, 23.55316630555556], |
| [42.68688913888889, 23.55318988888889], |
| [42.688146277777776, 23.553206972222224], |
| [42.68755158333333, 23.553219694444444], |
| [42.68869205555555, 23.55330775], |
| [42.68712933333333, 23.553369944444444], |
| [42.687776166666666, 23.553416861111117], |
| [42.688352888888886, 23.553481416666667], |
| [42.68736447222222, 23.55354133333333], |
| [42.68800347222222, 23.55361427777778], |
| [42.68855961111111, 23.55363263888889], |
| [42.68760519444445, 23.553714527777775], |
| [42.688229666666665, 23.553808916666668], |
| [42.68784438888888, 23.55391138888889], |
| [42.68843561111111, 23.553958055555555], |
| [42.688079305555554, 23.554097472222224], |
| [42.68830763888889, 23.55428075], |
| ]) |
| |
| assert points_from_clustering.shape == (NUM_LOW_ALT_POINTS, 2) |
| return points_from_clustering |
| |
|
|
| def degrees_to_decimal(degrees: Rational, minutes: Rational, seconds: Rational, direction: str): |
| assert(isinstance(degrees, Rational)) |
| assert(isinstance(minutes, Rational)) |
| assert(isinstance(seconds, Rational)) |
| degrees = float(degrees) |
| minutes = float(minutes) |
| seconds = float(seconds) |
|
|
| return (degrees + minutes / 60 + seconds / 3600) * (-1 if direction in ['W', 'S'] else 1) |
|
|
|
|
| def get_exif_data(path): |
| image = Image.open(path) |
| exif_data = {} |
| |
| exif = image.getexif() |
| assert exif is not None, path |
|
|
| for tag, value in exif.items(): |
| tag_name = TAGS.get(tag, tag) |
| exif_data[tag_name] = value |
|
|
| assert 'DateTime' in exif_data, path |
| exif_data['DateTime'] = datetime.datetime.strptime(exif_data['DateTime'], '%Y:%m:%d %H:%M:%S') |
| |
| assert 'GPSInfo' in exif_data |
| gps_info = {} |
|
|
| for key,value in exif.get_ifd(0x8825).items(): |
| decode = GPSTAGS.get(key, key) |
| gps_info[decode] = value |
|
|
| assert 'GPSLatitude' in gps_info, path |
| assert 'GPSLongitude' in gps_info, path |
| assert 'GPSAltitude' in gps_info, path |
|
|
| for key in gps_info.keys(): |
| if key == 'GPSLatitude': |
| decim = degrees_to_decimal(*gps_info[key], gps_info['GPSLatitudeRef']) |
| gps_info[key] = decim |
| if key == 'GPSLongitude': |
| decim = degrees_to_decimal(*gps_info[key], gps_info['GPSLongitudeRef']) |
| gps_info[key] = decim |
|
|
| exif_data['decoded_gps_info'] = gps_info |
| return exif_data |
|
|
|
|
| def test_all_dates_are_present(dates, expected_low_alt_dirs): |
| assert len(dates) == len(expected_low_alt_dirs), dates |
|
|
|
|
| def test_lowalt_folder_integrity(dates, expected_low_alt_dirs, expected_coordinates): |
| NUM_NEIGHBORS = 2 |
| neighbors = NearestNeighbors(n_neighbors=NUM_NEIGHBORS).fit(expected_coordinates) |
| |
| points = [] |
| for d in dates: |
| |
| if d.name == "14.05.2025": |
| continue |
| low_alt_dirs = { lad for lad in d.glob("*m") if lad.is_dir() } |
| |
| assert len(low_alt_dirs) > 0 |
| assert {n.name for n in low_alt_dirs} == expected_low_alt_dirs[d.name], d.name |
|
|
| for low_alt_dir in low_alt_dirs: |
| altitude = int(low_alt_dir.name[:-1]) + FIELD_ALTITUDE |
|
|
| jpegs = list(low_alt_dir.glob("*.JPG")) |
| jpegs.sort() |
| assert len(jpegs) == NUM_LOW_ALT_POINTS |
|
|
| tifs = list(low_alt_dir.glob("*.TIF")) |
| assert len(tifs) == 5 * NUM_LOW_ALT_POINTS |
|
|
| found_coordinates = {} |
| for f in jpegs: |
| ed = get_exif_data(f) |
| assert ed['DateTime'].strftime("%d.%m.%Y") == d.name |
|
|
| gps = ed['decoded_gps_info'] |
| assert isclose(gps['GPSAltitude'], altitude, rel_tol=0.02), f'{low_alt_dir}, {f}' |
|
|
| point = (gps['GPSLatitude'], gps['GPSLongitude']) |
| indices = neighbors.kneighbors(np.expand_dims(point, 0), return_distance=False).flatten() |
| |
| for i in range(NUM_NEIGHBORS): |
| nearest_point = tuple(expected_coordinates[indices[i]]) |
| if nearest_point not in found_coordinates: |
| found_coordinates[nearest_point] = point |
| break |
| else: |
| print(( |
| f"WARN: {f} nearest point {nearest_point} for {point} is already claimed by {found_coordinates[nearest_point]}. " |
| f"Distance: {distance.distance(point, nearest_point).m}" |
| ) |
| ) |
| |
| assert nearest_point is not None, (f, point) |
| assert distance.distance(point, nearest_point).m < 5.95, (f, point, nearest_point) |
|
|
| points.append( |
| { |
| 'date': d.name, |
| 'altitude': gps['GPSAltitude'], |
| 'height': altitude - FIELD_ALTITUDE, |
| 'file': str(f), |
| 'geometry': point, |
| 'x': point[0], |
| 'y': point[1], |
| 'ref_x': nearest_point[0], |
| 'ref_y': nearest_point[1], |
| } |
| ) |
| |
| jpeg_number = int(NUMBER_REGEX.match(f.name).group(1)) |
| assert jpeg_number > 0 and jpeg_number < 9999 |
| tif_files = [f.parent / f"DJI_{jpeg_number + i:04d}.TIF" for i in range(1, 6)] |
| |
| for tif in tif_files: |
| assert tif.exists(), tif |
| tif_ed = get_exif_data(tif) |
| assert tif_ed['DateTime'].strftime("%d.%m.%Y") == d.name, tif |
| tif_gps = tif_ed['decoded_gps_info'] |
|
|
| |
| assert isclose(tif_gps['GPSAltitude'], gps['GPSAltitude'], rel_tol=1e-3), tif |
| assert isclose(tif_gps['GPSLatitude'], gps['GPSLatitude'], rel_tol=1e-7), tif |
| assert isclose(tif_gps['GPSLongitude'], gps['GPSLongitude'], rel_tol=1e-7), tif |
| assert len(found_coordinates) == NUM_LOW_ALT_POINTS, (d.name, low_alt_dir.name) |
| |
| pd.DataFrame(points).to_csv('points.csv', index=False) |
|
|
|
|
| def test_aerial_folder_integrity(dates): |
| |
| for d in dates: |
| |
| aerial = d / "aerial" |
| assert aerial.exists() |
| |
| jpegs = list(aerial.glob("**/*.JPG")) |
| assert len(jpegs) >= NUM_AERIAL_POINTS and len(jpegs) <= NUM_AERIAL_POINTS + 2 |
|
|
| tifs = list(aerial.glob("**/*.TIF")) |
| assert len(tifs) == len(jpegs) * 5 |
|
|
| for f in jpegs + tifs: |
| ed = get_exif_data(f) |
| assert ed['DateTime'].strftime("%d.%m.%Y") == d.name |
|
|
|
|
| def test_low_alt_aerial_folder_integrity(low_alt_dates): |
| |
| for d in low_alt_dates: |
| |
| aerial = d / "aerial" |
| assert aerial.exists() |
| |
| jpegs = list(aerial.glob("**/*.JPG")) |
| assert len(jpegs) == 935 or len(jpegs) == 1309 or len(jpegs) == 2575 |
|
|
| tifs = list(aerial.glob("**/*.TIF")) |
| assert len(tifs) == len(jpegs) * 5 |
|
|
| for f in jpegs + tifs: |
| ed = get_exif_data(f) |
| assert ed['DateTime'].strftime("%d.%m.%Y") == d.name |
|
|
|
|
| def test_low_alt_terra_folder_integrity(low_alt_dates, expected_reconstructed_files): |
| for d in low_alt_dates: |
| for subdir in [d / "terra/lu"]: |
| assert subdir.exists() |
| assert subdir.is_dir() |
| |
| assert {f.name for f in subdir.iterdir()} == {"map", "mission.json"}, subdir |
| assert {f.name for f in (subdir / "map").iterdir() if f.is_dir()} == {"index_map", "index_map_color" }, subdir / "map" |
| assert {f.name for f in (subdir / "map/index_map").iterdir() if f.is_dir()} == set(), subdir / "map/index_map" |
| assert {f.name for f in (subdir / "map/index_map_color").iterdir() if f.is_dir()} == set(), subdir / "map/index_map_color" |
| assert (subdir / "map/SDK_Log.txt").exists() == False, subdir |
|
|
| for f in [subdir / "map" / f for f in expected_reconstructed_files]: |
| assert f.exists() |
| assert f.is_file() |
| dataset = rasterio.open(f) |
| if not str(f).endswith("dsm.tif"): |
| |
| assert dataset.width >= 34060 and dataset.width <= 54977, f |
| assert dataset.height >=24880 and dataset.height <= 56957, f |
| |
| if not str(f).endswith("_local.tif"): |
| assert dataset.crs.to_epsg() == 4326, f |
| b = dataset.bounds |
| assert b.left >= 23.550687255395935, f |
| assert b.right <= 23.554677806626483, f |
| assert b.top <= 42.689341982307795, f |
| assert b.bottom >= 42.68628839237808, f |
| assert f.with_suffix(".prj").exists() |
| assert f.with_suffix(".tfw").exists() |
| |
| if "index_map" in str(f) and "index_map_color" not in str(f): |
| data = dataset.read() |
| assert np.nanmin(data) >= -1 |
| assert np.nanmax(data) <= 1 |
| |
|
|
| def test_terra_folder_integrity(dates, expected_reconstructed_files): |
| for d in dates: |
| for subdir in [d / "terra/default", d / "terra/lu"]: |
| assert subdir.exists() |
| assert subdir.is_dir() |
| |
| assert {f.name for f in subdir.iterdir()} == {"map", "mission.json"}, subdir |
| assert {f.name for f in (subdir / "map").iterdir() if f.is_dir()} == {"index_map", "index_map_color" }, subdir / "map" |
| assert {f.name for f in (subdir / "map/index_map").iterdir() if f.is_dir()} == set(), subdir / "map/index_map" |
| assert {f.name for f in (subdir / "map/index_map_color").iterdir() if f.is_dir()} == set(), subdir / "map/index_map_color" |
| assert (subdir / "map/SDK_Log.txt").exists() == False, subdir |
|
|
| for f in [subdir / "map" / f for f in expected_reconstructed_files]: |
| assert f.exists() |
| assert f.is_file() |
| dataset = rasterio.open(f) |
| if not str(f).endswith("dsm.tif"): |
| print(dataset.width, dataset.height, f) |
| assert dataset.width >= 11596 and dataset.width <= 12035, f |
| assert dataset.height >=12028 and dataset.height <= 12388, f |
| |
| if not str(f).endswith("_local.tif"): |
| assert dataset.crs.to_epsg() == 4326, f |
| b = dataset.bounds |
| assert isclose(b.left, 23.550687255395935, rel_tol=1e-5), f |
| assert isclose(b.right, 23.554677806626483, rel_tol=1e-5), f |
| assert isclose(b.top, 42.689341982307795, rel_tol=1e-5), f |
| assert isclose(b.bottom, 42.68628839237808, rel_tol=1e-5), f |
| assert f.with_suffix(".prj").exists() |
| assert f.with_suffix(".tfw").exists() |
| |
| if "index_map" in str(f) and "index_map_color" not in str(f): |
| data = dataset.read() |
| assert np.nanmin(data) >= -1 |
| assert np.nanmax(data) <= 1 |
|
|
|
|
| def test_extra_folder_exists(dates): |
| for d in dates: |
| extra_dir = (d / "extra") |
| assert extra_dir.exists() == False, extra_dir.absolute() |
|
|