相关文章推荐

常见空间数据格式包括:文本xy坐标(包括.dat, .csv, .xls, .txt)、.shp、GeoJson、图像(包括GeoTiff等)、netCDF。以上格式数据在Python中皆可处理,不过可能需要用到多个不同的工具包,本文将介绍作者所熟悉的几种除GDAL外的Python包供读者探讨。

Macbook Pro 2011b, OSX,python3.5

本节介绍如何使用Pandas读取及简单处理文本文件存储的点坐标,并使用基于Matplotlib的CartoPy绘制地图,力图用 最简单 的方法来解决一些常见的问题。pandas用来处理文本表格数据非常舒服,推荐Mckinney著,机械工业出版社的《利用Python进行数据分析》。

  • 建议使用conda安装方式,如安装CartoPy遇见麻烦,可以通过conda-forge安装,执行代码: conda install cartopy -c conda-forge
  • 如果认为Anaconda太大,很多内置的包用不上,可参考使用Miniconda,一键安装脚本可参考 Nicola的博客
  • ax = plt.axes(projection=ccrs.PlateCarree())
    ax.set_extent([70, 140, 15, 55])
    ax.stock_img();
    ax.scatter(data['经度'],data['纬度'],s=0.3,c='g');
    
    plt.figure(figsize=(4,3))
    ax = data['气压传感器拔海高度(米)'].plot(kind='hist');
    ax.set_xlim([0,5000])
    plt.xlabel('elevation (m)');
    
    c_list = [country for country in countries if country.attributes['ADMIN']=='China']
    countries = reader.records()
    
    def get_record(key,value):
        countries = reader.records()
        result = [country for country in countries if country.attributes[key]==value]
        countries = reader.records()
        return result
    
    ax = plt.axes(projection=ccrs.PlateCarree())
    ax.set_extent([45,90,35,55])
    for r in cas:
        ax.add_geometries(r.geometry,ccrs.PlateCarree(),
                          facecolor='green',edgecolor='black',alpha=0.2,linewidth=0.5)
        ax.text(r.geometry.centroid.x,r.geometry.centroid.y,r.attributes['ADMIN'],
                horizontalalignment='center',
                verticalalignment='center',
                transform=ccrs.Geodetic())
    
    ax = plt.axes(projection=ccrs.PlateCarree())
    ax.set_extent([45,90,35,55])
    for r in cas:
        color = cm.Greens((r.attributes['POP_EST'] - pop_min) / (pop_max - pop_min))
        ax.add_geometries(r.geometry,ccrs.PlateCarree(),
                          facecolor=color,edgecolor='black',linewidth=0.5)
        ax.text(r.geometry.centroid.x,r.geometry.centroid.y,r.attributes['ADMIN'],
                horizontalalignment='center',
                verticalalignment='center',
                transform=ccrs.Geodetic())
    sm = plt.cm.ScalarMappable(cmap='Greens', norm=plt.Normalize(vmin=pop_min, vmax=pop_max))
    # fake up the array of the scalar mappable. Urgh...
    sm._A = []
    plt.colorbar(sm); 
    plt.title('Population estimates');
    ax = plt.axes(projection=ccrs.PlateCarree())
    ax.set_extent([45,90,35,55])
    for r in cas:
        color = cm.Greens((r.attributes['GDP_MD_EST'] - gdp_min) / (gdp_max - gdp_min))
        ax.add_geometries(r.geometry,ccrs.PlateCarree(),
                          facecolor=color,edgecolor='black',linewidth=0.5)
        ax.text(r.geometry.centroid.x,r.geometry.centroid.y,r.attributes['ADMIN'],
                horizontalalignment='center',
                verticalalignment='center',
                transform=ccrs.Geodetic())
    ax.set_xticks([45,55,65,75,85], crs=ccrs.PlateCarree())
    ax.set_yticks([35,45,55], crs=ccrs.PlateCarree())
    lon_formatter = LongitudeFormatter(zero_direction_label=True)
    lat_formatter = LatitudeFormatter()
    ax.xaxis.set_major_formatter(lon_formatter)
    ax.yaxis.set_major_formatter(lat_formatter)
    plt.title('GDP estimates');
    
     
    推荐文章