2
votes

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!

1
HI, what is the output of police.dtypes? And also what is the output of type(police.loc[0,'geometry'])Bob Haffner
Does police.geometry works? also try to add the file format, e.g. 'serilanka_policestations.shp'.Alz
Thank you both! Bob, the output of police.dtypes is object for everything (including geometry). Latitude and Longitude are both float64. The output of type(police.loc[0,'geometry']) is string.user7999601
Alireza, police.geometry doesn't work either, and neither does adding a ".shp" after the file name. :(user7999601
type(police.loc[0,'geometry']) shouldn't be string, but should be a shapely.geometry.Point type. So something went wrong with reading that file. Are you able to share the shapefile? And you can also open an issue at github.com/geopandas/geopandasjoris

1 Answers

2
votes

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.