Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write geotiff #28

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
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))
area_box = ((138.632071411, -32.447310785), (138.644218874, -32.456979174))

if __name__ == '__main__':
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)
Expand Down Expand Up @@ -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))
print(np.array(lat_array))
geo_tiff.write("tests/inputs/red_new.tif")
20 changes: 19 additions & 1 deletion geotiff/geotiff.py
Original file line number Diff line number Diff line change
@@ -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]]
Expand Down Expand Up @@ -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)

3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ pytest
tifffile==2021.7.2
numpy
pyproj
zarr
zarr
tifftools
5 changes: 3 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
@@ -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"

Expand Down Expand Up @@ -49,6 +49,7 @@ def run(self):
'numpy',
'pyproj',
'zarr',
'tifftools',
],
cmdclass={
'verify': VerifyVersionCommand,
Expand Down
2 changes: 1 addition & 1 deletion tests/test_geotiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down