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.

5

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

| improve this answer | |
  • 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
  • I thought that shp files only had one geometry type, but I've ran across shapefiles that do have multiple geometry types, beware! – Felipe Gutierrez May 18 at 14:58
  • That's right, polygon shapefile can contain polygons and multipolygons and line shapefile can contain linestrings and multilinestrings. – user30184 Aug 5 at 13:20
6

See the cookbook

print(geom.GetGeometryName())
| improve this answer | |
4

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!

| improve this answer | |
2

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()
ogr.GeometryTypeToName(layer_defn.GetGeomType())
'Polygon'
| improve this answer | |
0

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

print(layer.GetGeomType() == wkbLineString)
>>> True
| improve this answer | |
  • 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
  • Negative integers may be OK because codes for 25D geometries are small negative integers. – user30184 May 19 at 7:20
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.

| improve this answer | |

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.