1

The primary answer to Adding GeoPandas Dataframe to PostGIS table? requires entering the geodataframe geometry column's integer SRID.

Getting the CRS attribute using the crs attribute like so

import geopandas as gpd
file_path = 'my_geodata.shp'

gdf = gpd.read_file(file_path)
geom_srid = gdf.geometry.crs['init']

gives a string like 'epsg:32616'.

The integer value can be extracted using regex:

geom_srid_num = re.search(r'\d+$', geom_srid)

But I'm not sure if the SRID is written in this format all the time, and if this method is reliable. Is there a more appropriate way to retrieve the integer SRID for a geodataframe geometry column?

3

In geopandas <= v0.6.3., gdf.crs returns a dictionary like {'init': 'epsg:EPSG_CODE'}. So, more appropriate way is to use tools of geopandas defined in geopandas.tools module.

geom_srid_num  = gpd.tools.crs.epsg_from_crs(gdf.crs)
print(geom_srid_num) # OUT: 32616 -> int

EDIT: As @snowman2 states in comment, epsg_from_crs is deprecated in geopandas v0.7.0. In this version gdf.crs returns pyproj.crs.CRS instance. Thus, the most appropriate way is to use to_epsg() method of CRS class.

gdf = gpd.read_file(file_path)
geom_srid_num  = gdf.crs.to_epsg()
print(geom_srid_num) # OUT: 32616 -> int
| improve this answer | |
  • 1
    That method is currently deprecated in geopandas master - will be version 0.7.0. I would recommend updating your suggestion following this logic: github.com/geopandas/geopandas/blob/… – snowman2 Feb 16 at 15:23
  • @snowman2 I've updated the answer. Thank you for your warning. I've updated my knowledge, too. I will update the answer when v0.7.0 is released. – Kadir Şahbaz Feb 16 at 20:13
  • I was using geopandas 0.4.0, which didn't have the newer method but it's good to know that it exists in the current version. – rovyko Feb 19 at 4:18

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.