Пример #1
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
Пример #2
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
Пример #3
0
def rgn_smry(request):
    """
    rgn_smry(request)
    
    Regional summary view.
    """
    smrylogger.debug("REGIONAL SUMMARY VIEW")

    # Set attributes.
    attr_dct = site_attr.set_attrs(request, "modis_lnd_cvr")

    # Create an instance of class Region_Region_Form.
    rgn_form = RegionYearForm_MODIS_LandCover(
        initial={"region": attr_dct["region"], "year": attr_dct["year"], "dscrpt_txt": attr_dct["dscrpt_txt"]}
    )

    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]
            # 1
            smrylogger.debug("1 rgn_smry( )|attr_dct: %s", attr_dct)

            # Set attributes in session to dictionary items.
            site_attr.set_session_attr(request, attr_dct)
            # 2
            smrylogger.debug("2 rgn_smry( )|attr_dct: %s", attr_dct)
        else:
            # 3
            smrylogger.debug("3 rgn_smry( )|Warning: rgn_form is invalid")
    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"]}
        )

    if attr_dct["dscrpt_txt"]:
        # Get descriptive text from the database.
        intro_txt_html = text_items.get_txt_html("rgn_smry", "rgn_smry_intro")
        trends_txt_html = text_items.get_txt_html("rgn_smry", "trends", replace=del_p)
        vcf_txt_html = text_items.get_txt_html("rgn_smry", "vcf", replace=del_p)
    else:
        intro_txt_html = ""
        trends_txt_html = ""
        vcf_txt_html = ""

    # Get file name attributes from the database.
    # 4
    smrylogger.debug("4 rgn_smry( )|attr_dct: %s", attr_dct)
    # 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"]
    rgn_img = RegionImages()
    rgn_img.rgn_smry_images(region_name, attr_dct["year"])

    # Create distribution table data as a list.
    table_list = create_distr_table_list(attr_dct["region"], attr_dct["year"])

    # Create request context from attributes.
    dct = rgn_img.attr.dct
    for key in attr_dct.keys():
        dct[key] = attr_dct[key]
    dct["width"] = "1030"
    dct["rgn_name"] = attr_dct["region"]
    dct["intro_txt"] = str(intro_txt_html)
    dct["trends_txt"] = str(trends_txt_html)
    dct["vcf_txt"] = str(vcf_txt_html)
    dct["form"] = rgn_form
    dct["form_help"] = rgn_form.form_help
    dct["table_list"] = table_list

    rgn_img = None

    # Load the region summary template.
    rgn_template = loader.get_template("smry/rgn_smry.html")

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

    return response