Geographic Information Systems Asked on December 10, 2020
I have a single polyline shapefile with multiple features (row). I want to export each row to separate shapefile with using GeoPandas. But I got Geometry error in the code. This is my shapefile:
and this is the code:
Work_directory = r'P:/directory/Test/20200812'
folder_out = Work_directory + "/00_processing"
Shp_UniqueRun= Work_directory + "/SHP/UniqueRuns1.shp"
gdf = gpd.read_file(Shp_UniqueRun)
for index, row in gdf.iterrows():
s = gdf.loc[index,'UniqueRun'] #use it for the name of output shapefiles
shp_output = folder_out + "/" + str(s) + ".shp" #output shapefile
NewSHP = gpd.GeoDataFrame(row)
NewSHP.to_file(shp_output)
error:
AttributeError: No geometry data set yet (expected in column 'geometry'.)
I think the problem gdf.loc
is returning a Series and not a dataframe and NewSHP = gpd.GeoDataFrame(row)
does not generate a valid geodataframe.
Try this instead to return a one row geodataframe:
import geopandas as gpd
df = gpd.read_file('/home/bera/GIS/Data/testdata/ak_riks.shp')
for i in df.index:
subdf = df.iloc[[i]] #https://stackoverflow.com/questions/45990001/forcing-pandas-iloc-to-return-a-single-row-dataframe/45990057
subdf.to_file('/home/bera/GIS/Data/testdata/out/Outshape_{0}.shp'.format(i))
Correct answer by BERA on December 10, 2020
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP