mn2_rec.py 35 KB

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