HUB-Datacube documentation

This documentation is structured as follows:

About

The HUB Datacube offers a high level interface for integrating heterogeneous raster and vector datasets (on-the-fly) into a user-defined reference pixel grid, resulting in an analysis-ready datacube.

Contact

Please provide feedback to Andreas Rabe (andreas.rabe@geo.hu-berlin.de) or create an issue on Bitbucket.

Installation

pip install

The following dependencies must be available: gdal, matplotlib (optional)

Install the latest released version:

python -m pip install https://bitbucket.org/hu-geomatics/hub-datacube/get/master.tar.gz

Or install the latest developer version:

python -m pip install https://bitbucket.org/hu-geomatics/hub-datacube/get/develop.tar.gz

conda install

Create a stand-alone Python environment with conda:

conda config --add channels conda-forge
conda create -n hubenv gdal matplotlib
activate hubenv

Install the latest released or developer version with pip (see pip install section above).

HUB Datacube Cookbook

General

Is HUB Datacube installed

Imports HUB Datacube and exits the program if the modules are not found.

import sys
try:
    import hubdc.core, hubdc.applier
except:
    sys.exit('ERROR: cannot find HUB Datacube modules')

Is the testdata installed

In this guide we use the EnMAP-Box testdata (https://bitbucket.org/hu-geomatics/enmap-box-testdata).

import sys
try:
    import enmapboxtestdata
except:
    sys.exit('ERROR: cannot find EnMAP-Box Testdata modules')

Is QGIS installed (optional)

We use QGIS 3 map canvas for quick looks (https://qgis.org).

try:
    import qgis, qgis.core, qgis.gui
except:
    exit('ERROR: cannot find QGIS modules')

Check versions installed

import hubdc
import enmapboxtestdata
import qgis.utils
print(hubdc.__version__)
print(enmapboxtestdata.__version__)
print(qgis.utils.Qgis.QGIS_VERSION)
0.16.0
0.9
3.4.3-Madeira

Raster dataset

Find API reference here: hubdc.core.RasterDataset

Open a raster dataset from file

import enmapboxtestdata
from hubdc.core import *

rasterDataset = openRasterDataset(filename=enmapboxtestdata.enmap)
print(rasterDataset)

Prints something like:

RasterDataset(gdalDataset=<osgeo.gdal.Dataset; proxy of <Swig Object of type 'GDALDatasetShadow *' at 0x000001CD7B004AE0> >)

Open a raster dataset from GDAL dataset

import enmapboxtestdata
from hubdc.core import *
from osgeo import gdal

# init with GDAL Dataset
gdalDataset = gdal.Open(enmapboxtestdata.enmap)
rasterDataset = RasterDataset(gdalDataset=gdalDataset)
print(rasterDataset)

# get the GDAL Dataset handle from a RasterDataset
print(rasterDataset.gdalDataset())

Prints something like:

RasterDataset(gdalDataset=<osgeo.gdal.Dataset; proxy of <Swig Object of type 'GDALDatasetShadow *' at 0x00000110C38B4B40> >)
<osgeo.gdal.Dataset; proxy of <Swig Object of type 'GDALDatasetShadow *' at 0x00000110C38B4B40> >

Close a raster dataset

Closing a raster dataset is useful in the middle of a script, to recover the resources held by accessing the dataset, remove file locks, etc. It is not necessary at the end of the script, as the Python garbage collector will do the same thing automatically when the script exits.

import enmapboxtestdata
from hubdc.core import *

rasterDataset = openRasterDataset(filename=enmapboxtestdata.enmap)
rasterDataset.close()
print(rasterDataset)

Prints:

RasterDataset(gdalDataset=None)

Get raster metadata

import enmapboxtestdata
from hubdc.core import *

rasterDataset = openRasterDataset(filename=enmapboxtestdata.enmap)

# get all domains
print('All domains:')
print(rasterDataset.metadataDict())

# get ENVI domain
print('ENVI domain:')
print(rasterDataset.metadataDomain(domain='ENVI'))

# get single item (list with wavelength casted to float)
print('ENVI wavelength:' )
print(rasterDataset.metadataItem(key='wavelength', domain='ENVI', dtype=float))

Prints:

All domains:
{'IMAGE_STRUCTURE':
    {
    'INTERLEAVE': 'BAND'
    },
 'ENVI':
    {
    'bands': '177',
    'band names': ['band 8', 'band 9', 'band 10', 'band 11', 'band 12', 'band 13', 'band 14', 'band 15', 'band 16', 'band 17', 'band 18', 'band 19', 'band 20', 'band 21', 'band 22', 'band 23', 'band 24', 'band 25', 'band 26', 'band 27', 'band 28', 'band 29', 'band 30', 'band 31', 'band 32', 'band 33', 'band 34', 'band 35', 'band 36', 'band 37', 'band 38', 'band 39', 'band 40', 'band 41', 'band 42', 'band 43', 'band 44', 'band 45', 'band 46', 'band 47', 'band 48', 'band 49', 'band 50', 'band 51', 'band 52', 'band 53', 'band 54', 'band 55', 'band 56', 'band 57', 'band 58', 'band 59', 'band 60', 'band 61', 'band 62', 'band 63', 'band 64', 'band 65', 'band 66', 'band 67', 'band 68', 'band 69', 'band 70', 'band 71', 'band 72', 'band 73', 'band 74', 'band 75', 'band 76', 'band 77', 'band 91', 'band 92', 'band 93', 'band 94', 'band 95', 'band 96', 'band 97', 'band 98', 'band 99', 'band 100', 'band 101', 'band 102', 'band 103', 'band 104', 'band 105', 'band 106', 'band 107', 'band 108', 'band 109', 'band 110', 'band 111', 'band 112', 'band 113', 'band 114', 'band 115', 'band 116', 'band 117', 'band 118', 'band 119', 'band 120', 'band 121', 'band 122', 'band 123', 'band 124', 'band 125', 'band 126', 'band 127', 'band 144', 'band 145', 'band 146', 'band 147', 'band 148', 'band 149', 'band 150', 'band 151', 'band 152', 'band 153', 'band 154', 'band 155', 'band 156', 'band 157', 'band 158', 'band 159', 'band 160', 'band 161', 'band 162', 'band 163', 'band 164', 'band 165', 'band 166', 'band 167', 'band 168', 'band 195', 'band 196', 'band 197', 'band 198', 'band 199', 'band 200', 'band 201', 'band 202', 'band 203', 'band 204', 'band 205', 'band 206', 'band 207', 'band 208', 'band 209', 'band 210', 'band 211', 'band 212', 'band 213', 'band 214', 'band 215', 'band 216', 'band 217', 'band 218', 'band 219', 'band 220', 'band 221', 'band 222', 'band 223', 'band 224', 'band 225', 'band 226', 'band 227', 'band 228', 'band 229', 'band 230', 'band 231', 'band 232', 'band 233', 'band 234', 'band 235', 'band 236', 'band 237', 'band 238', 'band 239'],
    'byte order': '0',
    'coordinate system string': ['PROJCS["UTM_Zone_33N"', 'GEOGCS["GCS_WGS_1984"', 'DATUM["D_WGS_1984"', 'SPHEROID["WGS_1984"', '6378137.0', '298.257223563]]', 'PRIMEM["Greenwich"', '0.0]', 'UNIT["Degree"', '0.0174532925199433]]', 'PROJECTION["Transverse_Mercator"]', 'PARAMETER["False_Easting"', '500000.0]', 'PARAMETER["False_Northing"', '0.0]', 'PARAMETER["Central_Meridian"', '15.0]', 'PARAMETER["Scale_Factor"', '0.9996]', 'PARAMETER["Latitude_Of_Origin"', '0.0]', 'UNIT["Meter"', '1.0]]'],
    'data ignore value': '-99',
    'data type': '2',
    'description': ['EnMAP02_Berlin_Urban_Gradient_2009.bsq', 'http://doi.org/10.5880/enmap.2016.008', 'spectral and spatial subset'],
    'file compression': '1',
    'file type': 'ENVI Standard',
    'fwhm': ['0.005800', '0.005800', '0.005800', '0.005800', '0.005800', '0.005800', '0.005800', '0.005800', '0.005800', '0.005800', '0.005900', '0.005900', '0.006000', '0.006000', '0.006100', '0.006100', '0.006200', '0.006200', '0.006300', '0.006400', '0.006400', '0.006500', '0.006600', '0.006600', '0.006700', '0.006800', '0.006900', '0.006900', '0.007000', '0.007100', '0.007200', '0.007300', '0.007300', '0.007400', '0.007500', '0.007600', '0.007700', '0.007800', '0.007900', '0.007900', '0.008000', '0.008100', '0.008200', '0.008300', '0.008400', '0.008400', '0.008500', '0.008600', '0.008700', '0.008700', '0.008800', '0.008900', '0.008900', '0.009000', '0.009100', '0.009100', '0.009200', '0.009300', '0.009300', '0.009400', '0.009400', '0.009500', '0.009500', '0.009600', '0.009600', '0.009600', '0.009600', '0.009700', '0.009700', '0.009700', '0.011800', '0.011900', '0.012100', '0.012200', '0.012400', '0.012500', '0.012700', '0.012800', '0.012900', '0.013100', '0.013200', '0.013300', '0.013400', '0.013500', '0.013600', '0.013700', '0.013800', '0.013900', '0.014000', '0.014000', '0.014100', '0.014100', '0.014200', '0.014200', '0.014300', '0.014300', '0.014300', '0.014400', '0.014400', '0.014400', '0.014400', '0.014400', '0.014400', '0.014400', '0.014400', '0.014400', '0.014400', '0.013700', '0.013600', '0.013600', '0.013500', '0.013500', '0.013400', '0.013400', '0.013300', '0.013200', '0.013200', '0.013100', '0.013100', '0.013000', '0.012900', '0.012900', '0.012800', '0.012800', '0.012700', '0.012700', '0.012600', '0.012500', '0.012500', '0.012400', '0.012400', '0.012300', '0.010900', '0.010800', '0.010800', '0.010700', '0.010700', '0.010600', '0.010600', '0.010500', '0.010500', '0.010400', '0.010400', '0.010400', '0.010300', '0.010300', '0.010200', '0.010200', '0.010100', '0.010100', '0.010100', '0.010000', '0.010000', '0.009900', '0.009900', '0.009900', '0.009800', '0.009800', '0.009700', '0.009700', '0.009700', '0.009600', '0.009600', '0.009600', '0.009500', '0.009500', '0.009400', '0.009400', '0.009400', '0.009300', '0.009300', '0.009300', '0.009200', '0.009200', '0.009100', '0.009100', '0.009100'],
    'header offset': '0',
    'interleave': 'bsq',
    'lines': '400',
    'samples': '220',
    'sensor type': 'Unknown',
    'wavelength': ['0.460000', '0.465000', '0.470000', '0.475000', '0.479000', '0.484000', '0.489000', '0.494000', '0.499000', '0.503000', '0.508000', '0.513000', '0.518000', '0.523000', '0.528000', '0.533000', '0.538000', '0.543000', '0.549000', '0.554000', '0.559000', '0.565000', '0.570000', '0.575000', '0.581000', '0.587000', '0.592000', '0.598000', '0.604000', '0.610000', '0.616000', '0.622000', '0.628000', '0.634000', '0.640000', '0.646000', '0.653000', '0.659000', '0.665000', '0.672000', '0.679000', '0.685000', '0.692000', '0.699000', '0.706000', '0.713000', '0.720000', '0.727000', '0.734000', '0.741000', '0.749000', '0.756000', '0.763000', '0.771000', '0.778000', '0.786000', '0.793000', '0.801000', '0.809000', '0.817000', '0.824000', '0.832000', '0.840000', '0.848000', '0.856000', '0.864000', '0.872000', '0.880000', '0.888000', '0.896000', '0.915000', '0.924000', '0.934000', '0.944000', '0.955000', '0.965000', '0.975000', '0.986000', '0.997000', '1.007000', '1.018000', '1.029000', '1.040000', '1.051000', '1.063000', '1.074000', '1.086000', '1.097000', '1.109000', '1.120000', '1.132000', '1.144000', '1.155000', '1.167000', '1.179000', '1.191000', '1.203000', '1.215000', '1.227000', '1.239000', '1.251000', '1.263000', '1.275000', '1.287000', '1.299000', '1.311000', '1.323000', '1.522000', '1.534000', '1.545000', '1.557000', '1.568000', '1.579000', '1.590000', '1.601000', '1.612000', '1.624000', '1.634000', '1.645000', '1.656000', '1.667000', '1.678000', '1.689000', '1.699000', '1.710000', '1.721000', '1.731000', '1.742000', '1.752000', '1.763000', '1.773000', '1.783000', '2.044000', '2.053000', '2.062000', '2.071000', '2.080000', '2.089000', '2.098000', '2.107000', '2.115000', '2.124000', '2.133000', '2.141000', '2.150000', '2.159000', '2.167000', '2.176000', '2.184000', '2.193000', '2.201000', '2.210000', '2.218000', '2.226000', '2.234000', '2.243000', '2.251000', '2.259000', '2.267000', '2.275000', '2.283000', '2.292000', '2.300000', '2.308000', '2.315000', '2.323000', '2.331000', '2.339000', '2.347000', '2.355000', '2.363000', '2.370000', '2.378000', '2.386000', '2.393000', '2.401000', '2.409000'],
    'wavelength units': 'Micrometers',
    'y start': '24',
    'z plot titles': ['wavelength [!7l!3m]!N', 'reflectance [*10000]']
    }
}

ENVI domain:
{
'bands': '177',
...
'z plot titles': ['wavelength [!7l!3m]!N', 'reflectance [*10000]']
}

ENVI wavelength:
[0.46, 0.465, 0.47, 0.475, 0.479, 0.484, 0.489, 0.494, 0.499, 0.503, 0.508, 0.513, 0.518, 0.523, 0.528, 0.533, 0.538, 0.543, 0.549, 0.554, 0.559, 0.565, 0.57, 0.575, 0.581, 0.587, 0.592, 0.598, 0.604, 0.61, 0.616, 0.622, 0.628, 0.634, 0.64, 0.646, 0.653, 0.659, 0.665, 0.672, 0.679, 0.685, 0.692, 0.699, 0.706, 0.713, 0.72, 0.727, 0.734, 0.741, 0.749, 0.756, 0.763, 0.771, 0.778, 0.786, 0.793, 0.801, 0.809, 0.817, 0.824, 0.832, 0.84, 0.848, 0.856, 0.864, 0.872, 0.88, 0.888, 0.896, 0.915, 0.924, 0.934, 0.944, 0.955, 0.965, 0.975, 0.986, 0.997, 1.007, 1.018, 1.029, 1.04, 1.051, 1.063, 1.074, 1.086, 1.097, 1.109, 1.12, 1.132, 1.144, 1.155, 1.167, 1.179, 1.191, 1.203, 1.215, 1.227, 1.239, 1.251, 1.263, 1.275, 1.287, 1.299, 1.311, 1.323, 1.522, 1.534, 1.545, 1.557, 1.568, 1.579, 1.59, 1.601, 1.612, 1.624, 1.634, 1.645, 1.656, 1.667, 1.678, 1.689, 1.699, 1.71, 1.721, 1.731, 1.742, 1.752, 1.763, 1.773, 1.783, 2.044, 2.053, 2.062, 2.071, 2.08, 2.089, 2.098, 2.107, 2.115, 2.124, 2.133, 2.141, 2.15, 2.159, 2.167, 2.176, 2.184, 2.193, 2.201, 2.21, 2.218, 2.226, 2.234, 2.243, 2.251, 2.259, 2.267, 2.275, 2.283, 2.292, 2.3, 2.308, 2.315, 2.323, 2.331, 2.339, 2.347, 2.355, 2.363, 2.37, 2.378, 2.386, 2.393, 2.401, 2.409]

Set raster metadata

import enmapboxtestdata
from hubdc.core import *

rasterDataset = openRasterDataset(filename=enmapboxtestdata.enmap)

# set multiple domains
copy = rasterDataset.translate()
copy.setMetadataDict(metadataDict={'domain1': {'a': 1, 'b': 2},
                                   'domain2': {'c': 3, 'd': 4}})
print({domain: copy.metadataDict()[domain] for domain in ['domain1', 'domain2']})

# set domain
copy = rasterDataset.translate()
copy.setMetadataDomain(metadataDomain={'a': 1, 'b': 2}, domain='domain')
print(copy.metadataDict()['domain'])

# set item
copy = rasterDataset.translate()
copy.setMetadataItem(key='a', value=1, domain='domain')
print(copy.metadataDict()['domain'])

Prints:

{'domain1': {'a': '1', 'b': '2'}, 'domain2': {'c': '3', 'd': '4'}}
{'a': '1', 'b': '2'}
{'a': '1'}

Get and set no data value

Warning

todo

Get raster band

import enmapboxtestdata
from hubdc.core import *

rasterDataset = openRasterDataset(filename=enmapboxtestdata.enmap)
rasterBandDataset = rasterDataset.band(index=0)
print(rasterBandDataset)

Prints:

RasterBandDataset(raster=RasterDataset(gdalDataset=<osgeo.gdal.Dataset; proxy of <Swig Object of type 'GDALDatasetShadow *' at 0x000001A6FFF34B40> >), index=0)

Read raster data

import enmapboxtestdata
from hubdc.core import *

rasterDataset = openRasterDataset(filename=enmapboxtestdata.enmap)

# read dataset

# - all bands as 3d array
print(rasterDataset.readAsArray().shape)
# - single band as 2d array
print(rasterDataset.band(index=0).readAsArray().shape)
# - pixel (z) profile as 1d array
print(rasterDataset.zprofile(pixel=Pixel(x=100, y=100)).shape)
# - column (x) profile as 1d array
print(rasterDataset.xprofile(row=Row(y=100, z=0)).shape)
# - row (y) profile as 1d array
print(rasterDataset.yprofile(column=Column(x=100, z=0)).shape)

# read dataset for given target grid (may include on-the-fly resampling and/or reprojection)

grid = Grid(extent=Extent(xmin=13, xmax=13.5, ymin=52, ymax=52.5, projection=Projection.wgs84()), resolution=0.001)
# - all bands as 3d array
print(rasterDataset.array(grid=grid, resampleAlg=gdal.GRA_Cubic).shape)
# - single band as 2d array
print(rasterDataset.band(index=0).array(grid=grid, resampleAlg=gdal.GRA_Cubic).shape)

Prints:

(177, 400, 220)
(400, 220)
(177,)
(220,)
(400,)
(177, 500, 500)
(500, 500)

Write raster data

Warning

todo

Loop through all raster bands

import enmapboxtestdata
from hubdc.core import *

rasterDataset = openRasterDataset(filename=enmapboxtestdata.enmap)
for band in rasterDataset.bands():
    array = band.readAsArray()
    noDataValue = band.noDataValue()
    values = array[array != noDataValue]
    min = values.min()
    max = values.max()
    mean = values.std()
    print('Band {} Stats: Minimum={}, Maximum={}, Mean={}'.format(band.index()+1, min, max, mean))

Prints:

Band 1 Stats: Minimum=110, Maximum=3759, Mean=212.88073662811726
Band 2 Stats: Minimum=105, Maximum=3757, Mean=214.96880571233484
Band 3 Stats: Minimum=96, Maximum=3792, Mean=217.37383030398556
...

Convert a vector to a raster

import enmapboxtestdata
from hubdc.core import *

vectorDataset = openVectorDataset(filename=enmapboxtestdata.landcover_polygons)
grid = vectorDataset.grid(resolution=5)
rasterDataset = vectorDataset.rasterize(grid, noDataValue=-9999, initValue=-9999,
                                        burnAttribute='level_3_id',
                                        filename='raster.tif', driver=GTiffDriver())
_images/vector_to_raster.png

Clip a raster with a vector

Clip a raster with the extent from a vector.

import enmapboxtestdata
from hubdc.core import *

rasterDataset = openRasterDataset(filename=enmapboxtestdata.enmap)
vectorDataset = openVectorDataset(filename=enmapboxtestdata.landcover_polygons)
grid = rasterDataset.grid().clip(extent=vectorDataset.extent())
clipped = rasterDataset.translate(grid=grid, filename='raster.tif', driver=GTiffDriver())

Note that the result raster grid is snapped to the original raster grid to prevent subpixel shifts. Because of this, some vector geometries may slightly lap over the grid borders.

_images/clip_raster_with_vector.png

Calculate zonal statistics

Calculates statistics on values (i.e. mean value) of a raster band within the zones given by a vector attribute.

In this example we use the level_3_id attribute as zones.

import enmapboxtestdata
from hubdc.core import *
import numpy as np

rasterDataset = openRasterDataset(filename=enmapboxtestdata.enmap)
vectorDataset = openVectorDataset(filename=enmapboxtestdata.landcover_polygons)

# rasterize zones attribute
zonesRasterDataset = vectorDataset.rasterize(grid=rasterDataset.grid(),
                                             burnAttribute='level_3_id')

# calculate zonal statistics for the first raster band
values = rasterDataset.band(index=0).readAsArray()
zones = zonesRasterDataset.band(index=0).readAsArray()

for i in np.unique(zones):
    mean = np.mean(values[zones == i]) # subset pixels for current zone and calculate mean
    print('Zone {} mean: {}'.format(i+1, mean))

Prints:

Zone 1.0 mean: 331.07156981342763
Zone 2.0 mean: 546.2222222222222
Zone 3.0 mean: 546.7610921501706
Zone 4.0 mean: 387.565625
Zone 5.0 mean: 365.40816326530614
Zone 6.0 mean: 838.8627450980392
Zone 7.0 mean: 191.05405405405406

Create raster from array

from hubdc.core import *
import numpy as np

array = np.array([[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
                  [ 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1],
                  [ 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1],
                  [ 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 1],
                  [ 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1],
                  [ 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 1],
                  [ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])

rasterDataset = RasterDataset.fromArray(array=array, filename='raster.tif', driver=GTiffDriver())
_images/raster_from_array.png

Create memory raster

Copy a raster into memory.

import enmapboxtestdata
from hubdc.core import *

rasterDataset = openRasterDataset(enmapboxtestdata.enmap)

# option a) with MemDriver
driver = MemDriver()
copy = rasterDataset.translate(driver=driver)

# option b) with in-memory files and any driver
driver = GTiffDriver()
options = [driver.Option.INTERLEAVE.BAND, driver.Option.COMPRESS.LZW]
copy = rasterDataset.translate(filename='/vsimem/raster.tif', driver=driver, options=options)

Replace no data value of raster with new value

import enmapboxtestdata
from hubdc.core import *

rasterDataset = openRasterDataset(filename=enmapboxtestdata.enmap)

# read data
array = rasterDataset.readAsArray()

# replace no data value
oldNoDataValue = rasterDataset.noDataValue()
newNoDataValue = -1
array[array==oldNoDataValue] = newNoDataValue

# write data
newRasterDataset = RasterDataset.fromArray(array=array, grid=rasterDataset.grid(),
                                           filename='raster.tif', driver=GTiffDriver())
newRasterDataset.setNoDataValue(value=newNoDataValue)

Raster band dataset

Get raster band information

import enmapboxtestdata
from hubdc.core import *

rasterDataset = openRasterDataset(filename=enmapboxtestdata.createClassification(30))
rasterBand = rasterDataset.band(index=0)
print('no data value: {}'.format(rasterBand.noDataValue()))
print('description: {}'.format(rasterBand.description()))
print('category names: {}'.format(rasterBand.categoryNames()))
print('category colors: {}'.format(rasterBand.categoryColors()))

Prints:

no data value: 0.0
description: Classification
category names: ['unclassified', 'impervious', 'low vegetation', 'tree', 'soil', 'water']
category colors: [(0, 0, 0, 255), (230, 0, 0, 255), (152, 230, 0, 255), (38, 115, 0, 255), (168, 112, 0, 255), (0, 100, 255, 255)]

Set raster band category names and colors

from hubdc.core import *

rasterDataset = RasterDataset.fromArray(array=[[[0, 1, 2, 3]]], filename='raster.bsq', driver=EnviDriver())
band = rasterDataset.band(index=0)
band.setCategoryNames(names=['unclassified', 'class 1', 'class 2', 'class 3'])
band.setCategoryColors(colors=[(0,0,0), (255, 0, 0), (0, 255, 0), (0, 0, 255)]) # list of rgb or rgba tuples
print(band.categoryNames())
print(band.categoryColors())

Prints:

['unclassified', 'class 1', 'class 2', 'class 3']
[(0, 0, 0, 255), (255, 0, 0, 255), (0, 255, 0, 255), (0, 0, 255, 255)]
..... Classes
class 1
class 2
class 3
_images/set_raster_band_categories.png

VectorData

Find API reference here: hubdc.core.VectorDataset

Warning

todo

Grid

Create grid

from hubflow.core import *

# create a grid in WGS84 for the whole world with 1 degree resolution
extent=Extent(xmin=-180.0, ymin=-90.0, xmax=180.0, ymax=90.0, projection=Projection.wgs84())
grid = Grid(extent=extent, resolution=1.0)
print(grid)

Prints:

Grid(extent=Extent(xmin=-180.0, xmax=180.0, ymin=-90.0, ymax=90.0, projection=Projection(wkt=GEOGCS["WGS 84",     DATUM["WGS_1984",         SPHEROID["WGS 84",6378137,298.257223563,             AUTHORITY["EPSG","7030"]],         AUTHORITY["EPSG","6326"]],     PRIMEM["Greenwich",0,         AUTHORITY["EPSG","8901"]],     UNIT["degree",0.0174532925199433,         AUTHORITY["EPSG","9122"]],     AUTHORITY["EPSG","4326"]])), resolution=Resolution(x=1.0, y=1.0), projection=Projection(wkt=GEOGCS["WGS 84",     DATUM["WGS_1984",         SPHEROID["WGS 84",6378137,298.257223563,             AUTHORITY["EPSG","7030"]],         AUTHORITY["EPSG","6326"]],     PRIMEM["Greenwich",0,         AUTHORITY["EPSG","8901"]],     UNIT["degree",0.0174532925199433,         AUTHORITY["EPSG","9122"]],     AUTHORITY["EPSG","4326"]])

Get grid information

print(grid.shape())
print(grid.size())
print(grid.geoTransform())

Prints:

(180, 360)
RasterSize(x=360, y=180)
(-180.0, 1.0, 0.0, 90.0, 0.0, -1.0)

Subset grid by pixel offset and size

subgrid = grid.subset(offset=(0, 0), size=(10, 10))
print(subgrid)
print(subgrid.size())

Prints:

Grid(extent=Extent(xmin=-180.0, xmax=-170.0, ymin=80.0, ymax=90.0, projection=Projection(wkt=GEOGCS["WGS 84",     DATUM["WGS_1984",         SPHEROID["WGS 84",6378137,298.257223563,             AUTHORITY["EPSG","7030"]],         AUTHORITY["EPSG","6326"]],     PRIMEM["Greenwich",0,         AUTHORITY["EPSG","8901"]],     UNIT["degree",0.0174532925199433,         AUTHORITY["EPSG","9122"]],     AUTHORITY["EPSG","4326"]])), resolution=Resolution(x=1.0, y=1.0), projection=Projection(wkt=GEOGCS["WGS 84",     DATUM["WGS_1984",         SPHEROID["WGS 84",6378137,298.257223563,             AUTHORITY["EPSG","7030"]],         AUTHORITY["EPSG","6326"]],     PRIMEM["Greenwich",0,         AUTHORITY["EPSG","8901"]],     UNIT["degree",0.0174532925199433,         AUTHORITY["EPSG","9122"]],     AUTHORITY["EPSG","4326"]])
RasterSize(x=10, y=10)

Create systematic subgrids (tiling scheme)

subgrids = grid.subgrids(size=(90, 90))
for subgrid, i, iy, ix in subgrids:
    print('x{}, y{}: {}'.format(ix, iy, subgrid))

Prints:

x0, y0: Grid(extent=Extent(xmin=-180.0, xmax=-90.0, ymin=0.0, ymax=90.0, projection=Projection(wkt=GEOGCS["WGS 84",     DATUM["WGS_1984",         SPHEROID["WGS 84",6378137,298.257223563,             AUTHORITY["EPSG","7030"]],         AUTHORITY["EPSG","6326"]],     PRIMEM["Greenwich",0,         AUTHORITY["EPSG","8901"]],     UNIT["degree",0.0174532925199433,         AUTHORITY["EPSG","9122"]],     AUTHORITY["EPSG","4326"]])), resolution=Resolution(x=1.0, y=1.0), projection=Projection(wkt=GEOGCS["WGS 84",     DATUM["WGS_1984",         SPHEROID["WGS 84",6378137,298.257223563,             AUTHORITY["EPSG","7030"]],         AUTHORITY["EPSG","6326"]],     PRIMEM["Greenwich",0,         AUTHORITY["EPSG","8901"]],     UNIT["degree",0.0174532925199433,         AUTHORITY["EPSG","9122"]],     AUTHORITY["EPSG","4326"]])
x1, y0: Grid(extent=Extent(xmin=-90.0, xmax=0.0, ymin=0.0, ymax=90.0, projection=Projection(wkt=GEOGCS["WGS 84",     DATUM["WGS_1984",         SPHEROID["WGS 84",6378137,298.257223563,             AUTHORITY["EPSG","7030"]],         AUTHORITY["EPSG","6326"]],     PRIMEM["Greenwich",0,         AUTHORITY["EPSG","8901"]],     UNIT["degree",0.0174532925199433,         AUTHORITY["EPSG","9122"]],     AUTHORITY["EPSG","4326"]])), resolution=Resolution(x=1.0, y=1.0), projection=Projection(wkt=GEOGCS["WGS 84",     DATUM["WGS_1984",         SPHEROID["WGS 84",6378137,298.257223563,             AUTHORITY["EPSG","7030"]],         AUTHORITY["EPSG","6326"]],     PRIMEM["Greenwich",0,         AUTHORITY["EPSG","8901"]],     UNIT["degree",0.0174532925199433,         AUTHORITY["EPSG","9122"]],     AUTHORITY["EPSG","4326"]])
x2, y0: Grid(extent=Extent(xmin=0.0, xmax=90.0, ymin=0.0, ymax=90.0, projection=Projection(wkt=GEOGCS["WGS 84",     DATUM["WGS_1984",         SPHEROID["WGS 84",6378137,298.257223563,             AUTHORITY["EPSG","7030"]],         AUTHORITY["EPSG","6326"]],     PRIMEM["Greenwich",0,         AUTHORITY["EPSG","8901"]],     UNIT["degree",0.0174532925199433,         AUTHORITY["EPSG","9122"]],     AUTHORITY["EPSG","4326"]])), resolution=Resolution(x=1.0, y=1.0), projection=Projection(wkt=GEOGCS["WGS 84",     DATUM["WGS_1984",         SPHEROID["WGS 84",6378137,298.257223563,             AUTHORITY["EPSG","7030"]],         AUTHORITY["EPSG","6326"]],     PRIMEM["Greenwich",0,         AUTHORITY["EPSG","8901"]],     UNIT["degree",0.0174532925199433,         AUTHORITY["EPSG","9122"]],     AUTHORITY["EPSG","4326"]])
x3, y0: Grid(extent=Extent(xmin=90.0, xmax=180.0, ymin=0.0, ymax=90.0, projection=Projection(wkt=GEOGCS["WGS 84",     DATUM["WGS_1984",         SPHEROID["WGS 84",6378137,298.257223563,             AUTHORITY["EPSG","7030"]],         AUTHORITY["EPSG","6326"]],     PRIMEM["Greenwich",0,         AUTHORITY["EPSG","8901"]],     UNIT["degree",0.0174532925199433,         AUTHORITY["EPSG","9122"]],     AUTHORITY["EPSG","4326"]])), resolution=Resolution(x=1.0, y=1.0), projection=Projection(wkt=GEOGCS["WGS 84",     DATUM["WGS_1984",         SPHEROID["WGS 84",6378137,298.257223563,             AUTHORITY["EPSG","7030"]],         AUTHORITY["EPSG","6326"]],     PRIMEM["Greenwich",0,         AUTHORITY["EPSG","8901"]],     UNIT["degree",0.0174532925199433,         AUTHORITY["EPSG","9122"]],     AUTHORITY["EPSG","4326"]])
x0, y1: Grid(extent=Extent(xmin=-180.0, xmax=-90.0, ymin=-90.0, ymax=0.0, projection=Projection(wkt=GEOGCS["WGS 84",     DATUM["WGS_1984",         SPHEROID["WGS 84",6378137,298.257223563,             AUTHORITY["EPSG","7030"]],         AUTHORITY["EPSG","6326"]],     PRIMEM["Greenwich",0,         AUTHORITY["EPSG","8901"]],     UNIT["degree",0.0174532925199433,         AUTHORITY["EPSG","9122"]],     AUTHORITY["EPSG","4326"]])), resolution=Resolution(x=1.0, y=1.0), projection=Projection(wkt=GEOGCS["WGS 84",     DATUM["WGS_1984",         SPHEROID["WGS 84",6378137,298.257223563,             AUTHORITY["EPSG","7030"]],         AUTHORITY["EPSG","6326"]],     PRIMEM["Greenwich",0,         AUTHORITY["EPSG","8901"]],     UNIT["degree",0.0174532925199433,         AUTHORITY["EPSG","9122"]],     AUTHORITY["EPSG","4326"]])
x1, y1: Grid(extent=Extent(xmin=-90.0, xmax=0.0, ymin=-90.0, ymax=0.0, projection=Projection(wkt=GEOGCS["WGS 84",     DATUM["WGS_1984",         SPHEROID["WGS 84",6378137,298.257223563,             AUTHORITY["EPSG","7030"]],         AUTHORITY["EPSG","6326"]],     PRIMEM["Greenwich",0,         AUTHORITY["EPSG","8901"]],     UNIT["degree",0.0174532925199433,         AUTHORITY["EPSG","9122"]],     AUTHORITY["EPSG","4326"]])), resolution=Resolution(x=1.0, y=1.0), projection=Projection(wkt=GEOGCS["WGS 84",     DATUM["WGS_1984",         SPHEROID["WGS 84",6378137,298.257223563,             AUTHORITY["EPSG","7030"]],         AUTHORITY["EPSG","6326"]],     PRIMEM["Greenwich",0,         AUTHORITY["EPSG","8901"]],     UNIT["degree",0.0174532925199433,         AUTHORITY["EPSG","9122"]],     AUTHORITY["EPSG","4326"]])
x2, y1: Grid(extent=Extent(xmin=0.0, xmax=90.0, ymin=-90.0, ymax=0.0, projection=Projection(wkt=GEOGCS["WGS 84",     DATUM["WGS_1984",         SPHEROID["WGS 84",6378137,298.257223563,             AUTHORITY["EPSG","7030"]],         AUTHORITY["EPSG","6326"]],     PRIMEM["Greenwich",0,         AUTHORITY["EPSG","8901"]],     UNIT["degree",0.0174532925199433,         AUTHORITY["EPSG","9122"]],     AUTHORITY["EPSG","4326"]])), resolution=Resolution(x=1.0, y=1.0), projection=Projection(wkt=GEOGCS["WGS 84",     DATUM["WGS_1984",         SPHEROID["WGS 84",6378137,298.257223563,             AUTHORITY["EPSG","7030"]],         AUTHORITY["EPSG","6326"]],     PRIMEM["Greenwich",0,         AUTHORITY["EPSG","8901"]],     UNIT["degree",0.0174532925199433,         AUTHORITY["EPSG","9122"]],     AUTHORITY["EPSG","4326"]])
x3, y1: Grid(extent=Extent(xmin=90.0, xmax=180.0, ymin=-90.0, ymax=0.0, projection=Projection(wkt=GEOGCS["WGS 84",     DATUM["WGS_1984",         SPHEROID["WGS 84",6378137,298.257223563,             AUTHORITY["EPSG","7030"]],         AUTHORITY["EPSG","6326"]],     PRIMEM["Greenwich",0,         AUTHORITY["EPSG","8901"]],     UNIT["degree",0.0174532925199433,         AUTHORITY["EPSG","9122"]],     AUTHORITY["EPSG","4326"]])), resolution=Resolution(x=1.0, y=1.0), projection=Projection(wkt=GEOGCS["WGS 84",     DATUM["WGS_1984",         SPHEROID["WGS 84",6378137,298.257223563,             AUTHORITY["EPSG","7030"]],         AUTHORITY["EPSG","6326"]],     PRIMEM["Greenwich",0,         AUTHORITY["EPSG","8901"]],     UNIT["degree",0.0174532925199433,         AUTHORITY["EPSG","9122"]],     AUTHORITY["EPSG","4326"]])

Anchor a grid to a point

from hubflow.core import *
import enmapboxtestdata

# Get raster grid.
rasterDataset = openRasterDataset(enmapboxtestdata.enmap)
print(rasterDataset.grid())

# Reproject that grid into WGS84 with 0.005 degree resolution.
extentWgs84 = rasterDataset.grid().extent().reproject(projection=Projection.wgs84())
gridWgs84 = Grid(extent=extentWgs84, resolution=0.005)
print(gridWgs84)

# Anchor the grid to (0, 0) degrees.
# This ensures that the upper left and lower right point coordinates are multiples of the 0.005 degree resolution

gridWgs84Anchored = gridWgs84.anchor(Point(x=0, y=0, projection=Projection.wgs84()))
print(gridWgs84Anchored)

Prints:

Get grid coordinates

from hubflow.core import *

extent=Extent(xmin=-180.0, ymin=-90.0, xmax=180.0, ymax=90.0, projection=Projection.wgs84())
grid = Grid(extent=extent, resolution=10)
print(grid)
print(grid.size())

# Get x center coordinats.
print(grid.xMapCoordinates())

# Get y center coordinats.
print(grid.yMapCoordinates())

Prints:

Grid(extent=Extent(xmin=-180.0, xmax=180.0, ymin=-90.0, ymax=90.0, projection=Projection(wkt=GEOGCS["WGS 84",     DATUM["WGS_1984",         SPHEROID["WGS 84",6378137,298.257223563,             AUTHORITY["EPSG","7030"]],         AUTHORITY["EPSG","6326"]],     PRIMEM["Greenwich",0,         AUTHORITY["EPSG","8901"]],     UNIT["degree",0.0174532925199433,         AUTHORITY["EPSG","9122"]],     AUTHORITY["EPSG","4326"]])), resolution=Resolution(x=10.0, y=10.0), projection=Projection(wkt=GEOGCS["WGS 84",     DATUM["WGS_1984",         SPHEROID["WGS 84",6378137,298.257223563,             AUTHORITY["EPSG","7030"]],         AUTHORITY["EPSG","6326"]],     PRIMEM["Greenwich",0,         AUTHORITY["EPSG","8901"]],     UNIT["degree",0.0174532925199433,         AUTHORITY["EPSG","9122"]],     AUTHORITY["EPSG","4326"]])
RasterSize(x=36, y=18)
[-175.0, -165.0, -155.0, -145.0, -135.0, -125.0, -115.0, -105.0, -95.0, -85.0, -75.0, -65.0, -55.0, -45.0, -35.0, -25.0, -15.0, -5.0, 5.0, 15.0, 25.0, 35.0, 45.0, 55.0, 65.0, 75.0, 85.0, 95.0, 105.0, 115.0, 125.0, 135.0, 145.0, 155.0, 165.0, 175.0]
[85.0, 75.0, 65.0, 55.0, 45.0, 35.0, 25.0, 15.0, 5.0, -5.0, -15.0, -25.0, -35.0, -45.0, -55.0, -65.0, -75.0, -85.0]

Extent

Create extent

from hubflow.core import *

# create a extent in WGS84 for the whole world
extent=Extent(xmin=-180.0, ymin=-90.0, xmax=180.0, ymax=90.0, projection=Projection.wgs84())
print(extent)

Prints:

Extent(xmin=-180.0, xmax=180.0, ymin=-90.0, ymax=90.0, projection=Projection(wkt=GEOGCS["WGS 84",     DATUM["WGS_1984",         SPHEROID["WGS 84",6378137,298.257223563,             AUTHORITY["EPSG","7030"]],         AUTHORITY["EPSG","6326"]],     PRIMEM["Greenwich",0,         AUTHORITY["EPSG","8901"]],     UNIT["degree",0.0174532925199433,         AUTHORITY["EPSG","9122"]],     AUTHORITY["EPSG","4326"]]))

Get extent information

Warning

todo

Geometric calculation

Warning

todo

RasterDriver

Create raster driver

from hubdc.core import *

# driver by name
print(RasterDriver(name='GTiff'))

# some predined driver
print(GTiffDriver())
print(EnviDriver())
print(ErdasDriver())

# by file extension
print(RasterDriver.fromFilename('raster.vrt'))

Prints:

RasterDriver(name='GTiff')
GTiffDriver()
EnviDriver()
ErdasDriver()
VrtDriver()

Specify GeoTiff creation options

from hubdc.core import *

driver = GTiffDriver()

# GTiff default options
print(driver.options())

# options for LZW compressed GTiff
print(driver.options() + [driver.Option.COMPRESS.LZW])

# options for JPEG compressed GTiff
print(driver.options() + [driver.Option.COMPRESS.JPEG, driver.Option.JPEG_QUALITY(75)])

# options for tiled GTiff
print(driver.options() + [driver.Option.TILED.YES, driver.Option.BLOCKXSIZE(256), driver.Option.BLOCKYSIZE(256)])

Prints:

['INTERLEAVE=BAND']
['INTERLEAVE=BAND', 'COMPRESS=LZW']
['INTERLEAVE=BAND', 'COMPRESS=JPEG', 'JPEG_QUALITY=75']
['INTERLEAVE=BAND', 'TILED=YES', 'BLOCKXSIZE=256', 'BLOCKYSIZE=256']

Projection

Create projection

from hubdc.core import *

# from well known text
print(Projection(wkt='GEOGCS["WGS84", DATUM["WGS_1984", SPHEROID["WGS84",6378137,298.257223563, AUTHORITY["EPSG","7030"]], AUTHORITY["EPSG","6326"]], PRIMEM["Greenwich",0, AUTHORITY["EPSG","8901"]], UNIT["degree",0.0174532925199433, AUTHORITY["EPSG","9122"]], AUTHORITY["EPSG","4326"]]'))

# from EPSG code
print(Projection.fromEpsg(epsg=4326))

# some predefined projections
print(Projection.wgs84())
print(Projection.wgs84WebMercator())
print(Projection.utm(zone=33, north=True))
print(Projection.utm(zone=33, north=False))

Prints:

Projection(wkt=GEOGCS["WGS84", DATUM["WGS_1984", SPHEROID["WGS84",6378137,298.257223563, AUTHORITY["EPSG","7030"]], AUTHORITY["EPSG","6326"]], PRIMEM["Greenwich",0, AUTHORITY["EPSG","8901"]], UNIT["degree",0.0174532925199433, AUTHORITY["EPSG","9122"]], AUTHORITY["EPSG","4326"]])
Projection(wkt=GEOGCS["WGS 84",     DATUM["WGS_1984",         SPHEROID["WGS 84",6378137,298.257223563,             AUTHORITY["EPSG","7030"]],         AUTHORITY["EPSG","6326"]],     PRIMEM["Greenwich",0,         AUTHORITY["EPSG","8901"]],     UNIT["degree",0.0174532925199433,         AUTHORITY["EPSG","9122"]],     AUTHORITY["EPSG","4326"]])
Projection(wkt=GEOGCS["WGS 84",     DATUM["WGS_1984",         SPHEROID["WGS 84",6378137,298.257223563,             AUTHORITY["EPSG","7030"]],         AUTHORITY["EPSG","6326"]],     PRIMEM["Greenwich",0,         AUTHORITY["EPSG","8901"]],     UNIT["degree",0.0174532925199433,         AUTHORITY["EPSG","9122"]],     AUTHORITY["EPSG","4326"]])
Projection(wkt=PROJCS["WGS 84 / Pseudo-Mercator",     GEOGCS["WGS 84",         DATUM["WGS_1984",             SPHEROID["WGS 84",6378137,298.257223563,                 AUTHORITY["EPSG","7030"]],             AUTHORITY["EPSG","6326"]],         PRIMEM["Greenwich",0,             AUTHORITY["EPSG","8901"]],         UNIT["degree",0.0174532925199433,             AUTHORITY["EPSG","9122"]],         AUTHORITY["EPSG","4326"]],     PROJECTION["Mercator_1SP"],     PARAMETER["central_meridian",0],     PARAMETER["scale_factor",1],     PARAMETER["false_easting",0],     PARAMETER["false_northing",0],     UNIT["metre",1,         AUTHORITY["EPSG","9001"]],     AXIS["X",EAST],     AXIS["Y",NORTH],     EXTENSION["PROJ4","+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext +no_defs"],     AUTHORITY["EPSG","3857"]])
Projection(wkt=PROJCS["WGS 84 / UTM zone 33N",     GEOGCS["WGS 84",         DATUM["WGS_1984",             SPHEROID["WGS 84",6378137,298.257223563,                 AUTHORITY["EPSG","7030"]],             AUTHORITY["EPSG","6326"]],         PRIMEM["Greenwich",0,             AUTHORITY["EPSG","8901"]],         UNIT["degree",0.0174532925199433,             AUTHORITY["EPSG","9122"]],         AUTHORITY["EPSG","4326"]],     PROJECTION["Transverse_Mercator"],     PARAMETER["latitude_of_origin",0],     PARAMETER["central_meridian",15],     PARAMETER["scale_factor",0.9996],     PARAMETER["false_easting",500000],     PARAMETER["false_northing",0],     UNIT["metre",1,         AUTHORITY["EPSG","9001"]],     AXIS["Easting",EAST],     AXIS["Northing",NORTH],     AUTHORITY["EPSG","32633"]])
Projection(wkt=PROJCS["WGS 84 / UTM zone 33S",     GEOGCS["WGS 84",         DATUM["WGS_1984",             SPHEROID["WGS 84",6378137,298.257223563,                 AUTHORITY["EPSG","7030"]],             AUTHORITY["EPSG","6326"]],         PRIMEM["Greenwich",0,             AUTHORITY["EPSG","8901"]],         UNIT["degree",0.0174532925199433,             AUTHORITY["EPSG","9122"]],         AUTHORITY["EPSG","4326"]],     PROJECTION["Transverse_Mercator"],     PARAMETER["latitude_of_origin",0],     PARAMETER["central_meridian",15],     PARAMETER["scale_factor",0.9996],     PARAMETER["false_easting",500000],     PARAMETER["false_northing",10000000],     UNIT["metre",1,         AUTHORITY["EPSG","9001"]],     AXIS["Easting",EAST],     AXIS["Northing",NORTH],     AUTHORITY["EPSG","32733"]])

Reproject point, geometry or extent

from hubdc.core import *

utm = Projection.utm(zone=33)
wgs84 = Projection.wgs84()

point = Point(x=380000, y=5800000, projection=utm)
extent = Extent(xmin=380000, xmax=390000, ymin=5800000, ymax=5830000, projection=utm)
geometry = Geometry(wkt='POLYGON((380000 5830000,390000 5830000,390000 5800000,380000 5800000,380000 5830000))', projection=utm)

print(point.reproject(projection=wgs84))
print(extent.reproject(projection=wgs84))
print(geometry.reproject(projection=wgs84))

Prints:

from hubdc.docutils import createDocPrint

print = createDocPrint(__file__)

# START
from hubdc.core import *

utm = Projection.utm(zone=33)
wgs84 = Projection.wgs84()

point = Point(x=380000, y=5800000, projection=utm)
extent = Extent(xmin=380000, xmax=390000, ymin=5800000, ymax=5830000, projection=utm)
geometry = Geometry(wkt='POLYGON((380000 5830000,390000 5830000,390000 5800000,380000 5800000,380000 5830000))', projection=utm)

print(point.reproject(projection=wgs84))
print(extent.reproject(projection=wgs84))
print(geometry.reproject(projection=wgs84))
# END

Get projection

import enmapboxtestdata
from hubdc.core import *

rasterDataset = openRasterDataset(filename=enmapboxtestdata.enmap)
vectorDataset = openVectorDataset(filename=enmapboxtestdata.landcover_polygons)
extent = rasterDataset.extent()
point = extent.upperLeft()
geometry = extent.geometry()

# from raster dataset
projection = rasterDataset.projection()

# from vector dataset
projection = vectorDataset.projection()

# from extent
projection = extent.projection()

# from point
projection = point.projection()

# from geometry
projection = geometry.projection()

Reproject a vector dataset

import enmapboxtestdata
from hubdc.core import *

vectorDataset = openVectorDataset(filename=enmapboxtestdata.landcover_polygons)
vectorDataset.reproject(projection=Projection.wgs84(), filename='vector.gpkg', driver=GeoPackageDriver())

Reproject a raster dataset

import enmapboxtestdata
from hubdc.core import *

rasterDataset = openRasterDataset(filename=enmapboxtestdata.enmap)

# define target grid with extent of original raster, but in WGS 84 projection and 0.0001 degree resolution
grid = Grid(extent=rasterDataset.extent().reproject(Projection.wgs84()), resolution=0.0001)

# reproject raster into target grid
warped = rasterDataset.warp(grid=grid, filename='raster.bsq', driver=EnviDriver())

Export projection

from hubdc.core import *

projection = Projection.wgs84()

# to well known text
print(projection.wkt())

# to OSR spatial reference
print(type(projection.osrSpatialReference()))

Prints:

GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0,
        AUTHORITY["EPSG","8901"]],
    UNIT["degree",0.0174532925199433,
        AUTHORITY["EPSG","9122"]],
    AUTHORITY["EPSG","4326"]]
<class 'osgeo.osr.SpatialReference'>

MapViewer

The map viewer is an interactive application for exploring maps.

View single map layer

Shortcuts for initialising a map viewer directly from a raster or vector dataset.

Multi band raster
import enmapboxtestdata
from hubdc.core import *

# Starting a map viewer from a multiband raster results in a MultiBandColorRenderer representation.
# Note that enmapboxtestdata.enmap has a default style defined (see .qml file next to it).
rasterDataset = openRasterDataset(filename=enmapboxtestdata.enmap)
MapViewer().addLayer(rasterDataset.mapLayer()).show()

_images/view_single_map1.png

Layer with MultiBandColorRenderer

Single band raster
# Starting a map viewer from a raster band results in a SingleBandGrayRenderer representation.
rasterBandDataset = rasterDataset.band(index=0)
MapViewer().addLayer(rasterBandDataset.mapLayer()).show()

_images/view_single_map2.png

Layer with SingleBandGrayRenderer

Vector
# Starting a map viewer from a vector results in a SingleSymbolRenderer representation by default.
# Note that enmapboxtestdata.landcover_points uses a CategorizedSymbolRenderer as default (see .qml file next to it).
vectorDataset = openVectorDataset(filename=enmapboxtestdata.landcover_points)
#vectorDataset.mapLayer().show()
#vectorDataset.mapViewer().show()
MapViewer().addLayer(vectorDataset.mapLayer()).show()

_images/view_single_map3.png

Layer with …

Paletted raster
# Starting a map viewer from a raster band with a color lookup table results in a PalettedRasterRenderer representation.
rasterDatasetWithLookupTable = openRasterDataset(enmapboxtestdata.createClassification(gridOrResolution=10, level='level_3_id'))
MapViewer().addLayer(rasterDatasetWithLookupTable.mapLayer()).show()
_images/view_single_map4.png

Layer with PalettedRasterRenderer

Add multiple map layer

import enmapboxtestdata
from hubdc.core import *

vectorDataset = openVectorDataset(filename=enmapboxtestdata.landcover_points)
rasterDataset = openRasterDataset(filename=enmapboxtestdata.enmap)

mapViewer = MapViewer()
mapViewer.addLayer(vectorDataset.mapLayer())
mapViewer.addLayer(rasterDataset.mapLayer())
mapViewer.show()
_images/view_multi_map.png

MapViewer with multiple map layer

Set viewer extent and projection

import enmapboxtestdata
from hubdc.core import *

rasterDataset = openRasterDataset(filename=enmapboxtestdata.enmap)

mapViewer = rasterDataset.mapViewer()

# change projection
mapViewer.setProjection(projection=Projection.wgs84())
mapViewer.show()
_images/set_extent1.png
# change extent
mapViewer.setExtent(Extent(xmin=13.29, xmax=13.32, ymin=52.47, ymax=52.49, projection=Projection.wgs84()))
mapViewer.show()
_images/set_extent2.png

Rendering configuration

MultiBandColorRenderer
import enmapboxtestdata
from hubdc.core import *

# setup map viewer
rasterDataset = openRasterDataset(filename=enmapboxtestdata.enmap)
mapViewer = MapViewer()
mapLayer = rasterDataset.mapLayer()
mapViewer.addLayer(mapLayer)

# setup layer rendering

# - default is to apply a 2% - 98% stretch on the selected bands
mapLayer.initMultiBandColorRenderer(redIndex=63, greenIndex=37, blueIndex=23)

# - but it's also possible to set min-max stretch values explicitely
mapLayer.initMultiBandColorRenderer(redIndex=63, greenIndex=37, blueIndex=23,
                                    redMin=809, redMax=2861,
                                    greenMin=183, greenMax=1370,
                                    blueMin=279, blueMax=1275)

mapViewer.show()
_images/rendering_MultiBandColorRenderer.png
SingleBandGrayRenderer
import enmapboxtestdata
from hubdc.core import *
from qgis.core import QgsSingleBandGrayRenderer, QgsContrastEnhancement

# setup map viewer
rasterDataset = openRasterDataset(filename=enmapboxtestdata.enmap)
rasterBandDataset = rasterDataset.band(index=0)
mapViewer = MapViewer()
mapLayer = rasterDataset.band(index=0).mapLayer()
mapViewer.addLayer(mapLayer)

# setup layer rendering

# - default is to apply a 2% - 98% stretch on the selected band
mapLayer.initSingleBandGrayRenderer(grayIndex=0)

# - but it's also possible to set min-max stretch values explicitely
mapLayer.initSingleBandGrayRenderer(grayIndex=0, grayMin=179, grayMax=1026)

mapViewer.show()
_images/rendering_QgsSingleBandGrayRenderer.png
PalettedRasterRenderer

Warning

todo

SingleSymbolRenderer

Warning

todo

CategorizedSymbolRenderer

Warning

todo

Save map viewer content to PNG file

import enmapboxtestdata
from hubdc.core import *

vectorDataset = openVectorDataset(filename=enmapboxtestdata.landcover_points)
rasterDataset = openRasterDataset(filename=enmapboxtestdata.enmap)

mapViewer = MapViewer()
mapViewer.addLayer(vectorDataset.mapLayer())
mapViewer.addLayer(rasterDataset.mapLayer())
mapViewer.save('image.png')

Applier Examples

To honor the similarities of HUBDC with RIOS, we replicate the examples known from RIOS, and add some more HUBDC specific examples.

Simple Example (explained in detail)

Use the Red and NIR bands of the Landsat LT51940232010189KIS01 scene to calculate the Normalized Difference Vegetation Index (NDVI). Additionally, use the BrandenburgDistricts vector polygon layer to mask the result.

_images/ndvi.png
"""
Calculate the Normalized Difference Vegetation Index (NDVI) for a Landsat 5 scene.
Mask the resulting image to the shape of Brandenburg (a federated state of Germany).
"""

import tempfile
import os
import numpy
from hubdc.applier import *
from hubdc.testdata import LT51940232010189KIS01, BrandenburgDistricts

# Set up input and output filenames.
applier = Applier()
applier.inputRaster.setRaster(key='red', value=ApplierInputRaster(filename=LT51940232010189KIS01.red))
applier.inputRaster.setRaster(key='nir', value=ApplierInputRaster(filename=LT51940232010189KIS01.nir))
applier.inputVector.setVector(key='brandenburg', value=ApplierInputVector(filename=BrandenburgDistricts.shp))
applier.outputRaster.setRaster(key='ndvi', value=ApplierOutputRaster(filename=os.path.join(tempfile.gettempdir(), 'ndvi.img')))

# Set up the operator to be applied
class NDVIOperator(ApplierOperator):
    def ufunc(operator):

        # read image data
        red = operator.inputRaster.raster(key='red').array()
        nir = operator.inputRaster.raster(key='nir').array()
        brandenburg = operator.inputVector.vector(key='brandenburg').array(initValue=0, burnValue=1)

        # calculate ndvi, clip to 0-1 and mask Brandenburg
        ndvi = numpy.float32(nir-red)/(nir+red)
        np.clip(ndvi, 0, 1, out=ndvi)
        ndvi[brandenburg==0] = 0

        # write ndvi data
        operator.outputRaster.raster(key='ndvi').setArray(array=ndvi)
        operator.outputRaster.raster(key='ndvi').setNoDataValue(0)

# Apply the operator to the inputs, creating the outputs.
applier.apply(operatorType=NDVIOperator)
print(applier.outputRaster.raster(key='ndvi').filename())

# Python prints something like:
# >>> c:\users\USER\appdata\local\temp\ndvi.img

The result is stored in the file called ndvi.img stored in the user tempdir.

HUB-Datacube Applier programs are usually structured in the following way:

  1. Initialize the Applier

    applier = Applier()
    
  2. Assigning raster inputs

    applier.inputRaster.setRaster(key='image1', value=ApplierInputRaster(filename=LT51940232010189KIS01.swir1))
    applier.inputRaster.setRaster(key='image2', value=ApplierInputRaster(filename=LT51940232010189KIS01.swir2))
    

    vector inputs

    applier.inputVector.setVector(key='brandenburg', value=ApplierInputVector(filename=BrandenburgDistricts.shp))
    

    … and raster outputs

    applier.outputRaster.setRaster(key='outimage', value=ApplierOutputRaster(filename=os.path.join(tempfile.gettempdir(), 'outimage.img')))
    

    Note that all input and output datasets are set using different members of the applier object:

    applier.inputRaster is an ApplierInputRasterGroup object, which is a container for ApplierInputRaster objects

    applier.inputVector is an ApplierInputVectorGroup object, which is a container for ApplierInputVector objects

    applier.outputRaster is an ApplierOutputRasterGroup object, which is a container for ApplierOutputRaster objects

  3. Implement an operator class derived from ApplierOperator and overwriting the ufunc method

    class NDVIOperator(ApplierOperator):
        def ufunc(operator):
            ...
    

    The ufunc method is usually structured into the sections:

    1. read data into numpy arrays

      # read image data
      red = operator.inputRaster.raster(key='red').array()
      nir = operator.inputRaster.raster(key='nir').array()
      brandenburg = operator.inputVector.vector(key='brandenburg').array(initValue=0, burnValue=1, dtype=numpy.uint8)
      

      Note that all input datasets are access using different members of the operator object:

      operator.inputRaster is identical to applier.inputRaster and used to access previously defined ApplierInputRaster objects, which can be used to read raster data, see array()

      operator.inputVector is identical to applier.inputVector and is used to access previously defined ApplierInputVector objects, which can be used to read and rasterize vector data, see array()

      Also note that input data is presented as numpy arrays, of the datatype corresponding to that in the raster files. It is the responsibility of the user to manage all conversions of datatypes.

      All blocks of data are 3-d numpy arrays. The first dimension corresponds to the number of layers in the image file, and will be present even when there is only one layer. The second and third dimensions represent the spatial extent (ysize, xsize) of the image block.

    2. data processing

      # calculate ndvi and mask Brandenburg
      ndvi = numpy.float32(nir-red)/(nir+red)
      ...
      
    3. write output data (and metadata - not shown here)

      # write ndvi data
      operator.outputRaster.raster(key='ndvi').setImageArray(array=ndvi)
      ...
      

      Note that output raster datasets are access using the operator.outputRaster, which is identical to applier.outputRaster and used to access previously defined ApplierOutputRaster objects, which can be used to write output numpy arrays, see setArray().

      The datatype of the output files will be inferred from the datatype of the given numpy arrays. So, to control the datatype of the output file, use for example the numpy.astype function to control the datatype of the output arrays.

Manage Metadata Example

You can read metadata from input and write metadata to output datasets

This simple example reads the wavelength information from the ENVI metadata domain of an input dataset and passes it to an output dataset:

def ufunc(operator):

    # copy raster data
    array = operator.inputRaster.raster(key='image').imageArray()
    operator.outputRaster.raster(key='outimage').setImageArray(array=array)

    # copy ENVI/wavelength metadata
    wavelength = operator.inputRaster.raster(key='image').metadataItem(key='wavelength', domain='ENVI')
    operator.outputRaster.raster(key='outimage').setMetadataItem(key='wavelength', value=wavelength, domain='ENVI')

See metadataItem() and setMetadataItem() for more details.

For more information on the GDAL Data and Metadata Model see the GDAL documentation.

For more information on the ENVI Metadata Model see The ENVI Header Format

Passing Other Data Example

Use additional arguments for passing other data into the operator user function, apart from the raster data itself. This is obviously useful for passing parameters into the processing.

Use the return statement to pass information out again.

A simple example, using it to pass in a single parameter, might be a program to multiply an input raster by a scale value and add an offset:

class ScaleOperator(ApplierOperator):
    def ufunc(operator, scale, offset):
        array = operator.inputRaster.raster(key='image').imageArray()
        scaled = array * scale + offset
        operator.outputRaster.raster(key='outimage').setImageArray(array=scaled)

applier.apply(operatorType=Scaleperator, scale=1, offset=0)

An example of using the return statement to accumulate information across blocks might be a program to calculate some statistic (e.g. the mean) across the whole raster:

class MeanOperator(ApplierOperator):
    def ufunc(operator):
        array = operator.inputRaster.raster(key='image').imageArray()
        blockTotal = img.sum()
        blockCount = img.size
        return blockTotal, blockCount

results = applier.apply(operatorType=MeanOperator)

total, count = 0., 0
for blockTotal, blockCount in results:
    total += blockTotal
    count += blockCount

print('Average value = ', total / count)

The total and count values are calculated from the list of blockTotal and blockCount values returned by the apply() method.

The values could be accumulated between blocks, as looping sequentially over all blocks in the image, but this approach would fail if the applier is used with multiprocessing enabled.

Of course, there already exist superior ways of calculating the mean value of an image, but the point about using the applier to do something like this would be that: a) opening the input rasters is taken care of; and b) it takes up very little memory, as only small blocks are in memory at one time. The same mechanism can be used to do more specialized calculations across the images.

Note that there are no output rasters from the last example - this is perfectly valid.

Controlling the Reference Pixel Grid Example

Use setProjection(), setResolution(), and setExtent() to explicitely control the grid projection, resolution and extent:

applier.controls.setProjection(projection=Projection('PROJCS["UTM_Zone_33N",GEOGCS["GCS_WGS_1984",DATUM["WGS_1984",SPHEROID["WGS_84",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",15.0],PARAMETER["Scale_Factor",0.9996],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1]]'))
applier.controls.setExtent(extent=Extent(xmin=4400000, xmax=450000, ymin=3100000, ymax=3200000)
applier.controls.setResolution(resolution=Resolution(x=30, y=30)

Other Controls

Other controls which can be manipulated are detailed in the ApplierControls class.

Arbitrary Numbers of Input (and Output) Files Example

As mentioned before, the applier members applier.inputRaster, applier.inputVector and applier.outputRaster are container objects of type ApplierInputRasterGroup, ApplierInputVectorGroup and ApplierOutputRasterGroup respectively.

These containers are used to store ApplierInputRaster, ApplierInputVector and ApplierOutputRaster objects respectively.

Furthermore, a container can store other containers of the same type, which enables the creation of more complex, nested dataset structures. This makes it possible to represent naming structures comparable to those on the users file system.

An example: given a small Landsat archive of 8 scenes in 4 footprints stored on the file system structured by path/row/scene. Let assume, we are only interested in the CFMask datasets:

C:\landsat\
    194\
        023\
            LC81940232015235LGN00\
                LC81940232015235LGN00_cfmask.img
                 ...
            LE71940232015275NSG00\
                LE71940232015275NSG00_cfmask.img
                ...
            LT41940231990126XXX05\
                LT41940231990126XXX05_cfmask.img
                ...
            LT51940232010189KIS01\
                LT51940232010189KIS01_cfmask.img
                ...
    194\
        024\
            LC81940242015235LGN00\
                LC81940242015235LGN00_cfmask.img
                ...
            LE71940242015275NSG00\
                LE71940242015275NSG00_cfmask.img
                ...
            LT41940241990126XXX03\
                LT41940241990126XXX03_cfmask.img
                ...
            LT51940242010189KIS01\
                LT51940242010189KIS01_cfmask.img
                ...

The CFMask datasets can be inserted manually (preserving the file structure) as follows:

landsat = applier.inputRaster.setGroup('landsat', value=ApplierInputRasterGroup())
path194 = landsat.setGroup('194', value=ApplierInputRasterGroup())
row023 = path194.setGroup(key='023', value=ApplierInputRasterGroup())
row024 = path194.setGroup(key='024', value=ApplierInputRasterGroup())

row023.setRaster(key='LC81940232015235LGN00_cfmask', value=ApplierInputRaster(filename=r'C:\landsat\194\023\LC81940232015235LGN00\LC81940232015235LGN00_cfmask.img'))
...
row024.setRaster(key='LT51940242010189KIS01_cfmask', value=ApplierInputRaster(filename=r'C:\landsat\194\023\LT51940242010189KIS01\LT51940242010189KIS01_cfmask.img'))

The same result can be achieved using the fromFolder() auxilliary method, which takes a folder and searches recursively for all raster matching the given extensions and passes a (optional) ufunc filter function:

ufunc = lambda root, basename, extension: basename.endswith('cfmask'))
applier.inputRaster.setGroup(key='landsat', value=ApplierInputRasterGroup.fromFolder(folder=r'C:\Work\data\gms\landsat',
                                                                                     extensions=['.img'],
                                                                                     ufunc=ufunc)

Inside the operator ufunc, individual datasets can then be accessed as follows:

def ufunc(operator):
    # access individual dataset
    cfmask = operator.inputRaster.group(key='landsat').group(key='194').group(key='023').group(key='LC81940232015235LGN00').raster(key='LC81940232015235LGN00_cfmask')
    array = cfmask.imageArray()

Or as a shortcut to this it is possible to also use key concatenation like so:

cfmask = operator.inputRaster.raster(key='landsat/194/023/LC81940232015235LGN00/LC81940232015235LGN00_cfmask')

To visit all datasets, the structure can be iterated in accordance to how it was created, from landsat, over pathes, over rows, over scenes, to the cfmask rasters:

def ufunc(operator):
    # iterate over all datasets
    landsat = operator.inputRaster.group(key='landsat')
    for path in landsat.groups():
        for row in path.groups():
            for scene in row.groups():
                cfmask = scene.findRaster(ufunc=lambda key, raster: key.endswith('cfmask'))
                array = cfmask.imageArray()

The rasters can also be flat iterated, ignoring the group structure completely:

def ufunc(operator):
    # flat iterate over all datasets
    for cfmask in operator.inputRaster.flatRasters():
        array = cfmask.imageArray()

Filters and Overlap Example

Because the applier operates on a per block basis, care must be taken to set the overlap correctly when working with filters. The overlap keyword must be consistently set when using input raster reading methods ( imageArray(), bandArray(), fractionArray()), input vector reading methods ( imageArray() fractionArray()), and output raster writing method ( setImageArray()).

Here is a simple convolution filter example:

import tempfile
import os
from scipy.ndimage import uniform_filter

from hubdc.applier import *
from hubdc.testdata import LT51940232010189KIS01

applier = Applier()
applier.inputRaster.setRaster(key='image', value=ApplierInputRaster(filename=LT51940232010189KIS01.band3))
applier.outputRaster.setRaster(key='outimage', value=ApplierOutputRaster(filename=os.path.join(tempfile.gettempdir(), 'smoothed.img')))

class SmoothOperator(ApplierOperator):
    def ufunc(operator):

        # does a spatial 11x11 uniform filter.
        # Note: for a 3x3 the overlap is 1, 5x5 overlap is 2, ..., 11x11 overlap is 5, etc
        overlap = 5
        array = operator.inputRaster.raster(key='image').imageArray(overlap=overlap)
        arraySmoothed = uniform_filter(array, size=11, mode='constant')
        operator.outputRaster.raster(key='outimage').setImageArray(array=arraySmoothed, overlap=overlap)

applier.apply(operatorType=SmoothOperator)

Many other Scipy filters are also available and can be used in a similar way.

Categorical Raster Inputs Example

On-the-fly resampling and reprojection of input rasters into the reference pixel grid is one key feature of the applier. However, for categorical raster inputs, this default behaviour can be insufficient in terms of information content preservation, even if the resampling algorithm is carefully choosen.

For example, if the goal is to process a categorical raster, where different categories are coded with unique ids, standard resampling algorithms will not be able to preserve the information content.

Sometimes it is sufficient to use the gdal.GRA_Mode algorithms, but in general it is not. To resample a categorical raster into a target pixel grid with a different resolution usually implies that the categorical information must be aggregated into pixel fraction, one for each category.

In the following example a Landsat CFMask image at 30 m is resampled into 250 m, resulting in a category fractions. The categories are: 0 is clear land, 1 is clear water, 2 is cloud shadow, 3 is ice or snow, 4 is cloud and 255 is the background. Use fractionArray() to achieve this:

cfmaskFractions250m = self.inputRaster.raster('cfmask30m').fractionArray(categories=[0, 1, 2, 3, 4, 255])

Categories at 250 m can then be calculated from the aggregated fractions:

cfmask250m = numpy.array([0, 1, 2, 4, 255])[cfmaskFractions250m.argmax(axis=0)]

Vector Inputs Example

Vector layers can be included into the processing:

applier = Applier()
applier.inputVector.setVector(key='vector', value=ApplierInputVector(filename=myShapefile))

Like any input raster file, vector layers can be accessed via the operator object inside the user function:

def ufunc(operator):
    vector = operator.inputVector.vector(key='vector')

Use imageArray() to get a rasterized version of the vector layer. The rasterization is a binary mask by default, that is initialized with 0 and all pixels covered by features are filled (burned) with a value of 1:

array = vector.imageArray()

This behaviour can be altered using the initValue and burnValue keywords:

array = vector.imageArray(initValue=0, burnValue=1)

Instead of a constant burn value, a burn attribute can be set by using the burnAttribute keyword:

array = vector.imageArray(burnAttribute='ID')

Use the filterSQL keyword to set an attribute query string in form of a SQL WHERE clause. Only features for which the query evaluates as true will be returned:

sqlWhere = "Name = 'Vegetation'"
array = vector.imageArray(filterSQL=sqlWhere)

Categorical Vector Inputs Example

In some situations it may be insufficient to simply burn a value or attribute value (i.e. categories) onto the target reference pixel grid. Depending on the detailedness of the vector shapes (i.e. scale of digitization), a simple burn or not burn decision may greatly degrade the information content if the target resolution (i.e. scale of rasterization) is much coarser.

In this case it would be desirable to rasterize the categories at the scale of digitization and afterwards aggregate this categorical information into pixel fraction, one for each category.

Take for example a vector layer with an attribute CLASS_ID coding features as 1 -> Impervious, 2 -> Vegetation, 3 -> Soil and 4 -> Other. To derieve aggregated pixel fractions for Impervious, Vegetation and Soil categories rasterization at 5 m resolution use fractionArray():

def ufunc(operator):
    vector = operator.inputVector.vector(key='vector')
    fractions = self.fractionArray('vector', categories=[1, 2, 3], categoryAttribute='CLASS_ID',
                                   resolution=Resolution(x=5, y=5).

Instaed of explicitly specifying the rasterization resolution, use the oversampling keyword to specify the factor by witch the target resolution should be oversampled. Note that if nothing is specified, an oversampling factor of 10 is used. So for example, if the target resolution is 30 m and rasterization should take place at 5 m resolution, use an oversampling factor of 6 (i.e. 30 m / 5 m = 6):

fractions = self.fractionArray('vector', categories=[1, 2, 3], categoryAttribute='CLASS_ID',
                               oversampling=6)

Categories at 30 m can then be calculated from the aggregated fractions:

categories = numpy.array([1, 2, 3])[fractions.argmax(axis=0)]

Parallel Processing Example

Each block can be processed on a seperate CPU using Python’s multiprocessing module. Making use of this facility is very easy and is as simple as setting some more options on the applier.controls object , see setNumThreads(). Note, that under Windows you need to use the if __name__ == '__main__': statement:

def ufunc(operator):
    ...

if __name__ == '__main__':

    applier = Applier()
    # ...
    applier.controls.setNumThreads(5)
    applier.apply(ufunc)

Parallel Writing Example

It is possible to have multiple writer processes. Using multiple writers (in case of multiple outputs) makes sense, because writing outputs is not only limitted by the hard drive, but also by data compression and other CPU intense overhead. Making use of this facility is also very easy and is as simple as setting some more options on the applier.controls object , see setNumWriter():

applier.controls.setNumWriter(5)

Setting GDAL Options Example

Via the applier.controls object you can set various GDAL config options (e.g. setGDALCacheMax()) to handle the trade of between processing times and memory consumption:

applier = Applier()
applier.controls.setGDALCacheMax(bytes=1000*2**20)
applier.controls.setGDALSwathSize(bytes=1000*2**20)
applier.controls.setGDALDisableReadDirOnOpen(disable=True)
applier.controls.setGDALMaxDatasetPoolSize(nfiles=1000)

hubdc.core

class hubdc.core.Column(x, z)[source]

Bases: object

Class for managing image column location.

__init__(x, z)[source]
Parameters:
  • x (int) –
  • z (int) –
x()[source]

Returns column x coordinate.

z()[source]

Returns column z coordinate.

class hubdc.core.ENVI[source]

Bases: object

static findHeader(filenameBinary, ext='.hdr')[source]
classmethod gdalType(enviType)[source]
static numpyType(enviType)[source]
static readHeader(filenameHeader)[source]
static typeSize(enviType)[source]
classmethod writeAttributeTable(filename, table)[source]

Write attribute table values to csv file next to the given binary file.

static writeHeader(filenameHeader, metadata)[source]
SPATIAL_KEYS = ['lines', 'samples', 'map info', 'projection info', 'coordinate system string']
class hubdc.core.ESRIShapefileDriver[source]

Bases: hubdc.core.VectorDriver

ESRI Shapefile driver.

__init__()[source]
Parameters:name (str) – e.g. ‘ESRI Shapefile’ or ‘GPKG’
class hubdc.core.EnviDriver[source]

Bases: hubdc.core.RasterDriver

ENVI driver.

class Option[source]

Bases: object

class INTERLEAVE[source]

Bases: object

BIL = 'INTERLEAVE=BIL'
BIP = 'INTERLEAVE=BIP'
BSQ = 'INTERLEAVE=BSQ'
class SUFFIX[source]

Bases: object

ADD = 'SUFFIX=ADD'
REPLACE = 'SUFFIX=REPLACE'
__init__()[source]

Create instance from GDAL driver name and (optional) a list of default creation options.

class hubdc.core.ErdasDriver[source]

Bases: hubdc.core.RasterDriver

Erdas Imagine driver.

__init__()[source]

Create instance from GDAL driver name and (optional) a list of default creation options.

class hubdc.core.Extent(xmin, xmax, ymin, ymax, projection)[source]

Bases: object

Class for managing extents (i.e. bounding boxes).

__init__(xmin, xmax, ymin, ymax, projection)[source]
Parameters:
  • xmin (number) –
  • xmax (number) –
  • ymin (number) –
  • ymax (number) –
  • projection (hubdc.core.Projection) –
centroid()[source]

Returns the centroid.

equal(other, tol=1e-05)[source]

Returns wether self is equal to other.

static fromGeometry(geometry)[source]

Create an extent from the bounding box a Geometry.

geometry()[source]

Returns self as a Geometry.

intersection(other)[source]

Returns a new instance which is the intersection of self and other in the projection of self.

intersects(other)[source]

Returns whether self and other intersects.

lowerLeft()[source]

Returns the lower left corner.

lowerRight()[source]

Returns the lower right corner.

projection()[source]

Returns the Projection.

reproject(projection)[source]

Reproject self into the given projection.

union(other)[source]

Returns a new instance which is the union of self with other in the projection of self.

upperLeft()[source]

Returns the upper left corner.

upperRight()[source]

Returns the upper right corner.

xmax()[source]

Returns the xmax.

xmin()[source]

Returns the xmin.

ymax()[source]

Returns the ymax.

ymin()[source]

Returns the ymin.

class hubdc.core.Feature(ogrFeature, projection=None)[source]

Bases: object

__init__(ogrFeature, projection=None)[source]

Initialize self. See help(type(self)) for accurate signature.

geometry()[source]
ogrFeature()[source]
projection()[source]
value(attribute)[source]
class hubdc.core.GTiffDriver[source]

Bases: hubdc.core.RasterDriver

GTiff driver.

class Option[source]

Bases: object

class ALPHA[source]

Bases: object

NON_PREMULTIPLIED = 'ALPHA=NON-PREMULTIPLIED'
PREMULTIPLIED = 'ALPHA=PREMULTIPLIED'
UNSPECIFIED = 'ALPHA=UNSPECIFIED'
YES = 'ALPHA=YES'
class BIGTIFF[source]

Bases: object

IF_NEEDED = 'BIGTIFF=IF_NEEDED'
IF_SAFER = 'BIGTIFF=IF_SAFER'
NO = 'BIGTIFF=NO'
YES = 'BIGTIFF=YES'
class COMPRESS[source]

Bases: object

CCITTFAX3 = 'COMPRESS=CCITTFAX3'
CCITTFAX4 = 'COMPRESS=CCITTFAX4'
CCITTRLE = 'COMPRESS=CCITTRLE'
DEFLATE = 'COMPRESS=PACKBITS'
JPEG = 'COMPRESS=JPEG'
LERC = 'COMPRESS=LERC'
LERC_DEFLATE = 'COMPRESS=LERC_DEFLATE'
LERC_ZSTD = 'COMPRESS=LERC_ZSTD'
LZMA = 'COMPRESS=LZMA'
LZW = 'COMPRESS=LZW'
NONE = 'COMPRESS=NONE'
PACKBITS = 'COMPRESS=JPEG'
WEBP = 'COMPRESS=WEBP'
ZSTD = 'COMPRESS=ZSTD'
class COPY_SRC_OVERVIEWS[source]

Bases: object

NO = 'COPY_SRC_OVERVIEWS=NO'
YES = 'COPY_SRC_OVERVIEWS=YES'
class GEOTIFF_KEYS_FLAVOR[source]

Bases: object

ESRI_PE = 'GEOTIFF_KEYS_FLAVOR=ESRI_PE'
STANDARD = 'GEOTIFF_KEYS_FLAVOR=STANDARD'
class INTERLEAVE[source]

Bases: object

BAND = 'INTERLEAVE=BAND'
PIXEL = 'INTERLEAVE=PIXEL'
class PHOTOMETRIC[source]

Bases: object

CIELAB = 'PHOTOMETRIC=CIELAB'
CMYK = 'PHOTOMETRIC=CMYK'
ICCLAB = 'PHOTOMETRIC=ICCLAB'
ITULAB = 'PHOTOMETRIC=ITULAB'
MINISBLACK = 'PHOTOMETRIC=MINISBLACK'
MINISWHITE = 'PHOTOMETRIC=MINISWHITE'
RGB = 'PHOTOMETRIC=RGB'
YCBCR = 'PHOTOMETRIC=YCBCR'
class PIXELTYPE[source]

Bases: object

DEFAULT = 'PIXELTYPE=DEFAULT'
SIGNEDBYTE = 'PIXELTYPE=SIGNEDBYTE'
class PREDICTOR[source]

Bases: object

FloatingPoint = 'PREDICTOR=3'
HorizontalDifferencing = 'PREDICTOR=2'
NONE = 'PREDICTOR=1'
class PROFILE[source]

Bases: object

BASELINE = 'PROFILE=BASELINE'
GDALGeoTIFF = 'PROFILE=GDALGeoTIFF'
GeoTIFF = 'PROFILE=GeoTIFF'
class SPARSE_OK[source]

Bases: object

FALSE = 'SPARSE_OK=FALSE'
TRUE = 'SPARSE_OK=TRUE'
class TILED[source]

Bases: object

NO = 'TILED=NO'
YES = 'TILED=YES'
class WEBP_LOSSLESS[source]

Bases: object

FALSE = 'WEBP_LOSSLESS=FALSE'
TRUE = 'WEBP_LOSSLESS=TRUE'
static BLOCKXSIZE(n=256)[source]
static BLOCKYSIZE(n=256)[source]
static JPEG_QUALITY(n=75)[source]
static MAX_Z_ERROR(threshold=0)[source]
static NBITS(n)[source]
static NUM_THREADS(n='ALL_CPUS')[source]
static WEBP_LEVEL(n=75)[source]
static ZLEVEL(n=6)[source]
static ZSTD_LEVEL(n=9)[source]
__init__()[source]

Create instance from GDAL driver name and (optional) a list of default creation options.

class hubdc.core.GeoPackageDriver[source]

Bases: hubdc.core.VectorDriver

ESRI Shapefile driver.

__init__()[source]
Parameters:name (str) – e.g. ‘ESRI Shapefile’ or ‘GPKG’
class hubdc.core.Geometry(wkt, projection)[source]

Bases: object

Class for managing geometries.

__init__(wkt, projection)[source]

Create by given well known text string and Projection.

intersection(other)[source]

Returns the intersection of self and other.

intersects(other)[source]

Returns whether self and other intersect.

ogrGeometry()[source]

Returns ogr.Geometry object.

projection()[source]

Returns the Projection.

reproject(projection)[source]

Reproject self into given projection.

union(other)[source]

Returns the union of self and other.

within(other)[source]

Returns whether self is within other.

wkt()[source]

Returns well known text string.

class hubdc.core.Grid(extent, resolution)[source]

Bases: object

Class for managing raster grids in terms of extent, resolution and projection.

__init__(extent, resolution)[source]
Parameters:
anchor(point)[source]

Returns a new instance that is anchored to the given point. Anchoring will result in a subpixel shift. See the source code for implementation details.

atResolution(resolution)[source]

Return grid with same extent and projection, but new resolution.

clip(extent)[source]

Return self clipped by given extent.

equal(other, tol=1e-05)[source]

Returns whether self is equal to other.

extent()[source]

Returns the Extent.

geoTransform()[source]

Returns a GDAL georeferencing transform tuple (xmin, xres, 0, ymax, 0, -yres) from bounds and resolution, without any rotation or shearing.

pixelBuffer(buffer, left=True, right=True, up=True, down=True)[source]

Returns a new instance with a pixel buffer applied in different directions.

Parameters:
  • buffer (int) – number of pixels to be buffered (can also be negativ)
  • left (bool) – whether to buffer to the left/west
  • right (bool) – whether to buffer to the right/east
  • up (bool) – whether to buffer upwards/north
  • down (bool) – whether to buffer downwards/south
Returns:

Return type:

hubdc.core.Grid

projection()[source]

Returns the Projection.

reproject_OLD(other)[source]

Returns a new instance with: a) extent reprojected into the projection of other, b) resolution of other, and c) anchored to other.

resolution()[source]

Returns the Resolution.

shape()[source]

Returns size as (ysize, xsize) tuple.

size()[source]

Returns the Size.

subgrids(size)[source]

Returns the decomposition of self into subgrids of given size. Subgrids at the border are trimmed to the extent of self.

subset(offset, size, trim=False)[source]

Returns a new instance that is a subset given by an offset location and a raster size. Optionally set trim=True to restrain the grid extent to the extent of self.

xMapCoordinates()[source]

Returns the list of map coordinates in x dimension.

xMapCoordinatesArray()[source]

Returns the 2d array of map x coordinates.

xPixelCoordinates(offset=0)[source]

Returns the list of pixel coordinates in x dimension with optional offset.

xPixelCoordinatesArray(offset=0)[source]

Returns the 2d array of pixel x coordinates with optional offset.

yMapCoordinates()[source]

Returns the list of map coordinates in y dimension.

yMapCoordinatesArray()[source]

Returns the 2d array of map y coordinates.

yPixelCoordinates(offset=0)[source]

Returns the list of pixel coordinates in y dimension with optional offset.

yPixelCoordinatesArray(offset=0)[source]

Returns the 2d array of pixel y coordinates with optional offset.

class hubdc.core.MapViewer[source]

Bases: object

__init__()[source]

Initialize self. See help(type(self)) for accurate signature.

addLayer(layer)[source]
extent()[source]
onKeyPressed(e)[source]
projection()[source]
resize(xsize=None, ysize=None)[source]
save(filename)[source]
setExtent(extent)[source]
setProjection(projection)[source]
show(size=None)[source]
class hubdc.core.MemDriver[source]

Bases: hubdc.core.RasterDriver

MEM driver.

__init__()[source]

Create instance from GDAL driver name and (optional) a list of default creation options.

class hubdc.core.MemoryDriver[source]

Bases: hubdc.core.VectorDriver

Memory driver.

__init__()[source]
Parameters:name (str) – e.g. ‘ESRI Shapefile’ or ‘GPKG’
class hubdc.core.MetadataFormatter[source]

Bases: object

Class for managing GDAL metadata value formatting.

classmethod stringToValue(string, dtype)[source]

Returns a representation of string as value of given dtype. If string represents a list of values in curly brackets (e.g. {1, 2, 3}), a list of values is returned.

classmethod valueToString(value)[source]

Returns a string representation of value.

class hubdc.core.Pixel(x, y)[source]

Bases: object

Class for managing image pixel location.

__init__(x, y)[source]
Parameters:
  • x (int) –
  • y (int) –
static parse(obj)[source]

Create instance from given Pixel or (number, number) object.

x()[source]

Returns pixel x coordinate.

y()[source]

Returns pixel y coordinate.

class hubdc.core.Point(x, y, projection)[source]

Bases: hubdc.core.Geometry

Class for managing map locations.

__init__(x, y, projection)[source]

Create point geometry by x and y coordinates and projection.

projection()[source]

Returns the Projection.

reproject(projection)[source]

Reproject self into given projection.

x()[source]

Returns map x coordinate.

y()[source]

Returns map y coordinate.

class hubdc.core.Projection(wkt)[source]

Bases: object

Class for managing projections.

__init__(wkt)[source]

Create by given well known text string.

equal(other)[source]

Returns whether self is equal to other.

static fromEpsg(epsg)[source]

Create by given epsg authority ID.

osrSpatialReference()[source]

Returns osr.SpatialReference object.

classmethod utm(zone, north=True)[source]

Create UTM projection of given zone.

static wgs84()[source]

Create WGS84 projection (epsg=4326), also see http://spatialreference.org/ref/epsg/wgs-84/

static wgs84WebMercator()[source]

Create WGS84 Web Mercator projection (epsg=3857), also see http://spatialreference.org/ref/sr-org/7483/

wkt()[source]

Returns the well known text string.

class hubdc.core.PseudoGrid(size)[source]

Bases: hubdc.core.Grid

__init__(size)[source]
Parameters:
static fromArray(array)[source]
class hubdc.core.RasterBandDataset(raster, index)[source]

Bases: object

Class for managing raster band datasets.

__init__(raster, index)[source]

Creating a new instance given a Raster and a raster band index.

array(grid=None, resampleAlg=<Mock name='mock.gdal.GRA_NearestNeighbour' id='139630847665320'>, noDataValue=None, errorThreshold=0.0, warpMemoryLimit=104857600, multithread=False)[source]

Returns raster band data as 2d array of shape = (ysize, xsize) for the given grid, where zsize is the number of raster bands, and ysize, xsize = grid.shape().

Parameters:
  • grid (hubdc.core.Grid) – if not specified self.grid() is used
  • resampleAlg (int) – one of the GDAL resampling algorithms gdal.GRA_*
  • noDataValue (float) – if not specified, no data value of self is used
  • errorThreshold (float) – error threshold for approximation transformer (in pixels)
  • warpMemoryLimit (int) – size of working buffer in bytes
  • multithread (bool) – whether to multithread computation and I/O operations
Returns:

Return type:

numpy.ndarray

categoryColors()[source]

Returns band category colors as list of rgba tuples.

categoryNames()[source]

Returns band category names.

copyMetadata(other)[source]

Copy raster and raster band metadata from self to other

description()[source]

Returns band description.

fill(value)[source]

Write constant value to the whole raster band.

flushCache()[source]

Flush the cache.

gdalBand()[source]

Return the gdal.Band.

index()[source]

Returns the raster band index.

mapLayer()[source]
mapViewer()[source]
metadataDict()[source]

Return the metadata dictionary for all domains.

metadataDomain(domain='')[source]

Return the metadata dictionary for the given domain.

metadataDomainList()[source]

Returns the list of metadata domain names.

metadataItem(key, domain='', default=None, required=False, dtype=<class 'str'>)[source]

Return the metadata item.

noDataValue(default=None, required=False)[source]

Returns band no data value. Returns default if no data value is undefined, or raises an error if required.

raster()[source]

Returns the RasterDataset.

readAsArray(grid=None, resample_alg=<Mock name='mock.gdal.GRA_NearestNeighbour' id='139630847665320'>)[source]

Returns raster band data as 2d array.

Parameters:
  • grid (hubdc.core.Grid) – if provided, only data inside the grid extent is returned.
  • resampleAlg (int) – one of the GDAL resampling algorithms (i.e. gdal.GRA_*)
Returns:

Return type:

numpy.ndarray

setCategoryColors(colors)[source]

Set band category colors from list of rgba tuples.

setCategoryNames(names)[source]

Set band category names.

setDescription(value)[source]

Set band description.

setMetadataItem(key, value, domain='')[source]

Set a metadata item. value can be a string, a number or a list of strings or numbers.

setNoDataValue(value)[source]

Set no data value.

writeArray(array, grid=None)[source]

Writes raster data.

Parameters:
  • array (3d array | list of 2d arrays) –
  • grid (hubdc.core.Grid) – if provided, data is written to the location given by the grid extent
xprofile(y)[source]

Returns raster data as 1d array for the given row y.

yprofile(x)[source]

Returns raster data as 1d array for the given column x.

class hubdc.core.RasterDataset(gdalDataset)[source]

Bases: object

Class for managing raster datasets files.

__init__(gdalDataset)[source]

Create an instance by a given gdal.Dataset.

acquisitionTime()[source]

Returns the acquisition time. Restore it from ‘acquisition time’ metadata item inside the ‘ENVI’ domain.

Returns:
Return type:datetime.datetime
array(indices=None, grid=None, resampleAlg=<Mock name='mock.gdal.GRA_NearestNeighbour' id='139630847665320'>, noDataValue=None, forceWarp=True, errorThreshold=0.0, warpMemoryLimit=104857600, multithread=False)[source]

Returns raster data as 3d array of shape = (zsize, ysize, xsize) for the given grid, where zsize is the number of raster bands, and ysize, xsize = grid.shape().

Parameters:
  • indices (list) – band indices to read (default is all bands)
  • grid (hubdc.core.Grid) – if not specified self.grid() is used
  • resampleAlg (int) – one of the GDAL resampling algorithms gdal.GRA_*
  • noDataValue (float) – if not specified, no data value of self is used
  • errorThreshold (float) – error threshold for approximation transformer (in pixels)
  • warpMemoryLimit (int) – size of working buffer in bytes
  • multithread (bool) – whether to multithread computation and I/O operations
Returns:

Return type:

numpy.ndarray

band(index)[source]

Return the RasterBandDataset given by index.

bands()[source]

Returns an iterator over each RasterBandDataset.

close()[source]

Close the gdal.Dataset.

copyCategories(other)[source]

Copy raster band category names and lookup tables.

copyMetadata(other)[source]

Copy raster and raster band metadata from other to self.

description()[source]

Returns the description.

driver()[source]

Return the Driver.

dtype()[source]

Returns the raster data type.

extent()[source]

Return the Extent.

filename()[source]

Return filename.

filenames()[source]

Return gdal dataset file list``

flushCache()[source]

Flush the cache.

static fromArray(array, grid=None, filename='', driver=None, options=None)[source]

Creates a new raster file with content, data type and number of bands given by array and with extent, resolution and projection given by grid.

Parameters:
Returns:

Return type:

hubdc.core.RasterDataset

gdalDataset()[source]

Return the gdal.Dataset.

gdalType()[source]

Returns the raster data type.

grid()[source]

Return the Grid.

mapLayer()[source]
metadataDict()[source]

Returns the metadata dictionary for all domains.

metadataDomain(domain='')[source]

Returns the metadata dictionary for the given domain.

metadataDomainList()[source]

Returns the list of metadata domain names.

metadataItem(key, domain='', dtype=<class 'str'>, required=False, default=None)[source]

Returns the value (casted to a specific dtype) of a metadata item.

noDataValue(default=None, required=False)[source]

Returns no data value. Returns default if all band no data values are undefined, or raises Raises an exception if not all bands share the same no data value.

If all bands are without a no data value, default is returned.

noDataValues(default=None, required=False)[source]

Returns band no data values. For bands without a no data value, default is returned, or if required is True, an error is raised

plotCategoryBand(index=0, noPlot=False, showPlot=True)[source]
plotMultibandColor(rgbindex=(0, 1, 2), rgbvmin=(None, None, None), rgbvmax=(None, None, None), rgbpmin=(None, None, None), rgbpmax=(None, None, None), noPlot=False, showPlot=True)[source]
plotSinglebandGrey(index=0, vmin=None, vmax=None, pmin=None, pmax=None, cmap='gray', noPlot=False, showPlot=True)[source]

cmap see https://matplotlib.org/examples/color/colormaps_reference.html https://matplotlib.org/api/_as_gen/matplotlib.pyplot.imshow.html

plotXProfile(row, plotWidget=None, yscale=1.0, **kwargs)[source]
plotYProfile(column, plotWidget=None, yscale=1.0, **kwargs)[source]
plotZProfile(pixel, plotWidget=None, spectral=False, xscale=1.0, yscale=1.0, **kwargs)[source]
projection()[source]

Return the Projection.

readAsArray(grid=None, resampleAlg=<Mock name='mock.gdal.GRA_NearestNeighbour' id='139630847665320'>)[source]

Returns raster data as 3d array.

Parameters:
  • grid (hubdc.core.Grid) – if provided, only data inside the grid extent is returned
  • resampleAlg (int) – one of the GDAL resampling algorithms (i.e. gdal.GRA_*)
Returns:

Return type:

numpy.ndarray

reopen(eAccess=<Mock name='mock.gdal.GA_ReadOnly' id='139630847665432'>)[source]

Returns re-opened version of itself. Useful in cases where flushCache is not sufficient.

setAcquisitionTime(acquisitionTime)[source]

Set the acquisition time. Store it as ‘acquisition time’ metadata item inside the ‘ENVI’ domain.

Parameters:acquisitionTime (datetime.datetime) –
setDescription(value)[source]

Set the description.

setGrid(grid)[source]

Set the Grid.

setMetadataDict(metadataDict)[source]

Set the metadata dictionary

setMetadataDomain(metadataDomain, domain)[source]

Set the metadata domain

setMetadataItem(key, value, domain)[source]

Set a metadata item. value can be a string, a number or a list of strings or numbers.

setNoDataValue(value)[source]

Set a single no data value to all bands.

setNoDataValues(values)[source]

Set band no data values.

shape()[source]

Returns the (zsize, ysize, xsize) tuple.

translate(grid=None, filename='', driver=None, options=None, resampleAlg=None, **kwargs)[source]

Returns a new instance of self translated into the given grid (default is self.grid()).

Parameters:
  • grid (hubdc.core.Grid) –
  • filename (str) –
  • driver (hubdc.core.RasterDriver) –
  • options (list) – raster creation options
  • resampleAlg (int) – GDAL resampling algorithm
  • kwargs – passed to gdal.TranslateOptions
Returns:

Return type:

hubdc.core.RasterDataset

warp(grid=None, filename='', driver=MemDriver(), options=None, resampleAlg=None, **kwargs)[source]

Returns a new instance of self warped into the given grid (default is self.grid()).

Parameters:
Returns:

Return type:

hubdc.core.RasterDataset

waveband(center)[source]

Return the RasterBandDataset given by center wavelenth in nanometers.

writeArray(array, grid=None)[source]

Writes raster data.

Parameters:
  • array (3d array | list of 2d arrays) –
  • grid (hubdc.core.Grid) – if provided, data is written to the location given by the grid extent
xprofile(row)[source]

Returns raster data as 1d array for the given row.

xsize()[source]

Returns raster x size in pixels.

yprofile(column)[source]

Returns raster data as 1d array for the given column.

ysize()[source]

Returns raster y size in pixels.

zprofile(pixel)[source]

Returns raster data as 1d array for the given pixel.

zsize()[source]

Returns raster z size in terms of number of raster bands.

class hubdc.core.RasterDriver(name, options=None)[source]

Bases: object

Class for managing raster drivers

__init__(name, options=None)[source]

Create instance from GDAL driver name and (optional) a list of default creation options.

create(grid, bands=1, gdalType=<Mock name='mock.gdal.GDT_Float32' id='139630848107016'>, filename='', options=None)[source]

Creates a new raster file with extent, resolution and projection given by grid.

Parameters:
  • grid (hubdc.core.Grid) –
  • bands (int) – number of raster bands
  • gdalType (int) – one of the gdal.GDT_* data types, or use gdal_array.NumericTypeCodeToGDALTypeCode
  • filename (str) – output filename
  • options (list) – raster creation options
Returns:

Return type:

hubdc.core.RasterDataset

equal(other)[source]

Returns whether self is equal to the other driver.

classmethod fromFilename(filename)[source]
gdalDriver()[source]

Returns the GDAL driver object.

name()[source]

Returns the driver name.

options()[source]

Returns default creation options.

prepareCreation(filename)[source]

Returns absolute filename and creates root folders if not existing.

setOptions(options=None)[source]

Set the default options.

class hubdc.core.RasterLayer(qgsRasterLayer)[source]

Bases: object

__init__(qgsRasterLayer)[source]

Initialize self. See help(type(self)) for accurate signature.

initMultiBandColorRenderer(redIndex=0, greenIndex=1, blueIndex=2, redMin=None, redMax=None, greenMin=None, greenMax=None, blueMin=None, blueMax=None, percent=2)[source]

Initialize a MultiBandColorRenderer for given band index.

initRendererFromQml(filename)[source]
initSingleBandGrayRenderer(grayIndex=0, grayMin=None, grayMax=None, percent=2)[source]

Initialize a SingleBandGrayRenderer.

initTrueColorRenderer(**kwargs)[source]
mapViewer()[source]
qgsLayer()[source]
show()[source]
class hubdc.core.RasterSize(x, y)[source]

Bases: object

Class for managing image sizes.

__init__(x, y)[source]
Parameters:
  • x (number) –
  • y (number) –
static parse(obj)[source]

Create instance by parsing the given RasterSize object, a (number, number) tuple or list

x()[source]

Returns the x size.

y()[source]

Returns the y size.

class hubdc.core.ResampleAlgHandler[source]

Bases: object

classmethod resampleAlgorithms()[source]
classmethod toString(resampleAlg)[source]

Return clear name of given resampleAlg.

classmethod translateResampleAlgorithms()[source]
classmethod warpResampleAlgorithms()[source]
class hubdc.core.Resolution(x, y)[source]

Bases: object

Class for managing pixel resolutions.

__init__(x, y)[source]
Parameters:
  • x (float > 0) – resolution in x dimension
  • y (float > 0) – resolution in y dimension
equal(other, tol=0.0)[source]

Returns whether self is equal to other.

static parse(obj)[source]

Create new instance from given Resolution object, number or (number, number) object.

x()[source]

Returns x resolution.

y()[source]

Returns y resolution.

class hubdc.core.Row(y, z)[source]

Bases: object

Class for managing image row location.

__init__(y, z)[source]
Parameters:
  • y (int) –
  • z (int) –
y()[source]

Returns row y coordinate.

z()[source]

Returns row z coordinate.

class hubdc.core.VectorDataset(ogrDataSource, layerNameOrIndex=0)[source]

Bases: object

Class for managing vector layer datasets.

__init__(ogrDataSource, layerNameOrIndex=0)[source]

Creates new instance from given ogr.DataSource and layer name or index given by nameOrIndex.

attributeTable()[source]
close()[source]

Closes the ogr.DataSourse and ogr.Layer

createFidDataset(filename, fidName='_fid')[source]

Create a vector dataset with same features but only one FID attribute.

delete()[source]

Closes and deletes/unlinks itself from disk/memory.

driver()[source]

Return the Driver.

extent()[source]

Returns the Extent.

extractPixel(rasterDataset)[source]

Extracts all pixel profiles covert by self, together with all associated attribute.

Returns (rasterValues, vectorValues) tuple.

featureCount()[source]

Returns the number of features.

features()[source]
fieldCount()[source]

Returns the number of attribute fields.

fieldNames()[source]

Returns the attribute field names.

fieldTypeNames()[source]

Returns the attribute field data type names.

filename()[source]

Returns the filename.

static fromPoints(points, attributes=None, filename='', driver=MemoryDriver())[source]

Create instance from given points. Projection is taken from the first point.

geometryTypeName()[source]

Return the geometry type name.

grid(resolution)[source]

Returns grid with extent of self and given resolution.

layerNameOrIndex()[source]

Returns the layer name/index.

mapLayer()[source]
mapViewer()[source]
metadataDict()[source]

Returns the metadata dictionary for all domains.

metadataDomain(domain='')[source]

Returns the metadata dictionary for the given domain.

metadataDomainList()[source]

Returns the list of metadata domain names.

metadataItem(key, domain='', dtype=<class 'str'>, required=False, default=None)[source]

Returns the value (casted to a specific dtype) of a metadata item.

ogrDataSource()[source]

Returns the ogr.DataSource.

ogrLayer()[source]

Returns the ogr.Layer.

projection()[source]

Returns the Projection.

rasterize(grid, gdalType=<Mock name='mock.gdal.GDT_Float32' id='139630848107016'>, initValue=0, burnValue=1, burnAttribute=None, allTouched=False, filterSQL=None, noDataValue=None, filename='', driver=None, options=None)[source]

Returns a Raster that is the rasterization of self into the given grid as.

Parameters:
  • grid (hubdc.core.Grid) –
  • gdalType (int) – one of the GDAL data types gdal.GDT_*
  • initValue (int) – value to pre-initialize the output array
  • burnValue (int) – value to burn into the output array for all objects; exclusive with burnAttribute
  • burnAttribute (str) – identifies an attribute field on the features to be used for a burn-in value; exclusive with burnValue
  • allTouched (bool) – whether to enable that all pixels touched by lines or polygons will be updated, not just those on the line render path, or whose center point is within the polygon
  • filterSQL (str) – set an SQL WHERE clause which will be used to filter vector features
  • noDataValue (float) – output raster no data value
  • filename (str) – output filename
  • driver (hubdc.core.RasterDriver) –
  • options (list) – raster creation options
Returns:

Return type:

hubdc.core.RasterDataset

reproject(projection, filename='', driver=MemoryDriver(), **kwargs)[source]

Returns a reprojection of self into the given projection. Optional kwargs are passed to gdal.VectorTranslateOptions.

setMetadataDict(metadataDict)[source]

Set the metadata dictionary

setMetadataDomain(metadataDomain, domain)[source]

Set the metadata domain

setMetadataItem(key, value, domain='')[source]

Set a metadata item. value can be a string, a number or a list of strings or numbers.

translate(filename='', driver=MemoryDriver(), **kwargs)[source]

Returns a translation of self. Optional kwargs are passed to gdal.VectorTranslateOptions.

zsize()[source]

Returns number of layers (i.e. 1).

class hubdc.core.VectorDriver(name)[source]

Bases: object

Class for managing OGR Drivers

__init__(name)[source]
Parameters:name (str) – e.g. ‘ESRI Shapefile’ or ‘GPKG’
delete(filename)[source]

Delete/unlink file given by filename.

equal(other)[source]

Returns whether self is equal to the other driver.

classmethod fromFilename(filename)[source]
name()[source]

Returns the driver name.

ogrDriver()[source]

Returns the OGR driver object.

prepareCreation(filename)[source]

Deletes filename if it already exist and creates subfolders if needed.

class hubdc.core.VectorLayer(qgsVectorLayer)[source]

Bases: object

__init__(qgsVectorLayer)[source]

Initialize self. See help(type(self)) for accurate signature.

mapViewer()[source]
qgsLayer()[source]
show()[source]
class hubdc.core.VrtDriver[source]

Bases: hubdc.core.RasterDriver

VRT driver.

__init__()[source]

Create instance from GDAL driver name and (optional) a list of default creation options.

hubdc.core.buildOverviews(filename, levels=None, minsize=1024, resampling='average')[source]

Build image overviews (a.k.a. image pyramid) for raster given by filename. If the list of overview levels is not specified, overviews are generated for levels of powers of 2 (i.e. levels=[2, 4, 8…]) up to the level where the size of the overview is smaller than minsize.

Parameters:
  • filename (str) – input filename
  • minsize (int) – defines the levels (powers of 2) in the case where levels is None
  • levels (Union[Tuple, List]) – list of overview levels
  • resampling (str) – one of those: 'average', 'gauss', 'cubic', 'cubicspline', 'lanczos', 'average_mp', 'average_magphase', 'mode'
hubdc.core.createVRTDataset(rasterDatasetsOrFilenames, filename='', **kwargs)[source]

Creates a virtual raster file (VRT) from raster datasets or filenames given by rastersOrFilenames.

Parameters:
  • filename (str) – output filename
  • rastersOrFilenames (Union[Tuple, List]) – list of filenames or rasters
  • kwargs – all additional keyword arguments are passed to gdal.BuildVRTOptions
Returns:

Return type:

hubdc.core.RasterDataset

hubdc.core.openRasterDataset(filename, eAccess=<Mock name='mock.gdal.GA_ReadOnly' id='139630847665432'>)[source]

Opens the raster given by filename.

Parameters:
  • filename (str) – input filename
  • eAccess (int) – access mode gdal.GA_ReadOnly or gdal.GA_Update
Returns:

Return type:

hubdc.core.RasterDataset

hubdc.core.openVectorDataset(filename, layerNameOrIndex=None, update=False)[source]

Opens the vector layer given by filename and layerNameOrIndex.

Parameters:
  • filename (str) – input filename
  • layerNameOrIndex (int | str) – layer index or name
  • update (bool) – whether to open in update mode
Returns:

Return type:

hubdc.core.VectorDataset

Indices and tables