mn2_rec.py 33 KB

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