diff --git a/scripts/python3/data_manip/formats/mascaretgeo_file.py b/scripts/python3/data_manip/formats/mascaretgeo_file.py index 8d0934b2751d9dac45a9041d67e2ec30a20c8253..cf0d511209f54bcc8362d70ef2864ca0094bffbe 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