0

I have been struggling with a problem related to Getting AttributeError: 'DataFrame' object has no attribute 'to_file' from GeoPandas even though file converted into GeoDataframe instead of Dataframe but it is not exactly the same.

I created a point geopandas dataframe and saved it to a shapefile without a problem

pnt_data = {'geometry': [<shapely.geometry.point.Point object at 0x7fa9820ffd10>, <shapely.geometry.point.Point object at 0x7fa95739ea90>,...

gp_df = gpd.GeoDataFrame(pnt_data, crs=crs)
gp_df.to_file(path_to_file, driver="ESRI Shapefile")

Now I want to save a line shapefile with the same points. Based on what I've read, I tried the following:

lineCoords = [(500191.6, 7656332.248), (500222.84883889696,
               7656358.309988857),...]
df = gpd.GeoDataFrame(lineCoords, crs=crs)

which got me the following dataframe

                                 geometry
0                  (500191.6, 7656332.248)
1  (500222.84883889696, 7656358.309988857)
2   (500238.1988799781, 7656371.130918575)
3  (500254.99698246736, 7656381.985583601)
4    (500273.2863161564, 7656390.07880751)
5  (500292.37426232523, 7656396.049595658)
6  (500312.1612412515, 7656398.9608608475)
7  (500331.89439794194, 7656402.217013037)
8  (500351.87729180406, 7656403.044027512)
9  (500371.78081168584, 7656401.081912234)

The only attribute that interests me are the coordinates and they are there. However, when I tried to save this to a shapefile using the same procedure I used for points

gp_df.to_file(path_to_file, driver="ESRI Shapefile")

and got the error message

AttributeError: 'Series' object has no attribute 'has_z'

Based on a similar problem I found that the solution could have been

gp_df['geometry'].to_file(filename, driver="ESRI Shapefile")

At this point I got the error in the title:

AttributeError: 'Series' object has no attribute 'to_file'

I also tried to convert the list of tuples with the coordinates to a linestring and do the same procedure

finalData = LineString (lineCoords)
gp_df = gpd.GeoDataFrame(finalData, crs=crs)

In this case I got

ValueError: DataFrame constructor not properly called!

Some recommended to use the raw list instead of the linestring, which was why I started the path above.

2

Looks like I was creating the dataframe with a wrong shape. The way it worked for me was to make sure the vertices were contained within a single object

finalLine_data = {'id': [1], 'geometry':[LineString(lineCoords)]}
gp_df = gpd.GeoDataFrame(finalLine_data, crs=crs)
gp_df['geometry'].to_file(path_to_file, driver="ESRI Shapefile")

The finalLine_data looks like this

{'id': [1], 'geometry': [<shapely.geometry.linestring.LineString object at 0x7f42af79c290>]}
   id                                           geometry
0   1  LINESTRING (500191.600 7656332.248, 500223.849...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.