Skip to content

Commit

Permalink
Now also masks with .vrt
Browse files Browse the repository at this point in the history
  • Loading branch information
sambowers committed Jun 4, 2018
1 parent 9b98660 commit 163c405
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
6 changes: 3 additions & 3 deletions biota/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,16 +394,16 @@ def updateMask(self, filename, buffer_size = 0., classes = [], output = False, s

file_type = filename.split('/')[-1].split('.')[-1]

assert file_type in ['shp', 'tif', 'tiff'], "Input filename must be a GeoTiff or a shapefile."
assert file_type in ['shp', 'tif', 'tiff', 'vrt'], "Input filename must be a GeoTiff, VRT, or a shapefile."

if file_type == 'shp':

# Rasterize the shapefile, optionally with a buffer
mask = biota.mask.maskShapefile(self, filename, buffer_size = buffer_size)

if file_type in ['tif', 'tiff']:
if file_type in ['tif', 'tiff', 'vrt']:

assert classes != [], "If adding a GeoTiff file to the mask, you must also specify the class values to add to the mask (e.g. classes = [20, 160, 170, 190, 210])."
assert classes != [], "If adding a GeoTiff or VRT file to the mask, you must also specify the class values to add to the mask (e.g. classes = [20, 160, 170, 190, 210])."

# Resample and extract values from shapefile, optionally with a buffer
mask = biota.mask.maskRaster(self, filename, classes = classes, buffer_size = buffer_size)
Expand Down
14 changes: 7 additions & 7 deletions biota/mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,13 @@ def getBBox(shp, field, value):
return bbox


def maskRaster(tile, tif, classes = [], buffer_size = 0.):
def maskRaster(tile, raster, classes = [], buffer_size = 0.):
'''
Extract a mask from a GeoTiff based on specified classes
Args:
tile: An ALOS tile (biota.LoadTile())
tif: A GeoTiff file, with integer values
raster: A GeoTiff or VRT file, with integer values
classes: A list of values to add to be masked
buffer_size: Optionally specify a buffer to add around maked pixels, in meters.
Expand All @@ -180,14 +180,14 @@ def maskRaster(tile, tif, classes = [], buffer_size = 0.):

from osgeo import gdal

assert tif.rstrip('/').split('.')[-1] == 'tif' or tif.rstrip('/').split('.')[-1] == 'tiff', "tif input must be a GeoTiff file."
assert raster.rstrip('/').split('.')[-1] == 'tif' or raster.rstrip('/').split('.')[-1] == 'tiff' or raster.rstrip('/').split('.')[-1] == 'vrt', "raster input must be a GeoTiff or VRT file."

tif = os.path.expanduser(tif)
assert os.path.exists(tif), "GeoTiff file %s does not exist in the file system."%tif
raster = os.path.expanduser(raster)
assert os.path.exists(raster), "GeoTiff file %s does not exist in the file system."%raster

# Open GeoTiff and get metadata
ds_source = gdal.Open(tif)
proj_source = biota.IO.loadProjection(tif)
ds_source = gdal.Open(raster)
proj_source = biota.IO.loadProjection(raster)

# Create output file matching ALOS tile
gdal_driver = gdal.GetDriverByName('MEM')
Expand Down

0 comments on commit 163c405

Please sign in to comment.