Forráskód Böngészése

Ajout de la validation étier pour le schéma 2.2

olivier.massot 7 éve
szülő
commit
0b012b5172

+ 11 - 6
core/validation.py

@@ -93,9 +93,9 @@ class GeomTypeError(StructureError):
     name = "Type de géométrie invalide"
     level = CRITICAL
     
-class PositionError(StructureError):
+class BoundingBoxError(StructureError):
     order_ = 11
-    name = "Position hors de la zone autorisée"
+    name = "Coordonnées hors de la zone autorisée"
     
 class InvalidGeometry(StructureError):
     order_ = 13
@@ -132,7 +132,10 @@ class DimensionError(TechnicalValidationError):
     order_ = 25
     name = "Elément de dimension"
 
-
+class PositionError(TechnicalValidationError):
+    order_ = 26
+    name = "Erreur de positionnement"
+    
 ###########    VALIDATION    ################
 
 class BaseValidator():
@@ -144,11 +147,12 @@ class BaseValidator():
         self.valid = True
         self.checkpoints = []
         self.errors = []
+        self._current_checkpoint_valid = True
         self.dt = 0
     
     def checkpoint(self, title):
-        
-        self.checkpoints.append(Checkpoint(title, (not self.errors)))
+        self.checkpoints.append(Checkpoint(title, self._current_checkpoint_valid))
+        self._current_checkpoint_valid = True
         if self.errors:
             self.valid = False
             if self.critical_happened():
@@ -158,6 +162,7 @@ class BaseValidator():
         return any([err.level == CRITICAL for err in self.errors])
              
     def log_error(self, validation_error):
+        self._current_checkpoint_valid = False
         self.errors.append(validation_error)
          
     @classmethod
@@ -301,7 +306,7 @@ class NetgeoValidator(BaseValidator):
                 x1, y1, x2, y2 = item.bounding_box
                 if any(x < xmin or x > xmax for x in (x1, x2)) or \
                    any(y < ymin or y > ymax for y in (y1, y2)):
-                    self.log_error(PositionError("Situé hors de l'emprise autorisée", filename=model.filename, field="geom"))
+                    self.log_error(BoundingBoxError("Situé hors de l'emprise autorisée", filename=model.filename, field="geom"))
 
                 v.validate(item.__dict__)
                

+ 1 - 1
schemas/common.py

@@ -8,7 +8,7 @@ from core import mn
 XMIN, XMAX, YMIN, YMAX = 1341999, 1429750, 8147750, 8294000
 SRID = "3949"
 
-BUFFER = 2
+TOLERANCE = 1
 
 with mn.ANTDb() as ant_db:
     INSEE_VALIDES = [row[0] for row in ant_db.read("SELECT code_insee FROM sig_referentiel.admn_cd50_com;")]

+ 43 - 14
schemas/netgeo_1_12_doe/validator.py

@@ -4,11 +4,12 @@
 '''
 from osgeo import ogr
 
-from core import mn, gis_
+from core import mn
 from core.gis_ import Feature
 from core.validation import NetgeoValidator, RelationError, \
     DuplicatedGeom, MissingItem, DimensionError, TechnicalValidationError, \
-    InvalidGeometry, UniqueError
+    InvalidGeometry, UniqueError, PositionError
+from schemas.common import TOLERANCE
 from schemas.netgeo_1_12_doe.models import Artere, Cable, Equipement, Noeud, \
     Tranchee, Zapbo
 
@@ -144,30 +145,30 @@ class Netgeo112DoeValidator(NetgeoValidator):
             if not artere.noeud_a or not artere.noeud_b:
                 continue
             
-            buffer_a, buffer_b = artere.points[0].Buffer(1), artere.points[-1].Buffer(1)
+            buffer_a, buffer_b = artere.points[0].Buffer(TOLERANCE), artere.points[-1].Buffer(TOLERANCE)
             
             if not (buffer_a.Contains(artere.noeud_a.points[0]) and buffer_b.Contains(artere.noeud_b.points[0])) \
                and not (buffer_a.Contains(artere.noeud_b.points[0]) and buffer_b.Contains(artere.noeud_a.points[0])):
 
                 self.log_error(MissingItem("Pas de noeud aux coordonnées attendues ('{}')".format(artere), filename=Artere.filename, field="geom"))
-            
+        
         
         # Cables: comparer la géométrie à celle des equipements (on utilise en fait la geom du noeud correspondant à l'équipement)
         for cable in cables:
             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:
                 continue
             
-            buffer_a, buffer_b = cable.points[0].Buffer(1), cable.points[-1].Buffer(1)
+            buffer_a, buffer_b = cable.points[0].Buffer(TOLERANCE), cable.points[-1].Buffer(TOLERANCE)
             
             if not (buffer_a.Contains(cable.equipement_a.noeud.points[0]) and buffer_b.Contains(cable.equipement_b.noeud.points[0])) \
                and not (buffer_a.Contains(cable.equipement_b.noeud.points[0]) and buffer_b.Contains(cable.equipement_a.noeud.points[0])):
             
                 self.log_error(MissingItem("Pas d'equipement aux coordonnées attendues ('{}')".format(cable), filename=Cable.filename, field="geom"))
             
-        
+        del buffer_a, buffer_b 
         
         # Verifie que chaque tranchée a au moins une artère
-        arteres_emprise = Feature.buffered_union(arteres, 0.5)
+        arteres_emprise = Feature.buffered_union(arteres, TOLERANCE)
         
         for tranchee in tranchees:
             if not arteres_emprise.Contains(tranchee.geom):
@@ -184,7 +185,7 @@ class Netgeo112DoeValidator(NetgeoValidator):
         del arteres_emprise
         
         # 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'')
-        cables_emprise = Feature.buffered_union(cables, 0.5)
+        cables_emprise = Feature.buffered_union(cables, TOLERANCE)
         
         for artere in arteres:
             if any(x in artere.AR_COMMENT.lower() for x in ['racco','client','adductio','attente','bus','sans cable']):
@@ -216,6 +217,12 @@ class Netgeo112DoeValidator(NetgeoValidator):
         
         # Toutes les zapbo contiennent au moins une prise
         for zapbo in zapbos.values():
+            
+            if len(zapbo.points) >= 499:
+                # passe l'erreur provoquée par la limite au nombre d'arguments en SQL
+                zapbo.nb_prises = None
+                continue
+            
             sql = """Select SUM(NB_PRISE) AS NB_PRISES FROM SIG_ANT.FTTH_MN_PRISE_LOT z 
                        WHERE SDO_INSIDE(z.GEOMETRY,
                                       SDO_GEOMETRY(2003, 3949, SDO_POINT_TYPE(null,null,null), SDO_ELEM_INFO_ARRAY(1,1003,1),  SDO_ORDINATE_ARRAY({}))
@@ -290,13 +297,35 @@ class Netgeo112DoeValidator(NetgeoValidator):
         
         # Contrôler dans la base si des éléments portant ces codes existent à des emplacements différents
         for noeud in noeuds.values():
-            sql = """Select z.ID_FTTH_MN_GR_NOEUD_GEO FROM SIG_ANT.FTTH_MN_GR_NOEUD_GEO z 
-                        WHERE z.NO_NOM='{}' 
-                       AND SDO_GEOM.SDO_DISTANCE(z.GEOMETRY, SDO_GEOMETRY(2001, 3949, SDO_POINT_TYPE({}, {}, NULL), NULL, NULL),0.005)>10;
-                       """.format(noeud.NO_NOM, noeud.geom.GetX(), noeud.geom.GetY())
-            if ant_db.exists(sql):
-                self.log_error(DuplicatedGeom("Un noeud portant ce nom existe déjà ailleurs sur le territoire: {}".format(noeud), filename=Noeud.filename, field="NO_NOM"))
+            sql = """SELECT z.NO_NOM, SDO_GEOM.SDO_DISTANCE(z.GEOMETRY, SDO_GEOMETRY(2001, 3949, SDO_POINT_TYPE({}, {}, NULL), NULL, NULL),0.005) AS DIST 
+                     FROM SIG_ANT.FTTH_MN_GR_NOEUD_GEO z 
+                     WHERE z.NO_NOM='{}';""".format(noeud.geom.GetX(), noeud.geom.GetY(), noeud.NO_NOM)
+            existing = ant_db.first(sql)
+            if existing:
+                if existing.DIST > TOLERANCE and existing.DIST < 20:
+                    self.log_error(PositionError("La position du noeud ne correspond pas à l'existant: {}".format(noeud), filename=Noeud.filename, field="geom"))
+                elif existing.DIST > 20:
+                    self.log_error(DuplicatedGeom("Un noeud portant ce nom existe déjà ailleurs sur le territoire: {}".format(noeud), filename=Noeud.filename, field="NO_NOM"))
         
+        for zapbo in zapbos.values():
+            sql = """SELECT z.ID_ZAPBO, SDO_GEOM.SDO_DISTANCE(SDO_GEOM.SDO_CENTROID(z.GEOMETRY,0.005), SDO_GEOMETRY(2001, 3949, SDO_POINT_TYPE({}, {}, NULL), NULL, NULL),0.005) AS DIST 
+                     FROM SIG_ANT.FTTH_MN_GR_ZAPBO_GEO z 
+                     WHERE z.ID_ZAPBO='{}';""".format(zapbo.geom.Centroid().GetX(), zapbo.geom.Centroid().GetY(), zapbo.ID_ZAPBO)
+            existing = ant_db.first(sql)
+            if existing:
+                if existing.DIST > TOLERANCE and existing.DIST < 20:
+                    self.log_error(PositionError("La position de la ZAPBO ne correspond pas à l'existant: {}".format(zapbo), filename=Zapbo.filename, field="geom"))
+                elif existing.DIST > 20:
+                    self.log_error(DuplicatedGeom("Une ZAPBO portant ce nom existe déjà ailleurs sur le territoire: {}".format(zapbo), filename=Zapbo.filename, field="ID_ZAPBO"))
+
+        # Contrôle si un equipement portant ce nom existe, mais associé à un noeud différent
+        for equipement in equipements.values():
+            sql = """SELECT z.EQ_NOM, z.EQ_NOM_NOEUD
+                     FROM SIG_ANT.FTTH_MN_GR_EQ_PASSIF z 
+                     WHERE z.EQ_NOM='{}';""".format(equipement.EQ_NOM)
+            existing = ant_db.first(sql)
+            if existing and existing.EQ_NOM_NOEUD != equipement.EQ_NOM_NOE:
+                self.log_error(DuplicatedGeom("Un équipement portant ce nom ({}) existe déjà et est associé à un noeud différent ({})".format(equipement.NO_NOM, existing.EQ_NOM_NOEUD), filename=Noeud.filename, field="geom"))
         
         
 if __name__ == "__main__":

+ 108 - 81
schemas/netgeo_2_2_doe/models.py

@@ -2,161 +2,188 @@
 
 @author: olivier.massot, 2018
 '''
