ENH: to_postgis when column is Geography and not Geometry
See original GitHub issue-
I have checked that this issue has not already been reported.
-
I have confirmed this bug exists on the latest version of geopandas.
-
(optional) I have confirmed this bug exists on the master branch of geopandas.
Note: Please read this guide detailing how to provide the necessary information for us to reproduce your bug.
Code Sample, a copy-pastable example
# Your code here
df = pandas.read_csv("file.csv")
gdf = GeoDataFrame(df, geometry=points_from_xy(df.lon, df.lat), crs="epsg:4326")
gdf.to_postgis("table_name", con=engine, if_exists="append")
Problem description
I am having a problem with the following:
/usr/local/lib/python3.9/site-packages/geopandas/io/sql.py in _write_postgis(gdf, name, con, schema, if_exists, index, index_label, chunksize, dtype)
412 # Only check SRID if table exists
413 if connection.dialect.has_table(connection, name, schema):
--> 414 target_srid = connection.execute(
415 "SELECT Find_SRID('{schema}', '{table}', '{geom_col}');".format(
416 schema=schema_name, table=name, geom_col=geom_name
sqlalchemy.exc.InternalError: (psycopg2.errors.RaiseException) find_srid() - could not find the corresponding SRID - is the geometry registered in the GEOMETRY_COLUMNS table? Is there an uppercase/lowercase mismatch?
CONTEXT: PL/pgSQL function find_srid(character varying,character varying,character varying) line 17 at RAISE
[SQL: SELECT Find_SRID('public', 'table_name', 'geom');]
My PostGIS table uses Geography type (the geom
column), not Geometry. Geopandas is trying to run Find_SRID
function which is not applicable for Geography types - it looks for SRID in GEOMETRY_COLUMNS, while Geography is stored in GEOGRAPHY_COLUMNS. Geopandas should use a custom query for Geography types.
Expected Output
Upload should work for Geography same as for Geometry.
Output of geopandas.show_versions()
SYSTEM INFO
python : 3.9.7 (default, Aug 31 2021, 18:43:40) [GCC 8.3.0] executable : /usr/local/bin/python machine : Linux-4.19.128-microsoft-standard-x86_64-with-glibc2.28
GEOS, GDAL, PROJ INFO
GEOS : 3.7.1 GEOS lib : /usr/lib/x86_64-linux-gnu/libgeos_c.so GDAL : 3.3.0 GDAL data dir: /usr/local/lib/python3.9/site-packages/fiona/gdal_data PROJ : 8.0.1 PROJ data dir: /usr/local/lib/python3.9/site-packages/pyproj/proj_dir/share/proj
PYTHON DEPENDENCIES
geopandas : 0.9.0 pandas : 1.3.2 fiona : 1.8.20 numpy : 1.21.1 shapely : 1.7.1 rtree : 0.9.7 pyproj : 3.1.0 matplotlib : 3.4.3 mapclassify: None geopy : None psycopg2 : 2.9.1 (dt dec pq3 ext lo64) geoalchemy2: 0.9.4 pyarrow : None pygeos : None
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (2 by maintainers)
Yeah, gladly - I’ll link a PR
Created a PR here https://github.com/geopandas/geopandas/pull/2096