Пример #1
0
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)
Пример #2
0
def test_update_on_startup(mocker):
	db = DataAccessHelper.createdb('postgresql://*****:*****@localhost:5432/det_autoup_onstart')
	DataAccessHelper.add_class_groups(db)
	susdata = [{'tablename': 'csAmz_150km', 
				'shpname': 'csAmz_150km_epsg_4326', 
				'as_attribute_name': 'id'}]
	for sudata in susdata:
		DataAccessHelper.add_spatial_unit(db, sudata['tablename'], sudata['shpname'])
	startdate = date(2021, 2, 28)
	enddate = date(2021, 1, 1)
	DataAccessHelper.determine_risk_indicators(db, startdate, enddate)
	deter_repo = DeterRepository()
	deter_url = 'postgresql://*****:*****@localhost:5432/DETER-B'
	date_now = datetime.now()
	DataAccessHelper.del_deter_data(deter_url, date_now)  # clean if last test fail
	rirepo = RiskIndicatorsRepository(susdata[0]['tablename'], susdata[0]['as_attribute_name'], db)	
	indicators_1 = rirepo.list()
	assert len(indicators_1) == 357
	assert indicators_1[0].date.isoformat() == '2021-02-28'
	assert indicators_1[0].percentage == 0.0004478673808316384
	assert indicators_1[0].area == 0.10077016068752663
	assert indicators_1[0].classname == 'DS'
	DataAccessHelper.add_deter_data(deter_url, 5, date_now)
	uc = DeterDailyUpdate(deter_repo, 1)
	mocker.patch.object(DeterDailyUpdate, '_get_enddate', return_value=date(2021, 1, 1))
	uc.execute(db)
	assert uc.last_update() == date_now.date().isoformat()
	indicators_2 = rirepo.list()
	assert len(indicators_2) == 358	
	yesterday_indicator = get_newest_indicator(indicators_2, date_now.date())
	assert yesterday_indicator.date.isoformat() == date_now.date().isoformat()
	assert round(yesterday_indicator.percentage, 5) == round(0.0028141977493854964, 5)
	assert round(yesterday_indicator.area, 5) == 0.63319
	assert yesterday_indicator.classname == 'DS'	
	uc.terminate()
	DataAccessHelper.del_deter_data(deter_url, date_now)
	db.drop()
def test_uc_basic():
    db = setdb(
        'postgresql://*****:*****@localhost:5432/det_risk_uc_basic')
    set_only_one_group(db)
    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()
    deter_repo = DeterRepository()
    startdate = datetime.date(2021, 1, 1)
    enddate = datetime.date(2020, 12, 1)
    groups_repo = DeterClassGroupRepository(db)
    class_groups = groups_repo.list()
    uc = DetermineRiskIndicators(su, deter_repo, None, class_groups, startdate,
                                 enddate)
    model_indicators = uc.execute()
    rirepo = RiskIndicatorsRepository(sutablename, as_attribute_name, db)
    rirepo.save(model_indicators)
    rilast = rirepo.get_most_recent()
    assert rilast.date == startdate
    indicators = rirepo.list()
    indicators_per_feature = {}
    expected_total_per_feature = determine_risk_indicators_results.total_per_feature
    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) == 322
    assert (len(indicators_per_feature) == len(expected_total_per_feature) ==
            132)
    for i in range(len(expected_total_per_feature)):
        # print(f'{{\'id\': {i.feature.id}, \'percentage\': {i.percentage}}},')
        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))
    expected_per_day = determine_risk_indicators_results.percentage_per_day
    gp = Geoprocessing()
    for i in range(len(indicators)):
        # print(f'{{\'id\': {indicators[i].feature.id}, \'percentage\': {indicators[i].percentage},'
        # 	+ f' \'area\': {indicators[i].area}, \'date\': \'{indicators[i].date}\'}},')
        assert int(indicators[i].area * 100 / indicators[i].percentage)\
          == int(gp.degrees_to_km2(indicators[i].feature.geom.area))
        assert expected_per_day[i]['id'] == indicators[i].feature.id
        assert round(expected_per_day[i]['percentage'],
                     5) == round(indicators[i].percentage, 5)
        assert expected_per_day[i]['date'] == str(indicators[i].date)
    db.drop()