I'm trying to create a filled polygon in Python with osgeo. When I set the polygon edge and fill with .SetStyleString(), PEN successfully sets the polygon outline colour, but BRUSH does not fill it. (Brush parameters are defined in section 2.4 of this specification.)
Here's my code:
from osgeo import ogr
# Create ring
ring = ogr.Geometry(ogr.wkbLinearRing)
ring.AddPoint(5.9559111595, 45.8179931641)
ring.AddPoint(10.4920501709, 45.8179931641)
ring.AddPoint(10.4920501709, 47.808380127)
ring.AddPoint(5.9559111595, 47.808380127)
ring.AddPoint(5.9559111595, 45.8179931641)
# Create polygon
poly = ogr.Geometry(ogr.wkbPolygon)
poly.AddGeometry(ring)
# output a KML
# (via examples on https://pcjericks.github.io/py-gdalogr-cookbook/geometry.html)
outDriver = ogr.GetDriverByName('KML')
outDataSource = outDriver.CreateDataSource('output.kml')
outLayer = outDataSource.CreateLayer('output.kml',geom_type=ogr.wkbPolygon)
featureDefn = outLayer.GetLayerDefn()
outFeature = ogr.Feature(featureDefn)
outFeature.SetGeometry(poly)
outFeature.SetStyleString("BRUSH(fc:#FF0000FF);PEN(c:#00FF00FF)")
outLayer.CreateFeature(outFeature)
outFeature = None
outDataSource = None
And here is the output:
<?xml version="1.0" encoding="utf-8" ?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document id="root_doc">
<Folder><name>output.kml</name>
<Placemark>
<Style><LineStyle><color>FF00FF00</color></LineStyle><PolyStyle><fill>0</fill></PolyStyle></Style>
<Polygon><outerBoundaryIs><LinearRing><coordinates>5.9559111595,45.8179931641,0 10.4920501709,45.8179931641,0 10.4920501709,47.808380127,0 5.9559111595,47.808380127,0 5.9559111595,45.8179931641,0</coordinates></LinearRing></outerBoundaryIs></Polygon>
</Placemark>
</Folder>
</Document></kml>
This should create a KML polygon with a green border and red fill. A successful result would have this line:
<Style><LineStyle><color>FF00FF00</color></LineStyle><PolyStyle><color>FF0000FF</color><fill>1</fill></PolyStyle></Style>
I'm not sure how to tell GDAL that the polygon should be filled.
Best Answer
I can see that you used the KML driver https://www.gdal.org/drv_kml.html which is rather limited in capabilities. Use the LIBKML driver https://www.gdal.org/drv_libkml.html instead.
I verified with ogr2ogr (GDAL 2.4.0dev) that KML driver writes
but LIBKML driver writes
The latter shows also as a filled polygon with Google Earth.