Commit ec57b91b authored by Hoek, Steven's avatar Hoek, Steven
Browse files

A library to copy a raster into memory. This is esp. useful for raster types...

A library to copy a raster into memory. This is esp. useful for raster types such as TileTiffRaster which seem not to support repeated reading
parent 5c95c1b2
......@@ -4,3 +4,4 @@ from .gridenvelope2d import GridEnvelope2D;
from .netcdf4envelope2d import Netcdf4Envelope2D;
from .hdf5raster import Hdf5Raster;
from .netcdf4raster import Netcdf4Raster;
from .rowtiffraster import RowTiffRaster
\ No newline at end of file
import lmgeo.formats.inmemoryraster as imr
from lmgeo.formats.raster import Raster
from lmgeo.formats.gridenvelope2d import GridEnvelope2D
import numpy as np
def copyToMemory(myraster):
# Check input
if not isinstance(myraster, Raster):
raise Exception("Input is not a raster")
if not isinstance(myraster, GridEnvelope2D):
raise Exception("Input is not a grid envelope")
result = None
try:
# Get particulars from the input
numpy_type = myraster.get_numpy_type()
mydata = np.zeros((myraster.nrows, myraster.ncols), dtype=numpy_type)
if numpy_type in [np.int, np.uint16, np.uint32, np.int16, np.int32]:
fmt = 'i'
else:
fmt = 'f'
nrows = myraster.nrows
ncols = myraster.ncols
nodatavalue = myraster.nodatavalue
# Use them to initialise a InMemory raster
result = imr.InMemoryRaster("C:\dummy_folder\dummyfile.nix", mydata, fmt)
if not result.open('w', ncols, nrows, myraster.xll, myraster.yll, myraster.dx, nodatavalue):
raise Exception("Unable to initialise InMemoryRaster.")
# Now copy the data
for i in range(myraster.nrows):
line = myraster.next()
line.shape = (ncols,)
result.data[i,:] = line
except Exception as e:
raise Exception("Error in module copylib: %s" + str(e))
finally:
result.reset()
myraster.reset()
return result
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment