def test_as_attribute_name(): url = 'postgresql://*****:*****@localhost:5432/as_attrname' db = AlchemyDataAccess() db.connect(url) db.create(True) db.add_postgis_extension() db.create_all_tables() SpatialUnitDynamicMapperFactory.instance().dataaccess = db tablename1 = 'amz_states' shpfilepath1 = os.path.join(os.path.dirname(__file__), '../../data', 'amz_states_epsg_4326.shp') as_attribute_name = 'NM_ESTADO' gp = Geoprocessing() sus1 = SpatialUnitInfoRepository(db) uc1 = AddSpatialUnit(tablename1, shpfilepath1, as_attribute_name, sus1, SpatialUnitDynamicMapperFactory.instance(), gp) su1 = uc1.execute(db) sus_list = sus1.list() assert len(sus_list) == 1 assert sus_list[0].dataname == tablename1 assert sus_list[0].as_attribute_name == 'NM_ESTADO' assert sus_list[0].centroid.lat == -6.384962796500002 assert sus_list[0].centroid.lng == -58.97111531179317 surepo1 = SpatialUnitDynamicMapperFactory.instance()\ .create_spatial_unit(tablename1, as_attribute_name) su1 = surepo1.get() assert len(su1.features) == 13 assert su1.features[0].name == 'ACRE' assert su1.features[1].name == 'AMAPĂ' db.drop()
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 _has_indicator_dalayed(self): sus_repo = SpatialUnitInfoRepository(self._dataaccess) sus_info = sus_repo.list() for su_info in sus_info: sutablename = su_info.dataname as_attribute_name = su_info.as_attribute_name rirepo = RiskIndicatorsRepository(sutablename, as_attribute_name, self._dataaccess) ri_most_recent = rirepo.get_most_recent() if self._deter_last_row.date > ri_most_recent.date: return True return False
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 __init__(self, da: DataAccess, every_in_minutes: int): deter_repo = DeterRepository() su_info_repo = SpatialUnitInfoRepository(da) init = Initializer(su_info_repo) init.execute(da) deter_daily_update = DeterDailyUpdate(deter_repo, every_in_minutes) deter_daily_update.execute(da)
def set_spatial_units(db): data_path = os.path.join(os.path.dirname(__file__), '../data') sus = [{ 'tablename': 'csAmz_150km', 'shpfile': f'{data_path}/csAmz_150km_epsg_4326.shp', 'as_attribute_name': 'id' }, { 'tablename': 'csAmz_300km', 'shpfile': f'{data_path}/csAmz_300km_epsg_4326.shp', 'as_attribute_name': 'id' }, { 'tablename': 'amz_states', 'shpfile': f'{data_path}/amz_states_epsg_4326.shp', 'as_attribute_name': 'NM_ESTADO' }, { 'tablename': 'amz_municipalities', 'shpfile': f'{data_path}/amz_municipalities_epsg_4326.shp', 'as_attribute_name': 'nm_municip' }] gp = Geoprocessing() sus_info_repo = SpatialUnitInfoRepository(db) for su in sus: uc = AddSpatialUnit(su['tablename'], su['shpfile'], su['as_attribute_name'], sus_info_repo, SpatialUnitDynamicMapperFactory.instance(), gp) uc.execute(db)
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 test_add_two_spatial_units(): url = 'postgresql://*****:*****@localhost:5432/two_sus' db = AlchemyDataAccess() db.connect(url) db.create(True) db.add_postgis_extension() db.create_all_tables() SpatialUnitDynamicMapperFactory.instance().dataaccess = db tablename1 = 'csAmz_150km' tablename2 = 'csAmz_300km' as_attribute_name = 'id' shpfilepath1 = os.path.join(os.path.dirname(__file__), '../../data', 'csAmz_150km_epsg_4326.shp') shpfilepath2 = os.path.join(os.path.dirname(__file__), '../../data', 'csAmz_300km_epsg_4326.shp') gp = Geoprocessing() sus1 = SpatialUnitInfoRepository(db) uc1 = AddSpatialUnit(tablename1, shpfilepath1, as_attribute_name, sus1, SpatialUnitDynamicMapperFactory.instance(), gp) uc2 = AddSpatialUnit(tablename2, shpfilepath2, as_attribute_name, sus1, SpatialUnitDynamicMapperFactory.instance(), gp) su1 = uc1.execute(db) su2 = uc2.execute(db) sus2 = SpatialUnitInfoRepository(db) sus_list = sus2.list() assert len(sus_list) == 2 assert sus_list[0].dataname == tablename1 assert sus_list[1].dataname == tablename2 assert sus_list[0].as_attribute_name == 'id' assert sus_list[1].as_attribute_name == 'id' assert sus_list[0].centroid.lat == -5.491382969006503 assert sus_list[0].centroid.lng == -58.467185764253415 assert sus_list[1].centroid.lat == -5.491382969006503 assert sus_list[1].centroid.lng == -57.792239759933764 surepo1 = SpatialUnitDynamicMapperFactory.instance()\ .create_spatial_unit(tablename1, as_attribute_name) surepo2 = SpatialUnitDynamicMapperFactory.instance()\ .create_spatial_unit(tablename2, as_attribute_name) su1 = surepo1.get() su2 = surepo2.get() assert len(su1.features) == 240 assert len(su2.features) == 70 db.drop()
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 set_spatial_units(db): sutablename = 'csAmz_150km' shpfilepath = os.path.join(os.path.dirname(__file__), '../../data', 'csAmz_150km_epsg_4326.shp') as_attribute_name = 'id' geoprocess = Geoprocessing() SpatialUnitDynamicMapperFactory.instance().dataaccess = db sunits = SpatialUnitInfoRepository(db) uc1 = AddSpatialUnit(sutablename, shpfilepath, as_attribute_name, sunits, SpatialUnitDynamicMapperFactory.instance(), geoprocess) uc1.execute(db) SpatialUnitDynamicMapperFactory.instance().add_class_mapper(sutablename)
def set_spatial_units(db): tablename1 = 'csAmz_150km' tablename2 = 'csAmz_300km' shpfilepath1 = os.path.join(os.path.dirname(__file__), '../../data', 'csAmz_150km_epsg_4326.shp') shpfilepath2 = os.path.join(os.path.dirname(__file__), '../../data', 'csAmz_300km_epsg_4326.shp') as_attribute_name = 'id' gp = Geoprocessing() sus1 = SpatialUnitInfoRepository(db) uc1 = AddSpatialUnit(tablename1, shpfilepath1, as_attribute_name, sus1, SpatialUnitDynamicMapperFactory.instance(), gp) uc2 = AddSpatialUnit(tablename2, shpfilepath2, as_attribute_name, sus1, SpatialUnitDynamicMapperFactory.instance(), gp) uc1.execute(db) uc2.execute(db)
import sys sys.path.insert(0,'..') import warnings warnings.filterwarnings("ignore") from ams.usecases import DeterDailyUpdate from ams.usecases import Initializer from ams.repository import (DeterRepository, SpatialUnitInfoRepository) from ams.dataaccess import AlchemyDataAccess from webapp.app.config import Config from ams.usecases.classify_deter_polygons import ClassifyDeterPolygons da = AlchemyDataAccess() da.connect(Config.DATABASE_URL) su_info_repo = SpatialUnitInfoRepository(da) init = Initializer(su_info_repo) init.execute(da) deter_repo = DeterRepository() update = DeterDailyUpdate(deter_repo, -1, Config.DATABASE_URL) update.execute(da, True) class_deter_polys = ClassifyDeterPolygons(Config.DATABASE_URL, Config.INPUT_GEOTIFF_FUNDIARY_STRUCTURE) class_deter_polys.execute()