8 from PyQt5.QtCore import *
9 from PyQt5.QtGui import *
11 from random import randint
13 def getChildrenInStudy(obj):
15 Given an object published in SALOME study (for instance a GEOM object), retreive its children.
17 obj: a SALOME object published in SALOME study
19 a dictionary [name] --> object
21 SO = salome.myStudy.FindObjectIOR(salome.myStudy.ConvertObjectToIOR(obj))
22 childIterator = salome.myStudy.NewChildIterator(SO)
24 while childIterator.More():
25 childItem = childIterator.Value()
26 print("item", childItem)
27 itemName = childItem.GetName()
28 itemID = childItem.GetID()
29 itemObj = salome.IDToObject(str(itemID))
30 children[itemName] = itemObj
34 def loadImage(document, imageFile, imageName, displayLevel):
36 load an image (photo, map...) in order to georeference it and use it as background for drawing shapes
38 document: current HYDROData document
39 imageFile: full path of the image file to load
40 imageName: name to give to the object loaded in the HYDRO document
41 displayLevel: integer >=0, the higher levels are displayed on top
43 the object image loaded
45 image = document.CreateObject(KIND_IMAGE)
46 image.SetName(imageName)
47 image.SetZLevel(displayLevel)
48 if not(image.LoadImage(imageFile)):
49 raise ValueError('problem while loading image %s'%imageFile)
53 def GeolocaliseImageCoords(image, a, b, c ,d):
55 Geolocalize a loaded image with to sets of points: pixels coordinates <--> absolutes coordinates in the geographic reference system.
57 image: loaded image to localize
58 a: first pixel coordinates QPoint(x, y) (integers)
59 b: second pixel coordinates QPoint(x, y) (integers)
60 c: first geographic point QPointF(X, Y) (floats)
61 d: second geographic point QPointF(X, Y) (floats)
63 image.SetLocalPoints(a, b)
64 image.SetGlobalPoints(1, c, d)
67 def GeolocaliseImageReference(image, imageRef, a, b, c ,d):
69 Geolocalize an image using another one already localized, with two pair of corresponding pixels on the images
71 image: loaded image to localize
72 imageRef: loaded image used as a reference
73 a, b : pixel coordinates on the image to localize QPoint(x, y) (integers)
74 c, d : pixel coordinates on the reference image QPoint(x, y) (integers)
76 image.SetLocalPoints(a, b)
77 image.SetGlobalPoints(3, c, d)
78 image.SetTrsfReferenceImage(imageRef)
81 def importPolylines(document, shapeFile, shapeName, iSpline, displayLevel):
83 Import a QGis shape (*.shp) containing one or several 2D polylines or polygons.
84 The polyline is either named from its name in the shape database if existing, or using the shapefile basename
85 with a suffix '_PolyXY'.
86 In case of several polylines, name are suffixed in sequence with '_n'.
88 document: current HYDROData document
89 shapeFile: full path of the shapefile (*.shp)
90 shapeName: used to retrieve the objects in the HYDRO document: see above, name is given without the sequence suffix
91 isSpline: boolean to convert the polylines in splines
92 displayLevel: integer >=0, the higher levels are displayed on top
96 HYDROData_PolylineXY.ImportShapesFromFile(shapeFile)
97 shapeType = 0 # polyline
99 shapeType = 1 # polyline
102 print("importPolylines %s %s"%(shapeName, shapeType))
103 index = 0 # for multiple shapes
105 shapeNameIndex = shapeName + "_%d" % index
107 print("try name %s"%shapeNameIndex)
108 shape = document.FindObjectByName(shapeNameIndex)
112 print("found %s"%shapeNameIndex)
113 for i in range(shape.NbSections()):
114 shape.SetSectionType(i, shapeType)
116 shape.SetZLevel(displayLevel)
118 if index <= 1: # no multiple shapes, try single shape
119 print("try name %s"%shapeName)
120 shape = document.FindObjectByName(shapeName) # single shape
121 if shape is not None:
122 print("found %s"%shapeName)
123 for i in range(shape.NbSections()):
124 shape.SetSectionType(i, shapeType)
126 shape.SetZLevel(displayLevel)
130 def splitShapeTool(document, aShape, toolShape, tolerance=1.e-2):
132 Split the shape (2D polyline or spline) by the shape tool.
133 return a list of all the split shapes, named after their original shape name, with a sequence suffix _n
135 document: current HYDROData document
136 aShape: a loaded shape (2D polyline or spline)
137 toolShape: a loaded shape (2D polyline or spline)
138 tolerance: used by the algorithm to detect intersections, default value 1.e-2
140 a list of all the split shapes, named after their original shape name, with a sequence suffix _n
142 name = aShape.GetName()
147 nameIndex = name + "_%d"%(index)
148 shape = document.FindObjectByName(nameIndex)
153 existingNbSplit[name] = index
154 print(existingNbSplit)
156 op = HYDROData_PolylineOperator()
157 op.SplitTool(document, aShape, toolShape, tolerance)
160 index = existingNbSplit[name]
163 nameIndex = name + "_%d"%(index)
164 shape = document.FindObjectByName(nameIndex)
168 print("found %s"%nameIndex)
170 listSplit.append(shape)
173 def splitShapesAll(document, shapeslist, precision=1.E-2):
175 Split all the shapes (2D polylines or splines) in the list by all the other shapes.
176 return a list of all the split shapes, named after their original shape name, with a sequence suffix _n
178 document: current HYDROData document
179 shapeslist: a list of loaded shapes (2D polylines or splines)
180 precision: used by the algorithm to detect intersections, default value 1.e-2
182 a list of all the split shapes, named after their original shape name, with a sequence suffix _n
184 names = [s.GetName() for s in shapeslist]
191 nameIndex = name + "_%d"%(index)
192 shape = document.FindObjectByName(nameIndex)
197 existingNbSplit[name] = index
198 print(existingNbSplit)
200 op = HYDROData_PolylineOperator()
201 op.SplitAll(document, shapeslist, precision)
205 index = existingNbSplit[name]
208 nameIndex = name + "_%d"%(index)
209 shape = document.FindObjectByName(nameIndex)
213 print("found %s"%nameIndex)
215 listSplit.append(shape)
219 def importBathymetry(document, bathyFile):
221 import a bathymetry file (*.xyz or *.asc)
223 document: current HYDROData document
224 bathyFile: full path of the bathymetry (*.xyz or *.shp)
226 the bathymetry object
228 bathy = document.CreateObject(KIND_BATHYMETRY)
229 a = os.path.splitext(bathyFile)
230 bathyName = os.path.basename(a[0])
231 bathy.SetName(bathyName)
232 bathy.SetAltitudesInverted(0)
233 if not(bathy.ImportFromFile(bathyFile)):
234 raise ValueError('problem while loading bathymetry')
238 def createImmersibleZone(document, imzName, polyLine, bathy, isImmersible, displayLevel):
240 Create an immersible or unsubmersible zone with a closed polyline and a bathymetry.
242 document: current HYDROData document
243 imzName: the name to give to the zone object
244 polyline: the closed 2D polyline or spline delimiting the zone
245 bathy: the bathymetry to associate to the zone
246 isImmersible: boolean
247 displayLevel: integer >=0, the higher levels are displayed on top
251 imz = document.CreateObject(KIND_IMMERSIBLE_ZONE)
253 imz.SetZLevel(displayLevel)
254 if bathy is not None:
255 imz.SetAltitudeObject(bathy)
256 imz.SetPolyline(polyLine)
257 imz.SetIsSubmersible(isImmersible)
258 imz.SetFillingColor(QColor(randint(0,255), randint(0,255), randint(0,255), 255))
262 def mergePolylines(document, polyName, polyLines, isConnectedBySegment = False, tolerance = 1.E-3):
264 Regroup several 2D polylines or spline in one, with several sections
266 document: current HYDROData document
267 polyName: name to give to the new object
268 polyLines: a list of loaded shapes (2D polylines or splines)
269 isConnectedBySegment: boolean, to add a segment connecting the polylines, default False
270 tolerance: used by the algorithm to detect coincidences, default 1.E-3
274 op=HYDROData_PolylineOperator()
275 op.Merge(document, polyName, polyLines, isConnectedBySegment, tolerance)
276 shape = document.FindObjectByName(polyName)
279 def createAxis3DDEmbankmentAltiProfile(document, axis3DName, axisXY, altiPts):
281 Create a 3D polyline to be used as an axis for an embankement, using a 2D axis and a list of altitudes along the axis.
283 document: current HYDROData_Document
284 axis3DName: name to give to the axis3D created
285 axisXY: 2D polyline (spline), Embankment line
286 altiPts: list of tuples(x, h) : parametric coordinate, altitude, along the axisXY
290 altiProfile = document.CreateObject(KIND_PROFILE)
291 altiProfile.SetName("%s_altiProfile"%axis3DName)
292 altiProfilePoints = []
294 altiProfilePoints.append(gp_XY(p[0], p[1]))
295 altiProfile.SetParametricPoints(altiProfilePoints)
297 axis3D = document.CreateObject(KIND_POLYLINE)
298 axis3D.SetName(axis3DName)
299 axis3D.SetPolylineXY(axisXY)
300 axis3D.SetProfileUZ(altiProfile.GetProfileUZ())
302 return (altiProfile, axis3D)
304 def createAxis3DDEmbankmentBathy(document, axis3DName, axisXY, aCloud):
306 Create a 3D polyline to be used as an axis for an embankement, using a 2D axis and a bathymetry to project the axis.
308 document: current HYDROData_Document
309 axis3DName: name to give to the axis3D created
310 axisXY: imported polyline, Embankment line
311 aCloud: altimetry, for projection of axisXY
315 altiProfile = document.CreateObject(KIND_PROFILE)
316 altiProfile.SetName("%s_altiProfile"%axis3DName)
317 axis3D = document.CreateObject(KIND_POLYLINE)
318 axis3D.SetName(axis3DName)
319 axis3D.SetPolylineXY(axisXY)
320 axis3D.SetAltitudeObject(aCloud)
321 axis3D.SetChildProfileUZ(altiProfile.GetProfileUZ())
324 return (altiProfile, axis3D)
326 def createEmbankmentSectionA(document, embankmentName, axis3D, sectionPoints, d, displayLevel):
328 Define the section of an embankement and extrude it along a 3D axis. Section is defined with a list of points (axis distance, altitude).
330 document: current HYDROData_Document
331 embankmentName: name to give to the embankment
332 axis3D: the 3D axis of the embankment
333 sectionPoints: a list of tuple (x, h) to define a half section, beginning to x=0 (center)
334 d calculation parameter, take 2 or 3 times the section width
335 displayLevel : z level for 2D representation: high values are drawn above low values
339 section = document.CreateObject(KIND_PROFILE)
340 section.SetName("%s_section"%embankmentName)
342 for i,p in enumerate(sectionPoints):
343 sectionPts.append(gp_XY(p[0], p[1]))
345 sectionPts.insert(0, gp_XY(-p[0], p[1]))
346 section.SetParametricPoints(sectionPts)
348 embankment = document.CreateObject(KIND_DIGUE)
349 embankment.SetName(embankmentName)
350 embankment.SetGuideLine(axis3D)
351 embankment.SetProfileMode(True)
352 embankment.SetProfile(section)
353 embankment.SetEquiDistance(d)
354 embankment.SetZLevel(displayLevel)
355 embankment.SetFillingColor(QColor(randint(0,255), randint(0,255), randint(0,255), 255))
357 return (section, embankment)
359 def createEmbankmentSectionB(document, embankmentName, axis3D, LC,DZ,CZ, d, displayLevel):
361 Define the section of an embankement and extrude it along a 3D axis. Section is defined with a width, an height, the position of the axis.
363 document: current HYDROData_Document
364 embankmentName: name to give to the embankment
365 axis3D: the 3D axis of the embankment
366 LC, DZ, CZ: width, height, height offset above axis3D
367 d calculation parameter, take 2 or 3 times the section width
368 displayLevel : z level for 2D representation: high values are drawn above low values
372 embankment = document.CreateObject(KIND_DIGUE)
373 embankment.SetName(embankmentName)
374 embankment.SetGuideLine(axis3D)
375 embankment.SetProfileMode(False)
376 embankment.SetLCValue(LC)
377 embankment.SetDeltaZValue(DZ)
378 embankment.SetCoteZValue(CZ)
379 embankment.SetEquiDistance(d)
380 embankment.SetZLevel(displayLevel)
381 embankment.SetFillingColor(QColor(randint(0,255), randint(0,255), randint(0,255), 255))
385 def loadStricklerTable(document, stricklerFile):
387 Load a table of Corine Land Cover types and codes, used for Strickler coefficients (editable text format)
389 document: current HYDROData document
390 stricklerFile: full path of the table (*.txt)
392 table loaded, code corresponding to Corine Land Cover used for the table ('CODE_06', 'CODE_12'...)
395 with open(stricklerFile) as f:
397 code = line.split()[0]
398 a = os.path.splitext(stricklerFile)
399 stricklerName = os.path.basename(a[0])
400 strickler_table = document.CreateObject( KIND_STRICKLER_TABLE )
401 strickler_table.Import(stricklerFile)
402 strickler_table.SetName(stricklerName)
403 strickler_table.SetAttrName(code)
404 strickler_table.Update()
405 return strickler_table, code
407 def loadLandCoverMap(document, landCoverFile, stricklerFile, displayLevel):
409 Load a Corine Land Cover Map
411 document: current HYDROData document
412 landCoverFile: shapeFile defining the map (*.shp)
413 stricklerFile: full path of the table (*.txt)
414 displayLevel : z level for 2D representation: high values are drawn above low values
416 land cover map loaded
422 with open(stricklerFile) as f:
424 items = line.split('"')
426 code = line.split()[0]
428 alltypes.append(items[1])
429 itm2 = items[-1].split()
430 allvalues.append(itm2[-1])
434 while code[0] != 'C': # the code begins with 'C'
435 code = code[1:] # get rid of encoding
438 a = os.path.splitext(landCoverFile)
439 landCoverName = os.path.basename(a[0])
440 landCoverDB = a[0] + '.dbf'
441 landCover = document.CreateObject(KIND_LAND_COVER_MAP)
445 landCover.SetName(landCoverName)
446 landCover.SetZLevel(displayLevel)
447 if not landCover.ImportSHP(landCoverFile):
448 raise ValueError('problem while loading LandCoverMap shape')
449 if landCover.ImportDBF(landCoverDB, code, allvalues, alltypes ) != landCover.DBFStatus_OK:
450 raise ValueError('problem while loading LandCoverMap data base')