コード例 #1
0
ファイル: mpl_handler.py プロジェクト: moghimis/sci-wms
def tricontouring_response(tri_subset, data, request, dpi=None):
    """
    triang_subset is a matplotlib.Tri object in lat/lon units (will be converted to projected coordinates)
    xmin, ymin, xmax, ymax is the bounding pox of the plot in PROJETED COORDINATES!!!
    request is the original getMap request object
    """

    dpi = dpi or 80.

    bbox = request.GET['bbox']
    width = request.GET['width']
    height = request.GET['height']
    colormap = request.GET['colormap']
    colorscalerange = request.GET['colorscalerange']
    cmin = colorscalerange.min
    cmax = colorscalerange.max
    crs = request.GET['crs']
    nlvls = request.GET['numcontours']

    EPSG4326 = pyproj.Proj(init='EPSG:4326')
    tri_subset.x, tri_subset.y = pyproj.transform(EPSG4326, crs, tri_subset.x,
                                                  tri_subset.y)

    fig = Figure(dpi=dpi, facecolor='none', edgecolor='none')
    fig.set_alpha(0)
    fig.set_figheight(height / dpi)
    fig.set_figwidth(width / dpi)

    ax = fig.add_axes([0., 0., 1., 1.], xticks=[], yticks=[])
    ax.set_axis_off()

    if request.GET['logscale'] is True:
        norm_func = mpl.colors.LogNorm
    else:
        norm_func = mpl.colors.Normalize

    # Set out of bound data to NaN so it shows transparent?
    # Set to black like ncWMS?
    # Configurable by user?
    if cmin is not None and cmax is not None:
        data[data > cmax] = cmax
        data[data < cmin] = cmin
        lvls = np.linspace(cmin, cmax, nlvls)
        norm = norm_func(vmin=cmin, vmax=cmax)
    else:
        lvls = nlvls
        norm = norm_func()

    if request.GET['image_type'] == 'filledcontours':
        ax.tricontourf(tri_subset, data, lvls, norm=norm, cmap=colormap)
    elif request.GET['image_type'] == 'contours':
        ax.tricontour(tri_subset, data, lvls, norm=norm, cmap=colormap)

    ax.set_xlim(bbox.minx, bbox.maxx)
    ax.set_ylim(bbox.miny, bbox.maxy)
    ax.set_frame_on(False)
    ax.set_clip_on(False)
    ax.set_position([0., 0., 1., 1.])

    return figure_response(fig, request)
コード例 #2
0
ファイル: glg_handler.py プロジェクト: moghimis/sci-wms
def filledcontour(request):
    # Create figure
    fig, ax, norm = create_axis(request, get_position(request))

    orientation = 'vertical'
    if request.GET['horizontal'] is True:
        orientation = 'horizontal'
    csr = request.GET['colorscalerange']

    if request.GET['logscale'] is True:
        levs = np.hstack(
            ([csr.min - 3],
             np.linspace(csr.min, csr.max,
                         request.GET['numcontours']), [csr.max + 40]))
        x, y = np.meshgrid(np.ones(2), np.ones(2))
        cs = ax.contourf(x,
                         y,
                         x,
                         levels=levs,
                         norm=norm,
                         cmap=get_cmap(request.GET['colormap']))
        cb = colorbar(mappable=cs,
                      cax=ax,
                      orientation=orientation,
                      spacing='proportional',
                      extendrect=False,
                      use_gridspec=True)
        if request.GET['showvalues'] is False:
            cb.set_ticks([])
        else:
            cb.set_ticks(levs[1:-1])
            cb.set_ticklabels(["%.1f" % x for x in levs[1:-1]])

    else:
        levs = np.linspace(csr.min, csr.max, request.GET['numcontours'])
        x, y = np.meshgrid(np.ones(2), np.ones(2))
        cs = ax.contourf(x,
                         y,
                         x,
                         levels=levs,
                         norm=norm,
                         cmap=get_cmap(request.GET['colormap']),
                         extend='both')
        cb = colorbar(mappable=cs,
                      cax=ax,
                      orientation=orientation,
                      spacing='proportional',
                      extendrect=False,
                      use_gridspec=True)
        if request.GET['showvalues'] is False:
            cb.set_ticks([])
        else:
            cb.set_ticks(levs)
            cb.set_ticklabels(["%.1f" % x for x in levs])

    if request.GET['showlabel'] is True:
        cb.set_label(request.GET['units'])

    # Return HttpResponse
    return figure_response(fig, request)
