Taking a long shot but does anyone have an idea of whether GDAL or OGR has a function to determine a shapefile or layer geometry type using python? I specifically want the output to print "POINT" for example.

I understand that the geometry can be printed to well-known text format and have the code to complete that, but specifically require the string format.

6 Answers 6


See the cookbook


I don´t wan to be punctilious with the code, but you don´t need to loop all the features, the shapefile always store only one geometry type:

        # get the data layer
    layer = datasource.GetLayer()

    # get the first feature
    feature = layer.GetNextFeature()

    geometry = feature.GetGeometryRef()


  • Thanks for this, my initial thought was that I needed loop over every record in the layer before passing the output to the next function. Help is much appreciated!
    – WhaleShark
    Jun 30, 2017 at 12:16
  • I thought that shp files only had one geometry type, but I've ran across shapefiles that do have multiple geometry types, beware! May 18, 2020 at 14:58
  • That's right, polygon shapefile can contain polygons and multipolygons and line shapefile can contain linestrings and multilinestrings.
    – user30184
    Aug 5, 2020 at 13:20

It's possible to get the layer geometry type without reading a feature.

ds = ogr.Open(your_input_file)
layer = ds.GetLayer()
layer_defn = layer.GetLayerDefn()

Thanks very much @sgillies, I solved the problem as such:

def geom_type():

    shapefile_location = r"E:\Cameron\Python\GDAL\SHP\Cadastral_Parcels\Parcels.shp"
    shapefile = ogr.Open(shapefile_location)
    layer = shapefile.GetLayer()

    for feature in layer:
        geometry = feature.GetGeometryRef()


Appreciate the help!


Why not directly use the layer.GetGeomType() method? > Gdal/Ogr python API

print(layer.GetGeomType() == wkbLineString)
>>> True
  • This returns the layer's type (LineString) and format (wkb), but the function isn't designed for individual geometries (returns a negative integer in my test). GetGeometryName() works on geometries and doesn't include the format (wkb) in the string return. Oct 25, 2018 at 21:06
  • Negative integers may be OK because codes for 25D geometries are small negative integers.
    – user30184
    May 19, 2020 at 7:20

One gotcha to be aware of, which caught me out. Don't forget to add:


If you want to process the features after determining the geometry type.

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.