mn2_rec.py 31 KB

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