5

When I use the GetGeomType() with GDAL/OGR it returns an integer. I want to know what geometry type each integer represents.

driver = ogr.GetDriverByName("FileGDB")
gdb = r"C:\Users\******\Documents\ArcGIS\Default.gdb"
ds = driver.Open(gdb, 0)
input_lyr_name = "Birmingham_Burglaries_2016"
lyr = ds.GetLayerByName(input_lyr_name)
# access the schema info
lyr_def = lyr.GetLayerDefn()

print lyr_def.GetGeomType()

Output: 1

I already know an alternative way to get the geometry type as below, but I am interested to match integers to the correct geometry type. Is there a list somewhere?

first_feat = lyr.GetFeature(1)
print first_feat.geometry().GetGeometryName()

Output: POINT

But this won't work on an empty dataset.

3
  • 1
    print ogr.GeometryTypeToName(lyr_def.GetGeomType())
    – user2856
    Commented May 5, 2017 at 10:20
  • @Luke smashing stuff. That will come in very handy. Commented May 5, 2017 at 10:25
  • Added as an answer
    – user2856
    Commented May 5, 2017 at 11:01

3 Answers 3

8

If you need to determine the type in code, use the ogr.wkb* constants as per @iant's answer.

Alternatively, if you just want to print out the type, you can use the ogr.GeometryTypeToName function.

print ogr.GeometryTypeToName(lyr_def.GetGeomType())

e.g

In [1]: from osgeo import ogr

In [2]: print ogr.GeometryTypeToName(ogr.wkbPoint)
Point

In [3]: print ogr.GeometryTypeToName(ogr.wkbPolygon)
Polygon

In [4]: print ogr.GeometryTypeToName(ogr.wkbLineString)
Line String
6

You will find the output variants of WKB types supported by GDAL here:

OGC 06-103r4 "OpenGIS® Implementation Standard for Geographic information - Simple feature access - Part 1: Common architecture", v1.2.1

From §8.2.8 (page 66):

enum WKBGeometryType {
wkbPoint = 1,
wkbLineString = 2,
wkbPolygon = 3,
wkbTriangle = 17
wkbMultiPoint = 4,
wkbMultiLineString = 5,
wkbMultiPolygon = 6,
wkbGeometryCollection = 7,
wkbPolyhedralSurface = 15,
wkbTIN = 16
wkbPointZ = 1001,
wkbLineStringZ = 1002,
wkbPolygonZ = 1003,
wkbTrianglez = 1017
wkbMultiPointZ = 1004,
wkbMultiLineStringZ = 1005,
wkbMultiPolygonZ = 1006,
wkbGeometryCollectionZ = 1007,
wkbPolyhedralSurfaceZ = 1015,
wkbTINZ = 1016
wkbPointM = 2001,
wkbLineStringM = 2002,
wkbPolygonM = 2003,
wkbTriangleM = 2017
wkbMultiPointM = 2004,
wkbMultiLineStringM = 2005,
wkbMultiPolygonM = 2006,
wkbGeometryCollectionM = 2007,
wkbPolyhedralSurfaceM = 2015,
wkbTINM = 2016
wkbPointZM = 3001,
wkbLineStringZM = 3002,
wkbPolygonZM = 3003,
wkbTriangleZM = 3017
wkbMultiPointZM = 3004,
wkbMultiLineStringZM = 3005,
wkbMultiPolygonZM = 3006,
wkbGeometryCollectionZM = 3007,
wkbPolyhedralSurfaceZM = 3015,
wkbTinZM = 3016,
} 
2
  • 1
    Perfect, after seeing that list I realised I had it all along but didn't cop it. Using help(ogr) all the data types were printed. Commented May 5, 2017 at 9:01
  • @Clubdebambos yep, that is another way!
    – mgri
    Commented May 5, 2017 at 9:08
3

It will return one of the constants on this page which judging from this email you can refer to directly in your code, e.g.:

driver = ogr.GetDriverByName("FileGDB")

gdb = r"C:\Users\******\Documents\ArcGIS\Default.gdb"
ds = driver.Open(gdb, 0)
input_lyr_name = "Birmingham_Burglaries_2016"
lyr = ds.GetLayerByName(input_lyr_name)
# access the schema info
lyr_def = lyr.GetLayerDefn()
lyr_type = lyr_def.GetGeomType()
if lyr_type == ogr.wkbPolygon or lyr_type == ogr.wkbMultiPolygon:
    print "Polygon"
elif lyr_type == ogr.wkbPoint:
    print "Point"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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