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 setdb(url): db = AlchemyDataAccess() db.connect(url) db.create_all(True) db.add_postgis_extension() SpatialUnitDynamicMapperFactory.instance().dataaccess = db return db
def setdb(url): db = AlchemyDataAccess() db.connect(url) db.create_all(True) db.add_postgis_extension() set_spatial_units(db) return db
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 __init__(self, config, params): self._config = config self._db = AlchemyDataAccess() self._query_limit = 20 self._classname = params['className'] self._spatial_unit = params['spatialUnit'] self._start_date = params['startDate'] self._temporal_unit = params['tempUnit'] self._start_period_date = self.get_prev_date_temporal_unit( temporal_unit=self._temporal_unit) self._name = params['click']['name'].replace('|', ' ') self._tableinfo = { 'csAmz_150km': { 'description': 'Célula 150x150 Km²', 'key': 'id' }, 'csAmz_300km': { 'description': 'Célula 300x300 Km²', 'key': 'id' }, 'amz_municipalities': { 'description': 'Município', 'key': 'nm_municip' }, 'amz_states': { 'description': 'Estado', 'key': 'NM_ESTADO' } } self._classes = pd.DataFrame({ 'code': pd.Series(['DS', 'DG', 'CS', 'MN'], dtype='str'), 'name': pd.Series([ 'Desmatamento', 'Degradação', 'Corte-Seletivo', 'Mineração' ], dtype='str'), 'color': pd.Series(['#0d0887', '#46039f', '#7201a8', '#9c179e'], dtype='str') }) self._temporal_units = { "7d": "Agregado Semanal", "15d": "Agregado 15 Dias", "1m": "Agregado Mensal", "3m": "Agregado 3 Meses", "1y": "Agregado Anual" } self._temporal_unit_sql = { 7: '''select TO_CHAR(date, 'YYYY/WW') as period,classname,sum(area) as area from "{0}_land_use" a inner join "{0}" b on a.suid = b.suid where {1} group by TO_CHAR(date, 'YYYY/WW'), classname order by 1 desc limit {2}''', 15: '''select concat(TO_CHAR(date, 'YYYY'),'/',to_char(TO_CHAR(date, 'WW')::int/2+1,'FM00')) as period, classname,sum(area) as area from "{0}_land_use" a inner join "{0}" b on a.suid = b.suid where {1} group by concat(TO_CHAR(date, 'YYYY'),'/',to_char(TO_CHAR(date, 'WW')::int/2+1,'FM00')), classname order by 1 desc limit {2}''', 31: '''select TO_CHAR(date, 'YYYY/MM') as period,classname,sum(area) as area from "{0}_land_use" a inner join "{0}" b on a.suid = b.suid where {1} group by TO_CHAR(date, 'YYYY/MM'), classname order by 1 desc limit {2}''', 124: '''select TO_CHAR(date, 'YYYY/Q') as period,classname, sum(area) as area from "{0}_land_use" a inner join "{0}" b on a.suid = b.suid where {1} group by TO_CHAR(date, 'YYYY/Q'),classname order by 1 desc limit {2}''', 366: '''select TO_CHAR(date, 'YYYY') as period,classname,sum(area) as area from "{0}_land_use" a inner join "{0}" b on a.suid = b.suid where {1} group by TO_CHAR(date, 'YYYY'),classname order by 1 desc limit {2}''' }
from flask import Flask, Blueprint from flask_cors import CORS from ams.dataaccess import AlchemyDataAccess from .config import Config db = AlchemyDataAccess() bp = Blueprint('', __name__) def create_app(config=Config): app = Flask(__name__) cors = CORS(app) app.config.from_object(config) app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False db.connect(config.DATABASE_URL) app.register_blueprint(bp) return app from . import routes
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()
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_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 createdb(url: str) -> DataAccess: db = AlchemyDataAccess() db.connect(url) db.create_all(True) db.add_postgis_extension() return db
from ams.usecases import DeterDailyUpdate, Initializer from ams.repository import (DeterRepository, SpatialUnitInfoRepository) from ams.dataaccess import AlchemyDataAccess db_url = 'postgresql://*****:*****@localhost:5432/AMS' da = AlchemyDataAccess() da.connect(db_url) su_info_repo = SpatialUnitInfoRepository(da) init = Initializer(su_info_repo) init.execute(da) deter_repo = DeterRepository() update = DeterDailyUpdate(deter_repo, 60) update.execute(da) update.terminate()