From 0a1e2985a194778899d10e086997b16b7ddd6187 Mon Sep 17 00:00:00 2001 From: kipling Date: Thu, 15 Jul 2021 22:00:40 +1000 Subject: [PATCH 1/4] Add tifftools and make write mathod --- geotiff/geotiff.py | 20 +++++++++++++++++++- requirements.txt | 3 ++- setup.py | 1 + 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/geotiff/geotiff.py b/geotiff/geotiff.py index f6699df..4bb6c1b 100644 --- a/geotiff/geotiff.py +++ b/geotiff/geotiff.py @@ -1,8 +1,9 @@ from typing import List, Optional, Tuple, Union -from tifffile import imread, TiffFile # type: ignore +from tifffile import imread, TiffFile, imwrite # type: ignore import numpy as np # type: ignore from pyproj import Transformer, CRS import zarr # type: ignore +import tifftools # type: ignore BBox = Tuple[Tuple[float, float], Tuple[float, float]] @@ -420,3 +421,20 @@ def read_box( tiff_array = self.read() cut_tif_array: np.ndarray = np.array(tiff_array[y_min:y_max, x_min:x_max]) return cut_tif_array + + def write(self, file: str): + print("WARNING: this method is expiremental and is UNSTABLE") + temp_file = "temp.tif" + imwrite(file = temp_file, data = self._z) + info_original = tifftools.read_tiff(self.file) + info_temp = tifftools.read_tiff(temp_file) + original_tag_dict = info_original['ifds'][0]['tags'] + temp_tag_dict = info_temp['ifds'][0]['tags'] + + for k, v in original_tag_dict.items(): + if k not in temp_tag_dict.keys(): + print(k, v) + info_temp['ifds'][0]['tags'][k] = v + + tifftools.write_tiff(info_temp, file, allowExisting=True) + \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index d0c9df8..2742a6b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,4 +2,5 @@ pytest tifffile==2021.7.2 numpy pyproj -zarr \ No newline at end of file +zarr +tifftools \ No newline at end of file diff --git a/setup.py b/setup.py index a3aa14c..5b546f1 100644 --- a/setup.py +++ b/setup.py @@ -49,6 +49,7 @@ def run(self): 'numpy', 'pyproj', 'zarr', + 'tifftools', ], cmdclass={ 'verify': VerifyVersionCommand, From 01bb1b9559c516c29bc853ec08393bdf088c11ab Mon Sep 17 00:00:00 2001 From: kipling Date: Thu, 15 Jul 2021 22:00:52 +1000 Subject: [PATCH 2/4] test write method --- example.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/example.py b/example.py index 2aa1876..5772cbf 100644 --- a/example.py +++ b/example.py @@ -4,8 +4,9 @@ from geotiff import GeoTiff # type: ignore -filename = "dem.tif" -# filename = "red.tif" +# filename = "dem.tif" +# filename = "dem_new.tif" +filename = "red.tif" dir = "./tests/inputs/" tiff_file = os.path.join(dir, filename) area_box: List[Tuple[float, float]] = ((138.632071411, -32.447310785), (138.644218874, -32.456979174)) @@ -14,7 +15,7 @@ print("testing read tiff") print(f"reading: {tiff_file}") print(f"Using bBox: {area_box}") - geo_tiff: GeoTiff = GeoTiff(tiff_file, crs_code=4326, as_crs=4326, band=0) + geo_tiff: GeoTiff = GeoTiff(tiff_file) print() print(geo_tiff.crs_code) @@ -48,4 +49,5 @@ print(np.array(lat_array)) lon_array, lat_array = geo_tiff.get_coord_arrays() print(np.array(lon_array)) - print(np.array(lat_array)) \ No newline at end of file + print(np.array(lat_array)) + geo_tiff.write("tests/inputs/red_new.tif") \ No newline at end of file From 49ef1709580068e864e7227ebce1c8bbe5c35f57 Mon Sep 17 00:00:00 2001 From: kipling Date: Thu, 15 Jul 2021 23:36:01 +1000 Subject: [PATCH 3/4] remove bad typing --- example.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/example.py b/example.py index 5772cbf..bba843f 100644 --- a/example.py +++ b/example.py @@ -9,7 +9,7 @@ filename = "red.tif" dir = "./tests/inputs/" tiff_file = os.path.join(dir, filename) -area_box: List[Tuple[float, float]] = ((138.632071411, -32.447310785), (138.644218874, -32.456979174)) +area_box = ((138.632071411, -32.447310785), (138.644218874, -32.456979174)) if __name__ == '__main__': print("testing read tiff") From e03a7229a9fba18af309262b7855a77d07a6d8cb Mon Sep 17 00:00:00 2001 From: kipling Date: Thu, 15 Jul 2021 23:40:56 +1000 Subject: [PATCH 4/4] make mypy happy --- setup.py | 4 ++-- tests/test_geotiff.py | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/setup.py b/setup.py index 5b546f1..2c25d55 100644 --- a/setup.py +++ b/setup.py @@ -1,7 +1,7 @@ -import setuptools +import setuptools # type: ignore import os import sys -from setuptools.command.install import install +from setuptools.command.install import install # type: ignore VERSION = "0.2a1" diff --git a/tests/test_geotiff.py b/tests/test_geotiff.py index b1a6d0c..0b62c4f 100644 --- a/tests/test_geotiff.py +++ b/tests/test_geotiff.py @@ -3,7 +3,7 @@ import pytest import os from geotiff import GeoTiff -import zarr +import zarr # type: ignore @pytest.fixture(params=["dem.tif", "gda_94_sand.tif", "sand_test.tif", "red.tif"]) def geo_tiff(request):