ValueError: Query missing geometry column 'geom'

Question:

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.

Asked By: kris1994

||

Answers:

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;
Answered By: Szymon Cogiel

This query worked for me:

 sql = "SELECT your_query_without_geom name_of_geometry_col as geom FROM table_name;
Answered By: Magnus
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.