Returns a string/buffer representation of the raster in the specified format. Wrapper for
        ST_AsGDALRaster function in the database.

def getAsGdalRaster(self, rasterFieldName, tableName, rasterIdFieldName, rasterId, gdalFormat, newSRID=None, **kwargs):
        """
        Returns a string/buffer representation of the raster in the specified format. Wrapper for
        ST_AsGDALRaster function in the database.
        """

        # Check gdalFormat
        if not (gdalFormat in RasterConverter.supportedGdalRasterFormats(self._session)):
            raise ValueError('FORMAT NOT SUPPORTED: {0} format is not supported '
                             'in this PostGIS installation.'.format(gdalFormat))

        # Setup srid
        if newSRID:
            srid = ', {0}'.format(newSRID)
        else:
            srid = ''

        # Compile options
        if kwargs:
            optionsList = []
            for key, value in kwargs.iteritems():
                kwargString = "'{0}={1}'".format(key, value)
                optionsList.append(kwargString)

            optionsString = ','.join(optionsList)
            options = ', ARRAY[{0}]'.format(optionsString)
        else:
            options = ''

        # Create statement
        statement = '''
                    SELECT ST_AsGDALRaster("{0}", '{1}'{5}{6})
                    FROM "{2}" WHERE "{3}"={4};
                    '''.format(rasterFieldName, gdalFormat, tableName, rasterIdFieldName, rasterId,  options, srid)

        # Execute query
        result = self._session.execute(statement).scalar()

        return bytes(result).decode('utf-8')