0

I have these shapefiles:

q=gpd.read_file(a.shp)
df=gpd.read_file(b.shp)
final = pd.merge(df, q, on=['FID_buffer'], how='inner')
#out= a certain directory
final.to_file(out)

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

I want after the join to export it back as shp

I tried with spatial join in geopandas but it said something with crs was wrong that's why I did with pandas but I can't export it.

final = gpd.sjoin(df, q, how="inner", op='intersects')

which gives:

AttributeError: 'DataFrame' object has no attribute 'crs'
4
  • Yes, that where I read about the second way which gives the crs error. How can I do what I am trying here and work? Thanks Aug 29, 2018 at 12:59
  • As I get it, I think is by attribute just like I did it with pandas but be able to export later. Aug 29, 2018 at 13:03
  • When you join by attribute (so a merge with pandas), the result will be a pandas DataFrame and will have two columns with geometry data (one of each original GeoDataFrame). Therefore, you need to explicitly convert it back to a GeoDataFrame specifying which geometry column should be used: GeoDataFrame(final, geometry='name_of_geometry_name')
    – joris
    Aug 29, 2018 at 13:09
  • Thanks. If you want make an answer about it and add more details. So people can see it better. Aug 29, 2018 at 13:21

1 Answer 1

1

When joining 2 GeoDataFrames by an attribute (so a merge with pandas and not a spatial join, which can certainly be sensible for a given application), the result will be a pandas DataFrame and not a GeoDataFrame.
It will also have two columns with geometry data (one of each original GeoDataFrame), and you will need to decide which of those columns you want to have as the 'active' geometry column (i.e. the column that is used for the spatial methods called on that GeoDataFrame).

Therefore, you need to explicitly convert it back to a GeoDataFrame specifying which geometry column should be used:

GeoDataFrame(final, geometry='name_of_geometry_name')
5
  • I asked the question. I wasn't on my pc and I created another account which I don't have anymore. I upvoted though. I wanted to ask is it safer to do spatial join with geopandas instead? How can you escape the crs problem with spatial join? Thanks
    – user122244
    Aug 30, 2018 at 7:48
  • It is not "safer" or "less safe" to do a spatial join with geopandas instead of a standard join in pandas. It is a different operation, and so it depends on your use case and desired result which one of the two you should use.
    – joris
    Aug 30, 2018 at 9:47
  • The error message about the crs indicates that the df you pass to the sjoin method is not a GeoDataFrame, but a normal pandas DataFrame. A spatial join needs two GeoDataFrames.
    – joris
    Aug 30, 2018 at 9:48
  • BTW, I think you can ask for a password reset for an account (as long as you have access to the email-adress)
    – joris
    Aug 30, 2018 at 9:49
  • So because one of the shapefiles lost the geometry column, it turned the geodataframe to just dataframe? Thus it could not perform the spatial join.
    – user122244
    Aug 30, 2018 at 9:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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