-from core import gis
-from core.cerberus_extend import is_french_date, is_float, is_int
+from core import gis_
+from core.cerberus_extend import is_int, is_float, \
+    is_modern_french_date, is_multi_int
 from core.validation import BaseGeoModel
 from schemas.common import INSEE_VALIDES, XMIN, YMIN, XMAX, YMAX
 
+STATUTS = ['EN ETUDE', 'EN REALISATION', 'EN SERVICE', 'HORS SERVICE']
 
 class Artere(BaseGeoModel):
     filename = "artere_geo.shp"
-    schema = {'geom': {'geometry': (gis.POLYLINE, (XMIN, YMIN, XMAX, YMAX))}, 
-              'AR_CODE': {'type': 'string', 'maxlength': 26}, 
+    geom_type = gis_.GEOM_LINE
+    bounding_box = (XMIN,YMIN,XMAX,YMAX)
+    schema = {'AR_CODE': {'type': 'string', 'maxlength': 26}, 
               'AR_NOM': {'type': 'string', 'maxlength': 26}, 
               'AR_ID_INSE': {'type': 'string', 'empty': False, 'allowed': INSEE_VALIDES}, 
-              'AR_LONG': {'validator': is_float}, 
+              'AR_LONG': {'empty': False, 'validator': is_float},
               'AR_ETAT': {'type': 'string', 'empty': False, 'allowed': ['0', '1', '2', '3', '4']}, 
               'AR_OCCP': {'type': 'string', 'empty': False, 'allowed': ['0', '1.1', '1.2', '2', '3', '4']}, 
-              'AR_NOEUD_A': {'type': 'string', 'maxlength': 20}, 
-              'AR_NOEUD_B': {'type': 'string', 'maxlength': 20}, 
-              'AR_NB_FOUR': {'type': 'string', 'maxlength': 10}, 
-              'AR_FOU_DIS': {'validator': is_int}, 
-              'AR_TYPE_FO': {'type': 'string', 'empty': False, 'multiallowed': ['PVC', 'PEHD', 'SOUS-TUBAGE PEHD', 'SOUS-TUBAGE  SOUPLE', 'FACADE', 'AERIEN', 'ENCORBELLEMENT', 'AUTRE']}, 
-              'AR_TYFO_AI': {'type': 'string', 'empty': False, 'multiallowed': ['PVC', 'PEH', 'TUB', 'FAC', 'ENC', 'APP']}, 
-              'AR_DIAM_FO': {'type': 'string', 'empty': False, 'multiallowed': ['10', '14', '18', '25', '28', '32', '40', '45', '60', 'NUL']}, 
-              'AR_PRO_FOU': {'type': 'string', 'empty': False, 'multiallowed': ['MANCHE NUMERIQUE', 'COLLECTIVITE', 'ORANGE', 'AUTRE (à préciser)']}, 
-              'AR_FAB': {'type': 'string', 'maxlength': 100}, 
-              'AR_REFFAB': {'type': 'string', 'maxlength': 100}, 
-              'AR_COULEUR': {'type': 'string', 'maxlength': 20}, 
-              'AR_AIGUIL': {'type': 'string', 'empty': False, 'allowed': ['OUI', 'NON']}, 
-              'AR_NBCABL': {'validator': is_int}, 
+              'AR_NOEUD_A': {'type': 'string', 'empty': False, 'maxlength': 20}, 
+              'AR_NOEUD_B': {'type': 'string', 'empty': False, 'maxlength': 20}, 
+              'AR_NB_FOUR': {'empty': False, 'validator': is_multi_int}, 
+              'AR_FOU_DIS': {'empty': False, 'validator': is_int}, 
+              'AR_TYPE_FO': {'type': 'string', 'multiallowed': ['PVC', 'PEHD', 'SOUS-TUBAGE PEHD', 'SOUS-TUBAGE  SOUPLE', 'FACADE', 'AERIEN', 'ENCORBELLEMENT', 'AUTRE']}, 
+              'AR_TYFO_AI': {'type': 'string', 'multiallowed': ['PVC', 'PEH', 'TUB', 'FAC', 'ENC', 'APP']}, 
+              'AR_DIAM_FO': {'type': 'string', 'multiallowed': ['10', '14', '18', '25', '28', '32', '40', '45', '60', '80', '150', 'NUL']}, 
+              'AR_PRO_FOU': {'type': 'string', 'multiallowed': ['MANCHE NUMERIQUE', 'COLLECTIVITE', 'ORANGE', 'PRIVE', 'ERDF', 'AUTRE (à préciser)']}, 
+              'AR_FABRICANT': {'type': 'string', 'empty': False, 'maxlength': 100},
+              'AR_REF_FABRICANT': {'type': 'string', 'empty': False, 'maxlength': 100},
+              'AR_COULEUR': {'type': 'string', 'empty': False, 'maxlength': 20},
+              'AR_AIGUILLEE': {'type': 'string', 'empty': False, 'maxlength': 3, 'allowed': ['OUI', 'NON']},
+              'AR_NB_CABLES': {'empty': False, 'validator': is_int},
               'AR_PRO_CAB': {'type': 'string', 'empty': False, 'allowed': ['MANCHE NUMERIQUE']}, 
-              'AR_GEST_FO': {'type': 'string', 'empty': False, 'multiallowed': ['MANCHE NUMERIQUE', 'MANCHE TELECOM', 'COLLECTIVITE', 'ORANGE', 'MANCHE FIBRE', 'ERDF', 'PRIVE', 'AUTRE (à préciser)', 'NUL']}, 
-              'AR_UTIL_FO': {'type': 'string', 'empty': False, 'multiallowed': ['MANCHE NUMERIQUE', 'MANCHE TELECOM', 'COLLECTIVITE', 'ORANGE', 'PRIVE', 'AUTRE (à préciser)', 'NUL']}, 
-              'AR_DATE_IN': {'empty': False, 'validator': is_french_date}, 
-              'AR_DATE_RE': {'empty': False, 'validator': is_french_date}, 
+              'AR_GEST_FO': {'type': 'string', 'multiallowed': ['MANCHE NUMERIQUE', 'MANCHE TELECOM', 'COLLECTIVITE', 'ORANGE', 'MANCHE FIBRE', 'PRIVE', 'ERDF', 'AUTRE (à préciser)', 'NUL']}, 
+              'AR_UTIL_FO': {'type': 'string', 'multiallowed': ['MANCHE NUMERIQUE', 'MANCHE TELECOM', 'COLLECTIVITE', 'ORANGE', 'MANCHE FIBRE', 'PRIVE', 'AUTRE (à préciser)', 'NUL']}, 
+              'AR_DATE_IN': {'empty': False, 'validator': is_modern_french_date}, 
+              'AR_DATE_RE': {'empty': False, 'validator': is_modern_french_date}, 
               'AR_REF_PLA': {'type': 'string', 'maxlength': 100}, 
               'AR_SRC_GEO': {'type': 'string', 'maxlength': 50}, 
               'AR_QLT_GEO': {'type': 'string', 'empty': False, 'allowed': ['A', 'B', 'C']}, 
               'AR_PRO_MD': {'type': 'string', 'empty': False, 'default': 'MANCHE NUMERIQUE', 'allowed': ['MANCHE NUMERIQUE']}, 
               'AR_COMMENT': {'type': 'string', 'maxlength': 300, 'empty': True}, 
-              'AR_STATUT': {'type': 'string', 'empty': False, 'allowed': ['EN ETUDE', 'EN REALISATION', 'EN SERVICE', 'HORS SERVICE']}}
+              'AR_STATUT': {'type': 'string', 'empty': False, 'allowed': STATUTS}}
+
+    def __repr__(self):
+        return "Artere {}".format(self.AR_CODE)
 
 class Cable(BaseGeoModel):
     filename = "cable_geo.shp"
