Can't set OGR layer geometry type in Python

by Drew   Last Updated October 12, 2018 19:22 PM

I'm trying to create a layer of geometry type polygon, with a feature of geometry type polygon, and a geometry of type polygon. However, no matter how I attempt to set the geom type of the feature, geometry, or layer, the layer is always set to multipolygon.

Here is some example code to show what I mean:

# Create test polygon
ring = ogr.Geometry(ogr.wkbLinearRing)
ring.AddPoint(1179091.1646903288, 712782.8838459781)
ring.AddPoint(1161053.0218226474, 667456.2684348812)
ring.AddPoint(1214704.933941905, 641092.8288590391)
ring.AddPoint(1228580.428455506, 682719.3123998424)
ring.AddPoint(1218405.0658121984, 721108.1805541387)
ring.AddPoint(1179091.1646903288, 712782.8838459781)
geom = ogr.Geometry(ogr.wkbPolygon)
geom.AddGeometry(ring)

print(geom.GetGeometryName()) # returns POLYGON

# Open the FGDB
driver = ogr.GetDriverByName("FileGDB")
path = "/path/to/polygon.gdb"
if not os.path.exists(path):
    data_source = driver.CreateDataSource(path)
else:
    data_source = driver.Open(path, 1)

# Define the spatial reference to use
srs = osr.SpatialReference()
srs.ImportFromEPSG(4326)

# Create the layer
layer_name = "polygon".encode("utf-8")
layer = data_source.CreateLayer(layer_name, srs, ogr.wkbPolygon)

print(ogr.GeometryTypeToName(layer.GetGeomType())) # returns Multi Polygon

# Create the feature
feat_def = ogr.FeatureDefn()
feat_def.SetGeomType(ogr.wkbPolygon)
feature = ogr.Feature(feat_def)

print(ogr.GeometryTypeToName(feature.GetDefnRef().GetGeomType())) # returns Polygon

# Set the geometry
feature.SetGeometry(geom)

print(feature.GetGeometryRef().GetGeometryName()) # returns POLYGON

# Create the feature in the layer
layer.CreateFeature(feature)

# Dereference the feature
feature = None

# Save and close the data source
data_source = None
  • The first print, I show that the geometry I created is of type polygon

  • The second print, I show that the layer's geometry type is of multipolygon, despite me initializing the layer with type polygon.

  • The third print shows that I did set the feature's geometry type to polygon when explicitly creating a FeatureDefn

  • The fourth print shows that after setting the feature's geometry, it is still type polygon

Here's the output of ogrinfo:

$ ogrinfo polygon.gdb/ polygon
INFO: Open of `polygon.gdb/'
      using driver `OpenFileGDB' successful.

Layer name: polygon
Geometry: Multi Polygon
Feature Count: 1
Extent: (1161053.021823, 641092.828859) - (1228580.428456, 721108.180554)
Layer SRS WKT:
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0,
        AUTHORITY["EPSG","8901"]],
    UNIT["degree",0.0174532925199433,
        AUTHORITY["EPSG","9122"]],
    AUTHORITY["EPSG","4326"]]
FID Column = OBJECTID
Geometry Column = SHAPE
OGRFeature(polygon):1
  MULTIPOLYGON Z (((1179091.16469033 712782.883845978 0,1218405.0658122 721108.180554139 0,1228580.42845551 682719.312399842 0,1214704.93394191 641092.828859039 0,1161053.02182265 667456.268434881 0,1179091.16469033 712782.883845978 0)))

I've looked everywhere in the (somewhat useless) OGR Python bindings API and have found nothing useful. So, how do I actually set the layer to be of geometry type polygon?

This is with Python 2.7.15 and GDAL 2.3.2 on CentOS 7.5.

Tags : python gdal ogr


Related Questions


python GDAL translate to convert and coloring

Updated March 03, 2018 02:22 AM

Module has no attribute Warp?

Updated July 28, 2017 20:22 PM

gdal.Warp workingType option default

Updated August 24, 2018 15:22 PM


What resampling method is used in gdal_merge?

Updated June 22, 2015 22:09 PM