Skip to content

Commit 76980a9

Browse files
export handler running with geodata
1 parent c94d1dd commit 76980a9

9 files changed

+289
-490
lines changed

g_s_defaults.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
"""
2323

2424
__author__ = 'Jannik Schilling'
25-
__date__ = '2024-03-03'
25+
__date__ = '2025-02-17'
2626
__copyright__ = '(C) 2021 by Jannik Schilling'
2727

2828
import os

g_s_export_helpers.py

+87-38
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,30 @@
3232
QgsProcessingException
3333
)
3434
from .g_s_defaults import (
35+
annotation_field_name,
3536
def_tables_dict,
37+
def_sections_dict,
3638
annotation_field_name
3739
)
3840

3941

4042

4143

44+
def get_annotations_from_raw_df(df_raw):
45+
"""
46+
extracts annotations / descriptions from the dataframe
47+
:param pd.DataFrame df_raw
48+
:return: dict
49+
"""
50+
if annotation_field_name in df_raw.columns:
51+
annot_dict = {k: v for k, v in zip(df_raw['Name'], df_raw[annotation_field_name])}
52+
annot_dict = {k: v for k, v in annot_dict.items() if pd.notna(v)}
53+
annot_dict = {k: v for k, v in annot_dict.items() if len(v) > 0}
54+
else:
55+
annot_dict = {}
56+
return annot_dict
57+
58+
4259

4360
def data_preparation(data_name, data_entry, export_params):
4461
"""
@@ -50,14 +67,15 @@ def data_preparation(data_name, data_entry, export_params):
5067
if data_name == 'OPTIONS':
5168
from .g_s_options import get_options_from_table
5269
options_df, main_infiltration_method = get_options_from_table(
53-
data_entry['data']['OPTIONS'].copy()
70+
data_entry['OPTIONS'].copy()
5471
)
5572
export_params['main_infiltration_method'] = main_infiltration_method
5673
return {'OPTIONS': {'data': options_df}}
57-
elif data_name == 'SUBCATCHMENTS':
74+
75+
elif data_name == 'SUBCATCHMENTS':
5876
from .g_s_subcatchments import get_subcatchments_from_layer
5977
subcatchments_df, subareas_df, infiltration_df = get_subcatchments_from_layer(
60-
data_entry['data']['SUBCATCHMENTS'].copy(),
78+
data_entry.copy(),
6179
export_params['main_infiltration_method']
6280
)
6381
return {
@@ -69,7 +87,7 @@ def data_preparation(data_name, data_entry, export_params):
6987
elif data_name == 'CONDUITS':
7088
from .g_s_links import get_conduits_from_shapefile
7189
conduits_df, xsections_df, losses_df = get_conduits_from_shapefile(
72-
data_entry['data']['CONDUITS'].copy()
90+
data_entry.copy()
7391
)
7492
return {
7593
'CONDUITS': {'data': conduits_df},
@@ -79,70 +97,71 @@ def data_preparation(data_name, data_entry, export_params):
7997

8098
elif data_name == 'PUMPS':
8199
from .g_s_links import get_pumps_from_shapefile
82-
pumps_df = get_pumps_from_shapefile(data_entry['data']['PUMPS'].copy())
100+
pumps_df = get_pumps_from_shapefile(data_entry.copy())
83101
return {'PUMPS': {'data': pumps_df}}
84102

85103
elif data_name == 'WEIRS':
86104
from .g_s_links import get_weirs_from_shapefile
87-
weirs_df, xsections_df = get_weirs_from_shapefile(data_entry['data']['WEIRS'].copy())
105+
weirs_df, xsections_df = get_weirs_from_shapefile(data_entry.copy())
88106
return {
89107
'WEIRS': {'data': weirs_df},
90108
'XSECTIONS': {'data': xsections_df}
91109
}
92110

93111
elif data_name == 'OUTLETS':
94112
from .g_s_links import get_outlets_from_shapefile
95-
outlets_df = get_outlets_from_shapefile(data_entry['data']['OUTLETS'].copy())
113+
outlets_df = get_outlets_from_shapefile(data_entry.copy())
96114
return {'OUTLETS': {'data': outlets_df}}
97115

98116
elif data_name == 'ORIFICES':
99117
from .g_s_links import get_orifices_from_shapefile
100-
orifices_df, xsections_df = get_orifices_from_shapefile(data_entry['data']['ORIFICES'].copy())
118+
orifices_df, xsections_df = get_orifices_from_shapefile(data_entry.copy())
101119
return {
102120
'ORIFICES': {'data': orifices_df},
103121
'XSECTIONS': {'data': xsections_df}
104122
}
105123

106124
elif data_name == 'JUNCTIONS':
107-
from .g_s_nodes import get_junctions_from_shapefile
108-
junctions_df = get_junctions_from_shapefile(data_entry['data']['JUNCTIONS'].copy())
125+
from .g_s_nodes import get_junctions_from_layer
126+
junctions_df = get_junctions_from_layer(data_entry.copy())
109127
return {'JUNCTIONS': {'data': junctions_df}}
110128

111129
elif data_name == 'OUTFALLS':
112130
from .g_s_nodes import get_outfalls_from_shapefile
113-
outfalls_df = get_outfalls_from_shapefile(data_entry['data']['OUTFALLS'].copy())
131+
outfalls_df = get_outfalls_from_shapefile(data_entry.copy())
114132
return {'OUTFALLS': {'data': outfalls_df}}
115133

116-
elif data_name == 'STORAGES':
117-
from .g_s_nodes import get_storages_from_shapefile
118-
storages_df = get_storages_from_shapefile(data_entry['data']['STORAGES'].copy())
119-
return {'STORAGES': {'data': storages_df}}
134+
elif data_name == 'STORAGE':
135+
from .g_s_nodes import get_storages_from_layer
136+
storages_df = get_storages_from_layer(data_entry.copy())
137+
return {'STORAGE': {'data': storages_df}}
120138

121139
elif data_name == 'DIVIDERS':
122-
from .g_s_nodes import get_dividers_from_shapefile
123-
dividers_df = get_dividers_from_shapefile(data_entry['data']['DIVIDERS'].copy())
140+
from .g_s_nodes import get_dividers_from_layer
141+
dividers_df = get_dividers_from_layer(data_entry.copy())
124142
return {'DIVIDERS': {'data': dividers_df}}
125-
143+
144+
#######
126145
elif data_name == 'CURVES':
127146
from .g_s_export_helpers import get_curves_from_table
128147
curves_dict = get_curves_from_table(
129-
data_entry['data']['CURVES'],
148+
data_entry['CURVES'],
130149
name_col='Name'
131150
)
132151
return {'CURVES': {'data': curves_dict}}
133152

134153
elif data_name == 'PATTERNS':
135154
from .g_s_export_helpers import get_patterns_from_table
136155
patterns_dict = get_patterns_from_table(
137-
data_entry['data']['PATTERNS'],
156+
data_entry['PATTERNS'],
138157
name_col='Name'
139158
)
140159
return {'PATTERNS': {'data': patterns_dict}}
141160

142161
elif data_name == 'TIMESERIES':
143162
from .g_s_export_helpers import get_timeseries_from_table
144163
timeseries_dict = get_timeseries_from_table(
145-
data_entry['data']['TIMESERIES'],
164+
data_entry['TIMESERIES'],
146165
name_col='Name',
147166
feedback=export_params['feedback']
148167
)
@@ -151,17 +170,28 @@ def data_preparation(data_name, data_entry, export_params):
151170
elif data_name == 'INFLOWS':
152171
from .g_s_nodes import get_inflows_from_table
153172
inflows_dict = get_inflows_from_table(
154-
data_entry['data']['INFLOWS'],
173+
data_entry['INFLOWS'],
155174
all_nodes,
156175
feedback=export_params['feedback']
157176
)
158177
return {'INFLOWS': {'data': inflows_dict}}
159178

160179
elif data_name == 'QUALITY':
161180
from .g_s_quality import get_quality_params_from_table
162-
quality_df = get_quality_params_from_table(data_entry['data']['QUALITY'].copy())
181+
quality_df = get_quality_params_from_table(data_entry['QUALITY'].copy())
163182
return {'QUALITY': {'data': quality_df}}
164183

184+
elif data_name == 'RAINGAGES':
185+
from .g_s_subcatchments import get_raingage_from_qgis_row
186+
rg_features_df = data_entry.copy()
187+
rg_features_df = rg_features_df.apply(
188+
lambda x: get_raingage_from_qgis_row(x),
189+
axis=1
190+
)
191+
rg_inp_cols = def_sections_dict['RAINGAGES']
192+
rg_features_df = rg_features_df[rg_inp_cols] # drop unnecessary
193+
return {'RAINGAGES': {'data': rg_features_df}}
194+
165195
else:
166196
raise QgsProcessingException(f'Unknown data name: {data_name}')
167197

@@ -175,33 +205,40 @@ def use_z_if_available(
175205
coords,
176206
use_z_bool,
177207
feedback,
178-
geom_type='Points',
208+
link_offsets='elevation',
179209
layer_name=None
180210
):
181211
"""
182-
replaces Elevation or InOffset/OutOffset by Z_Coords+
212+
replaces Elevation or InOffset/OutOffset by Z_Coords and removes Z_Coords from coords dict
183213
:param pd.DataFrame df
184-
:param pd.DataFrame / dict coords
214+
:param dict coords
185215
:param bool use_z_bool
186216
:param QgsProcessingFeedback feedback
217+
:param str link_offsets
218+
:param str layer_name
187219
"""
188-
if geom_type == 'lines':
220+
if list(coords.keys())[0] == 'VERTICES': # lines
221+
coords_dict = coords['VERTICES']['data']
189222
if use_z_bool:
190223
# if not na
191-
df['InOffset'] = [coords[l_name]['Z_Coord'].tolist()[0] for l_name in df['Name']]
192-
df['OutOffset'] = [coords[l_name]['Z_Coord'].tolist()[-1] for l_name in df['Name']]
193-
coords = {
194-
l_name: df_coord[
224+
df['InOffset'] = [coords_dict[l_name]['Z_Coord'].tolist()[0] for l_name in df['Name']]
225+
df['OutOffset'] = [coords_dict[l_name]['Z_Coord'].tolist()[-1] for l_name in df['Name']]
226+
coords_dict_without_z = {
227+
l_name: df_i[
195228
['X_Coord', 'Y_Coord']
196-
] for l_name, df_coord in coords.items()
229+
] for l_name, df_i in coords_dict.items()
197230
} # remove z
198-
else:
231+
coords['VERTICES']['data'] = coords_dict_without_z
232+
else: # points -> pd.DataFrame
233+
coords_df = coords['COORDINATES']['data']
199234
if use_z_bool:
200235
# if not na
201-
df['Elevation'] = coords['Z_Coord']
202-
coords.drop("Z_Coord", axis=1, inplace=True)
236+
df['Elevation'] = coords_df['Z_Coord']
237+
coords_df_without_z = coords_df.drop("Z_Coord", axis=1, inplace=False)
238+
coords['COORDINATES']['data'] = coords_df_without_z
203239
return df, coords
204240

241+
205242
def get_coords_from_geometry(df):
206243
"""
207244
extracts coords from any gpd.geodataframe
@@ -258,23 +295,35 @@ def get_coords_from_geometry(df):
258295
['Name', 'X_Coord', 'Y_Coord', 'Z_Coord']
259296
)
260297
)
261-
return extr_coords_df
298+
return {'COORDINATES': {'data': extr_coords_df}}
262299

263300
# case lines
264301
elif all(
265302
QgsWkbTypes.displayString(
266303
g_type.wkbType()
267304
) in line_t_names for g_type in df.geometry
268305
):
269-
return {na: extract_xy_from_line(line_geom) for line_geom, na in zip(df.geometry, df.Name)}
306+
extracted_vertices = {
307+
na: extract_xy_from_line(line_geom) for line_geom, na in zip(
308+
df.geometry,
309+
df.Name
310+
)
311+
}
312+
return {'VERTICES': {'data': extracted_vertices}}
270313

271314
# case polygons
272315
elif all(
273316
QgsWkbTypes.displayString(
274317
g_type.wkbType()
275318
) in polygon_t_names for g_type in df.geometry
276319
):
277-
return {na: extract_xy_from_area(polyg_geom) for polyg_geom, na in zip(df.geometry, df.Name)}
320+
extracted_vertices = {
321+
na: extract_xy_from_area(polyg_geom) for polyg_geom, na in zip(
322+
df.geometry,
323+
df.Name
324+
)
325+
}
326+
return {'POLYGONS': {'data': extracted_vertices}}
278327
else:
279328
raise QgsProcessingException(
280329
'Geometry type of one or more features could not be handled'

g_s_links.py

+7-40
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,9 @@ def get_conduits_from_shapefile(conduits_raw):
6767
removes columns which are not needed, replaces empty values with '' or '*'
6868
:param pd.DataFrame conduits_raw
6969
"""
70-
# check if all columns exist
71-
qgis_conduits_cols = list(def_qgis_fields_dict['CONDUITS'].keys())
7270
conduits_cols = def_sections_dict['CONDUITS']
7371
xsections_cols = def_sections_dict['XSECTIONS']
7472
losses_cols = def_sections_dict['LOSSES']
75-
cond_layer_name = 'Conduits Layer'
76-
check_columns(cond_layer_name,
77-
qgis_conduits_cols,
78-
conduits_raw.keys())
7973
conduits_df = conduits_raw[conduits_cols].copy()
8074
conduits_df = conduits_df.apply(lambda x: x.astype("string"), axis = 0)
8175
conduits_df['Name'] = [str(x) for x in conduits_df['Name']]
@@ -170,14 +164,7 @@ def del_first_last_vt(link):
170164
# pumps
171165
def get_pumps_from_shapefile(pumps_raw):
172166
"""prepares pumps data for writing an input file"""
173-
# check if all columns exist
174167
pumps_cols = list(def_qgis_fields_dict['PUMPS'].keys())
175-
pumps_layer_name = 'Pumps Layer'
176-
check_columns(
177-
pumps_layer_name,
178-
pumps_cols,
179-
pumps_raw.keys()
180-
)
181168
pumps_df = pumps_raw[pumps_cols].copy()
182169
pumps_df['Name'] = [str(x) for x in pumps_df['Name']]
183170
pumps_df['PumpCurve'] = pumps_df['PumpCurve'].fillna('*')
@@ -200,14 +187,7 @@ def get_pumps_from_shapefile(pumps_raw):
200187

201188
def get_weirs_from_shapefile(weirs_raw):
202189
"""prepares weirs data for writing an input file"""
203-
weirs_qgis_cols = list(def_qgis_fields_dict['WEIRS'].keys())
204190
weirs_inp_cols = def_sections_dict['WEIRS']
205-
weirs_layer_name = 'Weirs Layer'
206-
check_columns(
207-
weirs_layer_name,
208-
weirs_qgis_cols,
209-
weirs_raw.columns
210-
)
211191
weirs_df = weirs_raw.copy()
212192
weirs_df['Name'] = [str(x) for x in weirs_df['Name']]
213193
weirs_df['CrestHeigh'] = weirs_df['CrestHeigh'].fillna('*')
@@ -248,14 +228,6 @@ def get_orifices_from_shapefile(orifices_raw):
248228
prepares orifices data for writing an input file
249229
param: pd.DataFrame orifices_raw
250230
"""
251-
# check if columns exist
252-
all_orifices_cols = list(def_qgis_fields_dict['ORIFICES'].keys())
253-
orifices_layer_name = 'Orifices Layer'
254-
check_columns(
255-
orifices_layer_name,
256-
all_orifices_cols,
257-
orifices_raw.columns
258-
)
259231
orifices_inp_cols = def_sections_dict['ORIFICES']
260232
orifices_df = orifices_raw.copy()
261233
orifices_df['Name'] = [str(x) for x in orifices_df['Name']]
@@ -282,20 +254,15 @@ def get_orifices_from_shapefile(orifices_raw):
282254

283255

284256
# outlets
257+
def get_outl_curve(outl_row):
258+
"""selects curve data according to rating curve type"""
259+
if outl_row['RateCurve'] in ['FUNCTIONAL/DEPTH', 'FUNCTIONAL/HEAD']:
260+
return outl_row['Qcoeff']
261+
else:
262+
return outl_row['CurveName']
263+
285264
def get_outlets_from_shapefile(outlets_raw):
286265
"""prepares outlets data for writing an input file"""
287-
def get_outl_curve(outl_row):
288-
"""selects curve data according to rating curve type"""
289-
if outl_row['RateCurve'] in ['FUNCTIONAL/DEPTH', 'FUNCTIONAL/HEAD']:
290-
return outl_row['Qcoeff']
291-
else:
292-
return outl_row['CurveName']
293-
# check columns
294-
outlets_cols = list(def_qgis_fields_dict['OUTLETS'].keys())
295-
outlets_layer_name = 'Outlets Layer'
296-
check_columns(outlets_layer_name,
297-
outlets_cols,
298-
outlets_raw.keys())
299266
outlets_raw['Name'] = [str(x) for x in outlets_raw['Name']]
300267
outlets_raw['Qcoeff'] = outlets_raw['Qcoeff'].fillna(1)
301268
outlets_raw['CurveName'] = outlets_raw['CurveName'].fillna('*')

0 commit comments

Comments
 (0)