-    schema = {'geom': {'geometry': (gis.POLYLINE, (XMIN, YMIN, XMAX, YMAX))}, 
-              'CA_CODE': {'type': 'string', 'maxlength': 18}, 
+    geom_type = gis_.GEOM_LINE
+    bounding_box = (XMIN,YMIN,XMAX,YMAX)
+    schema = {'CA_CODE': {'type': 'string', 'maxlength': 18}, 
               'CA_NOM': {'type': 'string', 'maxlength': 18}, 
-              'CA_NUMERO': {'type': 'string', 'maxlength': 17}, 
+              'CA_NUMERO': {'type': 'string', 'maxlength': 17},  
               'CA_EMPRISE': {'type': 'string', 'maxlength': 10}, 
               'CA_FAB': {'type': 'string', 'maxlength': 100}, 
               'CA_REFFAB': {'type': 'string', 'maxlength': 100}, 
               'CA_MODELE': {'type': 'string', 'maxlength': 10}, 
-              'CA_TYPE': {'type': 'string', 'maxlength': 10, 'empty': False, 'allowed': ['AERIEN', 'IMMEUBLE', 'FACADE', 'MIXTE', 'SOUTERRAIN']}, 
-              'CA_TYPFCT': {'type': 'string', 'maxlength': 3, 'empty': False, 'allowed': ['CDI', 'CTR', 'CBM', 'RAC', 'CBO']}, 
+              'CA_TYPE': {'type': 'string', 'maxlength': 10, 'empty': False, 'allowed': ['AERIEN', 'IMMEUBLE', 'FACADE', 'MIXTE', 'SOUTERRAIN']},
+              'CA_TYPFCT': {'type': 'string', 'maxlength': 3, 'empty': False, 'allowed': ['CDI', 'CTR', 'CBM', 'RAC', 'CBO']},
               'CA_ETAT': {'type': 'string', 'maxlength': 1, 'empty': False, 'allowed': ['0', '1', '2', '3', '4']}, 
               'CA_LONG': {'validator': is_float}, 
               'CA_EQ_A': {'type': 'string', 'maxlength': 18}, 
               'CA_EQ_B': {'type': 'string', 'maxlength': 18}, 
-              'CA_DIAMETR': {'validator': is_int}, 
+              'CA_DIAMETR': {'empty': False, 'validator': is_float}, 
               'CA_COULEUR': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['NOIR', 'BLEU', 'BLANC']}, 
               'CA_TECHNOL': {'type': 'string', 'maxlength': 17, 'empty': False, 'allowed': ['G657A2_M6', 'G657A2_M12']}, 
-              'CA_NB_FO_U': {'validator': is_int}, 
-              'CA_NB_FO_D': {'validator': is_int}, 
+              'CA_NB_FO': {'validator': is_int}, 
+              'CA_NB_FO_U': {'empty': False, 'validator': is_int}, 
+              'CA_NB_FO_D': {'empty': False, 'validator': is_int}, 
               'CA_PRO': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['MANCHE NUMERIQUE']}, 
               'CA_GEST': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['MANCHE FIBRE']}, 
-              'CA_DATE_IN': {'empty': False, 'validator': is_french_date}, 
+              'CA_DATE_IN': {'empty': False, 'validator': is_modern_french_date}, 
               'CA_COMMENT': {'type': 'string', 'maxlength': 300, 'empty': True}, 
-              'CA_STATUT': {'type': 'string', 'maxlength': 14, 'empty': False, 'allowed': ['EN ETUDE', 'EN REALISATION', 'EN SERVICE', 'HORS SERVICE']}}
+              'CA_STATUT': {'type': 'string', 'empty': False, 'allowed': STATUTS}}
 
+    def __repr__(self):
+        return "Cable {}-{}".format(self.CA_EQ_A, self.CA_EQ_B)
+    
 class Equipement(BaseGeoModel):
     filename = "equipement_passif.shp"
