def index(request): """ index(request) Site Front Page. Presents introductory text and a map of NCA regions. A form allows selection of a region. On submit, user is redirected to the rgn_smry view at the selected region. """ # smrylogger.debug("INDEX VIEW") # Initialize a session. site_attr.init_session(request) attr_dct = site_attr.set_attrs(request, "modis_lnd_cvr") smrylogger.debug("index( )|attr_dct: %s", attr_dct) # Region selection form. if request.method == "POST": rgn_form = RegionForm(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] # smrylogger.debug("index( )|attr_dct: %s", attr_dct) # Set session attributes to dictionary items. site_attr.set_session_attr(request, attr_dct) # Create response to redirect to region summary. response = HttpResponseRedirect("rgn_smry/") else: # Create an instance of class RegionForm. rgn_form = RegionForm( initial={"region": abbrv_to_rgn[attr_dct["region"]], "dscrpt_txt": (attr_dct["dscrpt_txt"] == "True")} ) # Get introductory text from the database and process to html. intro_text = text_items.get_txt_html("index", "intro_text") intro_text_1 = text_items.get_txt_html("index", "intro_text_1", replace={"<p>": "", "</p>": "<br /><br />"}) # Load the region summary template. index_template = loader.get_template("smry/index.html") context = RequestContext( request, {"form": rgn_form, "form_help": rgn_form.form_help, "intro_text": intro_text, "intro_text_1": intro_text_1}, ) response = HttpResponse(index_template.render(context)) return response
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
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
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
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