def getAsKmlPng(self, tableName, rasterId=1, rasterIdFieldName='id', rasterFieldName='raster', documentName='default',
                    alpha=1.0,  drawOrder=0, noDataValue=0, cellSize=None, resampleMethod='NearestNeighbour', discreet=False):
        """
        Creates a KML wrapper and PNG represent of the raster. Returns a string of the kml file contents and
        a binary string of the PNG contents. The color ramp used to generate the PNG is embedded in the ExtendedData
        tag of the GroundOverlay.
        IMPORTANT: The PNG image is referenced in the kml as 'raster.png', thus it must be written to file with that
        name for the kml to recognize it.
        """

        # Get the color ramp and parameters
        minValue, maxValue = self.getMinMaxOfRasters(session=self._session,
                                                     table=tableName,
                                                     rasterIds=(str(rasterId), ),
                                                     rasterIdField=rasterIdFieldName,
                                                     rasterField=rasterFieldName,
                                                     noDataValue=noDataValue)

        mappedColorRamp = ColorRampGenerator.mapColorRampToValues(colorRamp=self._colorRamp,
                                                                  minValue=minValue,
                                                                  maxValue=maxValue,
                                                                  alpha=alpha)

        # Join strings in list to create ramp
        rampString = mappedColorRamp.getPostGisColorRampString()

        # Get a PNG representation of the raster
        result = self.getRastersAsPngs(session=self._session,
                                       tableName=tableName,
                                       rasterIds=(str(rasterId),),
                                       postGisRampString=rampString,
                                       rasterField=rasterFieldName,
                                       rasterIdField=rasterIdFieldName,
                                       cellSize=cellSize,
                                       resampleMethod=resampleMethod)

        for row in result:
            binaryPNG = row.png

        # Determine extents for the KML wrapper file via query
        statement = '''
                    SELECT (foo.metadata).*
                    FROM (
                    SELECT ST_MetaData(ST_Transform({0}, 4326, 'Bilinear')) as metadata
                    FROM {1}
                    WHERE {2}={3}
                    ) As foo;
                    '''.format(rasterFieldName, tableName, rasterIdFieldName, rasterId)

        result = self._session.execute(statement)

        for row in result:
            upperLeftY = row.upperlefty
            scaleY = row.scaley
            height = row.height

            upperLeftX = row.upperleftx
            scaleX = row.scalex
            width = row.width

        north = upperLeftY
        south = upperLeftY + (scaleY * height)
        east = upperLeftX + (scaleX * width)
        west = upperLeftX

        # Initialize KML Document
        kml = ET.Element('kml', xmlns='http://www.opengis.net/kml/2.2')
        document = ET.SubElement(kml, 'Document')
        docName = ET.SubElement(document, 'name')
        docName.text = documentName


        # GroundOverlay
        groundOverlay = ET.SubElement(document, 'GroundOverlay')
        overlayName = ET.SubElement(groundOverlay, 'name')
        overlayName.text = 'Overlay'

        # DrawOrder
        drawOrderElement = ET.SubElement(groundOverlay, 'drawOrder')
        drawOrderElement.text = str(drawOrder)

        # Define Region
        regionElement = ET.SubElement(groundOverlay, 'Region')
        latLonBox = ET.SubElement(regionElement, 'LatLonBox')

        northElement = ET.SubElement(latLonBox, 'north')
        northElement.text = str(north)

        southElement = ET.SubElement(latLonBox, 'south')
        southElement.text = str(south)

        eastElement = ET.SubElement(latLonBox, 'east')
        eastElement.text = str(east)

        westElement = ET.SubElement(latLonBox, 'west')
        westElement.text = str(west)


        # Href to PNG
        iconElement = ET.SubElement(groundOverlay, 'Icon')
        hrefElement = ET.SubElement(iconElement, 'href')
        hrefElement.text = 'raster.png'

        # LatLonBox
        latLonBox = ET.SubElement(groundOverlay, 'LatLonBox')

        northElement = ET.SubElement(latLonBox, 'north')
        northElement.text = str(north)

        southElement = ET.SubElement(latLonBox, 'south')
        southElement.text = str(south)

        eastElement = ET.SubElement(latLonBox, 'east')
        eastElement.text = str(east)

        westElement = ET.SubElement(latLonBox, 'west')
        westElement.text = str(west)

        if not discreet:
            # Embed the color ramp in SLD format
            document.append(ET.fromstring(mappedColorRamp.getColorMapAsContinuousSLD()))
        else:
            # Determine values for discreet color ramp
            statement = '''
                        SELECT (pvc).*
                        FROM (
                              SELECT ST_ValueCount({0}) as pvc
                              FROM {1}
                              WHERE {2}={3}
                             ) As foo
                        ORDER BY (pvc).value;
                        '''.format(rasterFieldName, tableName, rasterIdFieldName, rasterId)

            result = self._session.execute(statement)

            values = []
            for row in result:
                values.append(row.value)

            document.append(ET.fromstring(mappedColorRamp.getColorMapAsDiscreetSLD(values)))

        return ET.tostring(kml), binaryPNG
