底图readshapefile ValueError [英] Basemap readshapefile ValueError
问题描述
我从美国人口普查中下载了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
更具体地说,这些文件给了我错误.如您所见,我下载了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)
问题:
- 我是否需要通过
Fiona
进行转换?还是ArcGIS
?为了 将其更改为正确的格式. - 有没有比
basemap
更好的选择?
问题是这些cb_文件是形状为3D PolygonZ对象的列表,即使Z维度全为0,readshapefile也需要它们是2D Polygon对象. ,就像这些cb_*
文件一样.您可以通过去除Z维度对其进行转换.>
我开始使用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.我认为应该是这样!
I downloaded a map from US Census in shapefile format. It has all the required information that I need, but for som reason there's a specific map that I need it's giving me this error:
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
More specifically these set of files give me the error. As you can see, I downloaded the 5m
resolution shapefile.
This is the code that I'm using to execute the command:
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)
Questions:
- Do I need to convert this through
Fiona
? orArcGIS
? in order to change it to the proper format. - Is there a better alternative to
basemap
?
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!
这篇关于底图readshapefile ValueError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!