1

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

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()
    print(geometry.GetGeometryName())

Greetings

  • 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! – CamMuller Jun 30 '17 at 12:16
3

See the cookbook

print(geom.GetGeometryName())
2

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()
        print(geometry.GetGeometryName())

geom_type()

Appreciate the help!

0

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. – Nate Wanner Oct 25 '18 at 21:06
0

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

layer.ResetReading()

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

New contributor
Eastpath is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

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.