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

More changes were applied to make the code suitable for Python 3. In addition,...

More changes were applied to make the code suitable for Python 3. In addition, a test module was started.
parent 7295a267
......@@ -52,7 +52,7 @@ class FloatingPointRaster(Raster, GridEnvelope2D):
# If file does not exist and mode[0] = 'w', create it!
if (mode[0] == 'w') and (not os.path.exists(self.folder + os.path.sep + self.name)):
self.datafile = file(self.folder + os.path.sep + self.name, 'w');
self.datafile = open(self.folder + os.path.sep + self.name, 'w');
self.__envelope = GridEnvelope2D.__init__(self, ncols, nrows, xll, yll, cellsize, cellsize);
return True;
else:
......@@ -134,10 +134,7 @@ class FloatingPointRaster(Raster, GridEnvelope2D):
else: raise ValueError("Invalid file name: " + self.name);
try:
# Open the file if it exists, otherwise create it
if os.path.exists(self.folder + os.path.sep + hdrFilename):
hf = open(self.folder + os.path.sep + hdrFilename, 'w');
else:
hf = file(self.folder + os.path.sep + hdrFilename, 'w');
hf = open(self.folder + os.path.sep + hdrFilename, 'w');
# Now write all the attributes
hf.write("ncols " + str(self.ncols) + "\n");
......
# Copyright (c) 2004-2015 Alterra, Wageningen-UR
# Steven Hoek (steven.hoek@wur.nl), April 2015
from gridenvelope2d import GridEnvelope2D;
from raster import Raster
from formats.gridenvelope2d import GridEnvelope2D;
from formats.raster import Raster
from math import fabs;
import const, os;
from formats.const import constants as const
import os;
from tifffile import TiffFile;
try:
import numpy as np
......@@ -53,7 +54,7 @@ class GeotiffRaster(GridEnvelope2D, Raster):
# If file does not exist and mode[0] = 'w', create it!
if (mode[0] == 'w') and (not os.path.exists(self.folder + os.path.sep + self.name)):
self.datafile = file(self.folder + os.path.sep + self.name, 'w');
self.datafile = open(self.folder + os.path.sep + self.name, 'w');
self.__envelope = GridEnvelope2D.__init__(self, ncols, nrows, xll, yll, cellsize, cellsize);
return True;
else:
......@@ -162,10 +163,7 @@ class GeotiffRaster(GridEnvelope2D, Raster):
else: raise ValueError("Invalid file name: " + self.name);
try:
# Open the file if it exists, otherwise create it
if os.path.exists(self.folder + os.path.sep + hdrFilename):
hf = open(self.folder + os.path.sep + hdrFilename, 'w');
else:
hf = file(self.folder + os.path.sep + hdrFilename, 'w');
hf = open(self.folder + os.path.sep + hdrFilename, 'w');
# Now write all the attributes
hf.write(str(self.dx) + "\n");
......@@ -178,8 +176,8 @@ class GeotiffRaster(GridEnvelope2D, Raster):
hf.write(str(self.dy) + "\n");
else:
hf.write(str(-1 * self.dy) + "\n");
hf.write(str(self.xul + 0.5 * dx) + "\n");
hf.write(str(self.yul + 0.5 * dy) + "\n");
hf.write(str(self.xul + 0.5 * self.dx) + "\n");
hf.write(str(self.yul + 0.5 * self.dy) + "\n");
except Exception as e:
msg = "Header file " + hdrFilename + " could not be written in folder " + self.folder;
raise IOError(msg + "(" + str(e) + ")");
......
......@@ -43,6 +43,7 @@ class InMemoryRaster(Raster, GridEnvelope2D):
self.yll = yll
self.dx = cellsize
self.dy = cellsize
self.cellsize = cellsize
self.nodatavalue = nodatavalue
GridEnvelope2D.__init__(self, ncols, nrows, self.xll, self.yll, self.dx, self.dy)
return True;
......
pyshp==2.1.0
pyproj=1.9.5.1
numpy==1.14.3
tifffile==2019.3.18
tables==3.5.2
netCDF4==1.5.1.2
libtiff==0.4.2
import os
import sys
<PAMDataset>
<PAMRasterBand band="1">
<Metadata>
<MDI key="STATISTICS_MAXIMUM">28</MDI>
<MDI key="STATISTICS_MEAN">8.9333333333333</MDI>
<MDI key="STATISTICS_MINIMUM">-10</MDI>
<MDI key="STATISTICS_STDDEV">11.221804172631</MDI>
<MDI key="STATISTICS_VALID_PERCENT">100</MDI>
</Metadata>
</PAMRasterBand>
</PAMDataset>
10
0
0
-10
5
295
ncols 40
nrows 30
xllcorner 3.25
yllcorner 50.625
cellsize 0.1
NODATA_value -1
These data apply to the Netherlands. To see the province boundaries more clearly, search for the gadm36_NLD_1 shapefile online.
\ No newline at end of file
ncols 40
nrows 30
xllcorner 3.25
yllcorner 50.625
cellsize 0.1
NODATA_value -1
import os.path
from formats.asciigrid import AsciiGrid
import numpy as np
from tifffile import imread, TiffFile
# Aim is to produce 2 *.npy files with data saved in numpy format, for use in test units
def main():
# First prepare files with integer data
ag = None
try:
fn = os.path.join('..', '..', 'geodata', 'provinces.asc')
ag = AsciiGrid(fn, 'i')
ag.open('r')
# The following will cause that the header is written to the given file
tmp = AsciiGrid(os.path.join('data', 'intasc.hdr'), 'i')
tmp.open('w', ag.ncols, ag.nrows, ag.xll, ag.yll, ag.cellsize, ag.nodatavalue)
tmp.close()
# We'll now write the data to a separate file
data = np.zeros((ag.nrows, ag.ncols), dtype=np.int)
for i in range(ag.nrows):
line = ag.next()
for k in range(ag.ncols):
data[i,k] = line[k]
# We'll leave numpy to do save the file; probably the pickle format will be used
np.save(os.path.join('data', 'intasc.npy'), data)
finally:
if ag != None: ag.close()
# Now prepare a file with float data
f = None
try:
# Derive the size from the TIFF file
fn = os.path.join('..', '..', 'geodata', 'float.tif')
with TiffFile(fn) as tif:
page = tif.pages[0]
nrows = page.tags['ImageLength'].value
ncols = page.tags['ImageWidth'].value
# Also derive the data from the TIFF file; divide by 10 to get some decimals!
data = imread(fn) / 10
# Derive the geographic origin and pixel size from the world file (manipulated)
fn = os.path.join('..', '..', 'geodata', 'float.wld')
f = open(fn, 'r')
lines = f.readlines()
dx = float(lines[0].strip())
dy = float(lines[3].strip())
xll = float(lines[4].strip()) - 0.5*dx
yll = float(lines[5].strip()) - (nrows - 0.5) * abs(dy)
# The following will cause that the header is written to the given file
tmp = AsciiGrid(os.path.join('data', 'fltasc.hdr'), 'f')
tmp.open('w', ncols, nrows, xll, yll, dx, -1)
tmp.close()
# We'll leave numpy to do save the file; probably the pickle format will be used
np.save(os.path.join('data', 'fltasc.npy'), data)
finally:
if f!= None: f.close()
if __name__ == "__main__":
main()
\ No newline at end of file
from formats.asciigrid import AsciiGrid
import numpy as np
import os.path
f = None
try:
# Test writing and reading of a grid with integer numbers
fn = os.path.join('data', 'intasc.npy')
data = np.load(fn)
hfn = os.path.join('data', 'intasc.hdr')
f = open(hfn, 'r')
header = f.readlines()
finally:
if f != None: f.close()
f = None
try:
# Test reading and writing of a grid with float numbers
fn = os.path.join('data', 'fltasc.npy')
data = np.load(fn)
finally:
if f != None: f.close()
\ No newline at end of file
from formats.bilraster import BilRaster
# Test reading and writing of a grid with integer numbers
# Test reading and writing of a grid with float numbers
\ No newline at end of file
from formats.floatingpointraster import FloatingPointRaster
# Test reading and writing of a grid with integer numbers
# Test reading and writing of a grid with float numbers
\ No newline at end of file
"""ClipLib - a Python library for clipping from rasters"""
from lmgeo.formats.gridenvelope2d import GridEnvelope2D
from formats.gridenvelope2d import GridEnvelope2D
from array import array
try:
import numpy as np
......
import lmgeo.formats.inmemoryraster as imr
from lmgeo.formats.raster import Raster
from lmgeo.formats.gridenvelope2d import GridEnvelope2D
import formats.inmemoryraster as imr
from formats.raster import Raster
from formats.gridenvelope2d import GridEnvelope2D
import numpy as np
def copy_to_memory(myraster):
......
import numpy as np
import lmgeo.toolbox.cliplib as cp
import lmgeo.formats.gridenvelope2d as ge
import toolbox.cliplib as cp
import formats.gridenvelope2d as ge
from pyproj import Proj, transform
# Constants
......
Markdown is supported
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