【问题标题】:GeoPandas plot function not workingGeoPandas 绘图功能不起作用
【发布时间】:2017-08-17 15:57:32
【问题描述】:

我有一个在 mapshaper 上看起来像这样的 shapefile:

但是当我尝试使用以下代码在 pandas 中绘制它时

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

jupyter notebook 给我一条错误消息,提示“AttributeError: 'str' object has no attribute 'type'”。

我不确定出了什么问题。我试图绘制 GeoPandas 数据集“naturalearth_cities”,它工作正常。见下文:

地理数据框在 pandas 中读取良好,但无法绘制:

任何帮助都非常非常感谢。谢谢你们!

【问题讨论】:

  • 嗨,police.dtypes 的输出是什么?还有type(police.loc[0,'geometry'])的输出是什么@
  • police.geometry 有效吗?也尝试添加文件格式,例如'serilanka_policestations.shp'.
  • 谢谢你们! Bob,police.dtypes 的输出是一切(包括几何)的对象。纬度和经度都是 float64。 type(police.loc[0,'geometry']) 的输出是字符串。
  • Alireza, police.geometry 也不起作用,在文件名后添加“.shp”也不起作用。 :(
  • type(police.loc[0,'geometry']) 不应该是字符串,而应该是 shapely.geometry.Point 类型。所以读取那个文件出了点​​问题。你能分享shapefile吗?你也可以在github.com/geopandas/geopandas 上打开一个问题

标签: python pandas geopandas


【解决方案1】:

geopandas GeoDataFrame 中的几何列不应包含字符串,而是实际的几何对象。 如果有字符串,可以按如下方式进行转换:

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

但是,我不得不说,上面是一个解决方法,就像你使用 geopandas read_file 来读取一个shapefile一样,你不应该以字符串结尾。

【讨论】:

    猜你喜欢
    • 2019-05-01
    • 2022-11-12
    • 2022-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多