How to convert postgres geometry to wkt geometry using sqlalchemy in python

Viewed 22

I am trying to get a table from postgres which have a geometry column in it using sqlalchemy. Though I got the desired table I am not able to convert the postgres encoded geometry into wkt geometry format. I tried using to_shape from geoalchemy2 library but it throws an error like below. What is the correct way of converting the encoded geometry into wkt geometry.

db_url = "URL";
engine = create_engine(db_url)
connection = engine.connect()
sql = """select * from table;"""
output= connection.execute(sqlalchemy.text(sql)).fetchall()
df = DataFrame(output)
     id              geometry   
0   1112    0102000020E610000002000000B89AD07F6A17C5BF630E...   
1   1014    0102000020E610000002000000B89AD07F6A17C5BF630E...   
2   1013    0102000020E610000002000000B89AD07F6A17C5BF630E...   

Now, trying to convert the encoded geometry into wkt geometry

from geoalchemy2.shape import to_shape

for col in df:
    print(to_shape(col.geometry))

Error:

AttributeError: 'str' object has no attribute 'geometry'
0 Answers
Related