Basemap readshapefile ValueError

回眸只為那壹抹淺笑 提交于 2019-12-03 17:37:58
paulperry

The problem is that these cb_ files are lists of shapely 3D PolygonZ objects, and readshapefile needs them to be 2D Polygon objects, even if the Z dimension is all 0's, as is the case with these cb_* files. You can convert them by stripping the Z dimension.

I started to use geopandas as a wrapper around basemap and other utilities and this is how I converted them:

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)

Install GeoPandas with pip install geopandas. I think that should be it!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!