Geographic Information Systems Asked by Erich Purpur on February 2, 2021
I’m trying to convert an excel file into a Geopandas dataframe. I’m trying to use shapely and wkt to do so.
After reading the file into pandas, I have a geometry column. I’m trying to do the following to convert the geometry column to an ‘object’ data type:
from shapely import wkt
my_df['geometry'] = my_df['geometry'].apply(wkt.loads)
and I receive the following error:
WKTReadingError: Could not create geometry because of errors while reading input.
To try and diagnose what the problem is, I also tried to convert this excel file to a geodataframe, using the following code:
import geopandas
my_df = gpd.GeoDataFrame(my_df, geometry='geometry')
and I get the following error:
TypeError: Input must be valid geometry objects: MULTIPOLYGON (((1314112.145833299 1027703.927083313, 1314091.947916642 1027761.937499985, 1314232.583333299 1027811.447916642, 1314240.99999997 1027814.395833313, 1314246.739583299 1027794.468749985, 1314292.71874997 1027692.947916642, 1314282.18749997 1027689.010416642, 1314136.364583299 1027634.374999985, 1314112.145833299 1027703.927083313)))
It appears I only have one bad geometry? (Or it could just be the first bad geometry).
Can I just skip this bad one in the wkt.loads step? I could not find documentation or other examples of how to do this.
This is how I do this. Note that I remove the failed geometries using dropna:
from shapely import wkt
import pandas as pd
def wkt_loads(x):
try:
return wkt.loads(x)
except Exception:
return None
df = pd.DataFrame(data=dict(wkt_geom=[
'POINT (10 10)',
'bad geometry',
'POINT (20 20)',
]))
df['geometry'] = df.wkt_geom.apply(wkt_loads)
df = df.dropna(subset=['geometry'])
Answered by JTh on February 2, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP