Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mise à jour des scripts GéoGAMA-Crue (support g1.3) #10

Merged
merged 2 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Journal des modifications
=========================

## [] - En cours
- Corrections scripts geogama


## [4.2] - 2024-09-27
### Nouveautés
- Prise en compte du déplacement de `Pm_TolStQ` du CCM vers le OPTI pour les initialisations de type Saint-Venant (g1.3)
Expand Down
171 changes: 86 additions & 85 deletions geogama/crue_vers_geogama.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
# coding: utf-8
"""
Lecture d'un sous-modèle Crue10
Lecture de plusieurs sous-modèles Crue10
"""
import os.path
import sys
import arcpy
import logging
import math
import re
import sys
import traceback

from crue10.emh.branche import BrancheAvecElementsSeuil
from crue10.utils import ExceptionCrue10, logger
from crue10.emh.section import LitNumerote
from crue10.emh.section import SectionIdem, SectionProfil, SectionSansGeometrie
from crue10.etude import Etude
from crue10.emh.section import LimiteGeom, SectionIdem, SectionProfil, SectionSansGeometrie
from crue10.utils import check_isinstance, check_preffix, ExceptionCrue10, logger
from crue10.utils import ExceptionCrue10, logger

import arcpy

#Parametres
GDB = arcpy.GetParameterAsText(0)
EDU_PATH = arcpy.GetParameterAsText(1)
LST_SOUSMODELE = arcpy.GetParameterAsText(2).split(";")

#Debug
# GDB = r"D:\Mandats\9011_FF - CNR - GeoGAMA-Crue Maintenance\Production\2-Realisation\Data\_UserFolder\bbb\BDD.gdb"
# EDU_PATH = r"D:\Mandats\9011_FF - CNR - GeoGAMA-Crue Maintenance\Production\2-Realisation\ImportExportCrue10\Etu_from_scratch_2-Sm\Etu_from_scratch.etu.xml"
# LST_SOUSMODELE = ['Sm_amont', 'Sm_aval']
# Parameters
if arcpy.GetArgumentCount() == 3:
GDB = arcpy.GetParameterAsText(0)
EDU_PATH = arcpy.GetParameterAsText(1)
LST_SOUSMODELE = arcpy.GetParameterAsText(2).split(";")
else:
# Debug
GDB = r"C:\temp\BDD.gdb"
EDU_PATH = r"Etu_from_scratch_2-Sm\Etu_from_scratch.etu.xml"
LST_SOUSMODELE = ['Sm_amont', 'Sm_aval']


#Constantes
# Constants
FCLASS_NOEUD = "EMG/Noeud"
FCLASS_CASIER = "EMG/Casier"
FCLASS_BRANCHE = "EMG/Branche"
Expand All @@ -38,10 +39,10 @@
TABLE_CASIER_LOI = "Casier_Loi"
FCLASS_SEUIL_ELEM = "Seuil_Elem"


arcpy.env.workspace = GDB

#Init Logger

# Init Logger
formatter = logging.Formatter('%(levelname)s: %(message)s')

logger.setLevel(logging.DEBUG)
Expand All @@ -57,6 +58,8 @@
# logger.addHandler(handler2)


# Functions

def truncateAllClass():
logger.info("Vidage des tables")

Expand All @@ -70,9 +73,11 @@ def truncateAllClass():
arcpy.DeleteRows_management(GDB+"/"+TABLE_CASIER_LOI)
arcpy.DeleteRows_management(GDB+"/"+FCLASS_SEUIL_ELEM)


def getDist(ptA, ptB):
return math.sqrt((ptA.X - ptB.X)**2 + (ptA.Y - ptB.Y)**2)


def getMPolyline(coords, distanceTot):
points = arcpy.Array()
currentDist = 0
Expand All @@ -84,18 +89,18 @@ def getMPolyline(coords, distanceTot):
if not lastPoint is None:
dist = getDist(point, lastPoint)
currentDist += dist

point.M = currentDist
points.append(point)

lastPoint = arcpy.Point(x, y)

if distanceTot is not None:
ratio = distanceTot / currentDist

for point in points:
point.M = point.M * ratio

return arcpy.Polyline(points)


Expand All @@ -110,51 +115,48 @@ def getTroncon(emh_name):
return ''
return m.group('troncon')[:10]


try:
# if True:

try:
logger.info("Parametres")
logger.info(GDB)
logger.info(EDU_PATH)
logger.info(LST_SOUSMODELE)


#Ouverture session d'édition
# Ouverture session d'édition
edit = arcpy.da.Editor(arcpy.env.workspace)
edit.startEditing(False, False)

# edit.startOperation()

