示例#1
0
文件: views.py 项目: wemanuel/smry
def land_cover(request):
    """
    land_cover(request)
    
    Global maps of the MODIS land cover data product (MCD12Q), rendered by the
    Google Earth Engine.
    """
    smrylogger.debug("LAND COVER VIEW")

    # Set attributes.
    attr_dct = site_attr.set_attrs(request, "modis_lnd_cvr")
    # 1
    smrylogger.debug("1 land_cover( )|attr_dct: %s", attr_dct)

    # Create an instance of YearForm_MODIS_LandCover.
    year_form = YearForm_MODIS_LandCover(
        request.POST,
        initial={
            "year": attr_dct["year"],
            "opacity": attr_dct["opacity"],
            "dscrpt_txt": attr_dct["dscrpt_txt"],
            "lrg_fmt": attr_dct["lrg_fmt"],
        },
    )
    # 2
    smrylogger.debug("2 land_cover( )|year_form.initial: %s", year_form.initial)

    if request.method == "POST":
        # Create an instance of class YearForm and populate with
        # request data.
        year_form = YearForm_MODIS_LandCover(request.POST)

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

            # Set attributes in session to dictionary items.
            site_attr.set_session_attr(request, attr_dct)
            # 3
            smrylogger.debug("3 land_cover( )|attr_dct: %s", attr_dct)
        else:
            # 4
            logmsg = "4 land_cover( )|Warning: year_form is invalid"
            smrylogger.debug(logmsg)

    else:
        # Create an instance of class YearForm.
        year_form = YearForm_MODIS_LandCover(
            initial={
                "year": attr_dct["year"],
                "opacity": attr_dct["opacity"],
                "dscrpt_txt": attr_dct["dscrpt_txt"],
                "lrg_fmt": attr_dct["lrg_fmt"],
            }
        )

    # Initialize Earth Engine. The configuration file config.py
    # contains credentials.
    ee.Initialize(config.EE_CREDENTIALS, config.EE_URL)

    # Get color palette.
    palette_str = palettes.igbp_lndcvr()

    # Create Image name.
    img_name = "MODIS/051/MCD12Q1/" + str(attr_dct["year"]) + "_01_01"
    # 5
    smrylogger.debug("5 land_cover( )|img_name: %s", img_name)

    # Get map ID.
    img = ee.Image(img_name).select(["Land_Cover_Type_1"])
    mapid = img.getMapId({"min": 0, "max": 17, "palette": palette_str})
    # 6
    smrylogger.debug("6 land_cover( )|mapid: %s", mapid)

    # Get ecoregions map ID.
    mapid_eco_rgns = ee_smry.ee_create_ecorgns_map()
    # 7
    smrylogger.debug("7 land_cover( )|mapid_eco_rgns: %s", mapid_eco_rgns)

    # Create request context from attributes.
    # Mapid and token for base map.
    dct = {"mapid": mapid["mapid"], "token": mapid["token"]}

    # Mapid and token for ecoregions FeatureCollection.
    dct["mapid_eco_rgns"] = mapid_eco_rgns["mapid"]
    dct["token_eco_rgns"] = mapid_eco_rgns["token"]

    for key in attr_dct.keys():
        dct[key] = attr_dct[key]

    # Set image size.
    (dct["width"], dct["height"]) = img_size(lrg_fmt=dct["lrg_fmt"])

    # Set opacity attribute.
    dct["opacity"] = 0.01 * float(dct["opacity"])

    dct["heading"] = "Global MODIS Land Cover"
    dct["form"] = year_form
    dct["form_action"] = "smry/land_cover/"
    dct["form_help"] = year_form.form_help
    # 8
    smrylogger.debug("8 land_cover( )|dct: %s", dct)

    # Load the ee map template.
    template = loader.get_template("land_cover/ee_map.html")

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

    return response
