SW1Dto2D 0.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,144 @@
1
+ # coding: utf-8
2
+
3
+ # Copyright (c) 2019-2022 CS GROUP - France, University of Toulouse (France)
4
+ #
5
+ # This file is part of the sw1dto2d Package. sw1dto2d is a package to convert
6
+ # 1D Shallow water results to 2D. It is designed to compute 2D geographic
7
+ # representation of the results of 1D shallow water models.
8
+ #
9
+ # This software is governed by the CeCILL license under French law and abiding
10
+ # by the rules of distribution of free software. You can use, modify and/or
11
+ # redistribute the software under the terms of the CeCILL license as circulated
12
+ # by CEA, CNRS and INRIA at the following URL: "http://www.cecill.info".
13
+ #
14
+ # As a counterpart to the access to the source code and rights to copy, modify
15
+ # and redistribute granted by the license, users are provided only with a
16
+ # limited warranty and the software's author, the holder of the economic
17
+ # rights, and the successive licensors have only limited liability.
18
+ #
19
+ # In this respect, the user's attention is drawn to the risks associated with
20
+ # loading, using, modifying and/or developing or reproducing the software by
21
+ # the user in light of its specific status of free software, that may mean that
22
+ # it is complicated to manipulate, and that also therefore means that it is
23
+ # reserved for developers and experienced professionals having in-depth
24
+ # computer knowledge. Users are therefore encouraged to load and test the
25
+ # software's suitability as regards their requirements in conditions enabling
26
+ # the security of their systems and/or data to be ensured and, more generally,
27
+ # to use and operate it in the same conditions as regards security.
28
+ #
29
+ # The fact that you are presently reading this means that you have had
30
+ # knowledge of the CeCILL license and that you accept its terms.
31
+
32
+ import fiona
33
+ from fiona.crs import from_epsg
34
+ import matplotlib.pyplot as plt
35
+ import numpy as np
36
+ import os
37
+ import pandas as pd
38
+ from shapely.geometry import LineString, mapping, Polygon, shape
39
+
40
+ from sw1dto2d.sw1dto2d import SW1Dto2D
41
+
42
+ def export_to_shp(fname, geo, sw1dto2d):
43
+ """ Export a geometry or a list of geometries to a ESRI/Shapefile
44
+
45
+ Parameters
46
+ ----------
47
+ fname (str): Path of the shp output file
48
+ geo (shapely.geometry.BaseGeometry or list): Geometry or list of geometries
49
+ """
50
+
51
+ if isinstance(geo, list):
52
+ schema = {"geometry": "LineString",
53
+ "properties": {("xs", "float")}}
54
+ fout = fiona.open(fname, "w", driver="ESRI Shapefile", crs=from_epsg(4326), schema=schema)
55
+ for index, line in enumerate(geo):
56
+ feature = {"geometry": mapping(line),
57
+ "properties": {"xs": sw1dto2d.curv_abscissa[index]}}
58
+ fout.write(feature)
59
+ fout.close()
60
+ elif isinstance(geo, Polygon):
61
+ schema = {"geometry": "Polygon",
62
+ "properties": {("ID", "int")}}
63
+ fout = fiona.open(fname, "w", driver="ESRI Shapefile", crs=from_epsg(4326), schema=schema)
64
+ feature = {"geometry": mapping(geo),
65
+ "properties": {"ID": 0}}
66
+ fout.write(feature)
67
+ fout.close()
68
+
69
+ def export_sections_points(fname, points, attributes):
70
+ """ Export points defining the cross-sections to a ESRI/Shapefile
71
+
72
+ Parameters
73
+ ----------
74
+ fname (str): Path of the shp output file
75
+ points (list): List of points
76
+ attributes (list): List of attributes dictionnaries for each point
77
+ """
78
+
79
+ schema = {"geometry": "Point",
80
+ "properties": {("xs_id", "int"),
81
+ ("xsnd_id", "int"),
82
+ ("abs", "float"),
83
+ ("loc", "str"),
84
+ ("x", "float"),
85
+ ("y", "float"),
86
+ ("z", "float")}}
87
+ fout = fiona.open(fname, "w", driver="ESRI Shapefile", crs=from_epsg(4326), schema=schema)
88
+ for index, point in enumerate(points):
89
+ feature = {"geometry": mapping(point),
90
+ "properties": attributes[index]}
91
+ fout.write(feature)
92
+ fout.close()
93
+
94
+ def test_brahmaputra():
95
+ """Test brahmaputra cross-section lines and points reconstruction."""
96
+
97
+ # Create output directory if necessary
98
+ if not os.path.isdir("out"):
99
+ os.mkdir("out")
100
+
101
+ # Load centerline
102
+ centerline_path = os.path.join(
103
+ os.path.dirname(__file__),
104
+ "data/brahmaputra/centerline.shp"
105
+ )
106
+ layer = fiona.open(centerline_path, "r")
107
+ centerline = shape(layer[0]["geometry"])
108
+
109
+ # Load Results
110
+ results_path = os.path.join(
111
+ os.path.dirname(__file__),
112
+ "data/brahmaputra/brahmaputra_results.csv"
113
+ )
114
+ results = pd.read_csv(results_path, sep=";")
115
+ # xs = results["xs"].unique()
116
+ # W = results["W"].values
117
+ # W = W.reshape((W.size//xs.size, xs.size))
118
+ # H = results["H"].values
119
+ # H = H.reshape((W.size//xs.size, xs.size))
120
+
121
+ # Instanciate SW1Dto2D object
122
+ # sw1dto2d = SW1Dto2D(xs, H, W, centerline)
123
+ sw1dto2d = SW1Dto2D(
124
+ model_output_1d=results,
125
+ curvilinear_abscissa_key="xs",
126
+ heights_key="H",
127
+ widths_key="W",
128
+ centerline=centerline
129
+ )
130
+
131
+ # Compute cross-sections parameters without normals optimization
132
+ sw1dto2d.compute_xs_parameters(optimize_normals=False)
133
+
134
+ # Export cross-sections cut lines (with maximum width as argument it=None for SW1Dto2D.compute_xs_geometry)
135
+ lines = sw1dto2d.compute_xs_cutlines()
136
+ export_to_shp("out/Brahmaputra_cutlines.shp", lines, sw1dto2d)
137
+
138
+ # Export cross-sections cut lines (with maximum width as argument it=None for SW1Dto2D.compute_xs_geometry)
139
+ lines = sw1dto2d.compute_xs_cutlines(extend=500)
140
+ export_to_shp("out/Brahmaputra_cutlines_extended.shp", lines, sw1dto2d)
141
+
142
+ # Export cross-sections cut lines (with maximum width as argument it=None for SW1Dto2D.compute_xs_geometry)
143
+ points, attributes = sw1dto2d.compute_xs_points(main_channel=100, overbanks=10, extend=2000, epsg=32645)
144
+ export_sections_points("out/Brahmaputra_cutlines_points.shp", points, attributes)
tests/test_garonne.py ADDED
@@ -0,0 +1,129 @@
1
+ # coding: utf-8
2
+
3
+ # Copyright (c) 2019-2022 CS GROUP - France, University of Toulouse (France)
4
+ #
5
+ # This file is part of the sw1dto2d Package. sw1dto2d is a package to convert
6
+ # 1D Shallow water results to 2D. It is designed to compute 2D geographic
7
+ # representation of the results of 1D shallow water models.
8
+ #
9
+ # This software is governed by the CeCILL license under French law and abiding
10
+ # by the rules of distribution of free software. You can use, modify and/or
11
+ # redistribute the software under the terms of the CeCILL license as circulated
12
+ # by CEA, CNRS and INRIA at the following URL: "http://www.cecill.info".
13
+ #
14
+ # As a counterpart to the access to the source code and rights to copy, modify
15
+ # and redistribute granted by the license, users are provided only with a
16
+ # limited warranty and the software's author, the holder of the economic
17
+ # rights, and the successive licensors have only limited liability.
18
+ #
19
+ # In this respect, the user's attention is drawn to the risks associated with
20
+ # loading, using, modifying and/or developing or reproducing the software by
21
+ # the user in light of its specific status of free software, that may mean that
22
+ # it is complicated to manipulate, and that also therefore means that it is
23
+ # reserved for developers and experienced professionals having in-depth
24
+ # computer knowledge. Users are therefore encouraged to load and test the
25
+ # software's suitability as regards their requirements in conditions enabling
26
+ # the security of their systems and/or data to be ensured and, more generally,
27
+ # to use and operate it in the same conditions as regards security.
28
+ #
29
+ # The fact that you are presently reading this means that you have had
30
+ # knowledge of the CeCILL license and that you accept its terms.
31
+
32
+ import fiona
33
+ from fiona.crs import from_epsg
34
+ import numpy as np
35
+ import os
36
+ import pandas as pd
37
+ from shapely.geometry import LineString, mapping, Polygon, shape
38
+
39
+ from sw1dto2d.sw1dto2d import SW1Dto2D
40
+
41
+ def export_to_shp(fname, geo, sw1dto2d):
42
+ """ Export a geometry or a list of geometries to a ESRI/Shapefile
43
+
44
+ Parameters
45
+ ----------
46
+ fname (str): Path of the shp output file
47
+ geo (shapely.geometry.BaseGeometry or list): Geometry or list of geometries
48
+ """
49
+
50
+ if isinstance(geo, list):
51
+ schema = {"geometry": "LineString",
52
+ "properties": {("xs", "float")}}
53
+ fout = fiona.open(fname, "w", driver="ESRI Shapefile", crs=from_epsg(4326), schema=schema)
54
+ for index, line in enumerate(geo):
55
+ feature = {"geometry": mapping(line),
56
+ "properties": {"xs": sw1dto2d.curv_abscissa[index]}}
57
+ fout.write(feature)
58
+ fout.close()
59
+ elif isinstance(geo, Polygon):
60
+ schema = {"geometry": "Polygon",
61
+ "properties": {("ID", "int")}}
62
+ fout = fiona.open(fname, "w", driver="ESRI Shapefile", crs=from_epsg(4326), schema=schema)
63
+ feature = {"geometry": mapping(geo),
64
+ "properties": {"ID": 0}}
65
+ fout.write(feature)
66
+ fout.close()
67
+
68
+ def test_garonne():
69
+ """Test garonne cross-section lines and points reconstruction."""
70
+
71
+ # Create output directory if necessary
72
+ if not os.path.isdir("out"):
73
+ os.mkdir("out")
74
+
75
+ # Load centerline
76
+ centerline_path = os.path.join(
77
+ os.path.dirname(__file__),
78
+ "data/garonne/centerline.shp"
79
+ )
80
+ layer = fiona.open(centerline_path, "r")
81
+ centerline = shape(layer[0]["geometry"])
82
+
83
+ # Load Results
84
+ results_path = os.path.join(
85
+ os.path.dirname(__file__),
86
+ "data/garonne/Garonne2019_results_selection.csv"
87
+ )
88
+ results = pd.read_csv(results_path, sep=";")
89
+ # xs = results["xs"].unique()
90
+ # W = results["W"].values
91
+ # W = W.reshape((W.size//xs.size, xs.size))
92
+ # H = results["H"].values
93
+ # H = H.reshape((W.size//xs.size, xs.size))
94
+
95
+ # Instanciate SW1Dto2D object
96
+ # sw1dto2d = SW1Dto2D(xs, H, W, centerline)
97
+ sw1dto2d = SW1Dto2D(
98
+ model_output_1d=results,
99
+ curvilinear_abscissa_key="xs",
100
+ heights_key="H",
101
+ widths_key="W",
102
+ centerline=centerline
103
+ )
104
+
105
+ # Compute cross-sections parameters without normals optimization
106
+ sw1dto2d.compute_xs_parameters(dx=50, optimize_normals=False)
107
+
108
+ # Export cross-sections cut lines (with maximum width as argument it=None for SW1Dto2D.compute_xs_geometry)
109
+ lines = sw1dto2d.compute_xs_cutlines()
110
+ export_to_shp("out/Garonne_cutlines_raw_normals.shp", lines, sw1dto2d)
111
+
112
+ # Compute cross-sections parameters with normals optimization
113
+ sw1dto2d.compute_xs_parameters(dx=50, optimize_normals=True)
114
+
115
+ # Export cross-sections cut lines (with maximum width as argument it=None for SW1Dto2D.compute_xs_geometry)
116
+ lines = sw1dto2d.compute_xs_cutlines()
117
+ export_to_shp("out/Garonne_cutlines_opt_normals.shp", lines, sw1dto2d)
118
+
119
+ # Export maximum water mask (argument it=None for SW1Dto2D.compute_water_mask)
120
+ poly = sw1dto2d.compute_water_mask()
121
+ export_to_shp("out/Garonne_max_water_mask.shp", poly, sw1dto2d)
122
+
123
+ # Export water mask at 10th time (argument it=10 for SW1Dto2D.compute_water_mask)
124
+ poly = sw1dto2d.compute_water_mask(it=10)
125
+ export_to_shp("out/Garonne_water_mask_it10.shp", poly, sw1dto2d)
126
+
127
+ if __name__ == "__main__":
128
+
129
+ test_garonne()