Repository navigation
Expand file tree
/
Copy pathpython_label_generator.py
More file actions
266 lines (226 loc) · 9.21 KB
/
Copy pathpython_label_generator.py
File metadata and controls
266 lines (226 loc) · 9.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
"""python_label_generator.py — Python label expression generator (Tool 5.4b).
Generates Esri "Python" label-expression source (`def FindLabel(...): ...`,
bracketed [FieldName] references) for ArcGIS Pro layers whose label class uses the
'Python' expressionEngine instead of Arcade. Mirrors arcade_label_generator.py's
expression-type taxonomy and JSON shape so the two tools stay interchangeable for a
given layer.
No arcpy dependency. stdlib + dataclasses only.
"""
from __future__ import annotations
import json
from dataclasses import dataclass
from pathlib import Path
from typing import Optional
from autogis.core.envmon.label_expression_common import (
LabelExpressionType, derive_label_fields,
)
EXPRESSION_ENGINE = "Python"
@dataclass
class PythonLabelSpec:
"""One Python label expression for a single analyte + expression type."""
layer_name: str
expression_type: str
analyte_field: str
value_field: str
units_field: str
sl_field: Optional[str]
expression: str
# ---------------------------------------------------------------------------
# Expression builders
# ---------------------------------------------------------------------------
def build_result_label_expression(
value_field: str,
units_field: str,
*,
nd_text: str = "ND",
) -> str:
"""Return Esri Python label-expression source for a result value with units.
Arcade's Text()/Number() never raise on non-numeric input (Number() yields NaN
instead) — a Python FindLabel that lets float() raise on a qualified value like
"0.5 J" or "<0.5" would instead leave that feature unlabeled in Pro. The
try/except mirrors Arcade's leniency: fall back to the raw value if it isn't
cleanly numeric, instead of crashing FindLabel.
Example output (Python label-expression code, run via Pro's 'Python' engine):
def FindLabel ( [ResultValue], [ReportedUnits] ):
v = [ResultValue]
if v is None or v == "" or v == "ND":
return "ND"
try:
return "{:,.2f} {}".format(float(v), [ReportedUnits])
except (TypeError, ValueError):
return "{} {}".format(v, [ReportedUnits])
"""
return (
f'def FindLabel ( [{value_field}], [{units_field}] ):\n'
f' v = [{value_field}]\n'
f' if v is None or v == "" or v == "{nd_text}":\n'
f' return "{nd_text}"\n'
f' try:\n'
f' return "{{:,.2f}} {{}}".format(float(v), [{units_field}])\n'
f' except (TypeError, ValueError):\n'
f' return "{{}} {{}}".format(v, [{units_field}])'
)
def build_exceedance_callout_expression(
value_field: str,
sl_field: str,
units_field: str,
*,
nd_text: str = "ND",
exceed_suffix: str = "**",
) -> str:
"""Return Esri Python label-expression source that appends '**' when the result
exceeds the screening level.
Same non-throwing rationale as build_result_label_expression: Arcade's Number()
never raises (it yields NaN), so this falls back to the raw value if it isn't
cleanly numeric rather than letting float() crash FindLabel and blank the label.
A non-numeric/missing screening level just disables the exceedance check.
Example output (Python label-expression code):
def FindLabel ( [ResultValue], [ScreeningLevel], [ReportedUnits] ):
v = [ResultValue]
sl = [ScreeningLevel]
if v is None or v == "" or v == "ND":
return "ND"
try:
num = float(v)
except (TypeError, ValueError):
return "{} {}".format(v, [ReportedUnits])
try:
exceeds = sl not in (None, "") and num > float(sl)
except (TypeError, ValueError):
exceeds = False
if exceeds:
return "{:,.2f} {}**".format(num, [ReportedUnits])
return "{:,.2f} {}".format(num, [ReportedUnits])
"""
return (
f'def FindLabel ( [{value_field}], [{sl_field}], [{units_field}] ):\n'
f' v = [{value_field}]\n'
f' sl = [{sl_field}]\n'
f' if v is None or v == "" or v == "{nd_text}":\n'
f' return "{nd_text}"\n'
f' try:\n'
f' num = float(v)\n'
f' except (TypeError, ValueError):\n'
f' return "{{}} {{}}".format(v, [{units_field}])\n'
f' try:\n'
f' exceeds = sl not in (None, "") and num > float(sl)\n'
f' except (TypeError, ValueError):\n'
f' exceeds = False\n'
f' if exceeds:\n'
f' return "{{:,.2f}} {{}}{exceed_suffix}".format(num, [{units_field}])\n'
f' return "{{:,.2f}} {{}}".format(num, [{units_field}])'
)
def _build_nd_callout_expression(
value_field: str,
*,
nd_text: str = "ND",
) -> str:
"""Return Esri Python label-expression source showing 'ND' label only (no
numeric value shown).
"""
return (
f'def FindLabel ( [{value_field}] ):\n'
f' v = [{value_field}]\n'
f' if v is None or v == "" or v == "{nd_text}":\n'
f' return "{nd_text}"\n'
f' return ""'
)
def _build_well_id_expression(analyte_field: str) -> str:
"""Return Esri Python label-expression source showing the location/well ID
field only."""
return (
f'def FindLabel ( [{analyte_field}] ):\n'
f' return [{analyte_field}]'
)
# ---------------------------------------------------------------------------
# Public API
# ---------------------------------------------------------------------------
def generate_python_labels(
analytes: list[str],
*,
field_prefix: str = "",
) -> list[PythonLabelSpec]:
"""Generate one PythonLabelSpec per analyte per expression type.
Mirrors arcade_label_generator.generate_arcade_labels() field-for-field —
see that function's docstring for parameter semantics.
Args:
analytes: List of canonical analyte names (e.g. ["Benzene", "PCE"]).
field_prefix: Optional prefix for field names (e.g. "Env_").
Returns:
List of PythonLabelSpec objects (may be empty when analytes is empty).
"""
if not analytes:
return []
specs: list[PythonLabelSpec] = []
for analyte in analytes:
fields = derive_label_fields(analyte, field_prefix)
specs.append(PythonLabelSpec(
layer_name=f"{fields.layer_base}_Result",
expression_type=LabelExpressionType.RESULT_WITH_UNITS,
analyte_field=fields.id_field,
value_field=fields.value_field,
units_field=fields.units_field,
sl_field=None,
expression=build_result_label_expression(fields.value_field, fields.units_field),
))
specs.append(PythonLabelSpec(
layer_name=f"{fields.layer_base}_Exceedance",
expression_type=LabelExpressionType.EXCEEDANCE_CALLOUT,
analyte_field=fields.id_field,
value_field=fields.value_field,
units_field=fields.units_field,
sl_field=fields.sl_field,
expression=build_exceedance_callout_expression(
fields.value_field, fields.sl_field, fields.units_field
),
))
specs.append(PythonLabelSpec(
layer_name=f"{fields.layer_base}_ND",
expression_type=LabelExpressionType.ND_CALLOUT,
analyte_field=fields.id_field,
value_field=fields.value_field,
units_field=fields.units_field,
sl_field=None,
expression=_build_nd_callout_expression(fields.value_field),
))
specs.append(PythonLabelSpec(
layer_name=f"{fields.layer_base}_WellID",
expression_type=LabelExpressionType.WELL_ID_ONLY,
analyte_field=fields.id_field,
value_field=fields.value_field,
units_field=fields.units_field,
sl_field=None,
expression=_build_well_id_expression(fields.id_field),
))
return specs
def write_label_expressions(specs: list[PythonLabelSpec], out_path: Path) -> None:
"""Serialise a list of PythonLabelSpec objects to a JSON file.
Each entry in the output array has:
- layer_name: str
- expression_type: str
- analyte_field: str
- value_field: str
- units_field: str
- sl_field: str | null
- python_expression: str
- expression_engine: str ("Python" -- the labelClass.expressionEngine value)
Args:
specs: List of PythonLabelSpec objects from generate_python_labels().
out_path: Destination .json file path (parent directories created if needed).
"""
out_path = Path(out_path)
out_path.parent.mkdir(parents=True, exist_ok=True)
payload = [
{
"layer_name": s.layer_name,
"expression_type": s.expression_type,
"analyte_field": s.analyte_field,
"value_field": s.value_field,
"units_field": s.units_field,
"sl_field": s.sl_field,
"python_expression": s.expression,
"expression_engine": EXPRESSION_ENGINE,
}
for s in specs
]
out_path.write_text(json.dumps(payload, indent=2), encoding="utf-8")