I am trying to read shapefiles. I have handled the error that could occur when reading a shapefile by writing a function read_gdf
(see code below) but I am still getting an error UnicodeDecodeError: 'utf-8' codec can't...
from pathlib import Path
def read_gdf(file:str,crs:str,current_crs:str)->gpd.GeoDataFrame:
#Read data
collection = list(fiona.open(file,'r'))
df1 = pd.DataFrame(collection)
df1['isvalid'] = df1['geometry'].apply(lambda x: isvalid(x))
df1 = df1[df1['isvalid'] == 1]
collection = json.loads(df1.to_json(orient='records'))
#Convert to geodataframe
return gpd.GeoDataFrame.from_features(collection, crs=current_crs).to_crs(crs)
gdfs = []
for file in Path(rootdir).rglob("*.shp"):
print('file is', file)
gdf = read_gdf(file, crs,current_crs=current_crs)
gdf.columns = map(str.lower, gdf.columns)
gdfs.append(gdf)
How can I ignore the line that has the error and continue reading the rest of the shapefile?
encoding=”iso-8859-1”
i manage to read the shape file. However, will this encoding damage the reading of the geometry? Why does it not work with the default encoding which isUTF-8
? what is the difference betweenUTF-8
and”iso-8859-1”