mn1_rec.py 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706
  1. '''
  2. Schéma de validation des données MN1
  3. @author: olivier.massot, 2018
  4. '''
  5. import logging
  6. from qgis.core import QgsProject
  7. from core.cerberus_ import is_float, is_multi_int, is_int, \
  8. is_modern_french_date, CerberusValidator, CerberusErrorHandler, \
  9. _translate_messages, is_positive_float, is_positive_int, \
  10. is_strictly_positive_float, is_strictly_positive_int
  11. from core.checking import BaseChecker, CheckingException
  12. from core.mncheck import QgsModel
  13. logger = logging.getLogger("mncheck")
  14. SCHEMA_NAME = "Schéma MN v1 REC"
  15. XMIN, XMAX, YMIN, YMAX = 1341999, 1429750, 8147750, 8294000
  16. CRS = 'EPSG:3949' # Coordinate Reference System
  17. TOLERANCE = 0.5
  18. class Artere(QgsModel):
  19. layername = "artere_geo"
  20. geom_type = QgsModel.GEOM_LINE
  21. crs = CRS
  22. bounding_box = (XMIN,YMIN,XMAX,YMAX)
  23. schema = {'AR_ID_INSE': {'type': 'string', 'empty': False, 'regex': r'50\d{3}'},
  24. 'AR_LONG': {'empty': False, 'validator': is_positive_float},
  25. 'AR_ETAT': {'type': 'string', 'empty': False, 'allowed': ['0', '1', '2', '3', '4']},
  26. 'AR_OCCP': {'type': 'string', 'empty': False, 'allowed': ['0', '1.1', '1.2', '2', '3', '4']},
  27. 'AR_NOEUD_A': {'type': 'string', 'empty': False, 'maxlength': 30},
  28. 'AR_NOEUD_B': {'type': 'string', 'empty': False, 'maxlength': 30},
  29. 'AR_NB_FOUR': {'empty': True, 'validator': is_multi_int},
  30. 'AR_FOU_DIS': {'empty': True, 'validator': is_int},
  31. 'AR_TYPE_FO': {'type': 'string', 'multiallowed': ['PVC', 'PEHD', 'SOUS-TUBAGE PEHD', 'SOUS-TUBAGE SOUPLE', 'FACADE', 'AERIEN', 'ENCORBELLEMENT', 'AUTRE']},
  32. 'AR_DIAM_FO': {'type': 'string', 'multiallowed': ['10', '14', '18', '25', '28', '32', '40', '45', '60', '80', '150', 'NUL', '']},
  33. 'AR_PRO_FOU': {'type': 'string', 'multiallowed': ['MANCHE NUMERIQUE', 'COLLECTIVITE', 'ORANGE', 'PRIVE', 'ERDF', 'ENEDIS', 'SDEM', 'AUTRE']},
  34. 'AR_PRO_CAB': {'type': 'string', 'empty': False, 'allowed': ['MANCHE NUMERIQUE']},
  35. 'AR_GEST_FO': {'type': 'string', 'multiallowed': ['MANCHE NUMERIQUE', 'MANCHE TELECOM', 'COLLECTIVITE', 'ORANGE', 'MANCHE FIBRE', 'PRIVE', 'ERDF', 'ENEDIS', 'SDEM', 'AUTRE', 'NUL']},
  36. 'AR_UTIL_FO': {'type': 'string', 'multiallowed': ['MANCHE NUMERIQUE', 'MANCHE TELECOM', 'COLLECTIVITE', 'ORANGE', 'MANCHE FIBRE', 'PRIVE', 'AUTRE', 'SDEM', 'NUL', '']},
  37. 'AR_DATE_IN': {'empty': True, 'validator': is_modern_french_date},
  38. 'AR_DATE_RE': {'empty': True, 'validator': is_modern_french_date},
  39. 'AR_REF_PLA': {'type': 'string', 'maxlength': 100},
  40. 'AR_SRC_GEO': {'type': 'string', 'empty': True, 'maxlength': 50},
  41. 'AR_QLT_GEO': {'type': 'string', 'empty': False, 'allowed': ['A', 'B', 'C']},
  42. 'AR_PRO_MD': {'type': 'string', 'empty': False, 'default': 'MANCHE NUMERIQUE', 'allowed': ['MANCHE NUMERIQUE']},
  43. 'AR_COMMENT': {'type': 'string', 'maxlength': 300, 'empty': True},
  44. 'AR_STATUT': {'type': 'string', 'empty': False, 'allowed': ['APS', 'APD', 'EXE', 'REC']}}
  45. def __repr__(self):
  46. try:
  47. return f"Artere {self.AR_NOEUD_A}-{self.AR_NOEUD_B}"
  48. except AttributeError:
  49. return f"Artere (code manquant)"
  50. class Cable(QgsModel):
  51. layername = "cable_geo"
  52. geom_type = QgsModel.GEOM_LINE
  53. crs = CRS
  54. bounding_box = (XMIN,YMIN,XMAX,YMAX)
  55. pk = "CA_NUMERO"
  56. schema = {'CA_NUMERO': {'type': 'string', 'maxlength': 30},
  57. 'CA_TYPE': {'type': 'string', 'maxlength': 10, 'empty': False, 'allowed': ['AERIEN', 'IMMEUBLE', 'FACADE', 'MIXTE', 'SOUTERRAIN']},
  58. 'CA_ETAT': {'type': 'string', 'maxlength': 1, 'empty': False, 'allowed': ['0', '1', '2', '3', '4']},
  59. 'CA_LONG': {'validator': is_strictly_positive_float},
  60. 'CA_EQ_A': {'type': 'string', 'maxlength': 30},
  61. 'CA_EQ_B': {'type': 'string', 'maxlength': 30},
  62. 'CA_DIAMETR': {'empty': False, 'validator': is_positive_float},
  63. 'CA_COULEUR': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['NOIR', 'BLEU', 'BLANC']},
  64. 'CA_TECHNOL': {'type': 'string', 'maxlength': 17, 'empty': False, 'allowed': ['G657A2_M6', 'G657A2_M12']},
  65. 'CA_NB_FO': {'empty': False, 'validator': is_strictly_positive_int},
  66. 'CA_NB_FO_U': {'empty': False, 'validator': is_positive_int},
  67. 'CA_NB_FO_D': {'empty': False, 'validator': is_positive_int},
  68. 'CA_PRO': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['MANCHE NUMERIQUE']},
  69. 'CA_GEST': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['MANCHE FIBRE']},
  70. 'CA_DATE_IN': {'empty': True, 'validator': is_modern_french_date},
  71. 'CA_COMMENT': {'type': 'string', 'maxlength': 300, 'empty': True},
  72. 'CA_STATUT': {'type': 'string', 'maxlength': 14, 'empty': False, 'allowed': ['APS', 'APD', 'EXE', 'REC']}}
  73. def __repr__(self):
  74. try:
  75. return f"Cable {self.CA_EQ_A}-{self.CA_EQ_B}"
  76. except AttributeError:
  77. return f"Cable (code manquant)"
  78. class Equipement(QgsModel):
  79. layername = "equipement_passif"
  80. geom_type = QgsModel.GEOM_POINT
  81. crs = CRS
  82. bounding_box = (XMIN,YMIN,XMAX,YMAX)
  83. pk = "EQ_NOM"
  84. schema = {'EQ_NOM': {'type': 'string', 'maxlength': 30, 'contains_any_of': ['PBO', 'BPE', 'BAI']},
  85. 'EQ_NOM_NOE': {'type': 'string', 'maxlength': 30},
  86. 'EQ_ETAT': {'type': 'string', 'maxlength': 1, 'empty': False, 'allowed': ['0', '1', '2', '3', '4']},
  87. 'EQ_OCCP': {'type': 'string', 'maxlength': 3, 'empty': False, 'allowed': ['0', '1.1', '1.2', '2', '3', '4']},
  88. 'EQ_TYPE': {'type': 'string', 'empty': False, 'allowed': ['PBO', 'PBOE', 'BPE', 'BAI']},
  89. 'EQ_TYPE_LQ': {'type': 'string', 'maxlength': 6, 'empty': False, 'allowed': ['PBO', 'BPE JB', 'BPE JD', 'BAIDC', 'BAIOP']},
  90. 'EQ_TYPE_PH': {'type': 'string', 'maxlength': 24, 'empty': False, 'allowed': ['PBO 6', 'PBO 12', 'BPE 12EP', 'BPE 24EP', 'BPE 48EP', 'BPE 72EP', 'BPE 96EP', 'BPE 144EP', 'BPE 288EP', 'BPE 576EP', 'BPE 720EP', 'BAI']},
  91. 'EQ_PRO': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['MANCHE NUMERIQUE', 'COLLECTIVITE', 'ORANGE', 'PRIVE', 'AUTRE', 'NUL']},
  92. 'EQ_GEST': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['MANCHE NUMERIQUE', 'MANCHE TELECOM', 'COLLECTIVITE', 'ORANGE', 'MANCHE FIBRE', 'PRIVE', 'AUTRE', 'NUL']},
  93. 'EQ_HAUT': {'empty': True, 'validator': is_float},
  94. 'EQ_DATE_IN': {'empty': True, 'validator': is_modern_french_date},
  95. 'EQ_COMMENT': {'type': 'string', 'maxlength': 300, 'empty': True},
  96. 'EQ_STATUT': {'type': 'string', 'maxlength': 14, 'empty': False, 'allowed': ['APS', 'APD', 'EXE', 'REC']}}
  97. class Noeud(QgsModel):
  98. layername = "noeud_geo"
  99. geom_type = QgsModel.GEOM_POINT
  100. crs = CRS
  101. bounding_box = (XMIN,YMIN,XMAX,YMAX)
  102. pk = "NO_NOM"
  103. schema = {'NO_NOM': {'type': 'string', 'maxlength': 30},
  104. 'NO_ID_INSE': {'type': 'string', 'empty': False, 'regex': r'50\d{3}'},
  105. 'NO_VOIE': {'type': 'string', 'empty': False, 'maxlength': 100},
  106. 'NO_ETAT': {'type': 'string', 'maxlength': 1, 'empty': False, 'allowed': ['0', '1', '2', '3', '4']},
  107. 'NO_OCCP': {'type': 'string', 'maxlength': 3, 'empty': False, 'allowed': ['0', '1.1', '1.2', '2', '3', '4']},
  108. 'NO_TYPE': {'type': 'string', 'maxlength': 3, 'empty': False, 'allowed': ['CHA', 'POT', 'LTE', 'SEM', 'FAC', 'OUV', 'IMM']},
  109. 'NO_TYPE_LQ': {'type': 'string', 'maxlength': 10, 'empty': False, 'allowed': ['CHTIR', 'CHRACC', 'POT', 'NRO', 'PM', 'MIMO', 'FAC', 'OUV', 'IMM']},
  110. 'NO_TYPE_PH': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['CHAMBRE', 'POTEAU', 'ARMOIRE', 'SHELTER', 'BATIMENT', 'SITE MIMO', 'FACADE', 'OUVRAGE', 'IMMEUBLE']},
  111. 'NO_CODE_PH': {'type': 'string', 'maxlength': 20},
  112. 'NO_TECH_PS': {'type': 'string', 'maxlength': 20, 'empty': True, 'multiallowed': ['COAX', 'CUT', 'ECL', 'ELEC', 'VP', 'OPT', 'NC']},
  113. 'NO_AMO': {'type': 'string', 'maxlength': 30},
  114. 'NO_PLINOX': {'required': False, 'type': 'string', 'maxlength': 3, 'allowed': ['OUI', 'NON']},
  115. 'NO_X': {'empty': True, 'validator': is_float},
  116. 'NO_Y': {'empty': True, 'validator': is_float},
  117. 'NO_PRO': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['MANCHE NUMERIQUE', 'COLLECTIVITE', 'ORANGE', 'ERDF', 'ENEDIS', 'PRIVE', 'SDEM', 'AUTRE', 'NUL']},
  118. 'NO_GEST': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['MANCHE NUMERIQUE', 'MANCHE TELECOM', 'COLLECTIVITE', 'ORANGE', 'ERDF', 'ENEDIS', 'SDEM', 'MANCHE FIBRE', 'PRIVE', 'AUTRE', 'NUL']},
  119. 'NO_HAUT': {'empty': True, 'validator': is_float},
  120. 'NO_DATE_IN': {'empty': True, 'validator': is_modern_french_date},
  121. 'NO_REF_PLA': {'type': 'string', 'maxlength': 100},
  122. 'NO_SRC_GEO': {'type': 'string', 'empty': True, 'maxlength': 50},
  123. 'NO_QLT_GEO': {'type': 'string', 'maxlength': 1, 'empty': False, 'allowed': ['A', 'B', 'C']},
  124. 'NO_PRO_MD': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['MANCHE NUMERIQUE']},
  125. 'NO_COMMENT': {'type': 'string', 'maxlength': 300, 'empty': True},
  126. 'NO_STATUT': {'type': 'string', 'maxlength': 14, 'empty': False, 'allowed': ['APS', 'APD', 'EXE', 'REC']}}
  127. class Tranchee(QgsModel):
  128. layername = "tranchee_geo"
  129. geom_type = QgsModel.GEOM_LINE
  130. crs = CRS
  131. bounding_box = (XMIN,YMIN,XMAX,YMAX)
  132. schema = {'TR_ID_INSE': {'type': 'string', 'empty': False, 'regex': r'50\d{3}'},
  133. 'TR_VOIE': {'type': 'string', 'empty': False, 'maxlength': 200},
  134. 'TR_TYP_IMP': {'type': 'string', 'empty': False, 'allowed': ['ACCOTEMENT STABILISE', 'ACCOTEMENT NON STABILISE', 'CHAUSSEE LOURDE', 'CHAUSSEE LEGERE', 'FOSSE', 'TROTTOIR', 'ESPACE VERT', 'ENCORBELLEMENT']},
  135. 'TR_MOD_POS': {'type': 'string', 'empty': False, 'allowed': ['TRADITIONNEL', 'MICRO TRANCHEE', 'FONCAGE 60', 'FONCAGE 90', 'FONCAGE 120', 'TRANCHEUSE', 'FORAGE URBAIN', 'FORAGE RURAL', 'ENCORBELLEMENT']},
  136. 'TR_LONG': {'empty': False, 'validator': is_strictly_positive_float},
  137. 'TR_LARG': {'empty':True, 'validator': is_strictly_positive_float},
  138. 'TR_REVET': {'empty':True, 'type': 'string', 'allowed': ['SABLE', 'BICOUCHE', 'ENROBE', 'GRAVIER', 'BETON', 'PAVE', 'TERRAIN NATUREL']},
  139. 'TR_CHARGE': {'empty':True, 'validator': is_positive_float},
  140. 'TR_GRILLAG': {'empty':True, 'validator': is_positive_float},
  141. 'TR_REMBLAI': {'type': 'string', 'maxlength': 50},
  142. 'TR_PLYNOX': {'type': 'string', 'empty': False, 'allowed': ['OUI', 'NON']},
  143. 'TR_PRO_VOI': {'type': 'string', 'empty': False, 'allowed': ['COMMUNE', 'COMMUNAUTE DE COMMUNES', 'DEPARTEMENT', 'ETAT', 'PRIVE']},
  144. 'TR_GEST_VO': {'type': 'string', 'empty': False, 'allowed': ['COMMUNE', 'COMMUNAUTE DE COMMUNES', 'DEPARTEMENT', 'ETAT', 'PRIVE']},
  145. 'TR_SCHEMA': {'maxlength': 100, 'empty': False, 'type': 'string'},
  146. 'TR_DATE_IN': {'empty': True, 'validator': is_modern_french_date},
  147. 'TR_SRC_GEO': {'type': 'string', 'empty': True, 'maxlength': 50},
  148. 'TR_QLT_GEO': {'type': 'string', 'empty': False, 'allowed': ['A', 'B', 'C']},
  149. 'TR_PRO_MD': {'type': 'string', 'empty': False, 'maxlength': 20, 'allowed': ['MANCHE NUMERIQUE']},
  150. 'TR_COMMENT': {'type': 'string', 'maxlength': 300, 'empty': True},
  151. 'TR_STATUT': {'type': 'string', 'empty': False, 'allowed': ['APS', 'APD', 'EXE', 'REC']}}
  152. def __repr__(self):
  153. try:
  154. return f"Tranchee {self.TR_VOIE}"
  155. except AttributeError:
  156. return f"Tranchee (voie manquante)"
  157. class Zapbo(QgsModel):
  158. layername = "zapbo_geo"
  159. required = False
  160. geom_type = QgsModel.GEOM_POLYGON
  161. crs = CRS
  162. bounding_box = (XMIN,YMIN,XMAX,YMAX)
  163. pk = "ID_ZAPBO"
  164. schema = {'ID_ZAPBO': {'type': 'string', 'empty': False, 'maxlength': 30, 'contains_any_of': ['PBO', 'BPE', 'PEC', 'BAI']},
  165. 'COMMENTAIR': {'type': 'string', 'maxlength': 254, 'empty': True},
  166. 'STATUT': {'type': 'string', 'empty': False, 'allowed': ['APS', 'APD', 'EXE', 'REC']}}
  167. models = [Artere, Cable, Equipement, Noeud, Tranchee, Zapbo]
  168. class Prise(QgsModel):
  169. layername = "FTTH_SITES_GEO_VALIDE"
  170. layername_bis = "SITE_GEO"
  171. ####### Validateur
  172. class Mn1Checker(BaseChecker):
  173. def setUp(self):
  174. self.dataset = {}
  175. for model in models:
  176. model.layer = next((l for l in QgsProject.instance().mapLayers().values() \
  177. if l.name().lower() == model.layername.lower()), None)
  178. self.dataset[model] = [model(f) for f in model.layer.getFeatures()] if model.layer else []
  179. self.arteres = self.dataset.get(Artere, [])
  180. self.cables = self.dataset.get(Cable, [])
  181. self.equipements = self.dataset.get(Equipement, [])
  182. self.noeuds = self.dataset.get(Noeud, [])
  183. self.tranchees = self.dataset.get(Tranchee, [])
  184. self.zapbos = self.dataset.get(Zapbo, [])
  185. for artere in self.arteres:
  186. artere.noeud_a = next((n for n in self.noeuds if n.NO_NOM == artere.AR_NOEUD_A), None)
  187. artere.noeud_b = next((n for n in self.noeuds if n.NO_NOM == artere.AR_NOEUD_B), None)
  188. for cable in self.cables:
  189. cable.equipement_a = next((e for e in self.equipements if e.EQ_NOM == cable.CA_EQ_A), None)
  190. cable.equipement_b = next((e for e in self.equipements if e.EQ_NOM == cable.CA_EQ_B), None)
  191. for equipement in self.equipements:
  192. equipement.noeud = next((n for n in self.noeuds if n.NO_NOM == equipement.EQ_NOM_NOE), None)
  193. self.prises = []
  194. Prise.layer = next((l for l in QgsProject.instance().mapLayers().values() \
  195. if l.name().lower() in [Prise.layername.lower(), Prise.layername_bis.lower()]), None)
  196. def _load_prises(self):
  197. """ on charge la couche des prises si elle existe """
  198. self.prises = []
  199. if Prise.layer:
  200. prises = [Prise(f) for f in Prise.layer.getFeatures()]
  201. self.prises = [p for p in prises if p.T_ETAT.lower() != "obsolete"]
  202. def test_load_layers(self):
  203. """ Chargement des données
  204. Contrôle la présence des couches attendues
  205. """
  206. for model in models:
  207. if model.layer is None:
  208. if model.required:
  209. self.log_critical("Couche manquante", model=model)
  210. else:
  211. self.log_error("Couche manquante", model=model)
  212. continue
  213. if model.pk:
  214. if not model.pk.lower() in [f.name().lower() for f in model.layer.fields()]:
  215. self.log_error(f"Clef primaire manquante ({model.pk})", model=model)
  216. def test_scr(self):
  217. """ Contrôle des projections
  218. Vérifie que les couches ont le bon sytème de projection
  219. """
  220. for model in models:
  221. if model.layer and model.layer.crs().authid() != model.crs:
  222. self.log_error(f"Mauvaise projection (attendu: {model.crs})", model=model)
  223. def _validate_structure(self, model, items):
  224. v = CerberusValidator(model.schema, purge_unknown=True, error_handler=CerberusErrorHandler, require_all=True)
  225. for item in items:
  226. v.validate(item.__dict__)
  227. for field, verrors in v.errors.items():
  228. for err in verrors:
  229. self.log_error(f"{field} : {_translate_messages(err)}", item=item)
  230. def test_structure_arteres(self):
  231. """ Structure des données: Artères
  232. Contrôle les données attributaires de la couche ARTERE_GEO:
  233. présence, format, valeurs autorisées...
  234. """
  235. self._validate_structure(Artere, self.arteres)
  236. def test_structure_cables(self):
  237. """ Structure des données: Cables
  238. Contrôle les données attributaires de la couche CABLE_GEO:
  239. présence, format, valeurs autorisées...
  240. """
  241. self._validate_structure(Cable, self.cables)
  242. def test_structure_equipements(self):
  243. """ Structure des données: Equipements
  244. Contrôle les données attributaires de la couche EQUIPEMENT_GEO:
  245. présence, format, valeurs autorisées...
  246. """
  247. self._validate_structure(Equipement, self.equipements)
  248. def test_structure_noeuds(self):
  249. """ Structure des données: Noeuds
  250. Contrôle les données attributaires de la couche NOEUD_GEO:
  251. présence, format, valeurs autorisées...
  252. """
  253. self._validate_structure(Noeud, self.noeuds)
  254. def test_structure_tranchees(self):
  255. """ Structure des données: Tranchées
  256. Contrôle les données attributaires de la couche TRANCHEE_GEO:
  257. présence, format, valeurs autorisées...
  258. """
  259. self._validate_structure(Tranchee, self.tranchees)
  260. def test_structure_zapbos(self):
  261. """ Structure des données: Zapbos
  262. Contrôle les données attributaires de la couche ZAPBO_GEO:
  263. présence, format, valeurs autorisées...
  264. """
  265. self._validate_structure(Zapbo, self.zapbos)
  266. def test_geometry_validity(self):
  267. """ Contrôle de la validité des géométries
  268. """
  269. for model in models:
  270. for item in self.dataset[model]:
  271. if not item.is_geometry_valid():
  272. self.log_error("La géométrie de l'objet est invalide", item=item)
  273. def test_geometry_type(self):
  274. """ Contrôle des types de géométries
  275. """
  276. for model in models:
  277. for item in self.dataset[model]:
  278. item_geom_type = item.get_geom_type()
  279. if item_geom_type != model.geom_type:
  280. expected, found = model.get_geom_name(model.geom_type), model.get_geom_name(item_geom_type)
  281. self.log_error(f"Type de géométrie invalide (attendu: {expected}, trouvé: {found})", item=item)
  282. def test_bounding_box(self):
  283. """ Contrôle des emprises
  284. Vérifie que les objets sont dans le périmètre attendu
  285. """
  286. for model in models:
  287. xmin, ymin, xmax, ymax = model.bounding_box
  288. for item in self.dataset[model]:
  289. x1, y1, x2, y2 = item.get_bounding_box()
  290. if any(x < xmin or x > xmax for x in (x1, x2)) or \
  291. any(y < ymin or y > ymax for y in (y1, y2)):
  292. self.log_error("Hors de l'emprise autorisée", item=item)
  293. def test_duplicates(self):
  294. """ Recherche de doublons
  295. Recherche d'éventuels doublons dans des champs qui supposent l'unicité
  296. """
  297. tmp = []
  298. for noeud in self.noeuds:
  299. if not noeud.NO_NOM:
  300. continue
  301. if not noeud.NO_NOM in tmp:
  302. tmp.append(noeud.NO_NOM)
  303. else:
  304. self.log_error("Doublons dans le champs NO_NOM", item=noeud)
  305. tmp = []
  306. for equipement in self.equipements:
  307. if not equipement.EQ_NOM:
  308. continue
  309. if not equipement.EQ_NOM in tmp:
  310. tmp.append(equipement.EQ_NOM)
  311. else:
  312. self.log_error("Doublons dans le champs EQ_NOM", item=equipement)
  313. tmp = []
  314. for zapbo in self.zapbos:
  315. if not zapbo.ID_ZAPBO:
  316. continue
  317. if not zapbo.ID_ZAPBO in tmp:
  318. tmp.append(zapbo.ID_ZAPBO)
  319. else:
  320. self.log_error("Doublons dans le champs ID_ZAPBO", item=zapbo)
  321. def test_constraints_arteres_noeuds(self):
  322. """ Application des contraintes: Artères / Noeuds
  323. Vérifie que les noeuds attachés aux artères existent
  324. """
  325. for artere in self.arteres:
  326. if artere.noeud_a is None:
  327. self.log_error(f"Le noeud lié '{artere.AR_NOEUD_A}' n'existe pas", item=artere)
  328. if artere.noeud_b is None:
  329. self.log_error(f"Le noeud lié '{artere.AR_NOEUD_B}' n'existe pas", item=artere)
  330. def test_constraints_cables_equipements(self):
  331. """ Application des contraintes: Equipements / Cables
  332. Vérifie que les équipements attachés aux cables existent """
  333. for cable in self.cables:
  334. if cable.equipement_a is None:
  335. self.log_error(f"L'équipement lié '{cable.CA_EQ_A}' n'existe pas", item=cable)
  336. if cable.equipement_b is None:
  337. self.log_error(f"L'équipement lié '{cable.CA_EQ_B}' n'existe pas", item=cable)
  338. if cable.CA_STATUT != cable.equipement_b.EQ_STATUT:
  339. self.log_error(f"L'équipement B du cable n'a pas le même statut que le cable", item=cable)
  340. def test_constraints_cables_equipements_b(self):
  341. """ Application des contraintes: Equipements B
  342. Vérifie que tous les équipements sont l'équipement B d'au moins un cable """
  343. equipements_b = [cable.CA_EQ_B for cable in self.cables]
  344. for equipement in self.equipements:
  345. if equipement.EQ_TYPE == "BAI":
  346. continue
  347. if not equipement.EQ_NOM in equipements_b:
  348. self.log_error(f"L'equipement '{equipement.EQ_NOM}' n'est l'équipement B d'aucun cable et n'est pas un PM", item=equipement)
  349. def test_constraints_equipements_noeuds(self):
  350. """ Application des contraintes: Noeuds / Equipements
  351. Vérifie que les noeuds attachés aux équipements existent
  352. """
  353. for equipement in self.equipements:
  354. if equipement.noeud is None:
  355. self.log_error(f"Le noeud lié '{equipement.EQ_NOM_NOE}' n'existe pas", item=equipement)
  356. def test_graphic_duplicates(self):
  357. """ Recherche de doublons graphiques """
  358. for i, tranchee in enumerate(self.tranchees):
  359. for other in self.tranchees[i+1:]:
  360. if tranchee.geom.equals(other.geom):
  361. self.log_error("Une entité graphique est dupliquée", item=tranchee)
  362. continue
  363. for i, artere in enumerate(self.arteres):
  364. for other in self.arteres[i+1:]:
  365. if artere.geom.equals(other.geom):
  366. self.log_error("Une entité graphique est dupliquée", item=artere)
  367. continue
  368. for i, cable in enumerate(self.cables):
  369. for other in self.cables[i+1:]:
  370. if cable.geom.equals(other.geom) and cable.CA_EQ_A == other.CA_EQ_A and cable.CA_EQ_B == other.CA_EQ_B:
  371. self.log_error("Une entité graphique est dupliquée", item=cable)
  372. continue
  373. for i, noeud in enumerate(self.noeuds):
  374. for other in self.noeuds[i+1:]:
  375. if noeud.geom.equals(other.geom):
  376. self.log_error("Une entité graphique est dupliquée", item=noeud)
  377. continue
  378. for i, zapbo in enumerate(self.zapbos):
  379. for other in self.zapbos[i+1:]:
  380. if zapbo.geom.equals(other.geom):
  381. self.log_error("Une entité graphique est dupliquée", item=zapbo)
  382. continue
  383. def test_positions_noeuds(self):
  384. """ Topologie: Noeuds / Artères
  385. Compare la géométrie des noeuds à celle des artères
  386. """
  387. for artere in self.arteres:
  388. if not artere.noeud_a or not artere.noeud_b:
  389. continue
  390. artere_points = artere.get_points()
  391. noeud_a_point = artere.noeud_a.get_points()[0]
  392. noeud_b_point = artere.noeud_b.get_points()[0]
  393. if not (artere_points[0].distanceSquared(noeud_a_point) <= TOLERANCE and \
  394. artere_points[-1].distanceSquared(noeud_b_point) <= TOLERANCE) \
  395. and not (artere_points[-1].distanceSquared(noeud_a_point) <= TOLERANCE and \
  396. artere_points[0].distanceSquared(noeud_b_point) <= TOLERANCE):
  397. self.log_error(f"Pas de noeud aux coordonnées attendues (noeuds attendus: {artere.noeud_a.NO_NOM} ou {artere.noeud_b.NO_NOM})", item=artere)
  398. def test_positions_equipements(self):
  399. """ Topologie: Equipements / Cables
  400. Compare la géométrie des équipements à celle des cables """
  401. for cable in self.cables:
  402. if not cable.equipement_a or not cable.equipement_b or not cable.is_geometry_valid() or not cable.equipement_a.noeud or not cable.equipement_b.noeud:
  403. continue
  404. #! attention: on utilise la géométrie du noeud associé à l'équipement, pas celle de l'équipement lui-même
  405. cable_points = cable.get_points()
  406. equip_a_point = cable.equipement_a.noeud.get_points()[0]
  407. equip_b_point = cable.equipement_b.noeud.get_points()[0]
  408. if not (cable_points[0].distanceSquared(equip_a_point) <= TOLERANCE and \
  409. cable_points[-1].distanceSquared(equip_b_point) <= TOLERANCE) \
  410. and not (cable_points[-1].distanceSquared(equip_a_point) <= TOLERANCE and \
  411. cable_points[0].distanceSquared(equip_b_point) <= TOLERANCE):
  412. self.log_error(f"Pas d'équipement aux coordonnées attendues (equipements attendus: {cable.equipement_a.EQ_NOM} ou {cable.equipement_b.EQ_NOM}", item=cable)
  413. def test_tranchee_artere(self):
  414. """ Topologie: Tranchées / Artères
  415. Compare la géométrie des tranchées à celle des artères """
  416. arteres_full_buffer = Artere.full_buffer(TOLERANCE)
  417. for tranchee in self.tranchees:
  418. if not arteres_full_buffer.contains(tranchee.geom):
  419. self.log_error("Tranchée ou portion de tranchée sans artère", item=tranchee)
  420. def test_cable_artere(self):
  421. """ Topologie: Cables / Artères
  422. Compare la géométrie des cables à celle des artères """
  423. # Vérifie que chaque cable a au moins une artère (sauf si commentaire contient 'baguette')
  424. arteres_full_buffer = Artere.full_buffer(TOLERANCE)
  425. for cable in self.cables:
  426. if "baguette" in cable.CA_COMMENT.lower() or not cable.is_geometry_valid():
  427. continue
  428. if not arteres_full_buffer.contains(cable.geom):
  429. self.log_error("Cable ou portion de cable sans artère", item=cable)
  430. def test_artere_cable(self):
  431. """ Topologie: Artères / Cables
  432. Compare la géométrie des artères à celle des cables """
  433. # Vérifie que chaque artère a au moins un cable (sauf si commentaire contient un de ces mots 'racco client adductio attente bus 'sans cable'')
  434. cables_full_buffer = Cable.full_buffer(TOLERANCE)
  435. for artere in self.arteres:
  436. if any(x in artere.AR_COMMENT.lower() for x in ['racco','client','adduction','attente','bus','sans cable']):
  437. continue
  438. if not cables_full_buffer.contains(artere.geom):
  439. self.log_error("Artère ou portion d'artère sans cable", item=artere)
  440. def test_arteres_enterrees(self):
  441. """ Données des artères enterrées
  442. Contrôle les données des artères enterrées ou en encorbellement """
  443. for artere in self.arteres:
  444. if artere.AR_TYPE_FO in ["FACADE", "AERIEN"]:
  445. continue
  446. for field in ["AR_NB_FOUR", "AR_FOU_DIS", "AR_DIAM_FO"]:
  447. if not hasattr(artere, field):
  448. self.log_error("Le champs est obligatoire: {}".format(field), item=artere)
  449. elif getattr(artere, field) == "":
  450. self.log_error("Le champs doit être renseigné: {}".format(field), item=artere)
  451. def test_dates_install(self):
  452. """ Dates d'installation
  453. Vérifie que les dates d'installation sont renseignées pour les équipements en service """
  454. for equipement in self.equipements:
  455. if equipement.EQ_STATUT == "REC" and not equipement.EQ_DATE_IN:
  456. self.log_error("Date d'installation (EQ_DATE_IN) manquante", item=equipement)
  457. for cable in self.cables:
  458. if cable.CA_STATUT == "REC" and not cable.CA_DATE_IN:
  459. self.log_error("Date d'installation (CA_DATE_IN) manquante", item=cable)
  460. for tranchee in self.tranchees:
  461. if tranchee.TR_STATUT == "REC" and not tranchee.TR_DATE_IN:
  462. self.log_error("Date d'installation (TR_DATE_IN) manquante", item=tranchee)
  463. def test_largeur_tranchees(self):
  464. """ Tranchées: Dimensions
  465. Vérifie que la cohérence des dimensions des tranchées """
  466. for tranchee in self.tranchees:
  467. if not ("FONCAGE" in tranchee.TR_MOD_POS or
  468. "FORAGE" in tranchee.TR_MOD_POS or
  469. "ENCORBELLEMENT" in tranchee.TR_MOD_POS):
  470. try:
  471. if float(tranchee.TR_LARG) > 0:
  472. continue
  473. except (TypeError, ValueError):
  474. pass
  475. self.log_error("La largeur de la tranchée doit être supérieure à 0", item=tranchee)
  476. def test_dimensions_fourreaux(self):
  477. """ Dimensions logiques: fourreaux
  478. Vérifie que les nombres de fourreaux renseignés sont cohérents """
  479. for artere in self.arteres:
  480. try:
  481. if not int(artere.AR_FOU_DIS) <= int(artere.AR_NB_FOUR):
  482. self.log_error("Le nombre de fourreaux disponibles (AR_FOU_DIS) doit être inférieur au nombre total (AR_NB_FOUR)", item=artere)
  483. except (TypeError, ValueError):
  484. pass
  485. for cable in self.cables:
  486. try:
  487. if not int(cable.CA_NB_FO_U) <= int(cable.CA_NB_FO):
  488. self.log_error("Le nombre de fourreaux utilisés (CA_NB_FO_U) doit être inférieur au nombre total (CA_NB_FO)", item=cable)
  489. if not int(cable.CA_NB_FO_D) <= int(cable.CA_NB_FO):
  490. self.log_error("Le nombre de fourreaux disponibles (CA_NB_FO_D) doit être inférieur au nombre total (CA_NB_FO)", item=cable)
  491. except (TypeError, ValueError):
  492. pass
  493. def test_prop_gest(self):
  494. """ Propriétaires / Gestionnaires
  495. Vérifie la cohérence des propriétaires et gestionnaires renseignés """
  496. cases = {
  497. "ORANGE": ["ORANGE"],
  498. "ERDF": ["ERDF"],
  499. "ENEDIS": ["ENEDIS"],
  500. "MANCHE NUMERIQUE": ["MANCHE FIBRE", "MANCHE TELECOM"]
  501. }
  502. for artere in self.arteres:
  503. for prop, gests in cases.items():
  504. if artere.AR_PRO_FOU.upper() == prop and not artere.AR_GEST_FO.upper() in gests:
  505. self.log_error("Propriétaire: {}, gestionnaire(s) possible(s): {} (renseigné: {})".format(prop, ', '.join(gests), artere.AR_GEST_FO), item=artere)
  506. for equipement in self.equipements:
  507. for prop, gests in cases.items():
  508. if equipement.EQ_PRO.upper() == prop and not equipement.EQ_GEST.upper() in gests:
  509. self.log_error("Propriétaire: {}, gestionnaire(s) possible(s): {} (renseigné: {})".format(prop, ', '.join(gests), equipement.EQ_GEST), item=equipement)
  510. for noeud in self.noeuds:
  511. for prop, gests in cases.items():
  512. if noeud.NO_PRO.upper() == prop and not noeud.NO_GEST.upper() in gests:
  513. self.log_error("Propriétaire: {}, gestionnaire(s) possible(s): {} (renseigné: {})".format(prop, ', '.join(gests), noeud.NO_GEST), item=noeud)
  514. def _za_for_pbo(self, pbo):
  515. # retourne la ZAPBO correspondant à la PBO en parametre, None si aucune
  516. if hasattr(pbo, 'zapbo') and pbo.zapbo:
  517. return pbo.zapbo
  518. for zapbo in self.zapbos:
  519. if zapbo.geom.contains(pbo.geom) and pbo.EQ_NOM in zapbo.ID_ZAPBO:
  520. return zapbo
  521. return None
  522. def test_pbos(self):
  523. """ Topologie: PBO / ZAPBO
  524. Compare la géométrie et le nom des équipements de type PBO à celle des ZAPBO
  525. """
  526. # Verifier que chaque equipement de type PBO est contenu dans une zapbo, et que le nom de la zapbo contient le nom de l'equipement
  527. for equipement in self.equipements:
  528. if not equipement.EQ_TYPE == "PBO":
  529. continue
  530. #zapbos englobant l'equipement
  531. candidates = [z for z in self.zapbos if z.geom.contains(equipement.noeud.geom)]
  532. # le pbo doit être contenu dans une zapbo
  533. if not candidates:
  534. self.log_error("Le PBO n'est contenu dans aucune ZAPBO", item=equipement)
  535. continue
  536. # On se base sur le nom pour trouver la zapbo correspondante
  537. equipement.zapbo = next((z for z in candidates if equipement.EQ_NOM in z.ID_ZAPBO), None)
  538. if not equipement.zapbo:
  539. self.log_error("Le nom de la PBO ne coincide avec le nom d'aucune des ZAPBO qui le contiennent", item=equipement)
  540. continue
  541. if equipement.EQ_STATUT != equipement.zapbo.STATUT:
  542. self.log_error("Le statut de la PBO ne coincide pas avec celui de sa ZAPBO", item=equipement)
  543. continue
  544. def test_comments(self):
  545. """ Présence de commentaires
  546. Vérifie la présence de commentaires là où ils sont attendus
  547. """
  548. for equipement in self.equipements:
  549. if equipement.EQ_STATUT != "REC" and not equipement.EQ_COMMENT:
  550. self.log_error("L'equipement n'est pas en REC, un commentaire devrait en préciser la raison", item=equipement)
  551. for zapbo in self.zapbos:
  552. if zapbo.STATUT != "REC" and not zapbo.COMMENTAIR:
  553. self.log_error("La Zapbo n'est pas en REC, un commentaire devrait en préciser la raison", item=zapbo)
  554. def test_zapbos_prises(self):
  555. """ Topologie: Zapbos / Prises
  556. Toutes les zapbo contiennent au moins une prise"""
  557. self._load_prises()
  558. if not self.prises:
  559. raise CheckingException("La couche des prises n'est pas chargée")
  560. for zapbo in self.zapbos:
  561. if not next((prise for prise in self.prises if zapbo.geom.contains(prise.geom)), 0):
  562. self.log_error("La Zapbo ne contient aucune prise", item=zapbo)
  563. def test_pbo_dimension(self):
  564. """ Dimensionnement des PBO
  565. Compare la couche des prises (si chargée) au dimensionnement des PBO"""
  566. self._load_prises()
  567. if not self.prises:
  568. raise CheckingException("La couche des prises n'est pas chargée")
  569. for equipement in self.equipements:
  570. if not equipement.EQ_TYPE == "PBO":
  571. continue
  572. zapbo = self._za_for_pbo(equipement)
  573. zapbo.nb_prises = sum([int(prise.NB_PRISE) for prise in self.prises if zapbo.geom.contains(prise.geom) and prise.NB_PRISE])
  574. # Controle du dimensionnement des PBO
  575. if equipement.EQ_TYPE_PH == 'PBO 6' and not zapbo.nb_prises < 6:
  576. self.log_error("Le PBO 6 contient plus de 5 prises", item=equipement)
  577. if equipement.EQ_TYPE_PH == 'PBO 12' and not zapbo.nb_prises >= 6 and zapbo.nb_prises <= 8:
  578. self.log_error("Le PBO 12 contient moins de 6 prises ou plus de 8 prises", item=equipement)
  579. if zapbo.STATUT == "REC" and not equipement.EQ_STATUT == "REC":
  580. self.log_error("Le statut du PBO n'est pas cohérent avec le statut de sa ZAPBO", item=equipement)
  581. if equipement.EQ_STATUT == "REC" and not zapbo.STATUT == "REC" and not zapbo.ID_ZAPBO[:4].lower() == "att_":
  582. self.log_error("Le statut du PBO n'est pas cohérent avec le statut de sa ZAPBO", item=equipement)
  583. checkers = [Mn1Checker]