Axione-IPE-Viewer/webapp/ipe_fetcher/axione.py

102 lines
4 KiB
Python
Raw Normal View History

2022-03-23 22:54:04 +01:00
from ipe_fetcher.model import AreaCoordinates, Building, FAIEligibilityStatus
from ipe_fetcher.sqlite_connector.cursor import getCursorWithSpatialite
from os.path import exists
AXIONE_ETAT_DEPLOYE = "DEPLOYE"
AXIONE_ETAT_DEPLOIEMENT = "EN COURS DE DEPLOIEMENT"
AXIONE_ETAT_ABANDONNE = "ABANDONNE"
AXIONE_ETAT_CIBLE = "CIBLE"
AXIONE_ETAT_SIGNE = "SIGNE"
AXIONE_ETAT_RAD_DEPLOIEMENT = "RAD EN COURS DE DEPLOIEMENT"
AXIONE_ETAT_RACCORDABLE_DEMANDE = "RACCORDABLE DEMANDE"
class Axione:
2022-04-12 20:39:33 +02:00
def __init__(self, db_axione_ipe_path: str, db_name: str):
2022-03-23 22:54:04 +01:00
self.db_axione_ipe_path = db_axione_ipe_path
2022-04-12 20:39:33 +02:00
self.db_name = db_name
2022-03-23 22:54:04 +01:00
# Check at least that the file exists
if not exists(self.db_axione_ipe_path):
raise ValueError(f"File {self.db_axione_ipe_path} does not exist")
def getAreaBuildings(
self, areaCoordinates: AreaCoordinates, existing_buildings: dict
) -> dict:
cur = None
# Try to get cursor on Axone database
try:
cur = getCursorWithSpatialite(self.db_axione_ipe_path)
except Exception as err:
print("Error while connecting to DB: ", err)
raise "Could not get Axione data"
# Let's first see how big is the area we're about to query.
# If it's too big, abort the request to prevent a server DOS.
cur.execute(
"""
SELECT Area(BuildMBR(:swx,:swy,:nex,:ney,4326))
""",
areaCoordinates,
)
req_area = cur.fetchone()[0]
if req_area <= 0.08:
cur.execute(
2022-04-12 20:39:33 +02:00
f"""
2022-03-23 22:54:04 +01:00
SELECT
X(ImmeubleGeoPoint),
Y(ImmeubleGeoPoint),
2022-04-12 20:39:33 +02:00
IdentifiantImmeuble,
EtatImmeuble,
NumeroVoieImmeuble,
TypeVoieImmeuble,
2022-04-12 23:17:40 +02:00
NomVoieImmeuble,
CodePostalImmeuble,
CommuneImmeuble
2022-04-12 20:39:33 +02:00
FROM {self.db_name}
2022-03-23 22:54:04 +01:00
WHERE ROWID IN (
SELECT ROWID FROM SpatialIndex
2022-04-12 20:39:33 +02:00
WHERE f_table_name = '{self.db_name}' AND
2022-03-23 22:54:04 +01:00
search_frame = BuildMBR(:swx, :swy, :nex, :ney, 4326))
""",
areaCoordinates,
)
2022-04-12 20:39:33 +02:00
2022-03-23 22:54:04 +01:00
if not existing_buildings:
existing_buildings = dict()
buildings = existing_buildings
for b in cur.fetchall():
etatImm = b[3]
idImm = b[2]
2022-04-12 20:39:33 +02:00
isEligible = etatImm == AXIONE_ETAT_DEPLOYE
2022-03-23 22:54:04 +01:00
aquilenetEligStatus = FAIEligibilityStatus(
isEligible=isEligible,
ftthStatus=etatImm,
2022-04-12 23:17:40 +02:00
reasonNotEligible="" if isEligible else "Pas encore deploye",
2022-03-23 22:54:04 +01:00
)
if buildings.get(idImm):
buildings[idImm]["aquilenetEligStatus"] = aquilenetEligStatus
2022-04-12 20:39:33 +02:00
if buildings[idImm].get('found_in'):
buildings[idImm]['found_in'].append("axione")
else:
buildings[idImm]['found_in'] = ["axione"]
2022-03-23 22:54:04 +01:00
else:
building = Building(
x=b[0],
y=b[1],
idImm=idImm,
numVoieImm=b[4],
typeVoieImm=b[5],
nomVoieImm=b[6],
2022-04-12 23:17:40 +02:00
codePostal=b[7],
commune=b[8],
2022-04-12 20:39:33 +02:00
bat_info="",
found_in = ["axione"],
2022-03-23 22:54:04 +01:00
aquilenetEligStatus=aquilenetEligStatus,
2022-04-12 23:17:40 +02:00
fdnEligStatus=FAIEligibilityStatus(isEligible=False, reasonNotEligible="", ftthStatus=""),
othersEligStatus=FAIEligibilityStatus(isEligible=False, reasonNotEligible="", ftthStatus=""),
2022-03-23 22:54:04 +01:00
)
buildings[idImm] = building
return buildings
else:
raise ValueError("The requested area is too wide, please reduce it")