#Vidage des tables
# Vidage des tables
# truncateAllClass()

# edit.stopOperation()
#Liste de snoeuds,pour éviter qu'ils soient insérés en double sur plusieurs sous-modèles

# Liste des noeuds, pour éviter qu'ils soient insérés en double sur plusieurs sous-modèles
hashNoeuds = {}

# Get Etude
study = Etude(EDU_PATH)

for sm_elem in LST_SOUSMODELE:
logger.info("Lecture Sous-modele- " + sm_elem)
# Get Sous-modele
sous_modele = study.get_sous_modele(sm_elem)
sous_modele.read_all()
#Suppression sections interpolées

# Suppression sections interpolées
sous_modele.remove_sectioninterpolee()
#Modifie le xp aval des sections sansgeometrie a partir des branches

# Modifie le xp aval des sections sansgeometrie a partir des branches
sous_modele.replace_zero_xp_sectionaval()

edit.startOperation()

logger.info("Noeuds - " + str(len(sous_modele.noeuds)))
with arcpy.da.InsertCursor(FCLASS_NOEUD, ["Troncon", "Nom_noeud", "SHAPE@"]) as cursor:
for nomNoeud in sous_modele.noeuds:
#Vérifie que le noeud n'a pas déjà été importé
# Vérifie que le noeud n'a pas déjà été importé
if nomNoeud not in hashNoeuds:
hashNoeuds[nomNoeud] = True
noeud = sous_modele.noeuds[nomNoeud]
Expand All @@ -165,7 +167,7 @@ def getTroncon(emh_name):

edit.stopOperation()
edit.startOperation()

logger.info("Casiers - " + str(len(sous_modele.casiers)))
with arcpy.da.InsertCursor(FCLASS_CASIER, ["Nom_casier", "Nom_noeud", "Distance_appli", "SHAPE@"]) as cursor:
for nomCasier in sous_modele.casiers:
Expand All @@ -176,18 +178,18 @@ def getTroncon(emh_name):

edit.stopOperation()
edit.startOperation()

logger.info("Branches - " + str(len(sous_modele.branches)))
with arcpy.da.InsertCursor(FCLASS_BRANCHE, ["Troncon", "Nom_branche", "Type_branche", "Longueur", "Nom_noeud_amont", "Nom_noeud_aval", "IsActif", "SHAPE@"]) as cursor:
for nomBranche in sous_modele.branches:
logger.debug("Branche - " + nomBranche)
branche = sous_modele.branches[nomBranche]
polyline = getMPolyline(branche.geom.coords, branche.length)
cursor.insertRow([getTroncon(nomBranche), nomBranche, branche.type, branche.length, branche.noeud_amont.id, branche.noeud_aval.id, branche.is_active, polyline])

edit.stopOperation()
edit.startOperation()

logger.info("Sections - " + str(len(sous_modele.sections)))
listSectionProfil = []
with arcpy.da.InsertCursor(TABLE_SECTION, ["Troncon", "Nom_section", "Type_section", "Nom_branche", "Xp", "Nom_section_parent", "Delta_Z"]) as cursor:
Expand All @@ -200,21 +202,21 @@ def getTroncon(emh_name):
dz = None
if isinstance(section, SectionSansGeometrie):
typeSection = 1
#TODO : Appeler méthode du coeur pour calculer le xp aval = branche.length
# TODO : Appeler méthode du coeur pour calculer le xp aval = branche.length
elif isinstance(section, SectionProfil):
typeSection = 2
#Merge lis numerotes
# Merge lits numérotés
section.merge_consecutive_lit_numerotes()
listSectionProfil.append(section)
elif isinstance(section, SectionIdem):
typeSection = 3
nom_section_parent = section.section_reference.id
dz = section.dz_section_reference
cursor.insertRow([getTroncon(nomBranche), nomSection, typeSection, nomBranche, section.xp ,nom_section_parent, dz])

edit.stopOperation()
edit.startOperation()

logger.info("ProfilPoints (par profil) - " + str(len(listSectionProfil)))
list_limite_point = []
with arcpy.da.InsertCursor(FCLASS_PROFIL_POINT, ["Nom_section", "Absc_proj", "Z", "SHAPE@"]) as cursor:
Expand All @@ -223,35 +225,33 @@ def getTroncon(emh_name):
logger.debug("Profil - " + profil.id)
# logger.debug("XY - " + str(len(profil.geom_trace.coords)))
# logger.debug("XZ - " + str(len(profil.xz)))

