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

188 lines
7.3 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
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"
cur.execute(f''' SELECT count(name) FROM sqlite_master WHERE type='table' AND name='{AXIONE_REFIMM_TABLE_NAME}' ''')
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:
return 4
2022-12-09 12:01:55 +01:00
else:
return 5
2022-11-16 16:10:30 +01:00
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]
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,
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
if isEligible and date_debut:
try:
date_formatted = datetime.strptime(date_debut, '%Y%m%d').date()
if date_formatted >= datetime.now().date():
etatImm = AXIONE_ETAT_DEPLOYE_NON_COMMANDABLE
date_commandable = date_formatted.strftime('%d/%m/%Y')
except ValueError as err:
print("Error while mainpulating DateDebutAcceptationCmdAcces from Axione DB: ", err)
2022-03-23 22:54:04 +01:00
aquilenetEligStatus = FAIEligibilityStatus(
isEligible=isEligible,
ftthStatus=etatImm,
2022-12-09 12:01:55 +01:00
reasonNotEligible=reasonNotEligible,
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
buildings[idImm]['etat_imm_priority'] = etat_priority
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-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="",
found_in = ["axione"],
etat_imm_priority=etat_priority,
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")
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"
cur.execute(
f"""
SELECT EtatImmeuble
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"
else:
isEligible = False
imm_elig = "NOT_AXIONE"
reasonNotEligible = "Axione ne gere pas ce batiment"
eligStatus = FAIEligibilityStatus(
isEligible=isEligible,
ftthStatus=imm_elig,
reasonNotEligible=reasonNotEligible,
)
return eligStatus