今天要用到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')
成功了。
可再用另一中國地圖來加載時就出錯了:
報錯:
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
最后:
大功告成!!!
最后在解決這個問題過程中發現一個資源: