def getAsKmlGrid(self, tableName, rasterId=1, rasterIdFieldName='id', rasterFieldName='raster', documentName='default', alpha=1.0, noDataValue=0, discreet=False):
        """
        Creates a KML file with each cell in the raster represented by a polygon. The result is a vector grid representation of the raster.
        Note that pixels with values between -1 and 0 are omitted as no data values. Also note that this method only works on the first band.
        Returns the kml document as a string.
        """
        # Validate alpha
        if not (alpha >= 0 and alpha <= 1.0):
            raise ValueError("RASTER CONVERSION ERROR: alpha must be between 0.0 and 1.0.")

        # 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)

        # Get polygons for each cell in kml format
        statement = '''
                    SELECT x, y, val, ST_AsKML(geom) AS polygon
                    FROM (
                    SELECT (ST_PixelAsPolygons(%s)).*
                    FROM %s WHERE %s=%s
                    ) AS foo
                    ORDER BY val;
                    ''' % (rasterFieldName, tableName, rasterIdFieldName, rasterId)

        result = self._session.execute(statement)

        # 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

        groupValue = -9999999.0
        uniqueValues = []

        # Add polygons to the kml file with styling
        for row in result:
            # Value will be None if it is a no data value
            if row.val:
                value = float(row.val)
            else:
                value = None

            polygonString = row.polygon
            i = int(row.x)
            j = int(row.y)

            # Only create placemarks for values that are no data values
            if value:
                # Collect unique values
                if value not in uniqueValues:
                    uniqueValues.append(value)

                # Create a new placemark for each group of values
                if value != groupValue:
                    placemark = ET.SubElement(document, 'Placemark')
                    placemarkName = ET.SubElement(placemark, 'name')
                    placemarkName.text = str(value)

                    # Create style tag and setup styles
                    style = ET.SubElement(placemark, 'Style')

                    # Set polygon line style
                    lineStyle = ET.SubElement(style, 'LineStyle')

                    # Set polygon line color and width
                    lineColor = ET.SubElement(lineStyle, 'color')
                    lineColor.text = self.LINE_COLOR
                    lineWidth = ET.SubElement(lineStyle, 'width')
                    lineWidth.text = str(self.LINE_WIDTH)

                    # Set polygon fill color
                    polyStyle = ET.SubElement(style, 'PolyStyle')
                    polyColor = ET.SubElement(polyStyle, 'color')

                    # Convert alpha from 0.0-1.0 decimal to 00-FF string
                    integerAlpha = mappedColorRamp.getAlphaAsInteger()

                    # Get RGB color from color ramp and convert to KML hex ABGR string with alpha
                    integerRGB = mappedColorRamp.getColorForValue(value)
                    hexABGR = '%02X%02X%02X%02X' % (integerAlpha,
                                                    integerRGB[mappedColorRamp.B],
                                                    integerRGB[mappedColorRamp.G],
                                                    integerRGB[mappedColorRamp.R])

                    # Set the polygon fill alpha and color
                    polyColor.text = hexABGR

                    # Create multigeometry tag
                    multigeometry = ET.SubElement(placemark, 'MultiGeometry')

                    # Create the data tag
                    extendedData = ET.SubElement(placemark, 'ExtendedData')

                    # Add value to data
                    valueData = ET.SubElement(extendedData, 'Data', name='value')
                    valueValue = ET.SubElement(valueData, 'value')
                    valueValue.text = str(value)

                    iData = ET.SubElement(extendedData, 'Data', name='i')
                    valueI = ET.SubElement(iData, 'value')
                    valueI.text = str(i)

                    jData = ET.SubElement(extendedData, 'Data', name='j')
                    valueJ = ET.SubElement(jData, 'value')
                    valueJ.text = str(j)

                    groupValue = value

                # Get polygon object from kml string and append to the current multigeometry group
                polygon = ET.fromstring(polygonString)
                multigeometry.append(polygon)

        if not discreet:
            # Embed the color ramp in SLD format
            document.append(ET.fromstring(mappedColorRamp.getColorMapAsContinuousSLD()))
        else:
            # Sort the unique values
            uniqueValues.sort()
            document.append(ET.fromstring(mappedColorRamp.getColorMapAsDiscreetSLD(uniqueValues)))

        return ET.tostring(kml)