Last active
November 15, 2017 21:31
-
-
Save grovduck/0eac07ccf441762680e7 to your computer and use it in GitHub Desktop.
[RAT support for rasterio (WIP)] Add raster attribute support to rasterio. The property raster_attributes is added to the DatasetReader object and the RAT is returned as an array of dictionaries, where each dictionary has a row's worth of field names/field values. One array is returned for each band in the dataset #rasterio #rat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add in _raster_attributes object | |
cdef class DatasetReader: | |
cdef public object _crs_wkt | |
cdef public object _transform | |
cdef public object _block_shapes | |
cdef public object _raster_attributes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@property | |
def raster_attributes(self): | |
"""Returns the default raster attribute table for each band. | |
""" | |
cdef void *hband = NULL | |
cdef void *hRat = NULL | |
cdef int i, j, col_count | |
cdef const char *s_val | |
if self._raster_attributes is None: | |
if self._hds == NULL: | |
raise ValueError("can't read closed raster file") | |
self._raster_attributes = [] | |
for i in range(self._count): | |
hband = _gdal.GDALGetRasterBand(self._hds, i+1) | |
if hband == NULL: | |
raise ValueError("Null band") | |
hRat = _gdal.GDALGetDefaultRAT(hband) | |
if hRat == NULL: | |
raise ValueError("Null raster attribute table") | |
col_count = _gdal.GDALRATGetColumnCount(hRat) | |
data_types = [] | |
col_names = [] | |
for j in range(col_count): | |
data_types.append(_gdal.GDALRATGetTypeOfCol(hRat, j)) | |
s_val = _gdal.GDALRATGetNameOfCol(hRat, j) | |
col_names.append(s_val.encode('utf-8')) | |
band_rat = [] | |
for i in range(_gdal.GDALRATGetRowCount(hRat)): | |
row = {} | |
for j in range(col_count): | |
dt, cn = data_types[j], col_names[j] | |
if dt == _gdal.GFT_Integer: | |
row[cn] = _gdal.GDALRATGetValueAsInt(hRat, i, j) | |
elif dt == _gdal.GFT_Real: | |
row[cn] = _gdal.GDALRATGetValueAsDouble(hRat, i, j) | |
elif dt == _gdal.GFT_String: | |
s_val = _gdal.GDALRATGetValueAsString(hRat, i, j) | |
row[cn] = s_val.encode('utf-8') | |
band_rat.append(row) | |
self._raster_attributes.append(band_rat) | |
return self._raster_attributes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add in GDAL RAT functions | |
ctypedef enum GDALRATFieldType: | |
GFT_Integer | |
GFT_Real | |
GFT_String | |
void *GDALGetDefaultRAT(void *band) | |
int GDALRATGetColumnCount(void *hRat) | |
int GDALRATGetRowCount(void *hRat) | |
const char *GDALRATGetNameOfCol(void *hRat, int col) | |
GDALRATFieldType GDALRATGetTypeOfCol(void *hRat, int col) | |
int GDALRATGetValueAsInt(void *hRat, int row, int col) | |
double GDALRATGetValueAsDouble(void *hRat, int row, int col) | |
const char *GDALRATGetValueAsString(void *hRat, int row, int col) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment