python basemap readshapefile二三事,,今天要用到basem


今天要用到basemap读取shp文件报错,查了很多资料,都没有解决。

先是:

fig,ax = plt.subplots(figsize=(15,10))from mpl_toolkits.basemap import Basemapm = Basemap(llcrnrlon=-128.,llcrnrlat=30.,urcrnrlon=-110.,urcrnrlat=43.,            projection=‘merc‘,lat_1=20.,lat_2=40.,lon_0=-60.,            resolution =‘l‘,area_thresh=1000.,ax = ax)fm = np.vectorize(m,otypes=[np.int32,np.int32])x,y = fm(housing.longitude,housing.latitude)m.scatter(x,y,s=housing[‘population‘]/50,alpha=0.4,         label=‘population‘,c=housing[‘median_house_value‘],          cmap=plt.get_cmap(‘jet‘))m.readshapefile(‘World_GIS_data/country‘,‘country‘)

  技术图片

成功了。

可再用另一中国地图来加载时就出错了:

报错:

ValueError: shapefile must have lat/lon vertices  - it looks like this one has verticesin map projection coordinates. You can convert the shapefile to geographiccoordinates using the shpproj utility from the shapelib tools(http://shapelib.maptools.org/shapelib-tools.html)我

我按着提示去找了shapelib,源码下载到了,需要编译。

我也没有vc++环境,太麻烦了,也不知道能成不。

想了好多办法都没成功,包括用pyshpelib,这个也还是要用到上面的shapelib。

然后找到了:

https://github.com/matplotlib/basemap/issues/285

说可以用geopandas来处理。

然后就是安装,这又是一个坑。

安装数次报错,找不着门道。

最后看到一篇文章。该作者可能也被坑惨了,总结写的很好(墙裂推荐):

https://geoffboeing.com/2014/09/using-geopandas-windows/

跟着上面这篇文章的终于将geopandas安装好了

然后改shapefile的crs为空字典。本来以为就好了,结果还是不行!!!

还是不行!

然后又看到这篇下面的解答,提到了ogr2ogr

https://stackoverflow.com/questions/54508676/convert-a-shapefile-to-geographic-coordinates

弄半天才明白的是安装的GDAL中的可执行文件。

后面又找到一个库,调用的ogr2ogr:

地址:

https://github.com/charlie1347/basemaps

最后:

技术图片

大功告成!!!

最后在解决这个问题过程中发现一个资源:

http://geoportal.statistics.gov.uk/datasets/nuts-level-1-january-2018-full-extent-boundaries-in-the-united-kingdom/data

python basemap readshapefile二三事

评论关闭