【问题标题】:Basemap readshapefile ValueError底图读取形状文件 ValueError
【发布时间】:2015-10-29 15:02:30
【问题描述】:

我从 US Census 下载了 shapefile 格式的地图。它包含我需要的所有必需信息,但出于某种原因,我需要一张特定的地图,它给了我这个错误:

Traceback (most recent call last):
  File "C:/Users/Leb/Desktop/Python/Kaggle/mapp.py", line 17, in <module>
    shp_info = m.readshapefile('gis/cb_2014_us_state_5m', 'states', drawbounds=True)
  File "C:\Program Files\Python 3.5\lib\site-packages\mpl_toolkits\basemap\__init__.py", line 2162, in readshapefile
    raise ValueError('readshapefile can only handle 2D shape types')
ValueError: readshapefile can only handle 2D shape types

更具体地说,these 文件集给了我错误。如您所见,我下载了5m 分辨率shapefile。

这是我用来执行命令的代码:

import matplotlib.pyplot as plt
from mpl_toolkits.basemap import Basemap as Basemap

m = Basemap(llcrnrlon=-119, llcrnrlat=22, urcrnrlon=-64, urcrnrlat=49,
            projection='lcc', lat_1=33, lat_2=45, lon_0=-95)
shp_info = m.readshapefile('gis/cb_2014_us_state_5m', 'states', drawbounds=True)

问题:

  1. 我需要通过Fiona 转换它吗?或ArcGIS?为了 将其更改为正确的格式。
  2. 有没有更好的替代basemap

【问题讨论】:

  • 你解决了吗?

标签: python matplotlib matplotlib-basemap


【解决方案1】:

问题在于这些 cb_ 文件是形状良好的 3D PolygonZ 对象的列表,并且 readshapefile 需要它们是 2D Polygon 对象,即使 Z 维度全为 0,就像这些 cb_* 文件一样。你可以convert them by stripping the Z dimension

我开始使用 geopandas 作为底图和其他实用程序的包装器,这就是我转换它们的方式:

def convert_3D_2D(geometry):
    '''
    Takes a GeoSeries of Multi/Polygons and returns a list of Multi/Polygons
    '''
    import geopandas as gp
    new_geo = []
    for p in geometry:
        if p.has_z:
            if p.geom_type == 'Polygon':
                lines = [xy[:2] for xy in list(p.exterior.coords)]
                new_p = Polygon(lines)
                new_geo.append(new_p)
            elif p.geom_type == 'MultiPolygon':
                new_multi_p = []
                for ap in p:
                    lines = [xy[:2] for xy in list(ap.exterior.coords)]
                    new_p = Polygon(lines)
                    new_multi_p.append(new_p)
                new_geo.append(MultiPolygon(new_multi_p))
    return new_geo

import geopandas as gp
some_df = gp.from_file('your_cb_file.shp')
some_df.geometry = convert_3D_2D(cbsa.geometry)

使用pip install geopandas 安装 GeoPandas。我觉得应该是这样!

【讨论】:

  • 这对我不起作用,from_file geopandas 不再支持它
猜你喜欢
  • 1970-01-01
  • 2023-03-13
  • 2014-04-12
  • 2015-11-24
  • 2017-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多