コード例 #3
0
ファイル: glg_handler.py プロジェクト: moghimis/sci-wms
def gradiant(request):
    # Create figure
    fig, ax, norm = create_axis(request, get_position(request))

    orientation = 'vertical'
    if request.GET['horizontal'] is True:
        orientation = 'horizontal'
    cb = matplotlib.colorbar.ColorbarBase(ax,
                                          cmap=get_cmap(
                                              request.GET['colormap']),
                                          norm=norm,
                                          orientation=orientation)

    if request.GET['showvalues'] is False:
        cb.set_ticks([])
    else:
        csr = request.GET['colorscalerange']
        ticks = np.linspace(csr.min, csr.max, 5)
        cb.set_ticks(ticks)
        cb.set_ticklabels(["%.1f" % x for x in ticks])

    if request.GET['showlabel'] is True:
        cb.set_label(request.GET['units'])

    # Return HttpResponse
    return figure_response(fig, request)
コード例 #4
0
ファイル: mpl_handler.py プロジェクト: moghimis/sci-wms
def pcolormesh_response(lon, lat, data, request, dpi=None):

    dpi = dpi or 80.

    bbox, width, height, colormap, cmin, cmax, crs = _get_common_params(
        request)

    EPSG4326 = pyproj.Proj(init='EPSG:4326')
    x, y = pyproj.transform(EPSG4326, crs, lon, lat)
    fig = Figure(dpi=dpi, facecolor='none', edgecolor='none')
    fig.set_alpha(0)
    fig.set_figheight(height / dpi)
    fig.set_figwidth(width / dpi)
    ax = fig.add_axes([0., 0., 1., 1.], xticks=[], yticks=[])
    ax.set_axis_off()

    if request.GET['logscale'] is True:
        norm_func = mpl.colors.LogNorm
    else:
        norm_func = mpl.colors.Normalize

    if cmin is not None and cmax is not None:
        data[data > cmax] = cmax
        data[data < cmin] = cmin
        norm = norm = norm_func(vmin=cmin, vmax=cmax)
    else:
        norm = norm_func()

    masked = np.ma.masked_invalid(data)
    ax.pcolormesh(x, y, masked, norm=norm, cmap=colormap)
    ax.set_xlim(bbox.minx, bbox.maxx)
    ax.set_ylim(bbox.miny, bbox.maxy)
    ax.set_frame_on(False)
    ax.set_clip_on(False)
    ax.set_position([0., 0., 1., 1.])

    return figure_response(fig, request)
コード例 #5
0
ファイル: mpl_handler.py プロジェクト: moghimis/sci-wms
def contouring_response(lon, lat, data, request, dpi=None):

    dpi = dpi or 80.

    bbox, width, height, colormap, cmin, cmax, crs = _get_common_params(
        request)
    nlvls = request.GET['numcontours']

    EPSG4326 = pyproj.Proj(init='EPSG:4326')
    x, y = pyproj.transform(EPSG4326, crs, lon, lat)

    fig = Figure(dpi=dpi, facecolor='none', edgecolor='none')
    fig.set_alpha(0)
    fig.set_figheight(height / dpi)
    fig.set_figwidth(width / dpi)

    ax = fig.add_axes([0., 0., 1., 1.], xticks=[], yticks=[])
    ax.set_axis_off()

    if request.GET['logscale'] is True:
        norm_func = mpl.colors.LogNorm
    else:
        norm_func = mpl.colors.Normalize

    if cmin is not None and cmax is not None:
        data[data > cmax] = cmax
        data[data < cmin] = cmin
        lvls = np.linspace(cmin, cmax, nlvls)
        norm = norm_func(vmin=cmin, vmax=cmax)
    else:
        lvls = nlvls
        norm = norm_func()

    if request.GET['image_type'] == 'filledcontours':
        ax.contourf(x, y, data, lvls, norm=norm, cmap=colormap)
    elif request.GET['image_type'] == 'contours':
        ax.contour(x, y, data, lvls, norm=norm, cmap=colormap)
    elif request.GET['image_type'] == 'filledhatches':
        hatches = DEFAULT_HATCHES[:nlvls]
        ax.contourf(x,
                    y,
                    data,
                    lvls,
                    norm=norm,
                    cmap=colormap,
                    hatches=hatches)
    elif request.GET['image_type'] == 'hatches':
        hatches = DEFAULT_HATCHES[:nlvls]
        ax.contourf(x,
                    y,
                    data,
                    lvls,
                    norm=norm,
                    colors='none',
                    hatches=hatches)

    ax.set_xlim(bbox.minx, bbox.maxx)
    ax.set_ylim(bbox.miny, bbox.maxy)
    ax.set_frame_on(False)
    ax.set_clip_on(False)
    ax.set_position([0., 0., 1., 1.])

    return figure_response(fig, request)
