Search code examples
pythonpostgisspatialgeopandas

ValueError: Query missing geometry column 'geom'


I am trying to count the area of the area that is less than 500 units away from the shortest line connecting factory A and station B. I am trying to query the PostGIS spatial database in Python, but I get an error:

ValueError: Query missing geometry column 'geom'

My code:

sql = """SELECT ST_area(St_buffer(st_ShortestLine(factory.geom, station.geom), 500)) AS area FROM factory, station WHERE station.names='B' AND factory.name='A';"""
area = gpd.read_postgis(sql=sql, con=con, geom_col='geom')

The query works fine in PgAdmin.


Solution

  • The geopandas data frame requires a geometry field I had the same problem I solved it by adding the geom field from the table I wanted to filter.

    The parameter geom_col='geometry' allows you to specify the name of this column that geopandas needs. In your case geom.

    Need to add geometries (SELECT table_name.geom).

    For example:

    SELECT your_query_with_out_geom, table_name.geom FROM table_name;