From c1e4169bb91078a298ca0a19ac20dcd2bb9a1c81 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Matthieu=20S=C3=A9cher?= <matthieu.secher@edf.fr>
Date: Wed, 10 May 2023 16:36:59 +0200
Subject: [PATCH] [scripts] Add new method to export PreCourlis file

Add a method to MascaretGeoFile class to generate a GIS file with the
format of PreCourlis (specific attributes to describe geometry and
other Mascaret-Courlis informations)
---
 .../data_manip/formats/mascaretgeo_file.py    | 58 +++++++++++++++++++
 1 file changed, 58 insertions(+)

diff --git a/scripts/python3/data_manip/formats/mascaretgeo_file.py b/scripts/python3/data_manip/formats/mascaretgeo_file.py
index 8d0934b275..cf0d511209 100644
--- a/scripts/python3/data_manip/formats/mascaretgeo_file.py
+++ b/scripts/python3/data_manip/formats/mascaretgeo_file.py
@@ -297,6 +297,64 @@ class MascaretGeoFile():
     def __repr__(self):
         return 'MascaretGeoFile: %s' % self.file_name
 
+    def save_precourlis(self, output_file_name, crs="EPSG:2154"):
+        """
+        Method to export a MascaretGeoFile into the PreCourlis format
+        (geopackage with specific attributes)
+        It requires X and Y GIS coordinates (georef)
+        """
+        import fiona
+
+        properties = [('sec_id', 'int'),
+                      ('sec_name', 'str:80'),
+                      ('abs_long', 'float'),
+                      ('axis_x', 'float'),
+                      ('axis_y', 'float'),
+                      ('layers', 'str:254'),
+                      ('p_id', 'str:100000'),
+                      ('topo_bat', 'str:100000'),
+                      ('abs_lat', 'str:100000'),
+                      ('zfond', 'str:100000')]
+
+        for layer_name in self.layer_names:
+            properties.append((layer_name, 'str'))
+
+        schema = {'geometry': 'LineString',
+                  'properties': OrderedDict(properties)}
+
+        dict_lines = []
+
+        reach = self.reaches[1]
+        for sec in reach:
+            coord = []
+            for x, y in zip(sec.x, sec.y):
+                coord.append((x, y))
+
+            properties_sec = [('sec_id', sec.id),
+                              ('sec_name', sec.name),
+                              ('abs_long', sec.pk),
+                              ('axis_x', sec.axis[0]),
+                              ('axis_y', sec.axis[1]),
+                              ('layers', ','.join(sec.layer_names)),
+                              ('p_id', ','.join(map(str,
+                                                range(sec.nb_points)))),
+                              ('topo_bat', ','.join(sec.topo_bath)),
+                              ('abs_lat', ','.join(map(str, sec.distances))),
+                              ('zfond', ','.join(map(str, sec.z)))]
+
+            for i, layer_name in enumerate(self.layer_names):
+                properties_sec.append((layer_name, ",".join(map(
+                    str, sec.layers_elev[i, :]))))
+
+            dict_lines.append({'geometry': {'type': 'LineString',
+                               'coordinates': coord},
+                               'properties': OrderedDict(properties_sec)})
+
+        with fiona.open(output_file_name, 'w', driver='GPKG', crs=crs,
+                        schema=schema) as shp:
+            for dict_line in dict_lines:
+                shp.write(dict_line)
+
     def add_reach(self, reach):
         """
         Add a single reach
-- 
GitLab