1

I tried calling gdf.geometry.name and I always get back "geometry" as the geometry column name. Using GDAL's ogrinfo, I get the actual name which is "geom". Is there another way of getting the correct geometry column name without parsing the output from ogr?

3
  • What is your data source for this GeoDataFrame? PostGIS, GPKG,...? – ThomasG77 Apr 28 at 19:40
  • GPKG. I see this issue with other formats as well though. I know GeoJSON doesnt have the concept of a "geometry" column so I dont expect to get a name for that file type. – Sogun Apr 28 at 19:43
  • 1
    Once the data is in a gpd.GeoDataFrame it's a copy of the data in a new format and has no link to the GeoPackage. The geometry column IS "geometry". Same would happen if you read a shapefile (that has "shape" as the geometry column), the geometry column would still be "geometry" in the GeoDataFrame. Also, same with a GeoJSON as it's no longer a GeoJSON, it's now a GeoDataFrame. – user2856 Apr 29 at 9:07
1

Not sure if it's possible via GeoPandas but at least quite easy with ogr Python bindings (you have them if you have Geopandas). The dirty way e.g parsing is not required.

import ogr
# PostgreSQL recipe
databaseServer,databaseName,databaseUser,databasePW = 'your_host', 'your_dbname', 'your_user', 'your_password'
connString = f"PG: host={databaseServer} dbname={databaseName} user={databaseUser} password={databasePW}" 

conn = ogr.Open(connString)

l = conn.GetLayerByName('your_layer_name')
print(l.GetGeometryColumn())

# GPKG recipe

conGpkg = "your_gpkg.gpkg"
conn1 = ogr.Open(conGpkg)
# List layers names
layers = [layer.GetName() for layer in conn1]
# First layer name
layer1 = layers[0]

l1 = conn1.GetLayerByName(layer1)
print(l1.GetGeometryColumn())
4
  • I have gdal installed and geopandas but I'm unable to use the Python bindings. from osgeo import ogr. When I try to do pipenv install gdal I get an installation failed error. – Sogun Apr 28 at 20:36
  • Did you try import ogr like stated in my code too? – ThomasG77 Apr 28 at 20:42
  • Also, Windows, Linux or Mac OS? Python GDAL bindings can be annoying to install depending of the OS – ThomasG77 Apr 28 at 20:45
  • import ogr just gives me a "No module named 'ogr' error". I'm on Mac OS. I just installed the python bindings myself using pipenv install GDAL==3.2.2 and it worked. One question I did have is it only worked if I run this in the Python CLI shell. If I throw this code in a python script I get a segmentation fault: zsh segmentation fault. – Sogun Apr 28 at 22:41

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.