-    schema = {'geom': {'geometry': (gis.POINT, (XMIN, YMIN, XMAX, YMAX))}, 
-              'EQ_CODE': {'type': 'string', 'maxlength': 18}, 
-              'EQ_NOM': {'type': 'string', 'maxlength': 18}, 
-              'EQ_NOM_NOE': {'type': 'string', 'maxlength': 14}, 
+    geom_type = gis_.GEOM_POINT
+    bounding_box = (XMIN,YMIN,XMAX,YMAX)
+    schema = {'EQ_CODE': {'type': 'string', 'maxlength': 18}, 
+              'EQ_NOM': {'type': 'string', 'maxlength': 10, 'contains_any_of': ['PBO', 'BPE', 'BAI']}, 
+              'EQ_NOM_NOE': {'type': 'string', 'maxlength': 30}, 
               'EQ_REF': {'type': 'string', 'maxlength': 100}, 
               'EQ_EMPRISE': {'type': 'string', 'maxlength': 7}, 
               'EQ_FABR': {'type': 'string', 'maxlength': 100}, 
-              'EQ_CAPFO': {'validator': is_int}, 
-              'EQ_NBMXEQ': {'validator': is_int}, 
-              'EQ_NBCAB': {'validator': is_int}, 
+              'EQ_CAPFO': {'empty': False, 'validator': is_int}, 
+              'EQ_NBMXEQ': {'empty': False, 'validator': is_int}, 
+              'EQ_NBCAB': {'empty': False, 'validator': is_int}, 
               'EQ_DIMENS': {'type': 'string', 'maxlength': 50}, 
               'EQ_TYPEQ': {'type': 'string', 'maxlength': 100}, 
-              'EQ_ETAT': {'type': 'string', 'maxlength': 1, 'empty': False, 'allowed': ['0', '1', '2', '3', '4']}, 
-              'EQ_OCCP': {'type': 'string', 'maxlength': 3, 'empty': False, 'allowed': ['0', '1.1', '1.2', '2', '3', '4']}, 
-              'EQ_TYPE': {'type': 'string', 'maxlength': 3, 'empty': False, 'allowed': ['PBO', 'PEC', 'BPE', 'BAI']}, 
-              'EQ_TYPSTRC': {'type': 'string', 'maxlength': 16, 'empty': False, 'allowed': ['CHAMBRE', 'AERIEN', 'FACADE', 'COLONNE MONTANTE', 'PIED IMMEUBLE', 'DTIO']}, 
-              'EQ_TYPE_LQ': {'type': 'string', 'maxlength': 6, 'empty': False, 'allowed': ['PBO', 'BPE JB', 'BPE JD', 'BAIDC', 'BAIOP']}, 
-              'EQ_TYPE_PH': {'type': 'string', 'maxlength': 24, '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']}, 
-              'EQ_PRO': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['MANCHE NUMERIQUE', 'COLLECTIVITE', 'ORANGE', 'PRIVE', 'AUTRE (à préciser)', 'NUL']}, 
-              'EQ_GEST': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['MANCHE NUMERIQUE', 'MANCHE TELECOM', 'COLLECTIVITE', 'ORANGE', 'PRIVE', 'MANCHE FIBRE', 'AUTRE (à préciser)', 'NUL']}, 
-              'EQ_HAUT': {'validator': is_float}, 
-              'EQ_DATE_IN': {'empty': False, 'validator': is_french_date}, 
+              'EQ_ETAT': {'type': 'string', 'empty': False, 'allowed': ['0', '1', '2', '3', '4']}, 
+              'EQ_OCCP': {'type': 'string', 'empty': False, 'allowed': ['0', '1.1', '1.2', '2', '3', '4']}, 
+              'EQ_TYPE': {'type': 'string', 'empty': False, 'allowed': ['PBO', 'PBOE', 'BPE', 'BAI']},
+              'EQ_TYPSTRC': {'type': 'string', 'empty': False, 'allowed': ['CHAMBRE', 'AERIEN', 'FACADE', 'COLONNE MONTANTE', 'PIED IMMEUBLE', 'DTIO']}, 
+              'EQ_TYPE_LQ': {'type': 'string', 'empty': False, 'allowed': ['PBO', 'BPE JB', 'BPE JD', 'BAIDC', 'BAIOP']}, 
+              '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']}, 
+              'EQ_PRO': {'type': 'string', 'empty': False, 'allowed': ['MANCHE NUMERIQUE', 'COLLECTIVITE', 'ORANGE', 'PRIVE', 'AUTRE (à préciser)', 'NUL']}, 
+              'EQ_GEST': {'type': 'string', 'empty': False, 'allowed': ['MANCHE NUMERIQUE', 'MANCHE TELECOM', 'COLLECTIVITE', 'ORANGE', 'MANCHE FIBRE', 'PRIVE', 'AUTRE (à préciser)', 'NUL']}, 
+              'EQ_HAUT': {'empty': False, 'validator': is_float}, 
+              'EQ_DATE_IN': {'empty': False, 'validator': is_modern_french_date}, 
               'EQ_COMMENT': {'type': 'string', 'maxlength': 300, 'empty': True}, 
-              'EQ_STATUT': {'type': 'string', 'maxlength': 14, 'empty': False, 'allowed': ['EN ETUDE', 'EN REALISATION', 'EN SERVICE', 'HORS SERVICE']}}
+              'EQ_STATUT': {'type': 'string', 'empty': False, 'allowed': STATUTS}}
+        
+    def __repr__(self):
+        return "Equipement {}".format(self.EQ_NOM)
 
 class Noeud(BaseGeoModel):
     filename = "noeud_geo.shp"
-    schema = {'geom': {'geometry': (gis.POINT, (XMIN, YMIN, XMAX, YMAX))}, 
-              'NO_CODE': {'type': 'string', 'maxlength': 18}, 
+    geom_type = gis_.GEOM_POINT
+    bounding_box = (XMIN,YMIN,XMAX,YMAX)
+    schema = {'NO_CODE': {'type': 'string', 'maxlength': 18}, 
+              'NO_NOM': {'type': 'string', 'maxlength': 30}, 
               'NO_ID_INSE': {'type': 'string', 'empty': False, 'allowed': INSEE_VALIDES}, 
-              'NO_NOM': {'type': 'string', 'maxlength': 20}, 
               'NO_VOIE': {'type': 'string', 'maxlength': 100}, 
               'NO_EMPRISE': {'type': 'string', 'maxlength': 10}, 
-              'NO_ETAT': {'type': 'string', 'maxlength': 1, 'empty': False, 'allowed': ['0', '1', '2', '3', '4']}, 
-              'NO_OCCP': {'type': 'string', 'maxlength': 3, 'empty': False, 'allowed': ['0', '1.1', '1.2', '2', '3', '4']}, 
-              'NO_TYPE': {'type': 'string', 'maxlength': 3, 'empty': False, 'allowed': ['CHA', 'POT', 'LTE', 'SEM', 'FAC', 'OUV', 'IMM']}, 
-              'NO_TYPFCT': {'type': 'string', 'maxlength': 14, 'empty': False, 'allowed': ['INTERCONNEXION', 'SATELLITE', 'PASSAGE', 'REGARD', 'INDETERMINE']}, 
-              'NO_TYPE_LQ': {'type': 'string', 'maxlength': 10, 'empty': False, 'allowed': ['CHTIR', 'CHRACC', 'POT', 'NRO', 'PM', 'MIMO', 'FAC', 'OUV', 'IMM']}, 
-              'NO_TYPE_PH': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['CHAMBRE', 'POTEAU', 'ARMOIRE', 'SHELTER', 'BATIMENT', 'SITE MIMO', 'FACADE', 'OUVRAGE', 'IMMEUBLE']}, 
+              'NO_ETAT': {'type': 'string', 'empty': False, 'allowed': ['0', '1', '2', '3', '4']}, 
+              'NO_OCCP': {'type': 'string', 'empty': False, 'allowed': ['0', '1.1', '1.2', '2', '3', '4']}, 
+              'NO_TYPE': {'type': 'string', 'empty': False, 'allowed': ['CHA', 'POT', 'LTE', 'SEM', 'FAC', 'OUV', 'IMM']}, 
+              'NO_TYPFCT': {'type': 'string', 'empty': False, 'allowed': ['INTERCONNEXION', 'SATELLITE', 'PASSAGE', 'REGARD', 'INDETERMINE']}, 
+              'NO_TYPE_LQ': {'type': 'string', 'empty': False, 'allowed': ['CHTIR', 'CHRACC', 'POT', 'NRO', 'PM', 'MIMO', 'FAC', 'OUV', 'IMM']}, 
+              'NO_TYPE_PH': {'type': 'string', 'empty': False, 'allowed': ['CHAMBRE', 'POTEAU', 'ARMOIRE', 'SHELTER', 'BATIMENT', 'SITE MIMO', 'FACADE', 'OUVRAGE', 'IMMEUBLE']}, 
               'NO_CODE_PH': {'type': 'string', 'maxlength': 20}, 
-              'NO_TECH_PS': {'type': 'string', 'maxlength': 4, 'empty': False, 'multiallowed': ['COAX', 'CUT', 'ECL', 'ELEC', 'VP', 'OPT', 'NC']}, 
+              'NO_TECH_PS': {'type': 'string', 'maxlength': 20, 'multiallowed': ['COAX', 'CUT', 'ECL', 'ELEC', 'VP', 'OPT', 'NC']}, 
               'NO_AMO': {'type': 'string', 'maxlength': 20}, 
-              'NO_PLINOX': {'type': 'string', 'maxlength': 3, 'empty': False, 'allowed': ['OUI', 'NON']}, 
-              'NO_X': {'validator': is_float}, 
-              'NO_Y': {'validator': is_float}, 
-              'NO_PRO': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['MANCHE NUMERIQUE', 'COLLECTIVITE', 'ORANGE', 'ERDF', 'PRIVE', 'AUTRE (à préciser)', 'NUL']}, 
-              'NO_GEST': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['MANCHE NUMERIQUE', 'MANCHE TELECOM', 'COLLECTIVITE', 'ORANGE', 'PRIVE', 'ERDF', 'MANCHE FIBRE', 'AUTRE (à préciser)', 'NUL']}, 
-              'NO_HAUT': {'empty': True, 'validator': is_float}, 
-              'NO_DATE_IN': {'empty': False, 'validator': is_french_date}, 
+              'NO_PLINOX': {'required':False, 'type': 'string', 'maxlength': 3, 'allowed': ['OUI', 'NON']}, 
+              'NO_X': {'empty': False, 'validator': is_float}, 
+              'NO_Y': {'empty': False, 'validator': is_float}, 
+              'NO_PRO': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['MANCHE NUMERIQUE', 'COLLECTIVITE', 'ORANGE', 'ERDF', 'PRIVE', 'ENEDIS', 'AUTRE (à préciser)', 'NUL']}, 
+              'NO_GEST': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['MANCHE NUMERIQUE', 'MANCHE TELECOM', 'COLLECTIVITE', 'ORANGE', 'ERDF', 'ENEDIS', 'MANCHE FIBRE', 'PRIVE', 'AUTRE (à préciser)', 'NUL']}, 
+              'NO_HAUT': {'empty': False, 'validator': is_float}, 
+              'NO_DATE_IN': {'empty': False, 'validator': is_modern_french_date}, 
               'NO_REF_PLA': {'type': 'string', 'maxlength': 100}, 
               'NO_SRC_GEO': {'type': 'string', 'maxlength': 50}, 
               'NO_QLT_GEO': {'type': 'string', 'maxlength': 1, 'empty': False, 'allowed': ['A', 'B', 'C']}, 
               'NO_PRO_MD': {'type': 'string', 'maxlength': 20, 'empty': False, 'allowed': ['MANCHE NUMERIQUE']}, 
               'NO_COMMENT': {'type': 'string', 'maxlength': 300, 'empty': True}, 
