mn3_apd.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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_positive_int, is_positive_float, CerberusValidator, \
  8. CerberusErrorHandler, _translate_messages
  9. from core.checking import BaseChecker
  10. from core.mncheck import QgsModel
  11. # TODO: verifier que les zapbos intersectent au moins une zasro et même nom que cette zasro
  12. # TODO: verifier que toute les prises comprises dans une zapbo soient contenues dans une seule et même zasro
  13. logger = logging.getLogger("mncheck")
  14. SCHEMA_NAME = "Schéma MN v3 APD"
  15. XMIN, XMAX, YMIN, YMAX = 1341999, 1429750, 8147750, 8294000
  16. CRS = 'EPSG:3949' # Coordinate Reference System
  17. TOLERANCE = 0.5
  18. STATUTS = ['ETUDE PRELIMINAIRE', 'ETUDE DE DIAGNOSTIC', 'AVANT-PROJET PROJET', 'PROJET', 'PASSATION DES MARCHES DE TRAVAUX',
  19. 'ETUDE D EXECUTION', 'TRAVAUX' , 'RECOLEMENT', 'MAINTIEN EN CONDITIONS OPERATIONNELLES']
  20. ##### Modeles
  21. class SiteTelecom(QgsModel):
  22. layername = "SITE_TELECOM"
  23. geom_type = QgsModel.GEOM_POINT
  24. bounding_box = (XMIN,YMIN,XMAX,YMAX)
  25. pk = "ST_CODE"
  26. schema = {'ST_CODE': {'type': 'string', 'empty': False},
  27. 'ST_NOM': {'type': 'string', 'empty': False},
  28. 'ST_TYPFCT': {'type': 'string', 'empty': False, 'allowed': ['SITE HEBERGEMENT',
  29. 'NOEUD RACCORDEMENT OPTIQUE',
  30. 'SOUS-REPARTITEUR OPTIQUE',
  31. 'SOUS-REPARTITEUR OPTIQUE COLOCALISE',
  32. 'NOEUD RACCORDEMENT D ABONNES',
  33. 'NOEUD RACCORDEMENT D ABONNES - HAUT DEBIT',
  34. 'NOEUD RACCORDEMENT D ABONNES - MONTEE EN DEBIT']},
  35. 'ST_STATUT': {'type': 'string', 'empty': False, 'allowed': [STATUTS]},
  36. 'ST_NBPRISE': {'empty': False, 'validator': is_positive_int}
  37. }
  38. class SiteClient(QgsModel):
  39. layername = "SITE_CLIENT"
  40. geom_type = QgsModel.GEOM_POINT
  41. bounding_box = (XMIN,YMIN,XMAX,YMAX)
  42. schema = {
  43. 'SC_TYPFON': {'type': 'string', 'empty': False, 'allowed': ['RESIDENTIEL', 'PROFESSIONNEL', 'OPERATEUR', 'TECHNIQUE']},
  44. 'SC_STATUT': {'type': 'string', 'empty': False, 'allowed': [STATUTS]},
  45. 'SC_NBPRISE': {'empty': False, 'validator': is_positive_int},
  46. 'SC_NBPRHAB': {'empty': False, 'validator': is_positive_int},
  47. 'SC_NBPRPRO': {'empty': False, 'validator': is_positive_int},
  48. 'SC_ID_SITE': {'type': 'string', 'empty': False},
  49. 'SC_NUM_RUE': {'type': 'string', 'empty': False},
  50. 'SC_REPET': {'type': 'string', 'empty': False},
  51. 'SC_DNUBAT': {'type': 'string', 'empty': False},
  52. 'SC_DESC': {'type': 'string', 'empty': False},
  53. 'SC_NOM_RUE': {'type': 'string', 'empty': False},
  54. 'SC_NOM_COM': {'type': 'string', 'empty': False},
  55. 'SC_ETAT': {'type': 'string', 'empty': False, 'allowed': ['VALIDE', 'OBSOLETE', 'NOUVEAU', 'FUTUR']},
  56. 'SC_ATT_PTO': {'empty': False, 'validator': is_positive_float}
  57. }
  58. class Cable(QgsModel):
  59. layername = "CABLE"
  60. geom_type = QgsModel.GEOM_LINE
  61. bounding_box = (XMIN,YMIN,XMAX,YMAX)
  62. schema = {'CA_TYPFON': {'type': 'string', 'empty': False, 'allowed': ['COLLECTE TRANSPORT DISTRIBUTION', 'COLLECTE', 'COLLECTE TRANSPORT', 'COLLECTE DISTRIBUTION', 'TRANSPORT DISTRIBUTION', 'TRANSPORT', 'DISTRIBUTION', 'RACCORDEMENT FINAL', 'BOUCLE METROPOLITAINE', 'LONGUE DISTANCE (LONG HAUL)', 'NON COMMUNIQUE']},
  63. 'CA_TYPSTR': {'type': 'string', 'empty': False, 'allowed': ['CONDUITE', 'AERIEN', 'COLONNE MONTANTE', 'IMMERGE', 'FACADE']},
  64. 'CA_STATUT': {'type': 'string', 'empty': False, 'allowed': [STATUTS]},
  65. 'CA_CAPFO': {'empty': False, 'allowed': [720,576,288,144,96,72,48,24,12]},
  66. 'CA_MODULO': {'empty': False, 'allowed': [6, 12]},
  67. 'CA_SUPPORT': {'type': 'string', 'empty': False, 'allowed': ['0', '1']}
  68. }
  69. class Zapbo(QgsModel):
  70. layername = "ZAPBO"
  71. geom_type = QgsModel.GEOM_POLYGON
  72. bounding_box = (XMIN,YMIN,XMAX,YMAX)
  73. schema = {'ZP_NBPRISE': {'empty': False, 'validator': is_positive_int},
  74. 'ZP_ISOLE': {'type': 'string', 'empty': False, 'allowed': ['0', '1']},
  75. 'ZP_STATUT': {'type': 'string', 'empty': False, 'allowed': [STATUTS]}
  76. }
  77. class Zasro(QgsModel):
  78. layername = "ZASRO"
  79. geom_type = QgsModel.GEOM_POLYGON
  80. bounding_box = (XMIN,YMIN,XMAX,YMAX)
  81. schema = {'ZS_CODE': {'type': 'string', 'empty': False},
  82. 'ZS_NBPRISE': {'empty': False, 'validator': is_positive_int}
  83. }
  84. class Adduction(QgsModel):
  85. layername = "ADDUCTION"
  86. geom_type = QgsModel.GEOM_LINE
  87. bounding_box = (XMIN,YMIN,XMAX,YMAX)
  88. schema = {'AD_ISOLE': {'type': 'string', 'empty': False, 'allowed': ['0', '1']},
  89. 'AD_LONG': {'empty': False, 'validator': is_positive_float}
  90. }
  91. models = [SiteTelecom, SiteClient, Cable, Zapbo, Zasro, Adduction]
  92. ####### Validateur
  93. class Mn3ApdChecker(BaseChecker):
  94. def setUp(self):
  95. self.dataset = {}
  96. for model in models:
  97. model.layer = next((l for l in QgsProject.instance().mapLayers().values() \
  98. if l.name().lower() == model.layername.lower()), None)
  99. self.dataset[model] = [model(f) for f in model.layer.getFeatures()] if model.layer else []
  100. self.sites_telecom = self.dataset.get(SiteTelecom, [])
  101. self.sites_client = self.dataset.get(SiteClient, [])
  102. self.cables = self.dataset.get(Cable, [])
  103. self.zapbos = self.dataset.get(Zapbo, [])
  104. self.zasros = self.dataset.get(Zasro, [])
  105. self.adductions = self.dataset.get(Adduction, [])
  106. def test_load_layers(self):
  107. """ Chargement des données
  108. Contrôle la présence des couches attendues
  109. """
  110. for model in models:
  111. if model.layer is None:
  112. self.log_error("Couche manquante", model=model)
  113. continue
  114. if model.pk:
  115. if not model.pk.lower() in [f.name().lower() for f in model.layer.fields()]:
  116. self.log_error(f"Clef primaire manquante ({model.pk})", model=model)
  117. continue
  118. def test_scr(self):
  119. """ Contrôle des projections
  120. Vérifie que les couches ont le bon sytème de projection
  121. """
  122. for model in models:
  123. if model.layer and model.layer.crs().authid() != model.crs:
  124. self.log_error(f"Mauvaise projection (attendu: {model.crs})", model=model)
  125. def _validate_structure(self, model, items):
  126. v = CerberusValidator(model.schema, purge_unknown=True, error_handler=CerberusErrorHandler, require_all=True)
  127. for item in items:
  128. v.validate(item.__dict__)
  129. for field, verrors in v.errors.items():
  130. for err in verrors:
  131. self.log_error(f"{field} : {_translate_messages(err)}", item=item)
  132. def test_structure_sites_telecom(self):
  133. """ Structure des données: SiteTelecom
  134. Contrôle les données attributaires de la couche SITE_TELECOM:
  135. présence, format, valeurs autorisées...
  136. """
  137. self._validate_structure(SiteTelecom, self.sites_telecom)
  138. def test_structure_sites_client(self):
  139. """ Structure des données: SiteClient
  140. Contrôle les données attributaires de la couche SITE_CLIENT:
  141. présence, format, valeurs autorisées...
  142. """
  143. self._validate_structure(SiteClient, self.sites_client)
  144. def test_structure_cables(self):
  145. """ Structure des données: Cables
  146. Contrôle les données attributaires de la couche CABLE:
  147. présence, format, valeurs autorisées...
  148. """
  149. self._validate_structure(Cable, self.cables)
  150. def test_structure_zapbos(self):
  151. """ Structure des données: Zapbo
  152. Contrôle les données attributaires de la couche ZAPBO:
  153. présence, format, valeurs autorisées...
  154. """
  155. self._validate_structure(Zapbo, self.zapbos)
  156. def test_structure_zasros(self):
  157. """ Structure des données: Zasro
  158. Contrôle les données attributaires de la couche ZASRO:
  159. présence, format, valeurs autorisées...
  160. """
  161. self._validate_structure(Zasro, self.zasros)
  162. def test_structure_adductions(self):
  163. """ Structure des données: Adduction
  164. Contrôle les données attributaires de la couche ADDUCTION:
  165. présence, format, valeurs autorisées...
  166. """
  167. self._validate_structure(Adduction, self.adductions)
  168. def test_geometry_validity(self):
  169. """ Contrôle de la validité des géométries
  170. """
  171. for model in models:
  172. for item in self.dataset[model]:
  173. if not item.is_geometry_valid():
  174. self.log_error("La géométrie de l'objet est invalide", item=item)
  175. def test_geometry_type(self):
  176. """ Contrôle des types de géométries
  177. """
  178. for model in models:
  179. for item in self.dataset[model]:
  180. item_geom_type = item.get_geom_type()
  181. if item_geom_type != model.geom_type:
  182. expected, found = model.get_geom_name(model.geom_type), model.get_geom_name(item_geom_type)
  183. self.log_error(f"Type de géométrie invalide (attendu: {expected}, trouvé: {found})", item=item)
  184. def test_bounding_box(self):
  185. """ Contrôle des emprises
  186. Vérifie que les objets sont dans le périmètre attendu
  187. """
  188. for model in models:
  189. xmin, ymin, xmax, ymax = model.bounding_box
  190. for item in self.dataset[model]:
  191. if not item.is_geometry_valid():
  192. continue
  193. x1, y1, x2, y2 = item.get_bounding_box()
  194. if any(x < xmin or x > xmax for x in (x1, x2)) or \
  195. any(y < ymin or y > ymax for y in (y1, y2)):
  196. self.log_error("Hors de l'emprise autorisée", item=item)
  197. def test_duplicates(self):
  198. """ Recherche de doublons
  199. Recherche d'éventuels doublons dans des champs qui supposent l'unicité
  200. """
  201. # doublons dans ST_CODE
  202. tmp = []
  203. for site_telecom in self.sites_telecom:
  204. if not site_telecom.ST_CODE:
  205. continue
  206. if not site_telecom.ST_CODE in tmp:
  207. tmp.append(site_telecom.ST_CODE)
  208. else:
  209. self.log_error("Doublons dans le champs ST_CODE", item=site_telecom)
  210. def test_dimension_zasro(self):
  211. """ Contrôle le dimensionnement des ZASRO """
  212. for zasro in self.zasros:
  213. if zasro.ZS.NBPRISES > 850:
  214. self.log_error("Le nombre de prises est supérieur à 850", item=zasro)
  215. def test_affaiblissement(self):
  216. """ Contrôle l'affaiblissement """
  217. for site_client in self.site_clients:
  218. if site_client.ZS.NBPRISES > 28:
  219. self.log_error("L'affaiblissement est supérieur à 28", item=site_client)
  220. def test_capacite_modulo(self):
  221. """ Contrôle l'affaiblissement """
  222. for cable in self.cables:
  223. if cable.CA_CAPFO in [720,576,288,144,96] and cable.MODULO != 12:
  224. self.log_error(f"Modulo invalide (capacite: {cable.CA_CAPFO}, modulo: {cable.MODULO})", item=cable)
  225. elif cable.CA_CAPFO in [48,24,12] and cable.MODULO != 6:
  226. self.log_error(f"Modulo invalide (capacite: {cable.CA_CAPFO}, modulo: {cable.MODULO})", item=cable)
  227. def test_longueur_racco(self):
  228. """ Contrôle la longueur des raccos """
  229. for adduction in self.adductions:
  230. if adduction.AD_ISOLE == "1" and adduction.AD_LONG <= 100:
  231. self.log_error("L'adduction ne devrait pas être consiudérée comme isolée (long.: {adduction.AD_LONG})", item=adduction)
  232. elif adduction.AD_ISOLE == "0" and adduction.AD_LONG > 100:
  233. self.log_error("L'adduction devrait être considérée comme isolée (long.: {adduction.AD_LONG})", item=adduction)
  234. def test_zapbos_prises(self):
  235. """ Topologie: Zapbos / Prises
  236. Toutes les zapbo contiennent au moins une prise"""
  237. for zapbo in self.zapbos:
  238. nb_prises = sum([site_client.SC_NBPRISE for site_client in self.site_clients if zapbo.geom.contains(site_client.geom)])
  239. if not nb_prises:
  240. self.log_error("La Zapbo ne contient aucune prise", item=zapbo)
  241. for site_client in self.site_clients:
  242. nb_zapbo = len([zapbo for zapbo in self.zapbos if zapbo.geom.contains(site_client.geom)])
  243. if nb_zapbo == 0:
  244. self.log_error("La prise n'est contenue dans aucune ZAPBO", item=site_client)
  245. elif nb_zapbo > 1:
  246. self.log_error("La prise est contenue dans plus d'une ZAPBO", item=site_client)
  247. checkers = [Mn3ApdChecker]