예제 #1
0
def plot_polygons(regions, file_extension='png'):
    """
    extract the polygon coordinate and plot it on a worldmap

    :param regions: list of ISO abreviations for polygons

    :return png: map_graphic.png
    """

    from cartopy.io.shapereader import Reader
    from cartopy.feature import ShapelyFeature
    from numpy import mean, append

    from blackswan import config
    DIR_SHP = config.shapefiles_path()

    if type(regions) == str:
        regions = list([regions])

    fname = join(DIR_SHP, "countries.shp")
    geos = Reader(fname).geometries()
    records = Reader(fname).records()
    central_latitude = []
    central_longitude = []

    for r in records:
        geo = geos.next()
        if r.attributes['ISO_A3'] in regions:
            x, y = geo.centroid.coords.xy
            central_longitude.append(x[0])
            central_latitude.append(y[0])

    fig = plt.figure(figsize=(20, 10))
    projection = ccrs.Orthographic(central_longitude=mean(central_longitude),
                                   central_latitude=mean(central_latitude),
                                   globe=None)  # Robinson()
    ax = plt.axes(projection=projection)

    geos = Reader(fname).geometries()
    records = Reader(fname).records()

    for r in records:
        geo = geos.next()
        if r.attributes['ISO_A3'] in regions:
            shape_feature = ShapelyFeature(geo, ccrs.PlateCarree(), edgecolor='black', color='coral')
            ax.add_feature(shape_feature)
    ax.coastlines()
    ax.gridlines()
    ax.stock_img()
    # ax.set_global()
    map_graphic = fig2plot(fig=fig, file_extension=file_extension)
    plt.close()

    return map_graphic
예제 #2
0
def plot_polygons(regions):
    """
    extract the polygon coordinate and plot it on a worldmap

    :param regions: list of ISO abreviations for polygons

    :return png: map_graphic.png
    """

    from cartopy.io.shapereader import Reader
    from cartopy.feature import ShapelyFeature
    from os.path import curdir, abspath

    from flyingpigeon import config
    DIR_SHP = config.shapefiles_dir()

    if type(regions) == str:
        regions = list([regions])

    fname = join(DIR_SHP, "countries.shp")
    geos = Reader(fname).geometries()
    records = Reader(fname).records()

    LOGGER.debug('')

    fig = plt.figure(figsize=(10, 10), facecolor='w',
                     edgecolor='k')  # dpi=600,
    projection = ccrs.Orthographic(central_longitude=0.0,
                                   central_latitude=0.0,
                                   globe=None)  # Robinson()
    ax = plt.axes(projection=projection)

    for r in records:
        geo = geos.next()
        if r.attributes['ISO_A3'] in regions:
            shape_feature = ShapelyFeature(geo,
                                           ccrs.PlateCarree(),
                                           edgecolor='black')
            ax.add_feature(shape_feature)
        ax.coastlines()
        # ax.set_global()

    o1, map_graphic = mkstemp(dir=abspath(curdir), suffix='.png')
    fig.savefig(map_graphic)
    plt.close()

    return map_graphic
예제 #3
0
from flyingpigeon import config
from os.path import join
DIR_SHP = config.shapefiles_dir()

fname = join(DIR_SHP, "countries.shp")
geos = Reader(fname).geometries()
<<<<<<< HEAD
records = Reader(fname).records()
=======
countries = reader.records()
>>>>>>> factsheetgenerator

ax = plt.axes(projection=ccrs.Robinson())
for r in records:
    geo = geos.next()
<<<<<<< HEAD
    if r.attributes['ISO_A3'] in ['DEU']:
        shape_feature = ShapelyFeature(geo, ccrs.PlateCarree(), edgecolor='black')
        ax.add_feature(shape_feature)
plt.show()



=======
    shape_feature = ShapelyFeature(geo, ccrs.PlateCarree(), edgecolor='black')
    ax.add_feature(shape_feature)
# plt.save('country_polygon.png')
plt.show()