-              'NO_STATUT': {'type': 'string', 'maxlength': 14, 'empty': False, 'allowed': ['EN ETUDE', 'EN REALISATION', 'EN SERVICE', 'HORS SERVICE']}}
+              'NO_STATUT': {'type': 'string', 'empty': False, 'allowed': STATUTS}}
 
+    def __repr__(self):
+        return "Noeud {}".format(self.NO_NOM)
+    
 class Tranchee(BaseGeoModel):
     filename = "tranchee_geo.shp"
-    schema = {'geom': {'geometry': (gis.POLYLINE, (XMIN, YMIN, XMAX, YMAX))}, 
-              'TR_CODE': {'type': 'string', 'maxlength': 23}, 
+    geom_type = gis_.GEOM_LINE
+    bounding_box = (XMIN,YMIN,XMAX,YMAX)
+    schema = {'TR_CODE': {'type': 'string', 'maxlength': 23}, 
               'TR_NOM': {'type': 'string', 'maxlength': 23}, 
               'TR_ID_INSE': {'type': 'string', 'empty': False, 'allowed': INSEE_VALIDES}, 
               'TR_VOIE': {'type': 'string', 'maxlength': 200}, 
               'TR_TYP_IMP': {'type': 'string', 'empty': False, 'allowed': ['ACCOTEMENT STABILISE', 'ACCOTEMENT NON STABILISE', 'CHAUSSEE LOURDE', 'CHAUSSEE LEGERE', 'FOSSE', 'TROTTOIR', 'ESPACE VERT', 'ENCORBELLEMENT']}, 
               'TR_MOD_POS': {'type': 'string', 'empty': False, 'allowed': ['TRADITIONNEL', 'MICRO TRANCHEE', 'FONCAGE 60', 'FONCAGE 90', 'FONCAGE 120', 'TRANCHEUSE', 'FORAGE URBAIN', 'FORAGE RURAL', 'ENCORBELLEMENT']}, 
               'TR_MPOS_AI': {'type': 'string', 'empty': False, 'allowed': ['TRA', 'ALL', 'FONCAGE 60', 'FON', 'FOR', 'ENC']}, 
-              'TR_LONG': {'validator': is_float}, 
-              'TR_LARG': {'validator': is_float}, 
-              'TR_REVET': {'type': 'string', 'empty': False, 'allowed': ['SABLE', 'BICOUCHE', 'ENROBE', 'BETON', 'PAVE', 'TERRAIN NATUREL']}, 
-              'TR_CHARGE': {'validator': is_float}, 
-              'TR_GRILLAG': {'validator': is_float}, 
+              'TR_LONG': {'empty': False, 'validator': is_float}, 
+              'TR_LARG': {'empty': False, 'validator': is_float}, 
+              'TR_REVET': {'empty':True, 'type': 'string', 'allowed': ['SABLE', 'BICOUCHE', 'ENROBE', 'BETON', 'PAVE', 'TERRAIN NATUREL']}, 
+              'TR_CHARGE': {'empty': False, 'validator': is_float}, 
+              'TR_GRILLAG': {'empty':True, 'validator': is_float}, 
               'TR_REMBLAI': {'type': 'string'}, 
               'TR_PLYNOX': {'type': 'string', 'empty': False, 'allowed': ['OUI', 'NON']}, 
               'TR_PRO_VOI': {'type': 'string', 'empty': False, 'allowed': ['COMMUNE', 'COMMUNAUTE DE COMMUNES', 'DEPARTEMENT', 'ETAT', 'PRIVE']}, 
               'TR_GEST_VO': {'type': 'string', 'empty': False, 'allowed': ['COMMUNE', 'COMMUNAUTE DE COMMUNES', 'DEPARTEMENT', 'ETAT', 'PRIVE']}, 
               'TR_SCHEMA': {'maxlength': 100, 'type': 'string'}, 
-              'TR_DATE_IN': {'empty': False, 'validator': is_french_date}, 
+              'TR_DATE_IN': {'empty': False, 'validator': is_modern_french_date}, 
               'TR_SRC_GEO': {'type': 'string', 'maxlength': 50}, 
               'TR_QLT_GEO': {'type': 'string', 'empty': False, 'allowed': ['A', 'B', 'C']}, 
               'TR_PRO_MD': {'type': 'string', 'maxlength': 20}, 
               'TR_COMMENT': {'type': 'string', 'maxlength': 300, 'empty': True}, 
-              'TR_STATUT': {'type': 'string', 'empty': False, 'allowed': ['EN ETUDE', 'EN REALISATION', 'EN SERVICE', 'HORS SERVICE']}}
+              'TR_STATUT': {'type': 'string', 'empty': False, 'allowed': STATUTS}}
 
+    def __repr__(self):
+        return "Tranchee {}".format(self.TR_VOIE)
+    
 class Zapbo(BaseGeoModel):
     filename = "zapbo_geo.shp"