コード例 #6
0
ファイル: mpl_handler.py プロジェクト: moghimis/sci-wms
def quiver_response(lon, lat, dx, dy, request, dpi=None):

    dpi = dpi or 80.

    bbox = request.GET['bbox']
    width = request.GET['width']
    height = request.GET['height']
    colormap = request.GET['colormap']
    colorscalerange = request.GET['colorscalerange']
    vectorscale = request.GET['vectorscale']
    cmin = colorscalerange.min
    cmax = colorscalerange.max
    crs = request.GET['crs']
    unit_vectors = None  # We don't support requesting these yet, but wouldn't be hard

    EPSG4326 = pyproj.Proj(init='EPSG:4326')
    x, y = pyproj.transform(EPSG4326, crs, lon,
                            lat)  # TODO order for non-inverse?

    fig = Figure(dpi=dpi, facecolor='none', edgecolor='none')
    fig.set_alpha(0)
    fig.set_figheight(height / dpi)
    fig.set_figwidth(width / dpi)

    ax = fig.add_axes([0., 0., 1., 1.], xticks=[], yticks=[])
    ax.set_axis_off()
    mags = np.sqrt(dx**2 + dy**2)

    cmap = mpl.cm.get_cmap(colormap)

    if request.GET['logscale'] is True:
        norm_func = mpl.colors.LogNorm
    else:
        norm_func = mpl.colors.Normalize

    # Set out of bound data to NaN so it shows transparent?
    # Set to black like ncWMS?
    # Configurable by user?
    if cmin is not None and cmax is not None:
        mags[mags > cmax] = cmax
        mags[mags < cmin] = cmin
        norm = norm_func(vmin=cmin, vmax=cmax)
    else:
        norm = norm_func()

    # plot unit vectors
    if unit_vectors:
        ax.quiver(x,
                  y,
                  dx / mags,
                  dy / mags,
                  mags,
                  cmap=cmap,
                  norm=norm,
                  scale=vectorscale)
    else:
        ax.quiver(x, y, dx, dy, mags, cmap=cmap, norm=norm, scale=vectorscale)

    ax.set_xlim(bbox.minx, bbox.maxx)
    ax.set_ylim(bbox.miny, bbox.maxy)
    ax.set_frame_on(False)
    ax.set_clip_on(False)
    ax.set_position([0., 0., 1., 1.])

    return figure_response(fig, request)
コード例 #7
0
ファイル: glg_handler.py プロジェクト: moghimis/sci-wms
def contour(request):

    # Create figure
    fig, ax, norm = create_axis(request)
    ax.set_axis_off()

    csr = request.GET['colorscalerange']

    if request.GET['logscale'] is True:
        levs = np.hstack(
            ([csr.min - 1],
             np.linspace(csr.min, csr.max,
                         request.GET['numcontours']), [csr.max + 1]))
        levs_labels = ["%.1f" % x for x in levs[1:-1]]
        if request.GET['showvalues'] is False:
            levs_labels = ['' for x in range(levs.size - 2)]
        x, y = np.meshgrid(np.ones(2), np.ones(2))
        cs = ax.contourf(x,
                         y,
                         x,
                         levels=levs,
                         norm=norm,
                         cmap=get_cmap(request.GET['colormap']))
        proxy = [
            plt.Rectangle((0, 0), 0, 0, fc=pc.get_facecolor()[0])
            for pc in cs.collections
        ]

    else:
        levs = np.linspace(csr.min, csr.max, request.GET['numcontours'])
        levs_labels = ["%.1f" % x for x in levs]
        if request.GET['showvalues'] is False:
            levs_labels = ['' for x in range(levs.size)]
        x, y = np.meshgrid(np.ones(2), np.ones(2))
        cs = ax.contourf(x,
                         y,
                         x,
                         levels=levs,
                         norm=norm,
                         cmap=get_cmap(request.GET['colormap']),
                         extend='max')
        proxy = [
            plt.Rectangle((0, 0), 0, 0, fc=pc.get_facecolor()[0])
            for pc in cs.collections
        ]

    params = dict()
    if request.GET['horizontal'] is True:
        columns = 5
        if request.GET['numcontours'] > 20:
            columns = request.GET['numcontours'] / 10
        params = dict(labelspacing=0, mode="expand", ncol=columns)

    cb = legend(proxy,
                levs_labels,
                loc=10,
                borderaxespad=0.,
                frameon=False,
                **params)

    if request.GET['showlabel'] is True:
        cb.set_title(request.GET['units'])

    # Return HttpResponse
    return figure_response(fig, request, bbox_extra_artists=(cb, ))