Salome HOME
Correction for hydro_test
[modules/hydro.git] / src / HYDROTools / changeCoords.py
1
2 import shutil
3 import numpy as np
4 import MEDLoader as ml
5 import medcoupling as mc
6
7 from pyproj import CRS
8 from pyproj import Transformer
9
10 def changeCoords(fileIn, fileOut, epsgIn=2154, epsgOut=2154, offsetXin=0, offsetYin=0, offsetXout=0, offsetYout=0):
11     """
12     change the coordinates of a MED mesh file, with an optional offset reference in Input, and an optional change of system of coordinates (for instance Lambert II --> Lambert 93). The system of coordinates are given with EPSG (European Petroleum Survey Group) integer codes. The transformation uses pyproj.
13
14     When there is a change of system of coordinates, input offset should be given in order to get the correct input coordinates before transformation. example, with an input file in LambertII and an origin offset of (500000, 6000000), to obtain the coordinates in Lambert93 without origin offset: changeCoords("medfileLambertII.med", "medfileLambert93.med", 27572, 2154, 500000, 6000000, 0, 0)
15
16     When there is only an origin offset change, use the same code for epsgIn and epsgOut.
17     The operation is: Xout = Xin + offsetXin -offsetXout, Yout = Yin + offsetYin -offsetYout.
18     If the epsgIn and epsgOut codes are equal, the transformation of system of coordinates (pyproj) is not called.
19
20     parameters:
21     fileIn: full path of input MED file
22     fileOut: full path of output MED file
23     epsgIn: integer code of the EPSG for the input system of coordinates (for instance, Lambert II is 27572)
24     epsgOut: integer code of the EPSG for the output system of coordinates (for instance, Lambert 93 is 2154)
25     offsetXin: X origin in the input system of coordinates (default 0)
26     offsetYin: Y origin in the input system of coordinates (default 0)
27     offsetXout: X origin in the output system of coordinates (default 0)
28     offsetYout: Y origin in the output system of coordinates (default 0)
29     return:
30     O if OK
31     """
32     # TODO: do we need to transform the exception in return codes ?
33     if fileOut != fileIn:
34         shutil.copyfile(fileIn, fileOut)
35
36     if epsgIn != epsgOut:
37         crs_in = CRS.from_epsg(epsgIn)
38         crs_out = CRS.from_epsg(epsgOut)
39         transformer = Transformer.from_crs(crs_in, crs_out)
40
41     meshMEDFileRead = ml.MEDFileMesh.New(fileIn)
42     coords = meshMEDFileRead.getCoords()
43     nb_comp = coords.getNumberOfComponents()
44
45     vx = coords[:,0]
46     vy = coords[:,1]
47     avx = vx.toNumPyArray() + offsetXin
48     avy = vy.toNumPyArray() + offsetYin
49
50     if nb_comp == 3:
51         vz = coords[:,2]
52         avz = vz.toNumPyArray()
53
54     if epsgIn != epsgOut:
55         navx, navy = transformer.transform(avx, avy)
56     else:
57         navx = avx
58         navy = avy
59     navx = navx - offsetXout
60     navy = navy - offsetYout
61
62     if nb_comp == 3:
63         navxy = np.stack((navx, navy, avz), axis=-1)
64         ncoords = mc.DataArrayDouble(navxy)
65         ncoords.setInfoOnComponents(["X [m]", "Y [m]", "Z [m]"])
66     else:
67         navxy = np.stack((navx, navy), axis=-1)
68         ncoords = mc.DataArrayDouble(navxy)
69         ncoords.setInfoOnComponents(["X [m]", "Y [m]"])
70     meshMEDFileRead.setCoords(ncoords)
71
72     meshMEDFileRead.write(fileOut, 0)
73     return 0