line = arcpy.Polyline(arcpy.Array([arcpy.Point(*coords) for coords in profil.geom_trace.coords[:]]))
i = 0
premier_point_x = 0
hashOIDPointParAbs = {}
for p in profil.xz:
absc_proj = p[0] # x
z = p[1] # z
absc_proj = p[0] # x
z = p[1] # z
if i == 0:
point = line.positionAlongLine(p[0])
premier_point_x = p[0]
else:
point = line.positionAlongLine(p[0] - premier_point_x)

id = cursor.insertRow([profil.id, absc_proj, z, point])
hashOIDPointParAbs[absc_proj] = id
i+=1
# print id
for limit_geom in profil.limites_geom:
oidPoint = hashOIDPointParAbs[limit_geom.xt]
if limit_geom.id == "Et_AxeHyd":
hashOIDPointParAbs[absc_proj] = id
i += 1
for _, limite in profil.limites_geom.items():
oidPoint = hashOIDPointParAbs[limite.xt]
if limite.id == "Et_AxeHyd":
id_limite = 14
elif limit_geom.id == "Et_Thalweg":
elif limite.id == "Et_Thalweg":
id_limite = 13
else:
continue
list_limite_point.append([oidPoint, id_limite])
# print oidPoint

list_limite_point.append([oidPoint, id_limite])

for j in range(0, 6):
if j == 5: # on prend le max
xt = profil.lits_numerotes[j - 1].xt_max
Expand All @@ -262,77 +262,78 @@ def getTroncon(emh_name):
list_limite_point.append([oidPoint, id_limite])
else:
logger.warning("Profil sans geom - " + profil.id)

del cursor

edit.stopOperation()
edit.startOperation()

logger.info("Limites - " + str(len(list_limite_point)))
with arcpy.da.InsertCursor(TABLE_LIMITE, ["OID_Profil_Point", "Nom_Limite"]) as cursor_limite:
for limite in list_limite_point:
cursor_limite.insertRow(limite)
del cursor_limite
del cursor_limite

edit.stopOperation()
edit.startOperation()

logger.info("Porfil_Trace - " + str(len(listSectionProfil)))
logger.info("Profil_Trace - " + str(len(listSectionProfil)))
with arcpy.da.InsertCursor(FCLASS_PROFIL_TRACE, ["Nom_section", "SHAPE@"]) as cursor:
for profil in listSectionProfil:
if profil.geom_trace is not None:
logger.debug("Profil - " + profil.id)
line = arcpy.Polyline(arcpy.Array([arcpy.Point(*coords) for coords in profil.geom_trace.coords[:]]))
cursor.insertRow([profil.id, line])
del cursor

edit.stopOperation()
edit.startOperation()

logger.info("Seuil_Elem")
with arcpy.da.InsertCursor(FCLASS_SEUIL_ELEM, ["Nom_branche", "Largeur", "Z_Seuil", "CoefD", "CoefPDC"]) as cursor:
for nomBranche in sous_modele.branches:
branche = sous_modele.branches[nomBranche]
if isinstance(branche, BrancheAvecElementsSeuil):
logger.debug("Seuil - " + nomBranche)
for seuil in branche.liste_elements_seuil: #larg, z_seuil, coeff_d, coeff_pdc
for seuil in branche.liste_elements_seuil: # larg, z_seuil, coeff_d, coeff_pdc
cursor.insertRow([nomBranche, seuil[0], seuil[1], seuil[2], seuil[3]])
del cursor

edit.stopOperation()
edit.startOperation()

logger.info("Casier_Loi")
with arcpy.da.InsertCursor(TABLE_CASIER_LOI, ["Nom_casier", "Xt", "Cote"]) as cursor:
for nomCasier in sous_modele.casiers:
casier = sous_modele.casiers[nomCasier]
if len(casier.profils_casier) > 1 :

if len(casier.profils_casier) > 1:
logger.debug("Casier_Loi - Fusion - " + nomCasier)
casier.fusion_profil_casiers()
if len(casier.profils_casier) > 0 :

if len(casier.profils_casier) > 0:
logger.debug("Casier_Loi - " + nomCasier)
for xz in casier.profils_casier[0].xz:
cursor.insertRow([nomCasier, xz[0], xz[1]])
cursor.insertRow([nomCasier, xz[0], xz[1]])
del cursor

edit.stopOperation()

logger.info("Enregistrement")
edit.stopEditing(True)


except ExceptionCrue10 as e:
# edit.stopOperation()
# edit.stopEditing(False)
logger.critical(e)


except Exception as ex:
# edit.stopOperation()
# edit.stopEditing(False)
logger.critical(ex)
traceback.print_exc()


logger.info("Fin du traitement")

logger.info("## Fin du traitement ##")
Loading
Loading