python basemap readshapefile二三事

释放双眼,带上耳机,听听看~!

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

先是:

fig,ax = plt.subplots(figsize=(15,10))
from mpl_toolkits.basemap import Basemap
m = 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\')

  python basemap readshapefile二三事

成功了。

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

报错:

ValueError: shapefile must have lat/lon vertices  - it looks like this one has vertices
in map projection coordinates. You can convert the shapefile to geographic
coordinates 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

最后:

python basemap readshapefile二三事

大功告成!!!

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

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

给TA打赏
共{{data.count}}人
人已打赏
随笔日记

ASP.NET Core MVC 之视图组件(View Component)

2020-11-9 6:20:53

随笔日记

早盘暴涨47%尾盘暴跌51% 同仁资源是怎样的存在?

2020-11-9 6:20:55

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索