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)

# 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 = 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">
      <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>

This should create a KML polygon with a green border and red fill. A successful result would have this line:


I'm not sure how to tell GDAL that the polygon should be filled.


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.