def execute(self, da: DataAccess) -> ConfigInfo: spatial_units_repo = SpatialUnitInfoRepository(da) self._spatial_units = spatial_units_repo.list() deter_class_group_repo = DeterClassGroupRepository(da) self._deter_class_groups = deter_class_group_repo.list() self._most_recent_risk_indicators = self._get_most_recent_risk_indicators( da) return ConfigInfo(self._spatial_units, self._deter_class_groups, self._most_recent_risk_indicators)
def set_only_one_group(db): group = DeterClassGroup('C1') group.add_class('CICATRIZ_DE_QUEIMADA') group.add_class('DEGRADACAO') group.add_class('MINERACAO') group.add_class('DESMATAMENTO_CR') group.add_class('DESMATAMENTO_VEG') group.add_class('CS_DESORDENADO') group.add_class('CS_GEOMETRICO') group_repo = DeterClassGroupRepository(db) group_repo.add(group)
def test_uc_historical(): db = setdb( 'postgresql://*****:*****@localhost:5432/det_risk_uc_historic') set_class_groups(db) deter_repo = DeterRepository() deter_hist = DeterHistoricalRepository() units_repo = SpatialUnitInfoRepository(db) units = units_repo.list() sutablename = units[0].dataname as_attribute_name = units[0].as_attribute_name surepo = SpatialUnitDynamicMapperFactory.instance()\ .create_spatial_unit(sutablename, as_attribute_name) su = surepo.get() startdate = datetime.date(2020, 2, 1) enddate = datetime.date(2019, 11, 1) groups_repo = DeterClassGroupRepository(db) class_groups = groups_repo.list() uc = DetermineRiskIndicators(su, deter_repo, deter_hist, class_groups, startdate, enddate) model_indicators = uc.execute() rirepo = RiskIndicatorsRepository(sutablename, as_attribute_name, db) rirepo.save(model_indicators) indicators = rirepo.list() indicators_per_feature = {} expected_total_per_feature = determine_risk_indicators_results.total_per_feature_historical for ind in indicators: if ind.feature.id not in indicators_per_feature: indicators_per_feature[ind.feature.id] = 0 indicators_per_feature[ind.feature.id] += ind.percentage assert len(model_indicators) == len(indicators) == 1350 assert (len(indicators_per_feature) == len(expected_total_per_feature) == 177) # for i in indicators_per_feature: # print(f'{{\'id\': {i}, \'percentage\': {indicators_per_feature[i]}}},') for i in range(len(expected_total_per_feature)): assert expected_total_per_feature[i]['id'] in indicators_per_feature assert (round(expected_total_per_feature[i]['percentage'], 5) == round( indicators_per_feature[expected_total_per_feature[i]['id']], 5)) # for i in indicators: # print(f'{{\'id\': {i.feature.id}, \'percentage\': {i.percentage},' # + f'\'classname\': \'{i.classname}\', \'date\': \'{i.date}\'}},') expected_classname = determine_risk_indicators_results.deter_historical gp = Geoprocessing() for i in range(len(indicators)): assert expected_classname[i]['id'] == indicators[i].feature.id assert round(expected_classname[i]['percentage'], 5) == round(indicators[i].percentage, 5) assert expected_classname[i]['date'] == str(indicators[i].date) assert int(indicators[i].area * 100 / indicators[i].percentage)\ == int(gp.degrees_to_km2(indicators[i].feature.geom.area)) db.drop()
def _determine_risks(self, startdate): sus_repo = SpatialUnitInfoRepository(self._dataaccess) sus_info = sus_repo.list() enddate = self._get_enddate() groups_repo = DeterClassGroupRepository(self._dataaccess) class_groups = groups_repo.list() for su_info in sus_info: sutablename = su_info.dataname as_attribute_name = su_info.as_attribute_name surepo = SpatialUnitDynamicMapperFactory.instance()\ .create_spatial_unit(sutablename, as_attribute_name) su = surepo.get() rirepo = RiskIndicatorsRepository(sutablename, as_attribute_name, self._dataaccess) uc = DetermineRiskIndicators(su, self._deter_repo, [], class_groups, startdate, enddate) indicators = uc.execute() rirepo.overwrite_from_date(indicators, enddate)
def determine_risk_indicators(db): deter_alerts = DeterRepository() startdate = datetime.date(2021, 2, 28) enddate = datetime.date(2020, 12, 31) groups_repo = DeterClassGroupRepository(db) class_groups = groups_repo.list() units_repo = SpatialUnitInfoRepository(db) units = units_repo.list() for u in units: sutablename = u.dataname as_attribute_name = u.as_attribute_name surepo = SpatialUnitDynamicMapperFactory.instance()\ .create_spatial_unit(sutablename, as_attribute_name) su = surepo.get() uc = DetermineRiskIndicators(su, deter_alerts, None, class_groups, startdate, enddate) model_indicators = uc.execute() rirepo = RiskIndicatorsRepository(sutablename, as_attribute_name, db) rirepo.save(model_indicators)
def determine_risk_indicators(db): deter_alerts = DeterRepository() deter_hist = DeterHistoricalRepository() startdate = datetime.datetime.now().date() enddate = datetime.date(2017, 1, 1) groups_repo = DeterClassGroupRepository(db) class_groups = groups_repo.list() units_repo = SpatialUnitInfoRepository(db) units = units_repo.list() for u in units: sutablename = u.dataname print(f'Processing {sutablename} risk indicators...') as_attribute_name = u.as_attribute_name surepo = SpatialUnitDynamicMapperFactory.instance()\ .create_spatial_unit(sutablename, as_attribute_name) su = surepo.get() uc = DetermineRiskIndicators(su, deter_alerts, deter_hist, class_groups, startdate, enddate) model_indicators = uc.execute() rirepo = RiskIndicatorsRepository(sutablename, as_attribute_name, db) rirepo.save(model_indicators)
def test_unknown_classname(): db = DataAccessHelper.createdb( 'postgresql://*****:*****@localhost:5432/det_unk_class') sudata = { 'tablename': 'amz_states', 'shpname': 'amz_states_epsg_4326', 'as_attribute_name': 'NM_ESTADO' } DataAccessHelper.add_spatial_unit(db, sudata['tablename'], sudata['shpname'], sudata['as_attribute_name']) group_dg = DeterClassGroup('DG') group_dg.add_class('CICATRIZ_DE_QUEIMADA') group_dg.add_class('DEGRADACAO') group_ds = DeterClassGroup('DS') group_ds.add_class('MINERACAO') group_ds.add_class('DESMATAMENTO_CR') group_ds.add_class('DESMATAMENTO_VEG') group_repo = DeterClassGroupRepository(db) group_repo.add(group_dg) group_repo.add(group_ds) surepo = SpatialUnitDynamicMapperFactory.instance()\ .create_spatial_unit(sudata['tablename'], sudata['as_attribute_name']) su = surepo.get() startdate = datetime.date(2021, 2, 1) enddate = datetime.date(2021, 1, 1) deter_repo = DeterRepository() deter_hist = [] class_groups = group_repo.list() uc = DetermineRiskIndicators(su, deter_repo, deter_hist, class_groups, startdate, enddate) indicators = uc.execute() assert len(indicators) == 61 for ind in indicators: assert ind.classname == 'DG' or 'DS' db.drop()
def set_class_groups(db): group_dg = DeterClassGroup('DG') group_dg.add_class('CICATRIZ_DE_QUEIMADA') group_dg.add_class('DEGRADACAO') group_ds = DeterClassGroup('DS') group_ds.add_class('MINERACAO') group_ds.add_class('DESMATAMENTO_CR') group_ds.add_class('DESMATAMENTO_VEG') group_cs = DeterClassGroup('CS') group_cs.add_class('CS_DESORDENADO') group_cs.add_class('CS_GEOMETRICO') group_repo = DeterClassGroupRepository(db) group_repo.add(group_dg) group_repo.add(group_ds) group_repo.add(group_cs)