Skip to content

Commit

Permalink
Rationalised loading of gdal
Browse files Browse the repository at this point in the history
  • Loading branch information
sambowers committed Oct 23, 2018
1 parent cfe41b3 commit 7347301
Showing 1 changed file with 15 additions and 34 deletions.
49 changes: 15 additions & 34 deletions biota/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import math
import numpy as np
import os
from osgeo import gdal
from scipy import ndimage
import scipy.stats as stats
import skimage.measure
Expand Down Expand Up @@ -41,9 +42,7 @@ def __init__(self, data_dir, lat, lon, year, forest_threshold = 10., area_thresh
"""
Loads data and metadata for an ALOS mosaic tile.
"""

from osgeo import gdal


# Test that inputs are of reasonable lats/lons/years
assert type(lat) == int, "Latitude must be an integer."
assert lat < 90. or lat > -90., "Latitude must be between -90 and 90 degrees."
Expand Down Expand Up @@ -254,9 +253,7 @@ def __getNodata(self, dtype = 6):
"""
Return nodata values
"""

from osgeo import gdal


# Generate a nodata value
if dtype == gdal.GDT_Byte:
nodata = 255
Expand Down Expand Up @@ -403,9 +400,9 @@ def updateMask(self, filename, buffer_size = 0., classes = [], output = False, s
# Add the new raster masks to the existing mask
self.mask = np.logical_or(self.mask, mask)

if output: self.__outputGeoTiff(self.mask, 'Mask', dtype = gdal.GDT_Byte)
if output: self.__outputGeoTiff(self.mask * 1, 'Mask', dtype = gdal.GDT_Byte)

if show: self.__showArray(self.mask, title = 'Mask', cmap = 'coolwarm')
if show: self.__showArray(self.mask * 1, title = 'Mask', cmap = 'coolwarm')

def resetMask(self):
"""
Expand Down Expand Up @@ -467,8 +464,6 @@ def getDate(self, output = False, show = False):
Loads date values into a numpy array.
"""

from osgeo import gdal

# Don't rerun processing if already present in memory
if not hasattr(self, 'date'):

Expand Down Expand Up @@ -507,9 +502,7 @@ def getDOY(self, output = False, show = False):
"""
Loads day of year values into a numpy array.
"""

from osgeo import gdal


# Don't rerun processing if already present in memory
if not hasattr(self, 'DOY'):

Expand Down Expand Up @@ -631,9 +624,7 @@ def getWoodyCover(self, output = False, show = False):
Get woody cover, based on a threshold of AGB.
min_area in ha
"""

from osgeo import gdal


# Don't rerun processing if already present in memory
if not hasattr(self, 'WoodyCover'):

Expand Down Expand Up @@ -671,9 +662,7 @@ def getForestPatches(self, output = False, show = False):
"""
Get numbered forest patches, based on woody cover threshold.
"""

from osgeo import gdal


# Don't rerun processing if already present in memory
if not hasattr(self, 'ForestPatches'):

Expand Down Expand Up @@ -702,9 +691,7 @@ def __outputGeoTiff(self, data, output_name, dtype = 6):
"""
Output a GeoTiff file.
"""

from osgeo import gdal


# Generate a standardised filename
filename = self.output_pattern%output_name

Expand All @@ -731,9 +718,7 @@ def __init__(self, tile_t1, tile_t2, change_intensity_threshold = 0.2, change_ma
'''
Initialise
'''

from osgeo import gdal


self.tile_t1 = tile_t1
self.tile_t2 = tile_t2

Expand Down Expand Up @@ -808,9 +793,7 @@ def __getNodata(self, dtype = 6):
"""
Return nodata values
"""

from osgeo import gdal


# Generate a nodata value
if dtype == gdal.GDT_Byte:
nodata = 255
Expand All @@ -829,10 +812,10 @@ def updateMask(self, filename, buffer_size = 0., classes = [], output = False, s

# Add the new raster masks to the existing mask
self.mask = np.logical_or(self.mask, mask)

if output: self.__outputGeoTiff(self.mask, 'Mask', dtype = gdal.GDT_Byte)
if output: self.__outputGeoTiff(self.mask * 1, 'Mask', dtype = gdal.GDT_Byte)

if show: self.__showArray(self.mask, title = 'Mask', cmap = 'coolwarm')
if show: self.__showArray(self.mask * 1, title = 'Mask', cmap = 'coolwarm')

def resetMask(self):
"""
Expand Down Expand Up @@ -906,9 +889,7 @@ def getChangeType(self, output = False, show = False):
intensity_threshold = threshold of proportional change with which to accept a change as real
area_threshold
'''

from osgeo import gdal


# Only run processing if not already done
if not hasattr(self, 'ChangeType'):

Expand Down

0 comments on commit 7347301

Please sign in to comment.