Wen
Wen

Reputation: 45

GeoPandas plot function not working

I have a shapefile that looks like this on mapshaper: mapshaper

But when I tried to plot it in pandas with the following code

police = gpd.read_file('srilanka_policestations')
police.plot()

jupyter notebook gives me an error message saying "AttributeError: 'str' object has no attribute 'type'".

I'm not sure what's wrong. I tried to plot the GeoPandas dataset "naturalearth_cities", and it works fine. See below:

jupyter notebook

The geo-dataframe reads fine in pandas, but it wouldn't plot: enter image description here

Any help is much, much appreciated. Thank you all!

Upvotes: 2

Views: 2412

Answers (1)

joris
joris

Reputation: 139312

The geometry column in a geopandas GeoDataFrame should not contain strings, but actual geometry objects. If you have strings, you can convert them as follows:

import shapely.wkt
police['geometry'] = police['geometry'].apply(shapely.wkt.loads)

However, I have to say that the above is a workaround, as if you used geopandas read_file to read a shapefile, you should not end up with strings.

Upvotes: 2

Related Questions