validator.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. '''
  2. @author: olivier.massot, 2018
  3. '''
  4. from core.validation import NetgeoValidator, RelationError, \
  5. DuplicatedGeom, MissingItem, DimensionError, TechnicalValidationError, \
  6. InvalidGeometry, UniqueError
  7. from schemas.common import TOLERANCE
  8. from schemas.mn1_rec.models import Artere, Cable, Equipement, Noeud, \
  9. Tranchee, Zapbo
  10. class Mn1RecValidator(NetgeoValidator):
  11. schema_name = "Mn1 REC"
  12. models = [Artere, Cable, Equipement, Noeud, Tranchee, Zapbo]
  13. def _technical_validation(self):
  14. # construction des index
  15. arteres = self.dataset[Artere]
  16. cables = self.dataset[Cable]
  17. tranchees = self.dataset[Tranchee]
  18. noeuds = {}
  19. for noeud in self.dataset[Noeud]:
  20. if not noeud.NO_NOM in noeuds:
  21. noeuds[noeud.NO_NOM] = noeud
  22. else:
  23. self.log_error(UniqueError("Doublons dans le champs: {}".format(noeud), filename=Noeud.filename, field="NO_NOM"))
  24. equipements = {}
  25. for equipement in self.dataset[Equipement]:
  26. if not equipement.EQ_NOM in equipements:
  27. equipements[equipement.EQ_NOM] = equipement
  28. else:
  29. self.log_error(UniqueError("Doublons dans le champs: {}".format(equipement), filename=Equipement.filename, field="EQ_NOM"))
  30. zapbos = {}
  31. for zapbo in self.dataset[Zapbo]:
  32. if not zapbo.ID_ZAPBO in zapbos:
  33. zapbos[zapbo.ID_ZAPBO] = zapbo
  34. else:
  35. self.log_error(UniqueError("Doublons dans le champs: {}".format(zapbo), filename=Zapbo.filename, field="ID_ZAPBO"))
  36. # contrôle de la validité des géométries
  37. for artere in arteres:
  38. if not artere.valid:
  39. self.log_error(InvalidGeometry("Géométrie invalide: {}".format(artere), filename=Artere.filename, field="geom"))
  40. for tranchee in tranchees:
  41. if not tranchee.valid:
  42. self.log_error(InvalidGeometry("Géométrie invalide: {}".format(tranchee), filename=Tranchee.filename, field="geom"))
  43. for cable in cables:
  44. if not "baguette" in cable.CA_COMMENT.lower() and not cable.valid:
  45. self.log_error(InvalidGeometry("Géométrie invalide: {}".format(cable), filename=Cable.filename, field="geom"))
  46. for noeud in noeuds.values():
  47. if not noeud.valid:
  48. self.log_error(InvalidGeometry("Géométrie invalide: {}".format(noeud), filename=Noeud.filename, field="geom"))
  49. for equipement in equipements.values():
  50. if not equipement.valid:
  51. self.log_error(InvalidGeometry("Géométrie invalide: {}".format(equipement), filename=Equipement.filename, field="geom"))
  52. for zapbo in zapbos.values():
  53. if not zapbo.valid:
  54. self.log_error(InvalidGeometry("Géométrie invalide: {}".format(zapbo), filename=Zapbo.filename, field="geom"))
  55. # rattachement les noeuds aux artères
  56. for artere in arteres:
  57. try:
  58. artere.noeud_a = noeuds[artere.AR_NOEUD_A]
  59. except KeyError:
  60. artere.noeud_a = None
  61. self.log_error(RelationError("Le noeud '{}' n'existe pas".format(artere.AR_NOEUD_A), filename=Artere.filename, field="AR_NOEUD_A"))
  62. try:
  63. artere.noeud_b = noeuds[artere.AR_NOEUD_B]
  64. except KeyError:
  65. artere.noeud_b = None
  66. self.log_error(RelationError("Le noeud '{}' n'existe pas".format(artere.AR_NOEUD_B), filename=Artere.filename, field="AR_NOEUD_A"))
  67. # rattachement des equipements aux cables
  68. for cable in cables:
  69. try:
  70. cable.equipement_a = equipements[cable.CA_EQ_A]
  71. except KeyError:
  72. cable.equipement_a = None
  73. self.log_error(RelationError("L'équipement '{}' n'existe pas".format(cable.CA_EQ_A), filename=Cable.filename, field="CA_EQ_A"))
  74. try:
  75. cable.equipement_b = equipements[cable.CA_EQ_B]
  76. except KeyError:
  77. cable.equipement_b = None
  78. self.log_error(RelationError("L'équipement '{}' n'existe pas".format(cable.CA_EQ_B), filename=Cable.filename, field="CA_EQ_B"))
  79. # rattachement des equipements aux noeuds
  80. for equipement in equipements.values():
  81. try:
  82. equipement.noeud = noeuds[equipement.EQ_NOM_NOE]
  83. except KeyError:
  84. equipement.noeud = None
  85. self.log_error(RelationError("Le noeud '{}' n'existe pas".format(equipement.EQ_NOM_NOE, equipement.EQ_NOM), filename=Equipement.filename, field="EQ_NOM_NOE"))
  86. # verifie que tous les equipements sont l'equipement B d'au moins un cable
  87. equipements_b = [cable.CA_EQ_B for cable in cables]
  88. for eq_id in equipements:
  89. if equipements[eq_id].EQ_TYPE == "BAI":
  90. continue
  91. if not eq_id in equipements_b:
  92. self.log_error(RelationError("L'equipement '{}' n'est l'équipement B d'aucun cable".format(eq_id), filename=Equipement.filename, field="EQ_NOM"))
  93. # controle des doublons graphiques
  94. for i, tranchee in enumerate(tranchees):
  95. for other in tranchees[i+1:]:
  96. if tranchee.geom == other.geom:
  97. self.log_error(DuplicatedGeom("Une entité graphique est dupliquée".format(tranchee), filename=Tranchee.filename, field="geom"))
  98. for i, artere in enumerate(arteres):
  99. for other in arteres[i+1:]:
  100. if artere.geom == other.geom:
  101. self.log_error(DuplicatedGeom("Une entité graphique est dupliquée ('{}')".format(artere), filename=Artere.filename, field="geom"))
  102. for i, cable in enumerate(cables):
  103. for other in cables[i+1:]:
  104. if cable.geom == other.geom and cable.CA_EQ_A == other.CA_EQ_A and cable.CA_EQ_B == other.CA_EQ_B:
  105. self.log_error(DuplicatedGeom("Une entité graphique est dupliquée ('{}')".format(cable), filename=Cable.filename, field="geom"))
  106. ls_noeuds = list(noeuds.values())
  107. for i, noeud in enumerate(ls_noeuds):
  108. for other in ls_noeuds[i+1:]:
  109. if noeud.geom == other.geom:
  110. self.log_error(DuplicatedGeom("Une entité graphique est dupliquée ('{}')".format(noeud), filename=Noeud.filename, field="geom"))
  111. del ls_noeuds
  112. ls_zapbos = list(zapbos.values())
  113. for i, zapbo in enumerate(ls_zapbos):
  114. for other in ls_zapbos[i+1:]:
  115. if zapbo.geom == other.geom:
  116. self.log_error(DuplicatedGeom("Une entité graphique est dupliquée ('{}')".format(zapbo), filename=Zapbo.filename, field="geom"))
  117. del ls_zapbos
  118. # Arteres: comparer la géométrie à celle des noeuds
  119. for artere in arteres:
  120. if not artere.noeud_a or not artere.noeud_b:
  121. continue
  122. buffer_a, buffer_b = artere.points[0].Buffer(TOLERANCE), artere.points[-1].Buffer(TOLERANCE)
  123. if not (buffer_a.Contains(artere.noeud_a.points[0]) and buffer_b.Contains(artere.noeud_b.points[0])) \
  124. and not (buffer_a.Contains(artere.noeud_b.points[0]) and buffer_b.Contains(artere.noeud_a.points[0])):
  125. self.log_error(MissingItem("Pas de noeud aux coordonnées attendues ('{}')".format(artere), filename=Artere.filename, field="geom"))
  126. # Cables: comparer la géométrie à celle des equipements (on utilise en fait la geom du noeud correspondant à l'équipement)
  127. for cable in cables:
  128. if not cable.equipement_a or not cable.equipement_b or not cable.valid or not cable.equipement_a.noeud or not cable.equipement_b.noeud:
  129. continue
  130. buffer_a, buffer_b = cable.points[0].Buffer(TOLERANCE), cable.points[-1].Buffer(TOLERANCE)
  131. if not (buffer_a.Contains(cable.equipement_a.noeud.points[0]) and buffer_b.Contains(cable.equipement_b.noeud.points[0])) \
  132. and not (buffer_a.Contains(cable.equipement_b.noeud.points[0]) and buffer_b.Contains(cable.equipement_a.noeud.points[0])):
  133. self.log_error(MissingItem("Pas d'equipement aux coordonnées attendues ('{}')".format(cable), filename=Cable.filename, field="geom"))
  134. del buffer_a, buffer_b
  135. # Verifie que chaque tranchée a au moins une artère
  136. arteres_emprise = Feature.buffered_union(arteres, TOLERANCE)
  137. for tranchee in tranchees:
  138. if not arteres_emprise.Contains(tranchee.geom):
  139. self.log_error(MissingItem("Tranchée sans artère ('{}')".format(tranchee), filename=Tranchee.filename, field="-"))
  140. # Verifie que chaque cable a au moins une artère (sauf si commentaire contient 'baguette')
  141. for cable in cables:
  142. if "baguette" in cable.CA_COMMENT.lower() or not cable.valid:
  143. continue
  144. if not arteres_emprise.Contains(cable.geom):
  145. self.log_error(MissingItem("Cable sans artère ('{}')".format(cable), filename=Cable.filename, field="-"))
  146. del arteres_emprise
  147. # Verifie que chaque artère a au moins un cable (sauf si commentaire contient un de ces mots 'racco client adductio attente bus 'sans cable'')
  148. cables_emprise = Feature.buffered_union(cables, TOLERANCE)
  149. for artere in arteres:
  150. if any(x in artere.AR_COMMENT.lower() for x in ['racco','client','adductio','attente','bus','sans cable']):
  151. continue
  152. if not cables_emprise.Contains(artere.geom):
  153. self.log_error(MissingItem("Artère sans cable ('{}')".format(artere), filename=Artere.filename, field="-"))
  154. del cables_emprise
  155. # Contrôle des dimensions logiques
  156. for artere in arteres:
  157. try:
  158. if not int(artere.AR_FOU_DIS) <= int(artere.AR_NB_FOUR):
  159. self.log_error(DimensionError("Le nombre de fourreaux disponibles doit être inférieur au nombre total ('{}')".format(artere), filename=Artere.filename, field="AR_FOU_DIS"))
  160. except (TypeError, ValueError):
  161. pass
  162. for cable in cables:
  163. try:
  164. if not int(cable.CA_NB_FO_U) <= int(cable.CA_NB_FO):
  165. self.log_error(DimensionError("Le nombre de fourreaux utilisés doit être inférieur au nombre total ('{}')".format(cable), filename=Cable.filename, field="CA_NB_FO_U"))
  166. if not int(cable.CA_NB_FO_D) <= int(cable.CA_NB_FO):
  167. self.log_error(DimensionError("Le nombre de fourreaux disponibles doit être inférieur au nombre total ('{}')".format(cable), filename=Cable.filename, field="CA_NB_FO_D"))
  168. except (TypeError, ValueError):
  169. pass
  170. # 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
  171. for equipement in equipements.values():
  172. if not equipement.EQ_TYPE == "PBO":
  173. continue
  174. #zapbos englobant l'equipement
  175. candidates = []
  176. for zapbo in zapbos.values():
  177. if zapbo.geom.Contains(equipement.geom):
  178. candidates.append(zapbo)
  179. # le pbo doit être contenu dans une zapbo
  180. if not candidates:
  181. self.log_error(MissingItem("Le PBO n'est contenu dans aucune ZAPBO: {}".format(equipement), filename=Equipement.filename, field="geom"))
  182. continue
  183. # On se base sur le nom pour trouver la zapbo correspondante
  184. try:
  185. equipement.zapbo = next((z for z in candidates if equipement.EQ_NOM in z.ID_ZAPBO))
  186. except StopIteration:
  187. self.log_error(MissingItem("Le nom du PBO ne coincide avec le nom d'aucune des ZAPBO qui le contient: {}".format(equipement), filename=Equipement.filename, field="EQ_NOM"))
  188. break
  189. if equipement.zapbo.nb_prises is None:
  190. equipement.zapbo.nb_prises = 0
  191. # Controle du dimensionnement des PBO
  192. if equipement.EQ_TYPE_PH == 'PBO 6' and not equipement.zapbo.nb_prises < 6:
  193. self.log_error(DimensionError("Le PBO 6 contient plus de 5 prises: {}".format(equipement), filename=Equipement.filename, field="-"))
  194. if equipement.EQ_TYPE_PH == 'PBO 12' and not equipement.zapbo.nb_prises >= 6 and equipement.zapbo.nb_prises <= 8:
  195. self.log_error(DimensionError("Le PBO 12 contient mois de 6 prises ou plus de 8 prises: {}".format(equipement), filename=Equipement.filename, field="-"))
  196. if equipement.zapbo.STATUT == "REC" and not equipement.EQ_STATUT == "REC":
  197. self.log_error(TechnicalValidationError("Le statut du PBO n'est pas cohérent avec le statut de sa ZAPBO: {}".format(equipement), filename=Equipement.filename, field="-"))
  198. if equipement.EQ_STATUT == "REC" and not equipement.zapbo.STATUT == "REC" and not equipement.zapbo.ID_ZAPBO[:4].lower() == "att_":
  199. self.log_error(TechnicalValidationError("Le statut du PBO n'est pas cohérent avec le statut de sa ZAPBO: {}".format(equipement), filename=Equipement.filename, field="-"))
  200. if __name__ == "__main__":
  201. from core.constants import MAIN
  202. subject = MAIN / "work" / "SOGETREL_026AP0_REC_181001_OK"
  203. report = Netgeo112DoeValidator.submit(subject)
  204. print(report)