-    schema = {'geom': {'geometry': (gis.POLYGON, (XMIN, YMIN, XMAX, YMAX))}, 
-              'ID_ZAPBO': {'type': 'string', 'maxlength': 30}, 
+    geom_type = gis_.GEOM_POLYGON
+    bounding_box = (XMIN,YMIN,XMAX,YMAX)
+    schema = {'ID_ZAPBO': {'type': 'string', 'maxlength': 30, 'contains_any_of': ['PBO', 'BPE']}, 
               'COMMENTAIR': {'type': 'string', 'maxlength': 254, 'empty': True}, 
-              'STATUT': {'type': 'string', 'empty': False, 'allowed': ['EN ETUDE', 'EN REALISATION', 'EN SERVICE', 'HORS SERVICE']}}
+              'STATUT': {'type': 'string', 'empty': False, 'allowed': STATUTS}}
+    
+    def __repr__(self):
+        return "Zapbo {}".format(self.ID_ZAPBO)

+ 298 - 31
schemas/netgeo_2_2_doe/validator.py

@@ -2,7 +2,15 @@
 
 @author: olivier.massot, 2018
 '''
-from core.validation import NetgeoValidator, DuplicatedPk, RelationError
+from osgeo import ogr
+import pypyodbc
+
+from core import mn
+from core.gis_ import Feature
+from core.validation import NetgeoValidator, RelationError, UniqueError, \
+    InvalidGeometry, DuplicatedGeom, MissingItem, DimensionError, \
+    TechnicalValidationError, PositionError
+from schemas.common import TOLERANCE
 from schemas.netgeo_2_2_doe.models import Artere, Cable, Equipement, Noeud, \
     Tranchee, Zapbo
 
@@ -13,61 +21,320 @@ class Netgeo22DoeValidator(NetgeoValidator):
     
     def _technical_validation(self):
         
-        # construction de l'index
-        self.index = {}
-        for model in self.dataset:
-            self.index[model] = {}
-            
-            for item in self.dataset[model]:
-                pk_value = getattr(item, model.pk)
-                if pk_value in self.index[model]:
-                    self.log_error(DuplicatedPk("Doublons dans le champs", filename=model.filename, field=model.pk))
-                else:
-                    self.index[model][pk_value] = item
-            
-        del self.dataset
-             
-        if self.errors: 
-            return
-            
+        # construction des index
+        arteres = self.dataset[Artere]
+        cables = self.dataset[Cable]
+        tranchees = self.dataset[Tranchee]
+        
+        noeuds = {}
+        for noeud in self.dataset[Noeud]:
+            if not noeud.NO_NOM in noeuds:
+                noeuds[noeud.NO_NOM] = noeud
+            else:
+                self.log_error(UniqueError("Doublons dans le champs: {}".format(noeud), filename=Noeud.filename, field="NO_NOM"))
+        
+        equipements = {}
+        for equipement in self.dataset[Equipement]:
+            if not equipement.EQ_NOM in equipements:
+                equipements[equipement.EQ_NOM] = equipement
+            else:
+                self.log_error(UniqueError("Doublons dans le champs: {}".format(equipement), filename=Equipement.filename, field="EQ_NOM"))
+                
+        zapbos = {}
+        for zapbo in self.dataset[Zapbo]:
+            if not zapbo.ID_ZAPBO in zapbos:
+                zapbos[zapbo.ID_ZAPBO] = zapbo
+            else:
+                self.log_error(UniqueError("Doublons dans le champs: {}".format(zapbo), filename=Zapbo.filename, field="ID_ZAPBO"))
+        
+        # contrôle de la validité des géométries
+        for artere in arteres:
+            if not artere.valid:
+                self.log_error(InvalidGeometry("Géométrie invalide: {}".format(artere), filename=Artere.filename, field="geom"))
+        for tranchee in tranchees:
+            if not tranchee.valid:
+                self.log_error(InvalidGeometry("Géométrie invalide: {}".format(tranchee), filename=Tranchee.filename, field="geom"))
+        for cable in cables:
+            if not "baguette" in cable.CA_COMMENT.lower() and not cable.valid:
+                self.log_error(InvalidGeometry("Géométrie invalide: {}".format(cable), filename=Cable.filename, field="geom"))
+        for noeud in noeuds.values():
+            if not noeud.valid:
+                self.log_error(InvalidGeometry("Géométrie invalide: {}".format(noeud), filename=Noeud.filename, field="geom"))
+        for equipement in equipements.values():
+            if not equipement.valid:
+                self.log_error(InvalidGeometry("Géométrie invalide: {}".format(equipement), filename=Equipement.filename, field="geom"))
+        for zapbo in zapbos.values():
+            if not zapbo.valid:
+                self.log_error(InvalidGeometry("Géométrie invalide: {}".format(zapbo), filename=Zapbo.filename, field="geom"))
+        
         # rattachement les noeuds aux artères     
-        for artere in self.index[Artere]:
+        for artere in arteres:
             try:
-                artere.noeud_a = self.index[Artere][artere.AR_NOEUD_A]
+                artere.noeud_a = noeuds[artere.AR_NOEUD_A]
             except KeyError:
+                artere.noeud_a = None
                 self.log_error(RelationError("Le noeud '{}' n'existe pas".format(artere.AR_NOEUD_A), filename=Artere.filename, field="AR_NOEUD_A"))
                 
             try:
-                artere.noeud_b = self.index[Artere][artere.AR_NOEUD_B]
+                artere.noeud_b = noeuds[artere.AR_NOEUD_B]
             except KeyError:
+                artere.noeud_b = None
                 self.log_error(RelationError("Le noeud '{}' n'existe pas".format(artere.AR_NOEUD_B), filename=Artere.filename, field="AR_NOEUD_A"))
         
         # rattachement des equipements aux cables
-        for cable in self.index[Cable]:
+        for cable in cables:
             try:
-                cable.equipement_a = self.index[Cable][cable.CA_EQ_A]
+                cable.equipement_a = equipements[cable.CA_EQ_A]
             except KeyError:
-                self.log_error(RelationError("L'équipement '{}' n'existe pas".format(cable.AR_NOEUD_A), filename=Cable.filename, field="CA_EQ_A"))
+                cable.equipement_a = None
+                self.log_error(RelationError("L'équipement '{}' n'existe pas".format(cable.CA_EQ_A), filename=Cable.filename, field="CA_EQ_A"))
                 
             try:
-                cable.equipement_b = self.index[Cable][cable.CA_EQ_B]
+                cable.equipement_b = equipements[cable.CA_EQ_B]
             except KeyError:
-                self.log_error(RelationError("L'équipement '{}' n'existe pas".format(cable.AR_NOEUD_B), filename=Cable.filename, field="CA_EQ_B"))
+                cable.equipement_b = None
+                self.log_error(RelationError("L'équipement '{}' n'existe pas".format(cable.CA_EQ_B), filename=Cable.filename, field="CA_EQ_B"))
+
+        # rattachement des equipements aux noeuds
+        for equipement in equipements.values():
+            try:
+                equipement.noeud = noeuds[equipement.EQ_NOM_NOE]
+            except KeyError:
+                equipement.noeud = None
+                self.log_error(RelationError("Le noeud '{}' n'existe pas".format(equipement.EQ_NOM_NOE, equipement.EQ_NOM), filename=Equipement.filename, field="EQ_NOM_NOE"))
+
+        # verifie que tous les equipements sont l'equipement B d'au moins un cable
+        equipements_b = [cable.CA_EQ_B for cable in cables]
+        for eq_id in equipements:
+            if equipements[eq_id].EQ_TYPE == "BAI":
+                continue
+            if not eq_id in equipements_b:
+                self.log_error(RelationError("L'equipement '{}' n'est l'équipement B d'aucun cable".format(eq_id), filename=Equipement.filename, field="EQ_NOM"))
+
+        # controle des doublons graphiques
+        for i, tranchee in enumerate(tranchees):
+            for other in tranchees[i+1:]:
+                if tranchee.geom == other.geom:
+                    self.log_error(DuplicatedGeom("Une entité graphique est dupliquée".format(tranchee), filename=Tranchee.filename, field="geom"))
+                    
+        for i, artere in enumerate(arteres):
+            for other in arteres[i+1:]:
+                if artere.geom == other.geom:
+                    self.log_error(DuplicatedGeom("Une entité graphique est dupliquée ('{}')".format(artere), filename=Artere.filename, field="geom"))
+
+        for i, cable in enumerate(cables):
+            for other in cables[i+1:]:
+                if cable.geom == other.geom and cable.CA_EQ_A == other.CA_EQ_A and cable.CA_EQ_B == other.CA_EQ_B:
+                    self.log_error(DuplicatedGeom("Une entité graphique est dupliquée ('{}')".format(cable), filename=Cable.filename, field="geom"))
+        
+        ls_noeuds = list(noeuds.values())
+        for i, noeud in enumerate(ls_noeuds):
+            for other in ls_noeuds[i+1:]:
+                if noeud.geom == other.geom:
+                    self.log_error(DuplicatedGeom("Une entité graphique est dupliquée ('{}')".format(noeud), filename=Noeud.filename, field="geom"))
+        del ls_noeuds
+        
+        ls_zapbos = list(zapbos.values())
+        for i, zapbo in enumerate(ls_zapbos):
+            for other in ls_zapbos[i+1:]:
+                if zapbo.geom == other.geom:
+                    self.log_error(DuplicatedGeom("Une entité graphique est dupliquée ('{}')".format(zapbo), filename=Zapbo.filename, field="geom"))
+        del ls_zapbos
+           
+        # Arteres: comparer la géométrie à celle des noeuds
+        for artere in arteres:
+            if not artere.noeud_a or not artere.noeud_b:
+                continue
+            
+            buffer_a, buffer_b = artere.points[0].Buffer(TOLERANCE), artere.points[-1].Buffer(TOLERANCE)
+            
+            if not (buffer_a.Contains(artere.noeud_a.points[0]) and buffer_b.Contains(artere.noeud_b.points[0])) \
+               and not (buffer_a.Contains(artere.noeud_b.points[0]) and buffer_b.Contains(artere.noeud_a.points[0])):
+
+                self.log_error(MissingItem("Pas de noeud aux coordonnées attendues ('{}')".format(artere), filename=Artere.filename, field="geom"))
+        
+        
+        # Cables: comparer la géométrie à celle des equipements (on utilise en fait la geom du noeud correspondant à l'équipement)
+        for cable in cables:
+            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:
+                continue
+            
+            buffer_a, buffer_b = cable.points[0].Buffer(TOLERANCE), cable.points[-1].Buffer(TOLERANCE)
+            
+            if not (buffer_a.Contains(cable.equipement_a.noeud.points[0]) and buffer_b.Contains(cable.equipement_b.noeud.points[0])) \
+               and not (buffer_a.Contains(cable.equipement_b.noeud.points[0]) and buffer_b.Contains(cable.equipement_a.noeud.points[0])):
+            
+                self.log_error(MissingItem("Pas d'equipement aux coordonnées attendues ('{}')".format(cable), filename=Cable.filename, field="geom"))
+            
+        del buffer_a, buffer_b 
+        
+        # Verifie que chaque tranchée a au moins une artère
+        arteres_emprise = Feature.buffered_union(arteres, TOLERANCE)
+        
+        for tranchee in tranchees:
+            if not arteres_emprise.Contains(tranchee.geom):
+                self.log_error(MissingItem("Tranchée sans artère ('{}')".format(tranchee), filename=Tranchee.filename, field="-"))
+        
+        
+        # Verifie que chaque cable a au moins une artère (sauf si commentaire contient 'baguette')
+        for cable in cables:
+            if "baguette" in cable.CA_COMMENT.lower() or not cable.valid:
+                continue
+            if not arteres_emprise.Contains(cable.geom):
+                self.log_error(MissingItem("Cable sans artère ('{}')".format(cable), filename=Cable.filename, field="-"))
+        
+        del arteres_emprise
+        
+        # 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'')
+        cables_emprise = Feature.buffered_union(cables, TOLERANCE)
+        
+        for artere in arteres:
+            if any(x in artere.AR_COMMENT.lower() for x in ['racco','client','adductio','attente','bus','sans cable']):
+                continue
+            if not cables_emprise.Contains(artere.geom):
+                self.log_error(MissingItem("Artère sans cable ('{}')".format(artere), filename=Artere.filename, field="-"))
+                
+        del cables_emprise
                 
-        if self.errors: 
-            return
+        # Contrôle des dimensions logiques
+        for artere in arteres:
+            try:
+                if not int(artere.AR_FOU_DIS) <= int(artere.AR_NB_FOUR):
+                    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"))
+            except (TypeError, ValueError):
+                pass
+        
+        for cable in cables:
+            try:
+                if not int(cable.CA_NB_FO_U) <= int(cable.CA_NB_FO):
+                    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"))
+                if not int(cable.CA_NB_FO_D) <= int(cable.CA_NB_FO):
+                    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"))
+            except (TypeError, ValueError):
+                pass
+        
+        ant_db = mn.ANTDb_0()
+        ant_db.execute("alter session set NLS_NUMERIC_CHARACTERS = '.,';") # definit le separateur decimal sur '.'
+        
+        # Toutes les zapbo contiennent au moins une prise
+        for zapbo in zapbos.values():
+            
+            if len(zapbo.points) >= 499:
+                # passe l'erreur provoquée par la limite au nombre d'arguments en SQL
+                zapbo.nb_prises = None
+                continue
+            
+            sql = """Select SUM(NB_PRISE) AS NB_PRISES FROM SIG_ANT.FTTH_MN_PRISE_LOT z 
+                       WHERE SDO_INSIDE(z.GEOMETRY,
+                                      SDO_GEOMETRY(2003, 3949, SDO_POINT_TYPE(null,null,null), SDO_ELEM_INFO_ARRAY(1,1003,1),  SDO_ORDINATE_ARRAY({}))
+                                         )='TRUE';""".format(", ".join(["{},{}".format(p.GetX(), p.GetY()) for p in zapbo.points]))
+            
+            zapbo.nb_prises = int(ant_db.first(sql).NB_PRISES)
+            if not zapbo.nb_prises:
+                self.log_error(MissingItem("La Zapbo ne contient aucune prise: {}".format(zapbo), filename=Zapbo.filename, field="-"))
+        
+        # Toutes les prises de la ou les ZAPM impactées sont dans une zapbo
+        zapms = {}
+        # > on déduit la liste des zapms à partir de la position des zapbos
+        for zapbo in zapbos.values():
+            centre = zapbo.geom.Centroid()
+            zapm = ant_db.first("""SELECT z.ID_ZAPM 
+                                  FROM SIG_ANT.FTTH_MN_ZAPM z
+                                  WHERE sdo_contains(z.GEOMETRY, 
+                                                     SDO_GEOMETRY(2001, 3949, SDO_POINT_TYPE({}, {}, NULL), NULL, NULL)) = 'TRUE'
+                               """.format(centre.GetX(), centre.GetY()))
+            try:
+                zapms[zapm.ID_ZAPM].append(zapbo)
+            except KeyError:
+                zapms[zapm.ID_ZAPM] = [zapbo]
+        
+        for id_zapm in zapms:
+            zapm_couverture = Feature.union(zapms[id_zapm])
+            for prise in ant_db.read("""SELECT t.X AS x, t.Y AS y
+                                        FROM SIG_ANT.FTTH_MN_PRISE_LOT z, 
+                                        TABLE(SDO_UTIL.GETVERTICES(z.GEOMETRY)) t 
+                                        WHERE T_ETAT<>'OBSOLETE' AND ID_ZAPM_PARTIELLE='{}';""".format(id_zapm)):
+                point = ogr.Geometry(ogr.wkbPoint)
+                point.AddPoint(prise.x, prise.y)
+                if not zapm_couverture.Contains(point):
+                    self.log_error(MissingItem("Certaines prises de la ZAPM ne sont pas comprises dans une ZAPBO: {}".format(id_zapm), filename=Zapbo.filename, field="-"))
+        
+        # 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
+        
+        for equipement in equipements.values():
+            if not equipement.EQ_TYPE == "PBO":
+                continue
+            
+            #zapbos englobant l'equipement
+            candidates = []
+            for zapbo in zapbos.values():
+                if zapbo.geom.Contains(equipement.geom):
+                    candidates.append(zapbo)
+                    
+            # le pbo doit être contenu dans une zapbo
+            if not candidates:
+                self.log_error(MissingItem("Le PBO n'est contenu dans aucune ZAPBO: {}".format(equipement), filename=Equipement.filename, field="geom"))
+                continue
+            
+            # On se base sur le nom pour trouver la zapbo correspondante
+            try:
+                equipement.zapbo = next((z for z in candidates if equipement.EQ_NOM in z.ID_ZAPBO))
+            except StopIteration:
+                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"))
+                break
+            
+            # Controle du dimensionnement des PBO
+            if equipement.zapbo.nb_prises is not None:
+                if equipement.EQ_TYPE_PH == 'PBO 6' and not equipement.zapbo.nb_prises < 6:
+                    self.log_error(DimensionError("Le PBO 6 contient plus de 5 prises: {}".format(equipement), filename=Equipement.filename, field="-"))
+            
+                if equipement.EQ_TYPE_PH == 'PBO 12' and not equipement.zapbo.nb_prises >= 6 and equipement.zapbo.nb_prises <= 8:
+                    self.log_error(DimensionError("Le PBO 12 contient mois de 6 prises ou plus de 8 prises: {}".format(equipement), filename=Equipement.filename, field="-"))
+        
+            if equipement.zapbo.STATUT == "REC" and not equipement.EQ_STATUT == "REC":
+                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="-"))
+        
+            if equipement.EQ_STATUT == "REC" and not equipement.zapbo.STATUT == "REC" and not equipement.zapbo.ID_ZAPBO[:4].lower() == "att_":
+                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="-"))
         
         # Contrôler dans la base si des éléments portant ces codes existent à des emplacements différents
+        for noeud in noeuds.values():
+            sql = """SELECT z.NO_NOM, SDO_GEOM.SDO_DISTANCE(z.GEOMETRY, SDO_GEOMETRY(2001, 3949, SDO_POINT_TYPE({}, {}, NULL), NULL, NULL),0.005) AS DIST 
+                     FROM SIG_ANT.FTTH_MN_GR_NOEUD_GEO z 
+                     WHERE z.NO_NOM='{}';""".format(noeud.geom.GetX(), noeud.geom.GetY(), noeud.NO_NOM)
+            existing = ant_db.first(sql)
+            if existing:
+                if existing.DIST > TOLERANCE and existing.DIST < 20:
+                    self.log_error(PositionError("La position du noeud ne correspond pas à l'existant: {}".format(noeud), filename=Noeud.filename, field="geom"))
+                elif existing.DIST > 20:
+                    self.log_error(DuplicatedGeom("Un noeud portant ce nom existe déjà ailleurs sur le territoire: {}".format(noeud), filename=Noeud.filename, field="NO_NOM"))
         
-        # Contrôler l'emprise des ZAPBO
+        for zapbo in zapbos.values():
+            sql = """SELECT z.ID_ZAPBO, SDO_GEOM.SDO_DISTANCE(SDO_GEOM.SDO_CENTROID(z.GEOMETRY,0.005), SDO_GEOMETRY(2001, 3949, SDO_POINT_TYPE({}, {}, NULL), NULL, NULL),0.005) AS DIST 
+                     FROM SIG_ANT.FTTH_MN_GR_ZAPBO_GEO z 
+                     WHERE z.ID_ZAPBO='{}';""".format(zapbo.geom.Centroid().GetX(), zapbo.geom.Centroid().GetY(), zapbo.ID_ZAPBO)
+            existing = ant_db.first(sql)
+            if existing:
+                if existing.DIST > TOLERANCE and existing.DIST < 20:
+                    self.log_error(PositionError("La position de la ZAPBO ne correspond pas à l'existant: {}".format(zapbo), filename=Zapbo.filename, field="geom"))
+                elif existing.DIST > 20:
+                    self.log_error(DuplicatedGeom("Une ZAPBO portant ce nom existe déjà ailleurs sur le territoire: {}".format(zapbo), filename=Zapbo.filename, field="ID_ZAPBO"))
+
+        # Contrôle si un equipement portant ce nom existe, mais associé à un noeud différent
+        for equipement in equipements.values():
+            sql = """SELECT z.EQ_NOM, z.EQ_NOM_NOEUD
+                     FROM SIG_ANT.FTTH_MN_GR_EQ_PASSIF z 
+                     WHERE z.EQ_NOM='{}';""".format(equipement.EQ_NOM)
+            existing = ant_db.first(sql)
+            if existing and existing.EQ_NOM_NOEUD != equipement.EQ_NOM_NOE:
+                self.log_error(DuplicatedGeom("Un équipement portant ce nom ({}) existe déjà et est associé à un noeud différent ({})".format(equipement.NO_NOM, existing.EQ_NOM_NOEUD), filename=Noeud.filename, field="geom"))
         
         
         
     
 if __name__ == "__main__":
     from core.constants import MAIN
-    subject = MAIN / "work" / "AXIANS_082AP0_REC_180920.zip"
-#     subject = MAIN / "work" / "STURNO_192AP1_REC_171211_OK"
+    subject = MAIN / "work" / "STURNO_228CP0_APD_180301_OK"
     report = Netgeo22DoeValidator.submit(subject)
     print(report)
     

+ 28 - 0
templates/readme.md

@@ -0,0 +1,28 @@
+## Tests effectués
+
+* **Chargement des données**
+
+  * Présence des fichiers attendus: "artere_geo.shp", "cable_geo.shp", "equipement_passif.shp", "noeud_geo.shp", "tranchee_geo.shp","zapbo_geo.shp"
+  * Fichiers au format SHP
+  * Types de géométrie
+
+* **Structure**
+
+  * Les coordonnées des entités sont dans la zone d'emprise du département de la Manche
+  * Les champs attendus sont présents
+  * Les champs obligatoires sont renseignés
+  * Type des données (entiers, décimaux, dates...)
+  * Les champs ne dépassent pas la longueur autorisée
+  * Contrôle des valeurs autorisées
+  
+* **Validation Métier**
+
+  * Contrôle des doubloins dans les codes des noeuds, equipements, zapbo.
+  * Contrôle des cardinalités: correspondance artère/noeuds, cables/equipements, equipement/noeud...etc.
+  * Contrôle des doublons graphiques
+  * Cohérence de la géographie (ex: les noeuds sont situés aux extrémités des artères)
+  * Contrôle de la logique des données (ex: le nombre des fourreaux utilisés n'est pas supérieur au nombre total)
+  * Comparaison avec la base de données (ex: un noeud portant le même code n'existe pas déjà ailleurs sur le territoire)
+  
+
+  

+ 6 - 4
templates/report.html

@@ -38,15 +38,17 @@
 		</table>
 		
 		<h3>Check-Points</h3>
-		<ul>
+		<table style="border: none;">
 	    {% for chk in report['checkpoints'] %}
+	    	<tr>
 	    	{%- if chk['valid'] -%}
-	    		<li><i class="fa fa-check valid"></i> <span>{{ chk['name'] }}</span></li>
+	    		<th><i class="fa fa-check valid"></i></th> <td>{{ chk['name'] }}</td>
 	    	{% else %}
-	    		<li><i class="fa fa-times error"></i> <span>{{ chk['name'] }}</span></li>
+	    		<th><i class="fa fa-times error"></i></th> <td>{{ chk['name'] }}</td>
 	    	{% endif %}
+	    	</tr>
 	    {% endfor %}
-	    </ul>
+	    </table>
     
         
     	{%- if report['errors'] -%}