示例#2
0
文件: views.py 项目: wemanuel/smry
def nlcd(request):
    """
    nlcd(request)
    
    National Land Cover Database (NLCD)
    """
    smrylogger.debug("NLCD VIEW")

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

    # Form with current year.
    year_form = YearForm_NLCD(
        request.POST,
        initial={
            "year": attr_dct["year"],
            "opacity": attr_dct["opacity"],
            "dscrpt_txt": attr_dct["dscrpt_txt"],
            "lrg_fmt": attr_dct["lrg_fmt"],
        },
    )
    # 1
    smrylogger.debug("1 nlcd( )|year_form.initial: %s", year_form.initial)

    if request.method == "POST":
        # Create an instance of class YearForm and populate with
        # request data.
        year_form = YearForm_NLCD(request.POST)

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

            # Set attributes in session to dictionary items.
            site_attr.set_session_attr(request, attr_dct)
            # 2
            smrylogger.debug("2 nlcd( )|attr_dct: %s", attr_dct)
        else:
            # 3
            smrylogger.warning("nlcd( )|Warning: year_form is invalid")

    else:
        # Create an instance of class RegionForm.
        year_form = YearForm_NLCD(
            initial={
                "year": attr_dct["year"],
                "opacity": attr_dct["opacity"],
                "dscrpt_txt": attr_dct["dscrpt_txt"],
                "lrg_fmt": attr_dct["lrg_fmt"],
            }
        )

    # Initialize Earth Engine. The configuration file config.py
    # contains credentials.
    ee.Initialize(config.EE_CREDENTIALS, config.EE_URL)

    # Get color palette.
    palette_str = palettes.nlcd_palette()

    # Derive the image name.
    img_name = "USGS/NLCD/NLCD" + str(attr_dct["year"])
    # 4
    smrylogger.debug("4 nlcd( )|img_name: %s", img_name)

    # Get map ID.
    img = ee.Image(img_name).select(["landcover"])
    mapid = img.getMapId({"min": 0, "max": 255, "palette": palette_str})
    # 5
    smrylogger.debug("5 nlcd( )|mapid: %s", mapid)

    # Get ecoregions map ID.
    mapid_eco_rgns = ee_smry.ee_create_ecorgns_map()
    # 6
    smrylogger.debug("6 nlcd( )|mapid_eco_rgns: %s", mapid_eco_rgns)

    # Create request context from attributes.
    # Mapid and token for base map.
    dct = {"mapid": mapid["mapid"], "token": mapid["token"]}

    # Mapid and token for ecoregions FeatureCollection.
    dct["mapid_eco_rgns"] = mapid_eco_rgns["mapid"]
    dct["token_eco_rgns"] = mapid_eco_rgns["token"]

    for key in attr_dct.keys():
        dct[key] = attr_dct[key]

    # Set image size.
    (dct["width"], dct["height"]) = img_size(lrg_fmt=dct["lrg_fmt"])

    # Set opacity attribute.
    dct["opacity"] = 0.01 * float(dct["opacity"])

    dct["heading"] = "U.S. National Land Cover Database"
    dct["form"] = year_form
    dct["form_action"] = "smry/nlcd/"
    dct["form_help"] = year_form.form_help
    # 7
    smrylogger.debug("cdl( )|dct: %s", dct)

    # Load the ee map template.
    template = loader.get_template("land_cover/ee_map.html")

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

    return response
示例#3
0
文件: views.py 项目: wemanuel/smry
def cdl(request):
    """
    cdl(request)
    
    USDA NASS Cropland Data Layer (CDL)
    """
    #     smrylogger.debug("CDL VIEW")

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

    # Form with current year.
    year_form = YearForm_CDL(
        request.POST,
        initial={"year": attr_dct["year"], "opacity": attr_dct["opacity"], "dscrpt_txt": attr_dct["dscrpt_txt"]},
    )
    # 1
    #     smrylogger.debug("1 cdl( )|year_form.initial: %s", year_form.initial)

    if request.method == "POST":
        # Create an instance of class YearForm and populate with
        # request data.
        year_form = YearForm_CDL(request.POST)

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

            # Set attributes in session to dictionary items.
            site_attr.set_session_attr(request, attr_dct)
            # 2
    #             smrylogger.debug("2 cdl( )|attr_dct: %s", attr_dct)
    #        else:
    # 3
    #             smrylogger.debug("3 cdl( )|Warning: year_form is invalid")

    else:
        # Create an instance of class RegionForm.
        year_form = YearForm_CDL(
            initial={"year": attr_dct["year"], "opacity": attr_dct["opacity"], "dscrpt_txt": attr_dct["dscrpt_txt"]}
        )

    # Initialize Earth Engine. The configuration file config.py
    # contains credentials.
    ee.Initialize(config.EE_CREDENTIALS, config.EE_URL)

    # Get color palette.
    palette_str = palettes.cdl_palette()

    # Derive the image name.
    img_name = "USDA/NASS/CDL/" + str(attr_dct["year"])
    # 4
    #     smrylogger.debug("cdl( )|img_name: %s", img_name)

    # Get map ID.
    img = ee.Image(img_name).select(["cropland"])
    mapid = img.getMapId({"min": 0, "max": 255, "palette": palette_str})
    # 5
    #     smrylogger.debug("5 cdl( )|mapid: %s", mapid)

    # Get ecoregions map ID.
    mapid_eco_rgns = ee_smry.ee_create_ecorgns_map()
    # 6
    #     smrylogger.debug("6 cdl( )|mapid_eco_rgns: %s", mapid_eco_rgns)

    # Create request context from attributes.
    # Mapid and token for base map.
    dct = {"mapid": mapid["mapid"], "token": mapid["token"]}

    # Mapid and token for ecoregions FeatureCollection.
    dct["mapid_eco_rgns"] = mapid_eco_rgns["mapid"]
    dct["token_eco_rgns"] = mapid_eco_rgns["token"]

    for key in attr_dct.keys():
        dct[key] = attr_dct[key]

    # Set opacity attribute.
    dct["opacity"] = 0.01 * float(dct["opacity"])

    dct["heading"] = "USDA NASS Cropland Data Layer"
    dct["form"] = year_form
    dct["form_action"] = "smry/cdl/"
    dct["form_help"] = year_form.form_help
    # 7
    #     smrylogger.debug("6 cdl( )|dct: %s", dct)

    # Load the ee map template.
    template = loader.get_template("land_cover/ee_map.html")

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

    return response