Exemple #1
0
def rgn_pie_charts(request):
    """
    rgn_pie_charts(request)
    
    Display pie charts at four years.
    """

    smrylogger.debug("REGION PIE CHARTS VIEW")

    # Set attributes to session values.
    attr_dct = site_attr.get_session_attr(request)
    # 1
    smrylogger.debug("1 rgn_pie_charts( )|attr_dct: %s", attr_dct)

    if request.method == "POST":
        # Create an instance of class Region_Year_Form and populate
        # with request data.
        rgn_form = RegionYearForm_MODIS_LandCover(request.POST)

        if rgn_form.is_valid():
            # Update attributes.
            for key in rgn_form.cleaned_data.keys():
                attr_dct[key] = rgn_form.cleaned_data[key]
            # 2
            smrylogger.debug("2 rgn_pie_charts( )|attr_dct: %s", attr_dct)

            # Set attributes in session to dictionary items.
            site_attr.set_session_attr(request, attr_dct)
    else:
        # Create an instance of class RegionForm.
        rgn_form = RegionYearForm_MODIS_LandCover(
            initial={"region": attr_dct["region"], "year": attr_dct["year"], "dscrpt_txt": attr_dct["dscrpt_txt"]}
        )

    # 3
    smrylogger.debug("3 rgn_pie_charts( )|attr_dct: %s", attr_dct)

    # Create year list for pie charts.
    chart_year_list = create_year_list(int(attr_dct["year"]), 2001, 2012, 2)

    # Create an instance of class RegionImages for access to
    # pie chart attributes.
    rgn_img = RegionImages(img_size="500px")

    # Get pie chart file names.
    # Expand USA48 to full region name.
    if attr_dct["region"] == "USA48":
        region_name = name_dcts.abbrv_to_rgn[attr_dct["region"]]
    else:
        region_name = attr_dct["region"]
    filenames = []

    # Iterate over year list.
    for year in chart_year_list:
        filename = rgn_img.chart_filter(region_name, year, "LC", rgn_img.img_size, "piechart")
        filenames.append(filename)

    # Create request context from attributes.
    dct = {}
    for key in attr_dct.keys():
        dct[key] = attr_dct[key]
    dct["width"] = "1030"
    dct["rgn_name"] = attr_dct["region"]
    dct["chart_year_list"] = chart_year_list
    dct["filenames"] = filenames
    dct["form"] = rgn_form
    dct["form_help"] = rgn_form.form_help

    rgn_img = None

    # Load the region map template.
    template = loader.get_template("smry/rgn_pie_charts.html")

    # Create context and render.
    context = RequestContext(request, dct)
    response = HttpResponse(template.render(context))

    return response
Exemple #2
0
def rgn_map(request):
    """
    rgn_map(request)
    
    Display a single map image for the specified region and year.
    """
    smrylogger.debug("REGIONAL MAP VIEW")

    # Set attributes to session values.
    attr_dct = site_attr.get_session_attr(request)

    rgn_form = RegionYearForm_MODIS_LandCover(
        request.POST,
        initial={"region": attr_dct["region"], "year": attr_dct["year"], "dscrpt_txt": attr_dct["dscrpt_txt"]},
    )
    # 1
    smrylogger.debug("1 rgn_map( )|rgn_form.initial: %s", rgn_form.initial)

    if request.method == "POST":
        # Create an instance of class RegionYearForm and populate
        # with request data.
        rgn_form = RegionYearForm_MODIS_LandCover(request.POST)

        if rgn_form.is_valid():
            # Update attributes.
            for key in rgn_form.cleaned_data.keys():
                attr_dct[key] = rgn_form.cleaned_data[key]

            # Set attributes in session to dictionary items.
            site_attr.set_session_attr(request, attr_dct)
            # 2
            smrylogger.debug("2 rgn_map( )|attr_dct: %s", attr_dct)
        else:
            # 3
            smrylogger.debug("3 rgn_map( )|Warning: rgn_form is invalid")
    else:
        # Create an instance of class RegionYearForm.
        rgn_form = RegionYearForm_MODIS_LandCover(
            initial={"region": attr_dct["region"], "year": attr_dct["year"], "dscrpt_txt": attr_dct["dscrpt_txt"]}
        )

    # Get file name attributes from the database.
    # Expand USA48 to full region name.
    if attr_dct["region"] == "USA48":
        region_name = name_dcts.abbrv_to_rgn[attr_dct["region"]]
    else:
        region_name = attr_dct["region"]
    img = RegionImages()
    img.rgn_map_images(region_name, attr_dct["year"])

    # Create request context from attributes.
    dct = img.attr.dct
    for key in attr_dct.keys():
        dct[key] = attr_dct[key]
    dct["width"] = "1030"
    dct["rgn_name"] = region_name
    dct["form"] = rgn_form
    dct["form_help"] = rgn_form.form_help

    img = None

    # Load the region map template.
    template = loader.get_template("smry/rgn_map.html")

    # Create context and render.
    context = RequestContext(request, dct)
    response = HttpResponse(template.render(context))

    return response