def getAsKmlPng ( self , tableName , rasterId = 1 , rasterIdFieldName = 'id' , rasterFieldName = 'raster' , documentName = 'default' , alpha = 1.0 , drawOrder = 0 , noDataValue = 0 , cellSize = None , resampleMethod = 'NearestNeighbour' , discreet = False ) : minValue , maxValue = self . getMinMaxOfRasters ( session = self . _session , table = tableName , rasterIds = ( str ( rasterId ) , ) , rasterIdField = rasterIdFieldName , rasterField = rasterFieldName , noDataValue = noDataValue ) mappedColorRamp = ColorRampGenerator . mapColorRampToValues ( colorRamp = self . _colorRamp , minValue = minValue , maxValue = maxValue , alpha = alpha ) rampString = mappedColorRamp . getPostGisColorRampString ( ) result = self . getRastersAsPngs ( session = self . _session , tableName = tableName , rasterIds = ( str ( rasterId ) , ) , postGisRampString = rampString , rasterField = rasterFieldName , rasterIdField = rasterIdFieldName , cellSize = cellSize , resampleMethod = resampleMethod ) for row in result : binaryPNG = row . png statement = '''
                    SELECT (foo.metadata).*
                    FROM (
                    SELECT ST_MetaData(ST_Transform({0}, 4326, 'Bilinear')) as metadata
                    FROM {1}
                    WHERE {2}={3}
                    ) As foo;
                    ''' . format ( rasterFieldName , tableName , rasterIdFieldName , rasterId ) result = self . _session . execute ( statement ) for row in result : upperLeftY = row . upperlefty scaleY = row . scaley height = row . height upperLeftX = row . upperleftx scaleX = row . scalex width = row . width north = upperLeftY south = upperLeftY + ( scaleY * height ) east = upperLeftX + ( scaleX * width ) west = upperLeftX kml = ET . Element ( 'kml' , xmlns = 'http://www.opengis.net/kml/2.2' ) document = ET . SubElement ( kml , 'Document' ) docName = ET . SubElement ( document , 'name' ) docName . text = documentName groundOverlay = ET . SubElement ( document , 'GroundOverlay' ) overlayName = ET . SubElement ( groundOverlay , 'name' ) overlayName . text = 'Overlay' drawOrderElement = ET . SubElement ( groundOverlay , 'drawOrder' ) drawOrderElement . text = str ( drawOrder ) regionElement = ET . SubElement ( groundOverlay , 'Region' ) latLonBox = ET . SubElement ( regionElement , 'LatLonBox' ) northElement = ET . SubElement ( latLonBox , 'north' ) northElement . text = str ( north ) southElement = ET . SubElement ( latLonBox , 'south' ) southElement . text = str ( south ) eastElement = ET . SubElement ( latLonBox , 'east' ) eastElement . text = str ( east ) westElement = ET . SubElement ( latLonBox , 'west' ) westElement . text = str ( west ) iconElement = ET . SubElement ( groundOverlay , 'Icon' ) hrefElement = ET . SubElement ( iconElement , 'href' ) hrefElement . text = 'raster.png' latLonBox = ET . SubElement ( groundOverlay , 'LatLonBox' ) northElement = ET . SubElement ( latLonBox , 'north' ) northElement . text = str ( north ) southElement = ET . SubElement ( latLonBox , 'south' ) southElement . text = str ( south ) eastElement = ET . SubElement ( latLonBox , 'east' ) eastElement . text = str ( east ) westElement = ET . SubElement ( latLonBox , 'west' ) westElement . text = str ( west ) if not discreet : document . append ( ET . fromstring ( mappedColorRamp . getColorMapAsContinuousSLD ( ) ) ) else : statement = '''
                        SELECT (pvc).*
                        FROM (
                              SELECT ST_ValueCount({0}) as pvc
                              FROM {1}
                              WHERE {2}={3}
                             ) As foo
                        ORDER BY (pvc).value;
                        ''' . format ( rasterFieldName , tableName , rasterIdFieldName , rasterId ) result = self . _session . execute ( statement ) values = [ ] for row in result : values . append ( row . value ) document . append ( ET . fromstring ( mappedColorRamp . getColorMapAsDiscreetSLD ( values ) ) ) return ET . tostring ( kml ) , binaryPNG
Creates a KML wrapper and PNG represent of the raster. Returns a string of the kml file contents and
        a binary string of the PNG contents. The color ramp used to generate the PNG is embedded in the ExtendedData
        tag of the GroundOverlay.
        IMPORTANT: The PNG image is referenced in the kml as 'raster.png', thus it must be written to file with that
        name for the kml to recognize it.