Add generic read and write
This commit is contained in:
parent
5daa290127
commit
a18b669437
@ -15,3 +15,5 @@ A = np.arange(10*10).reshape(10,10)
|
||||
|
||||
t = triskele.Triskele(A)
|
||||
print (t.filter())
|
||||
|
||||
print(triskele.read('/tmp/outfile.tif'))
|
||||
|
49
python/triskele/CreaTIFF.py
Normal file
49
python/triskele/CreaTIFF.py
Normal file
@ -0,0 +1,49 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
# \file CreaTIFF.py
|
||||
# \brief TODO
|
||||
# \author Florent Guiotte <florent.guiotte@gmail.com>
|
||||
# \version 0.1
|
||||
# \date 23 mars 2018
|
||||
#
|
||||
# TODO details
|
||||
|
||||
import numpy as np
|
||||
import gdal
|
||||
|
||||
np_to_gdt = {
|
||||
np.uint8: gdal.GDT_Byte,
|
||||
np.float32: gdal.GDT_Float32,
|
||||
np.float64: gdal.GDT_Float64,
|
||||
np.uint16: gdal.GDT_UInt16,
|
||||
np.uint32: gdal.GDT_UInt32,
|
||||
np.int64: gdal.GDT_TypeCount
|
||||
}
|
||||
|
||||
def write(fname, X):
|
||||
"""X.shape = (length, width) or
|
||||
X.shape = (length, width, samples)"""
|
||||
|
||||
width = X.shape[1]
|
||||
length = X.shape[0]
|
||||
|
||||
if len(X.shape) < 3:
|
||||
samples = 1
|
||||
X = np.expand_dims(X, 2)
|
||||
else:
|
||||
samples = X.shape[2]
|
||||
|
||||
driver = gdal.GetDriverByName('GTiff')
|
||||
dst_ds = driver.Create(str(fname), width, length, samples, np_to_gdt[X.dtype.type])
|
||||
|
||||
for i in range(samples):
|
||||
dst_ds.GetRasterBand(i + 1).WriteArray(X[:,:,i].astype(np.uint8))
|
||||
dst_ds.FlushCache()
|
||||
|
||||
def read(fname):
|
||||
gdl_data = gdal.Open(str(fname))
|
||||
|
||||
if gdl_data.RasterCount == 1:
|
||||
return gdl_data.ReadAsArray()
|
||||
else:
|
||||
return np.rollaxis(gdl_data.ReadAsArray(), 0, 3)
|
@ -1 +1,2 @@
|
||||
from .Triskele import Triskele
|
||||
from .CreaTIFF import write, read
|
||||
|
Loading…
Reference in New Issue
Block a user