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

120 lines
4.5 KiB
Python
Raw Normal View History

from os.path import exists
2022-04-12 20:39:33 +02:00
from ipe_fetcher.model import AreaCoordinates, Building, FAIEligibilityStatus
from ipe_fetcher.sqlite_connector.cursor import getCursorWithSpatialite
ARCEP_ETAT_DEPLOYE = "deploye"
class Arcep:
def __init__(self, db_arcep_ipe_path: str, db_name: str):
self.db_arcep_ipe_path = db_arcep_ipe_path
self.db_name = db_name
# Check at least that the file exists
if not exists(self.db_arcep_ipe_path):
raise ValueError(f"File {self.db_arcep_ipe_path} does not exist")
@staticmethod
def _get_etat_priority(etat_imm):
if etat_imm == ARCEP_ETAT_DEPLOYE:
return 0
elif etat_imm == "en cours de deploiement":
return 1
elif etat_imm != "abandonne":
return 3
else:
return 4
2022-04-12 20:39:33 +02:00
def getAreaBuildings(
self, areaCoordinates: AreaCoordinates, existing_buildings: dict
) -> dict:
cur = None
# Try to get cursor on Axone database
try:
cur = getCursorWithSpatialite(self.db_arcep_ipe_path)
except Exception as err:
print("Error while connecting to DB: ", err)
raise "Could not get ARCEP 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(
f"""
SELECT
X(ImmeubleGeoPoint),
Y(ImmeubleGeoPoint),
imb_id,
imb_etat,
num_voie,
type_voie,
nom_voie,
2022-04-12 23:17:40 +02:00
batiment,
code_poste,
nom_com
2022-04-12 20:39:33 +02:00
FROM {self.db_name}
WHERE ROWID IN (
SELECT ROWID FROM SpatialIndex
WHERE f_table_name = '{self.db_name}' AND
search_frame = BuildMBR(:swx, :swy, :nex, :ney, 4326))
""",
areaCoordinates,
)
if not existing_buildings:
existing_buildings = dict()
buildings = existing_buildings
for b in cur.fetchall():
x=b[0]
y=b[1]
idImm = b[2]
etatImm = b[3]
numVoieImm=b[4]
typeVoieImm=b[5]
nomVoieImm=b[6]
bat_info=b[7]
2022-04-12 23:17:40 +02:00
codePostal=b[8]
commune=b[9]
2022-04-12 20:39:33 +02:00
isEligible = etatImm == ARCEP_ETAT_DEPLOYE
othersEligStatus = FAIEligibilityStatus(
isEligible=isEligible,
ftthStatus=etatImm,
reasonNotEligible=None if isEligible else "Pas encore deploye",
)
etat_priority = self._get_etat_priority(etatImm)
2022-04-12 20:39:33 +02:00
if buildings.get(idImm):
buildings[idImm]["othersEligStatus"] = othersEligStatus
buildings[idImm]["bat_info"] = bat_info
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("arcep")
else:
buildings[idImm]['found_in'] = ["arcep"]
else:
building = Building(
x=x,
y=y,
idImm=idImm,
numVoieImm=numVoieImm,
typeVoieImm=typeVoieImm,
nomVoieImm=nomVoieImm,
2022-04-12 23:17:40 +02:00
codePostal=codePostal,
commune=commune,
2022-04-12 20:39:33 +02:00
bat_info=bat_info,
found_in = ["arcep"],
etat_imm_priority=etat_priority,
2022-04-12 23:17:40 +02:00
aquilenetEligStatus=FAIEligibilityStatus(isEligible=False, reasonNotEligible="", ftthStatus=""),
fdnEligStatus=FAIEligibilityStatus(isEligible=False, reasonNotEligible="", ftthStatus=""),
2022-04-12 20:39:33 +02:00
othersEligStatus=othersEligStatus,
)
buildings[idImm] = building
return buildings
else:
raise ValueError("The requested area is too wide, please reduce it")