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

211 lines
8 KiB
Python
Raw Normal View History

2022-12-09 12:01:55 +01:00
from datetime import datetime
from os.path import exists
2022-11-16 16:10:30 +01:00
2022-03-23 22:54:04 +01:00
from ipe_fetcher.model import AreaCoordinates, Building, FAIEligibilityStatus
2022-11-16 16:10:30 +01:00
from ipe_fetcher.sqlite_connector.cursor import getCursorWithSpatialite, getBasicCursor
2022-03-23 22:54:04 +01:00
AXIONE_ETAT_DEPLOYE = "DEPLOYE"
2022-12-09 12:01:55 +01:00
AXIONE_ETAT_DEPLOYE_NON_COMMANDABLE = "DEPLOYE MAIS NON COMMANDABLE"
2022-03-23 22:54:04 +01:00
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"
2022-11-16 16:10:30 +01:00
AXIONE_REFIMM_TABLE_NAME = "refimm"
2022-03-23 22:54:04 +01:00
2023-01-11 15:35:12 +01:00
AXIONE_MAX_AREA = 0.08
2022-03-23 22:54:04 +01:00
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")
2022-11-16 16:10:30 +01:00
try:
cur = getBasicCursor(self.db_axione_ipe_path)
except Exception as err:
print("Error while connecting to DB: ", err)
raise "Could not connect to axione DB"
2023-01-11 16:44:06 +01:00
cur.execute(
f""" SELECT count(name) FROM sqlite_master WHERE type='table' AND name='{AXIONE_REFIMM_TABLE_NAME}' """
)
2022-11-16 16:10:30 +01:00
self.db_name_refimm = db_name
if cur.fetchone()[0] == 1:
self.db_name_refimm = AXIONE_REFIMM_TABLE_NAME
@staticmethod
def _get_etat_priority(etat_imm):
2022-12-09 12:01:55 +01:00
if etat_imm in AXIONE_ETAT_DEPLOYE:
return 0
2022-12-09 12:01:55 +01:00
elif etat_imm == AXIONE_ETAT_DEPLOYE_NON_COMMANDABLE:
return 1
2022-12-09 12:01:55 +01:00
elif etat_imm == AXIONE_ETAT_DEPLOIEMENT:
return 2
2022-12-09 12:01:55 +01:00
elif etat_imm == AXIONE_ETAT_RAD_DEPLOIEMENT:
return 3
2022-12-09 12:01:55 +01:00
elif etat_imm != AXIONE_ETAT_ABANDONNE:
2023-01-11 15:35:12 +01:00
return 20
2022-12-09 12:01:55 +01:00
else:
2023-01-11 15:35:12 +01:00
return 21
2022-03-23 22:54:04 +01:00
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]
2023-01-11 15:35:12 +01:00
if req_area <= AXIONE_MAX_AREA:
2022-03-23 22:54:04 +01:00
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,
2022-12-09 12:01:55 +01:00
CommuneImmeuble,
DateDebutAcceptationCmdAcces
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-11-16 16:10:30 +01:00
res = cur.fetchall()
2022-03-23 22:54:04 +01:00
if not existing_buildings:
existing_buildings = dict()
buildings = existing_buildings
2022-11-16 16:10:30 +01:00
for b in res:
2022-03-23 22:54:04 +01:00
etatImm = b[3]
idImm = b[2]
2022-04-12 20:39:33 +02:00
isEligible = etatImm == AXIONE_ETAT_DEPLOYE
2022-12-09 12:01:55 +01:00
date_debut = b[9]
reasonNotEligible = "" if isEligible else "Pas encore deploye"
date_commandable = ""
# C'est bien déployé, cependant ce n'est pas encore commandable (donc bientôt et on a la date)
# On laisse isEligible = True, côté JS il faut regarder le statut pour laj l'affichage en conséquence
2023-01-11 15:35:12 +01:00
2023-01-11 16:44:06 +01:00
if date_debut:
2022-12-09 12:01:55 +01:00
try:
2023-01-11 16:44:06 +01:00
date_formatted = datetime.strptime(date_debut, "%Y%m%d").date()
2023-01-11 15:35:12 +01:00
2022-12-09 12:01:55 +01:00
if date_formatted >= datetime.now().date():
2023-01-11 16:44:06 +01:00
if isEligible:
etatImm = AXIONE_ETAT_DEPLOYE_NON_COMMANDABLE
date_commandable = date_formatted.strftime("%d/%m/%Y")
2022-12-09 12:01:55 +01:00
except ValueError as err:
2023-01-11 16:44:06 +01:00
print(
"Error while mainpulating DateDebutAcceptationCmdAcces from Axione DB: ",
err,
)
2022-12-09 12:01:55 +01:00
2022-03-23 22:54:04 +01:00
aquilenetEligStatus = FAIEligibilityStatus(
isEligible=isEligible,
ftthStatus=etatImm,
2022-12-09 12:01:55 +01:00
reasonNotEligible=reasonNotEligible,
2023-01-11 16:44:06 +01:00
dateCommandable=date_commandable,
2022-03-23 22:54:04 +01:00
)
etat_priority = self._get_etat_priority(etatImm)
2022-03-23 22:54:04 +01:00
if buildings.get(idImm):
buildings[idImm]["aquilenetEligStatus"] = aquilenetEligStatus
2023-01-11 16:44:06 +01:00
buildings[idImm]["etat_imm_priority"] = etat_priority
if buildings[idImm].get("found_in"):
buildings[idImm]["found_in"].append("axione")
2022-04-12 20:39:33 +02:00
else:
2023-01-11 16:44:06 +01:00
buildings[idImm]["found_in"] = ["axione"]
2022-12-09 12:01:55 +01:00
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="",
2023-01-11 16:44:06 +01:00
found_in=["axione"],
etat_imm_priority=etat_priority,
2022-03-23 22:54:04 +01:00
aquilenetEligStatus=aquilenetEligStatus,
2023-01-11 16:44:06 +01: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")
2022-11-16 16:10:30 +01:00
def get_eligibilite_per_id_immeuble(self, id_immeuble: str):
# Try to get cursor on Axione database
try:
cur = getBasicCursor(self.db_axione_ipe_path)
except Exception as err:
print("Error while connecting to DB: ", err)
raise "Could not get Axione data"
2023-01-11 16:44:06 +01:00
try:
cur.execute(
f"""
SELECT EtatImmeuble, DateDebutAcceptationCmdAcces
FROM {self.db_name_refimm}
WHERE IdentifiantImmeuble == '{id_immeuble}'
"""
)
res = cur.fetchone()
if res:
imm_elig = res[0]
isEligible = imm_elig == AXIONE_ETAT_DEPLOYE
reasonNotEligible = "" if isEligible else "Pas encore deploye"
dateCommandable = res[1]
else:
imm_elig = "NOT_AXIONE"
isEligible = False
reasonNotEligible = "Axione ne gere pas ce batiment"
dateCommandable = ""
except Exception as err:
2022-11-16 16:10:30 +01:00
imm_elig = "NOT_AXIONE"
2023-01-11 16:44:06 +01:00
isEligible = False
2022-11-16 16:10:30 +01:00
reasonNotEligible = "Axione ne gere pas ce batiment"
2023-01-11 16:44:06 +01:00
dateCommandable = ""
2022-11-16 16:10:30 +01:00
eligStatus = FAIEligibilityStatus(
isEligible=isEligible,
ftthStatus=imm_elig,
reasonNotEligible=reasonNotEligible,
2023-01-11 16:44:06 +01:00
dateCommandable=dateCommandable,
2022-11-16 16:10:30 +01:00
)
return eligStatus