Skip to content

Commit

Permalink
Made read_postgis raise ValueError if the geom_col is specified twice…
Browse files Browse the repository at this point in the history
… (current error is cryptic) (#2849)
  • Loading branch information
iboates committed May 1, 2023
1 parent cd844e6 commit 0a7010b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
6 changes: 6 additions & 0 deletions geopandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ def _df_to_geodf(df, geom_col="geom", crs=None):
if geom_col not in df:
raise ValueError("Query missing geometry column '{}'".format(geom_col))

if df.columns.to_list().count(geom_col) > 1:
raise ValueError(
f"Duplicate geometry column '{geom_col}' detected in SQL query output. Only"
"one geometry column is allowed."
)

geoms = df[geom_col].dropna()

if not geoms.empty:
Expand Down
11 changes: 11 additions & 0 deletions geopandas/io/tests/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -734,3 +734,14 @@ def test_append_with_different_crs(self, engine_postgis, df_nybb):
# Should raise error when appending
with pytest.raises(ValueError, match="CRS of the target table"):
write_postgis(df_nybb2, con=engine, name=table, if_exists="append")

def test_duplicate_geometry_column_fails(self, engine_postgis):
"""
Tests that a ValueError is raised if an SQL query returns two geometry columns.
"""
engine = engine_postgis

sql = "select ST_MakePoint(0, 0) as geom, ST_MakePoint(0, 0) as geom;"

with pytest.raises(ValueError):
read_postgis(sql, engine, geom_col="geom")

0 comments on commit 0a7010b

Please sign in to comment.