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 test_intersection_area(): url = 'postgresql://*****:*****@localhost:5432/intersection_area' db = AlchemyDataAccess() db.connect(url) db.create(True) db.add_postgis_extension() shpfilepath = os.path.join(os.path.dirname(__file__), '../../data', 'csAmz_150km_epsg_4326.shp') geoprocess = Geoprocessing() geoprocess.export_shp_to_postgis(shpfilepath, 'csAmz_150km', 'suid', db.engine, True) add_suid('csAmz_150km', db.engine) SpatialUnitDynamicMapperFactory.instance().dataaccess = db SpatialUnitDynamicMapperFactory.instance().add_class_mapper('csAmz_150km') sunit = SpatialUnitDynamicMapperFactory.instance().create_spatial_unit( 'csAmz_150km', 'id') feats = sunit.list() deter = DeterRepository() a1 = deter.get(59241) a1geom = a1.geom for f in feats: fgeom = f.geom if fgeom.intersects(a1geom): area_info1 = geoprocess.intersection_area(fgeom, a1geom) area_info2 = geoprocess.intersection_area(a1geom, fgeom) assert area_info1['percentage'] == 0.001008271044465656 assert area_info2['percentage'] == 100 assert area_info1['area'] == area_info2[ 'area'] == 0.22686098500569116 assert fgeom.intersection(a1geom).area == a1geom.area db.drop()
def _get_most_recent_risk_indicators(self, da): result = {} SpatialUnitDynamicMapperFactory.instance().dataaccess = da for sui in self._spatial_units: dataname = sui.dataname SpatialUnitDynamicMapperFactory.instance().add_class_mapper( dataname) rirepo = RiskIndicatorsRepository(dataname, sui.as_attribute_name, da) result[dataname] = rirepo.get_most_recent() return result
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)
def setdb(url): db = AlchemyDataAccess() db.connect(url) db.create_all(True) db.add_postgis_extension() SpatialUnitDynamicMapperFactory.instance().dataaccess = db return db
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 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 test_export_shp_to_postgis(): url = 'postgresql://*****:*****@localhost:5432/shp_to_postgis' db = AlchemyDataAccess() db.connect(url) db.create(True) db.add_postgis_extension() shpfilepath = os.path.join(os.path.dirname(__file__), '../../data', 'csAmz_150km_epsg_4326.shp') geoprocess = Geoprocessing() geoprocess.export_shp_to_postgis(shpfilepath, 'csAmz_150km', 'suid', db.engine, True) add_suid('csAmz_150km', db.engine) SpatialUnitDynamicMapperFactory.instance().dataaccess = db SpatialUnitDynamicMapperFactory.instance().add_class_mapper('csAmz_150km') sunit = SpatialUnitDynamicMapperFactory.instance().create_spatial_unit( 'csAmz_150km', 'id') cells = sunit.list() assert len(cells) == 240 assert cells[0].id == 0 assert cells[-1].id == 239 db.drop()
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()