def populate_datamart(dbsession): # AdminLevelType for i in [ ("COU", "Country", "Administrative division of level 0"), ("PRO", "Province", "Administrative division of level 1"), ("REG", "Region", "Administrative division of level 2"), ]: if AdminLevelType.get(dbsession, i[0]): continue r = AdminLevelType() r.mnemonic, r.title, r.description = i dbsession.add(r) # HazardLevel for i in [ ("HIG", "High", 1), ("MED", "Medium", 2), ("LOW", "Low", 3), ("VLO", "Very low", 4), ]: if HazardLevel.get(dbsession, i[0]): continue r = HazardLevel() r.mnemonic, r.title, r.order = i dbsession.add(r) # HazardType for i in [ ("FL", "River flood", 1), ("UF", "Urban flood", 2), ("CF", "Coastal flood", 3), ("EQ", "Earthquake", 4), ("LS", "Landslide", 5), ("TS", "Tsunami", 6), ("VA", "Volcano", 7), ("CY", "Cyclone", 8), ("DG", "Water scarcity", 9), ("EH", "Extreme heat", 10), ("WF", "Wildfire", 11), ("AP", "Air pollution", 12), ]: if HazardType.get(dbsession, i[0]): continue r = HazardType() r.mnemonic, r.title, r.order = i dbsession.add(r) # HazardCategory hazardlevels = dbsession.query(HazardLevel) for hazardtype in dbsession.query(HazardType): for hazardlevel in hazardlevels: if HazardCategory.get(dbsession, hazardtype, hazardlevel): continue r = HazardCategory() r.hazardtype = hazardtype r.hazardlevel = hazardlevel r.general_recommendation = "General recommendation for {} {}".format( hazardtype.mnemonic, hazardlevel.mnemonic) dbsession.add(r)
def populate_preprocessed(type): hazardset_id = "preprocessed" hazardtype = HazardType.get(DBSession, type) regions = DBSession.query(Region).all() print("Populating hazardset {}".format(hazardset_id)) hazardset = HazardSet( id=hazardset_id, hazardtype=hazardtype, local=False, data_lastupdated_date=datetime.now(), metadata_lastupdated_date=datetime.now(), regions=regions, ) DBSession.add(hazardset) layer = Layer( hazardlevel=None, mask=False, return_period=None, data_lastupdated_date=datetime.now(), metadata_lastupdated_date=datetime.now(), geonode_id=new_geonode_id(DBSession), download_url="test", calculation_method_quality=5, scientific_quality=1, local=False, downloaded=True, ) hazardset.layers.append(layer) hazardset.complete = True DBSession.flush()
def test_not_corresponding_rasters(self, open_mock): """Difference in origin, resolution or size must not complete""" hazardset_id = "notpreprocessed" hazardtype = HazardType.get(DBSession, "FL") regions = DBSession.query(Region).all() hazardset = HazardSet( id=hazardset_id, hazardtype=hazardtype, local=False, data_lastupdated_date=datetime.now(), metadata_lastupdated_date=datetime.now(), regions=regions, ) DBSession.add(hazardset) for level in ["HIG", "MED", "LOW"]: layer = Layer( hazardlevel=HazardLevel.get(DBSession, level), mask=False, return_period=None, data_lastupdated_date=datetime.now(), metadata_lastupdated_date=datetime.now(), geonode_id=new_geonode_id(DBSession), download_url="test", calculation_method_quality=5, scientific_quality=1, local=False, downloaded=True, ) hazardset.layers.append(layer) mask_layer = Layer( hazardlevel=None, mask=True, return_period=None, data_lastupdated_date=datetime.now(), metadata_lastupdated_date=datetime.now(), geonode_id=new_geonode_id(DBSession), download_url="test", calculation_method_quality=5, scientific_quality=1, local=False, downloaded=True, ) hazardset.layers.append(mask_layer) DBSession.flush() self.completer().execute() hazardset = DBSession.query(HazardSet).one() self.assertEqual( hazardset.complete_error, "All layers should have the same origin, resolution and size", ) self.assertEqual(hazardset.complete, False)
def test_preprocessed_hig(self, open_mock): """Test preprocessed HIG""" hazardtype = HazardType.get(DBSession, preprocessed_type) hazardtype_settings = settings["hazard_types"][hazardtype.mnemonic] open_mock.side_effect = [global_reader(hazardtype_settings["values"]["HIG"][0])] self.processor().execute(hazardset_id="preprocessed") output = DBSession.query(Output).first() self.assertEqual(output.hazardlevel.mnemonic, "HIG")
def climate_rec_new(request): hazard_type = request.matchdict["hazard_type"] hazardtype = HazardType.get(request.dbsession, hazard_type) if hazardtype is None: raise HTTPNotFound obj = ClimateChangeRecommendation() obj.hazardtype = hazardtype return climate_rec_process(request, obj)
def climate_rec_hazardtype(request): hazard_type = request.matchdict["hazard_type"] hazardtype = HazardType.get(request.dbsession, hazard_type) if hazardtype is None: raise HTTPNotFound hazard_types = request.dbsession.query(HazardType).order_by( HazardType.order) climate_recs = request.dbsession.query(ClimateChangeRecommendation).filter( ClimateChangeRecommendation.hazardtype == hazardtype) return {"hazard_types": hazard_types, "climate_recs": climate_recs}
def test_open_exception(self, open_mock): """Test handling of open exception""" hazardset_id = "notpreprocessed" hazardtype = HazardType.get(DBSession, "EQ") regions = DBSession.query(Region).all() hazardset = HazardSet( id=hazardset_id, hazardtype=hazardtype, local=False, data_lastupdated_date=datetime.now(), metadata_lastupdated_date=datetime.now(), regions=regions, ) DBSession.add(hazardset) for level in ["HIG", "MED", "LOW"]: layer = Layer( hazardlevel=HazardLevel.get(DBSession, level), mask=False, return_period=None, data_lastupdated_date=datetime.now(), metadata_lastupdated_date=datetime.now(), geonode_id=new_geonode_id(DBSession), download_url="test", calculation_method_quality=5, scientific_quality=1, local=False, downloaded=True, ) hazardset.layers.append(layer) DBSession.flush() self.completer().execute() hazardset = DBSession.query(HazardSet).one() self.assertEqual(hazardset.complete_error, "Error opening layer notpreprocessed") self.assertEqual(hazardset.complete, False)
def test_complete_preprocessed(self, open_mock): """Test complete preprocessed hazardset""" hazardset_id = "preprocessed" hazardtype = HazardType.get(DBSession, "VA") regions = DBSession.query(Region).all() hazardset = HazardSet( id=hazardset_id, hazardtype=hazardtype, local=False, data_lastupdated_date=datetime.now(), metadata_lastupdated_date=datetime.now(), regions=regions, ) DBSession.add(hazardset) layer = Layer( hazardlevel=None, mask=False, return_period=None, data_lastupdated_date=datetime.now(), metadata_lastupdated_date=datetime.now(), geonode_id=new_geonode_id(DBSession), download_url="test", calculation_method_quality=5, scientific_quality=1, local=False, downloaded=True, ) hazardset.layers.append(layer) DBSession.flush() self.completer().execute() hazardset = DBSession.query(HazardSet).one() self.assertEqual(hazardset.complete_error, None) self.assertEqual(hazardset.complete, True)
def admindiv_hazardsets_hazardtype(request): try: hazardtype = request.matchdict.get("hazardtype") except: raise HTTPBadRequest(detail="incorrect value for parameter " '"hazardtype"') if HazardType.get(request.dbsession, hazardtype) is None: raise HTTPBadRequest(detail="hazardtype doesn't exist") query = (request.dbsession.query(AdministrativeDivision).join( HazardCategoryAdministrativeDivisionAssociation).join( HazardCategory).join(HazardType).filter( HazardType.mnemonic == hazardtype).join(AdminLevelType).filter( AdminLevelType.id == 3).order_by( AdministrativeDivision.name).options( contains_eager( AdministrativeDivision.hazardcategories))) data = [{ "code": row.code, "name": row.name, "level_2": row.parent.name, "level_1": row.parent.parent.name, "hazardset": row.hazardcategories[0].hazardsets[0].id if row.hazardcategories[0].hazardsets else None, "hazard_level": row.hazardcategories[0].hazardcategory.hazardlevel.mnemonic, } for row in query] return data
def hazardtype_from_geonode(self, geonode_name): for mnemonic, type_settings in self.settings["hazard_types"].items(): if type_settings["hazard_type"] == geonode_name: return HazardType.get(self.dbsession, str(mnemonic)) return None
def populate_notpreprocessed(type, unit): hazardset_id = "notpreprocessed" hazardtype = HazardType.get(DBSession, type) hazardtype_settings = settings["hazard_types"][hazardtype.mnemonic] regions = DBSession.query(Region).all() print("Populating hazardset {}".format(hazardset_id)) hazardset = HazardSet( id=hazardset_id, hazardtype=hazardtype, local=False, data_lastupdated_date=datetime.now(), metadata_lastupdated_date=datetime.now(), regions=regions, ) DBSession.add(hazardset) return_periods = hazardtype_settings["return_periods"] for level in ("HIG", "MED", "LOW"): hazardlevel = HazardLevel.get(DBSession, level) level_return_periods = return_periods[level] if isinstance(level_return_periods, list): return_period = level_return_periods[0] else: return_period = level_return_periods layer = Layer( hazardlevel=hazardlevel, mask=False, return_period=return_period, hazardunit=unit, data_lastupdated_date=datetime.now(), metadata_lastupdated_date=datetime.now(), geonode_id=new_geonode_id(DBSession), download_url="test", calculation_method_quality=5, scientific_quality=1, local=False, downloaded=True, ) hazardset.layers.append(layer) mask_return_periods = hazardtype_settings["mask_return_period"] if isinstance(mask_return_periods, list): mask_return_period = mask_return_periods[0] else: mask_return_period = mask_return_periods layer = Layer( hazardlevel=None, mask=True, return_period=mask_return_period, hazardunit=unit, data_lastupdated_date=datetime.now(), metadata_lastupdated_date=datetime.now(), geonode_id=new_geonode_id(DBSession), download_url="test", calculation_method_quality=5, scientific_quality=1, local=False, downloaded=True, ) hazardset.layers.append(layer) hazardset.complete = True DBSession.flush()
def do_execute(self): self.dbsession.query(HazardCategoryTechnicalRecommendationAssociation).delete() self.dbsession.query(TechnicalRecommendation).delete() # First load general recommendations with open("data/general_recommendations.csv", encoding="windows-1252") as csvfile: recommendations = csv.reader(csvfile, delimiter=",") for row in recommendations: hazardcategory = ( self.dbsession.query(HazardCategory) .join(HazardLevel) .join(HazardType) .filter(HazardLevel.mnemonic == row[1]) .filter(HazardType.mnemonic == row[0]) .one() ) hazardcategory.general_recommendation = row[2] self.dbsession.add(hazardcategory) categories = [] for type_ in ["EQ", "FL", "CY", "TS", "CF", "VA", "DG"]: for level in ["HIG", "MED", "LOW", "VLO"]: hazardcategory = ( self.dbsession.query(HazardCategory) .join(HazardLevel) .join(HazardType) .filter(HazardLevel.mnemonic == level) .filter(HazardType.mnemonic == type_) .one() ) categories.append(hazardcategory) # Then technical recommendations hctra = HazardCategoryTechnicalRecommendationAssociation with open("data/technical_recommendations.csv", encoding="windows-1252") as csvfile: recommendations = csv.reader(csvfile, delimiter=",") next(recommendations, None) # skip the headers for row in recommendations: technical_rec = TechnicalRecommendation(**{"text": row[0]}) associations = technical_rec.hazardcategory_associations # the other columns are hazard category (type / level) for col_index in range(1, 28): value = row[col_index] if value != "" and value != "Y": association = hctra(order=value) association.hazardcategory = categories[col_index - 1] associations.append(association) self.dbsession.add(technical_rec) # Climate change recommendations self.dbsession.query(ClimateChangeRecommendation).delete() # hazard types and corresponding columns hazard_types = [ ("FL", 6), ("EQ", 7), ("CY", 8), ("CF", 9), ("DG", 10), ("TS", 11), ("VA", 12), ("LS", 13), ] with open("data/climate_change_recommendations.csv", encoding="windows-1252") as csvfile: countries = csv.reader(csvfile, delimiter=",") next(countries, None) # skip the headers for row in countries: division = ( self.dbsession.query(AdministrativeDivision) .filter(AdministrativeDivision.code == row[1]) .one_or_none() ) if not division: continue for hazard_type, column in hazard_types: hazardtype = HazardType.get(self.dbsession, hazard_type) text = row[column] if text == "NA": continue climate_rec = ( self.dbsession.query(ClimateChangeRecommendation) .filter(ClimateChangeRecommendation.text == text) .filter(ClimateChangeRecommendation.hazardtype == hazardtype) .first() ) if climate_rec is None: climate_rec = ClimateChangeRecommendation() climate_rec.text = text climate_rec.hazardtype = hazardtype self.dbsession.add(climate_rec) association = CcrAd( administrativedivision=division, hazardtype=hazardtype ) climate_rec.associations.append(association)
def populate_processing(): hazardtype_eq = HazardType.get("EQ") hazardtype_fl = HazardType.get("FL") hazardset_id = "hazardset1" print("Populating hazardset {}".format(hazardset_id)) hazardset1 = HazardSet() hazardset1.id = hazardset_id hazardset1.hazardtype = hazardtype_eq hazardset1.local = False hazardset1.calculation_method_quality = 0 hazardset1.data_lastupdated_date = datetime.now() hazardset1.metadata_lastupdated_date = datetime.now() hazardset1.complete = True hazardset1.layers.extend(make_layers()) DBSession.add(hazardset1) hazardset_id = "hazardset1b" print("Populating hazardset {}".format(hazardset_id)) hazardset1b = HazardSet() hazardset1b.id = hazardset_id hazardset1b.hazardtype = hazardtype_fl hazardset1b.local = True hazardset1b.calculation_method_quality = 3 hazardset1b.data_lastupdated_date = datetime.now() hazardset1b.metadata_lastupdated_date = datetime.now() hazardset1b.complete = True hazardset1b.layers.extend(make_layers()) DBSession.add(hazardset1b) # create hazardsets 2, 3 ... 5 hazardset_id = "hazardset2" # calculation_method_quality = 1 / scientific_quality = 0 print("Populating hazardset {}".format(hazardset_id)) hazardset2 = HazardSet() hazardset2.id = hazardset_id hazardset2.hazardtype = hazardtype_eq hazardset2.local = True hazardset2.calculation_method_quality = 1 hazardset2.scientific_quality = 0 hazardset2.data_lastupdated_date = datetime.now() hazardset2.metadata_lastupdated_date = datetime.now() hazardset2.complete = True hazardset2.layers.extend(make_layers()) DBSession.add(hazardset2) hazardset_id = "hazardset3" # date = 2015-01-01 / global / # calculation_method_quality = 1 / scientific_quality = 2 print("Populating hazardset {}".format(hazardset_id)) hazardset3 = HazardSet() hazardset3.id = hazardset_id hazardset3.hazardtype = hazardtype_eq hazardset3.local = False hazardset3.calculation_method_quality = 1 hazardset3.scientific_quality = 2 hazardset3.data_lastupdated_date = datetime(2015, 1, 1, 0, 0) hazardset3.metadata_lastupdated_date = datetime.now() hazardset3.complete = True hazardset3.layers.extend(make_layers()) DBSession.add(hazardset3) hazardset_id = "hazardset4" # date = 2015-01-01 / local / # calculation_method_quality = 1 / scientific_quality = 2 print("Populating hazardset {}".format(hazardset_id)) hazardset4 = HazardSet() hazardset4.id = hazardset_id hazardset4.hazardtype = hazardtype_eq hazardset4.local = True hazardset4.calculation_method_quality = 1 hazardset4.scientific_quality = 2 hazardset4.data_lastupdated_date = datetime(2015, 1, 1, 0, 0) hazardset4.metadata_lastupdated_date = datetime.now() hazardset4.complete = True hazardset4.layers.extend(make_layers()) DBSession.add(hazardset4) hazardset_id = "hazardset5" # date = now() / local / # calculation_method_quality = 1 / scientific_quality = 2 print("Populating hazardset {}".format(hazardset_id)) hazardset5 = HazardSet() hazardset5.id = hazardset_id hazardset5.hazardtype = hazardtype_eq hazardset5.local = True hazardset5.calculation_method_quality = 1 hazardset5.scientific_quality = 2 hazardset5.data_lastupdated_date = datetime.now() hazardset5.metadata_lastupdated_date = datetime.now() hazardset5.complete = True hazardset5.layers.extend(make_layers()) DBSession.add(hazardset5) hazardset_id = "hazardset6" # date = now() / global / # calculation_method_quality = 1 / scientific_quality = 2 print("Populating hazardset {}".format(hazardset_id)) hazardset6 = HazardSet() hazardset6.id = hazardset_id hazardset6.hazardtype = hazardtype_eq hazardset6.local = False hazardset6.calculation_method_quality = 1 hazardset6.scientific_quality = 2 hazardset6.data_lastupdated_date = datetime.now() hazardset6.metadata_lastupdated_date = datetime.now() hazardset6.complete = True hazardset6.layers.extend(make_layers()) DBSession.add(hazardset6) # populate output table # admin div (code 30) has only one hazardset for EQ admin30 = (DBSession.query(AdministrativeDivision).filter( AdministrativeDivision.code == 30).one()) # => test outcome = hazardset1 output1 = Output() output1.hazardset = hazardset1 output1.administrativedivision = admin30 output1.hazardlevel = HazardLevel.get("HIG") DBSession.add(output1) # admin div (code 30) also has another hazardset # but this one is for FL # => test outcome = hazardset1b output1b = Output() output1b.hazardset = hazardset1b output1b.administrativedivision = admin30 output1b.hazardlevel = HazardLevel.get("NPR") DBSession.add(output1b) # admin div (code 31) has 2 hazardsets, # one with a higher calculation_method_quality # => test outcome = hazardset2 admin31 = (DBSession.query(AdministrativeDivision).filter( AdministrativeDivision.code == 31).one()) output2 = Output() output2.hazardset = hazardset1 # calculation_method_quality = 0 output2.administrativedivision = admin31 output2.hazardlevel = HazardLevel.get("MED") DBSession.add(output2) output3 = Output() output3.hazardset = hazardset2 # calculation_method_quality = 1 output3.administrativedivision = admin31 output3.hazardlevel = HazardLevel.get("LOW") DBSession.add(output3) # admin div (code 32) has 2 hazardsets, # both share the same calculation_method_quality, # one with a higher scientific_quality # => test outcome = hazardset3 admin32 = (DBSession.query(AdministrativeDivision).filter( AdministrativeDivision.code == 32).one()) output4 = Output() output4.hazardset = hazardset2 # calculation_method_quality = 1 / scientific_quality = 0 output4.administrativedivision = admin32 output4.hazardlevel = HazardLevel.get("MED") DBSession.add(output4) output5 = Output() output5.hazardset = hazardset3 # calculation_method_quality = 1 / scientific_quality = 2 output5.administrativedivision = admin32 output5.hazardlevel = HazardLevel.get("LOW") DBSession.add(output5) # admin div (code 33) has 2 hazardsets, # both share the same ratings, one is global, one local # => test outcome = hazardset4 admin33 = (DBSession.query(AdministrativeDivision).filter( AdministrativeDivision.code == 33).one()) output6 = Output() output6.hazardset = hazardset3 # global / calculation_method_quality = 1 / scientific_quality = 2 output6.administrativedivision = admin33 output6.hazardlevel = HazardLevel.get("MED") DBSession.add(output6) output7 = Output() output7.hazardset = hazardset4 # local / calculation_method_quality = 1 / scientific_quality = 2 output7.administrativedivision = admin33 output7.hazardlevel = HazardLevel.get("LOW") DBSession.add(output7) # admin div (code 34) has 2 hazardsets, # both share the same ratings, are local, one is more recent # => test outcome = hazardset5 admin34 = (DBSession.query(AdministrativeDivision).filter( AdministrativeDivision.code == 34).one()) output8 = Output() output8.hazardset = hazardset4 # date = 2015-01-01 / local / # calculation_method_quality = 1 / scientific_quality = 2 output8.administrativedivision = admin34 output8.hazardlevel = HazardLevel.get("MED") DBSession.add(output8) output9 = Output() output9.hazardset = hazardset5 # date = now() / local / # calculation_method_quality = 1 / scientific_quality = 2 output9.administrativedivision = admin34 output9.hazardlevel = HazardLevel.get("LOW") DBSession.add(output9) # admin div (code 35) has 2 hazardsets, # both share the same ratings, are global, one is more recent # => test outcome = hazardset6 admin35 = (DBSession.query(AdministrativeDivision).filter( AdministrativeDivision.code == 35).one()) output10 = Output() output10.hazardset = hazardset3 # date = 2015-01-01 / global / # calculation_method_quality = 1 / scientific_quality = 2 output10.administrativedivision = admin35 output10.hazardlevel = HazardLevel.get("MED") DBSession.add(output10) output11 = Output() output11.hazardset = hazardset6 # date = now() / global / # calculation_method_quality = 1 / scientific_quality = 2 output11.administrativedivision = admin35 output11.hazardlevel = HazardLevel.get("LOW") DBSession.add(output11) DBSession.flush()
def report(request): try: division_code = request.matchdict.get("divisioncode") except: raise HTTPBadRequest(detail="incorrect value for parameter " '"divisioncode"') selected_hazard = request.matchdict.get("hazardtype", None) hazard_types = get_hazard_types(request, division_code) hazard_category = None division = get_division(request, division_code) if selected_hazard is not None: try: hazard_category = get_info_for_hazard_type(request, selected_hazard, division) except NoResultFound: url = request.route_url("report_overview", division=division) return HTTPFound(location=url) # Get the geometry for division and compute its extent cte = (select([ func.box2d(AdministrativeDivision.geom).label("box2d") ]).where(AdministrativeDivision.code == division_code).cte("bounds")) bounds = list( request.dbsession.query( func.ST_XMIN(cte.c.box2d), func.ST_YMIN(cte.c.box2d), func.ST_XMAX(cte.c.box2d), func.ST_YMAX(cte.c.box2d), ).one()) division_bounds = bounds # There are some cases where divisions cross the date line. In this case, # we need to shift the longitude. # But for performance, we don't do it if not required. if division_bounds[2] - division_bounds[0] > 180: # compute a 0-360 version of the extent cte = (select([ func.ST_Translate( func.ST_Shift_Longitude( func.ST_Translate( func.ST_Transform( AdministrativeDivision.geom_simplified, 4326), 180, 0)), -180, 0, ).label("shift") ]).where(AdministrativeDivision.code == division_code).cte("bounds")) bounds_shifted = list( request.dbsession.query( func.ST_XMIN(cte.c.shift), func.ST_YMIN(cte.c.shift), func.ST_XMAX(cte.c.shift), func.ST_YMAX(cte.c.shift), ).one()) # Use the 0-360 if it's smaller if bounds_shifted[2] - bounds_shifted[0] < bounds[2] - bounds[0]: division_bounds = bounds_shifted feedback_params = {} feedback_params["entry.1144401731"] = "{} - {}".format( division.code, division.name).encode("utf8") if selected_hazard is not None: feedback_params["entry.93444540"] = HazardType.get( request.dbsession, selected_hazard).title.encode("utf8") feedback_form_url = "{}?{}".format( request.registry.settings["feedback_form_url"], urllib.parse.urlencode(feedback_params), ) context = { "hazards": hazard_types, "hazards_sorted": sorted(hazard_types, key=lambda a: a["hazardlevel"].order), "division": division, "bounds": division_bounds, "parents": get_parents(division), "parent_division": division.parent, "date": datetime.datetime.now(), "feedback_form_url": feedback_form_url, "request": request, } if hazard_category is not None: context.update(hazard_category) return context
def climate_rec_process(request, obj): if request.method == "GET": hazard_types = request.dbsession.query(HazardType).order_by( HazardType.order) association_subq = (request.dbsession.query(CcrAd).filter( CcrAd.hazardtype == obj.hazardtype).subquery()) admin_divs = (request.dbsession.query( AdministrativeDivision, ClimateChangeRecommendation).select_from( AdministrativeDivision).outerjoin( association_subq, association_subq.c.administrativedivision_id == AdministrativeDivision.id, ).outerjoin( ClimateChangeRecommendation, ClimateChangeRecommendation.id == association_subq.c.climatechangerecommendation_id, ).join(AdminLevelType).filter( AdminLevelType.mnemonic == "COU").order_by( AdministrativeDivision.name)) if obj.id is None: action = request.route_url("admin_climate_rec_new", hazard_type=obj.hazardtype.mnemonic) else: action = request.route_url("admin_climate_rec_edit", id=obj.id) return { "obj": obj, "action": action, "hazard_types": hazard_types, "admin_divs": admin_divs, } if request.method == "POST": if inspect(obj).transient: request.dbsession.add(obj) obj.hazardtype = HazardType.get(request.dbsession, request.POST.get("hazard_type")) obj.text = request.POST.get("text") admindiv_ids = request.POST.getall("associations") # Remove unchecked ones for association in obj.associations: if association.administrativedivision_id not in admindiv_ids: request.dbsession.delete(association) # Add new ones for admindiv_id in admindiv_ids: association = request.dbsession.query(CcrAd).get( (admindiv_id, obj.hazardtype.id)) if association is None: association = CcrAd(administrativedivision_id=admindiv_id, hazardtype=obj.hazardtype) obj.associations.append(association) else: association.climatechangerecommendation = obj request.dbsession.flush() return HTTPFound( request.route_url("admin_climate_rec_edit", id=obj.hazardtype.mnemonic))
def populate_db(): DBSession.query(Output).delete() DBSession.query(Layer).delete() DBSession.query(HazardSet).delete() DBSession.query(HazardTypeFurtherResourceAssociation).delete() DBSession.query(FurtherResource).delete() DBSession.query(HazardCategoryTechnicalRecommendationAssociation).delete() DBSession.query(TechnicalRecommendation).delete() DBSession.query(ClimateChangeRecommendation).delete() DBSession.query(HazardCategoryAdministrativeDivisionAssociation).delete() DBSession.query(CAdHt).delete() DBSession.query(Contact).delete() DBSession.query(Region).delete() DBSession.query(AdministrativeDivision).delete() hazardtype_eq = (DBSession.query(HazardType).filter( HazardType.mnemonic == "EQ").one()) hazardset1 = HazardSet() hazardset1.id = "hazardset1" hazardset1.hazardtype = hazardtype_eq hazardset1.data_lastupdated_date = datetime.now() hazardset1.metadata_lastupdated_date = datetime.now() hazardset1.detail_url = "http://domain.com/path/" hazardset1.owner_organization = "data_provider" DBSession.add(hazardset1) shape = MultiPolygon([Polygon([(0, 0), (0, 1), (1, 1), (1, 0), (0, 0)])]) geometry = from_shape(shape, 4326) # admin_div_10 is a country (division level 1) admin_div_10 = AdministrativeDivision(**{ "code": 10, "leveltype_id": 1, "name": "Division level 1" }) admin_div_10.geom = geometry DBSession.add(admin_div_10) # admin_div_11 is another country (division level 1) admin_div_11 = AdministrativeDivision(**{ "code": 11, "leveltype_id": 1, "name": "Division level 1 2" }) admin_div_11.geom = geometry DBSession.add(admin_div_11) # admin_div_12 is another country (division level 1) admin_div_12 = AdministrativeDivision(**{ "code": 12, "leveltype_id": 1, "name": "Division level 1 3" }) admin_div_12.geom = geometry DBSession.add(admin_div_12) # admin_div_13 is another country (division level 1) admin_div_13 = AdministrativeDivision(**{ "code": 13, "leveltype_id": 1, "name": "Division level 1 4" }) admin_div_13.geom = geometry DBSession.add(admin_div_13) # admin_div_20 is a province (division level 2) # its parent is admin_div_10 admin_div_20 = AdministrativeDivision(**{ "code": 20, "leveltype_id": 2, "name": "Division level 2" }) admin_div_20.parent_code = admin_div_10.code admin_div_20.geom = geometry DBSession.add(admin_div_20) shape = MultiPolygon( [Polygon([(0, 0), (0, 1), (0.5, 1), (0.5, 0), (0, 0)])]) geometry = from_shape(shape, 4326) # admin_div_31 is a region (division level 3) # its parent is admin_div_20 admin_div_31 = AdministrativeDivision(**{ "code": 31, "leveltype_id": 3, "name": "Division level 3 - 1" }) admin_div_31.parent_code = admin_div_20.code admin_div_31.geom = geometry admin_div_31.hazardcategories = [] shape = MultiPolygon( [Polygon([(0.5, 0), (0.5, 1), (1, 1), (1, 0), (0.5, 0)])]) geometry = from_shape(shape, 4326) # admin_div_32 is a region (division level 3) # its parent is also admin_div_20 admin_div_32 = AdministrativeDivision(**{ "code": 32, "leveltype_id": 3, "name": "Division level 3 - 2" }) admin_div_32.parent_code = admin_div_20.code admin_div_32.geom = geometry admin_div_32.hazardcategories = [] # Here's a quick, graphical recap: # # admin_div_10 -> admin_div_20 -> admin_div_31 # `-> admin_div_32 # admin_div_11 # admin_div_12 # admin_div_13 # GeoNode Regions # global_region contains all countries, **except admin_div_12** global_region = Region(**{"id": 1, "level": 0, "name": "Global region"}) global_region.administrativedivisions.append(admin_div_10) global_region.administrativedivisions.append(admin_div_11) global_region.administrativedivisions.append(admin_div_13) # region_1 is a country # it matches GAULS's admin_div_10 region_1 = Region(**{"id": 2, "level": 3, "name": "Country 1"}) region_1.administrativedivisions.append(admin_div_10) # region_2 is another country # it matches GAULS's admin_div_11 region_2 = Region(**{"id": 3, "level": 3, "name": "Country 2"}) region_2.administrativedivisions.append(admin_div_11) # region_3 is another country # it matches GAULS's admin_div_12 region_3 = Region(**{"id": 4, "level": 3, "name": "Country 3"}) region_3.administrativedivisions.append(admin_div_12) # Here's a quick, graphical recap: # # global_region -> admin_div_10 (region_1) -> admin_div_20 -> admin_div_31 # ` `-> admin_div_32 # ` # ` -> admin_div_11 (region_2) # `-> admin_div_13 # # region_3 = admin_div_12 category_eq_hig = HazardCategory.get(DBSession, "EQ", "HIG") category_eq_hig.general_recommendation = "General recommendation for EQ HIG" category_fl_hig = HazardCategory.get(DBSession, "FL", "HIG") # admin_div_31 has (EQ, HIGH) association = HazardCategoryAdministrativeDivisionAssociation( **{"hazardcategory": category_eq_hig}) association.hazardsets.append(hazardset1) admin_div_31.hazardcategories.append(association) # admin_div_31 has (RF, HIGH) admin_div_32.hazardcategories.append( HazardCategoryAdministrativeDivisionAssociation( **{"hazardcategory": category_fl_hig})) # admin_div_32 has (EQ, HIGH) admin_div_32.hazardcategories.append( HazardCategoryAdministrativeDivisionAssociation( **{"hazardcategory": category_eq_hig})) # admin_div_10 has (EQ, HIGH) admin_div_10.hazardcategories.append( HazardCategoryAdministrativeDivisionAssociation( **{"hazardcategory": category_eq_hig})) # admin_div_11 has no category (this is tested) # admin_div_12 has (EQ, HIGH) admin_div_12.hazardcategories.append( HazardCategoryAdministrativeDivisionAssociation( **{"hazardcategory": category_eq_hig})) # admin_div_13 has (EQ, HIGH) admin_div_13.hazardcategories.append( HazardCategoryAdministrativeDivisionAssociation( **{"hazardcategory": category_eq_hig})) climate_rec = ClimateChangeRecommendation( text="Climate change recommendation", hazardtype=HazardType.get(DBSession, "EQ")) climate_rec.associations.append( CcrAd(administrativedivision=admin_div_10, hazardtype=HazardType.get(DBSession, "EQ"))) DBSession.add(climate_rec) climate_rec = ClimateChangeRecommendation( text="Climate change recommendation 2", hazardtype=HazardType.get(DBSession, "EQ")) climate_rec.associations.append( CcrAd(administrativedivision=admin_div_11, hazardtype=HazardType.get(DBSession, "EQ"))) DBSession.add(climate_rec) technical_rec = TechnicalRecommendation( **{ "text": "Recommendation #1 for earthquake, applied to" " hazard categories HIG, MED and LOW" }) association = HazardCategoryTechnicalRecommendationAssociation(order=1) association.hazardcategory = category_eq_hig technical_rec.hazardcategory_associations.append(association) DBSession.add(technical_rec) technical_rec = TechnicalRecommendation(**{ "text": "Educational web resources on earthquakes and" " seismic hazard" }) association = HazardCategoryTechnicalRecommendationAssociation(order=1) association.hazardcategory = category_eq_hig technical_rec.hazardcategory_associations.append(association) DBSession.add(technical_rec) category_fl_med = HazardCategory.get(DBSession, "FL", "MED") category_fl_med.general_recommendation = "General recommendation for FL MED" admin_div_31.hazardcategories.append( HazardCategoryAdministrativeDivisionAssociation( **{"hazardcategory": category_fl_med})) DBSession.add(admin_div_31) admin_div_32.hazardcategories.append( HazardCategoryAdministrativeDivisionAssociation( **{"hazardcategory": category_fl_med})) DBSession.add(admin_div_32) # generic further resource for EQ: # it should be found on every EQ report page # (except admin_div_12 which is not linked with global region) further_resource = FurtherResource( **{ "text": "Educational web resources on earthquakes and" + " seismic hazard", "id": 3, }) association = HazardTypeFurtherResourceAssociation() association.hazardtype = hazardtype_eq association.region = global_region further_resource.hazardtype_associations.append(association) DBSession.add(further_resource) # further resource for EQ & region 1: # it should be found only on region 1 (and sub-divisions) page further_resource = FurtherResource(**{ "text": "Further resource for earthquake for region 1", "id": 5 }) association = HazardTypeFurtherResourceAssociation() association.hazardtype = hazardtype_eq association.region = region_1 further_resource.hazardtype_associations.append(association) DBSession.add(further_resource) # contact for EQ & admin_div_11: contact1 = Contact( **{ "name": "Contact name", "url": "http://domain.com", "phone": "0123456789", "email": "*****@*****.**", }) DBSession.add(contact1) association = CAdHt() association.hazardtype = hazardtype_eq association.administrativedivision = admin_div_10 association.contact = contact1 DBSession.add(association) # contact for EQ & admin_div_11: contact2 = Contact( **{ "name": "Contact name 2", "url": "http://domain.com", "phone": "0123456789", "email": "*****@*****.**", }) DBSession.add(contact1) association = CAdHt() association.hazardtype = hazardtype_eq association.administrativedivision = admin_div_10 association.contact = contact2 DBSession.add(association) Publication.new(DBSession) DBSession.flush()