question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:open
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
georgebvcommented, Sep 2, 2021

Yeah, gladly - I’ll link a PR

0reactions
georgebvcommented, Sep 2, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

18. Geography — Introduction to PostGIS
Geographic coordinates do not represent a linear distance from an origin as ... with a geography column is much like that for creating...
Read more >
ST_Intersects - PostGIS
Description. Compares two geometries and returns true if they intersect. Geometries intersect if they have any point in common. For geography, a distance ......
Read more >
Chapter 15. PostGIS Special Functions Index
15.8. PostGIS Functions that support 3D ; AddGeometryColumn - Adds a geometry column to an existing table. ; Box3D - Returns a BOX3D...
Read more >
Chapter 4. Data Management - PostGIS
Creating a spatial index works the same as for geometry columns. PostGIS will note that the column type is GEOGRAPHY and create an...
Read more >
9. Geometries — Introduction to PostGIS
f_geometry_column is the name of the column that geometry containing column – for feature ... Do some or all of your nyc tables...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found