BERATools 0.5.4__tar.gz → 0.6.0__tar.gz
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.
- {beratools-0.5.4 → beratools-0.6.0}/.gitignore +3 -0
- {beratools-0.5.4 → beratools-0.6.0}/PKG-INFO +18 -2
- {beratools-0.5.4 → beratools-0.6.0}/README.md +16 -1
- {beratools-0.5.4 → beratools-0.6.0}/beratools/cli/entry.py +1 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/core/algo_centerline.py +8 -4
- beratools-0.6.0/beratools/core/pipeline.py +249 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/gui/assets/beratools.json +28 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/gui/bt_data.py +10 -1
- {beratools-0.5.4 → beratools-0.6.0}/beratools/gui/bt_gui_main.py +227 -56
- beratools-0.6.0/beratools/gui/main.py +39 -0
- beratools-0.6.0/beratools/gui/pipeline_panel.py +320 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/gui/tool_widgets.py +142 -33
- {beratools-0.5.4 → beratools-0.6.0}/beratools/tools/line_footprint_relative.py +38 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/utility/env_checks.py +22 -1
- {beratools-0.5.4 → beratools-0.6.0}/beratools/utility/tool_args.py +2 -0
- {beratools-0.5.4 → beratools-0.6.0}/pyproject.toml +1 -0
- beratools-0.5.4/beratools/gui/main.py +0 -23
- {beratools-0.5.4 → beratools-0.6.0}/LICENSE +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/__init__.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/cli/__init__.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/core/__init__.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/core/algo_astar.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/core/algo_canopy_footprint_absolute.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/core/algo_check_seed_line.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/core/algo_check_seed_line_validate.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/core/algo_common.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/core/algo_cost.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/core/algo_dijkstra.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/core/algo_geometry.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/core/algo_line_grouping.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/core/algo_merge_lines.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/core/algo_seed_line_correction.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/core/algo_split_with_lines.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/core/algo_vertex_optimization.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/core/algo_vertex_preclean.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/core/alt_spatial_common.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/core/canopy_threshold_relative.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/core/constants.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/core/line_footprint_functions.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/core/logger.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/core/tool_base.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/core/tool_geo_simplify.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/external/__init__.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/external/geo_simplify/geo-simplify-linux-x64 +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/external/geo_simplify/geo-simplify-win-x64.exe +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/external/polygon_centerline/LICENSE.md +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/external/polygon_centerline/UPSTREAM.md +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/external/polygon_centerline/__init__.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/external/polygon_centerline/_src.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/external/polygon_centerline/exceptions.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/gui/assets/BERALogo.ico +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/gui/assets/BERALogo.png +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/gui/assets/BERA_GUI_Main.png +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/gui/assets/BERA_Splash.gif +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/gui/assets/BERA_WizardImage.png +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/gui/assets/closed.gif +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/gui/assets/closed.png +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/gui/assets/gui.json +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/gui/assets/open.gif +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/gui/assets/open.png +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/gui/assets/tool.gif +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/gui/assets/tool.png +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/gui/geometry_types.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/tools/__init__.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/tools/canopy_footprint_absolute.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/tools/centerline.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/tools/check_seed_line.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/tools/common.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/tools/ground_footprint.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/tools/tool_template.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/tools/vertex_optimization.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/utility/spatial_common.py +0 -0
- {beratools-0.5.4 → beratools-0.6.0}/beratools/utility/unit_conversion.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: BERATools
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.6.0
|
|
4
4
|
Summary: An advanced forest line feature analysis platform
|
|
5
5
|
Project-URL: Homepage, https://github.com/BERATool/beratools
|
|
6
6
|
Author-email: AppliedGRG <appliedgrg@gmail.com>, Richard Zeng <richardqzeng@gmail.com>
|
|
@@ -21,6 +21,7 @@ Requires-Dist: igraph
|
|
|
21
21
|
Requires-Dist: networkit
|
|
22
22
|
Requires-Dist: numpy<2.4,>=1.26
|
|
23
23
|
Requires-Dist: pandarallel
|
|
24
|
+
Requires-Dist: pandas<3,>=2.0
|
|
24
25
|
Requires-Dist: pyogrio>=0.12.0
|
|
25
26
|
Requires-Dist: pyqt5
|
|
26
27
|
Requires-Dist: rasterio>=1.5.0
|
|
@@ -74,7 +75,7 @@ Here are the ways to install BERA Tools:
|
|
|
74
75
|
|
|
75
76
|
### Windows Installer
|
|
76
77
|
|
|
77
|
-
Windows installer is provided with releases. Check the [latest release](https://github.com/BERATool/beratools/releases/latest).
|
|
78
|
+
Windows installer is provided with releases. Check the [latest release](https://github.com/BERATool/beratools/releases/latest). Signing is currently off by default; unsigned assets are named `beratools-installer-x.y.z-unsigned.exe` and identified as unsigned in the release notes. Windows may display an unknown-publisher or SmartScreen warning. Maintainers can enable SignPath per run according to the BERA Tools [Code signing policy](CODE_SIGNING_POLICY.md).
|
|
78
79
|
|
|
79
80
|
### Install with Anaconda
|
|
80
81
|
|
|
@@ -103,6 +104,21 @@ Install with Anaconda works on Windows, macOS, and Linux.
|
|
|
103
104
|
|
|
104
105
|
For more information about installation, check the [BERA Tools Installation](https://beratool.github.io/beratools/user/installation/).
|
|
105
106
|
|
|
107
|
+
## Pipelines
|
|
108
|
+
|
|
109
|
+
The **Pipeline** tab, next to **Tool history** in the GUI, chains tools into steps that run one after
|
|
110
|
+
another. Drag tools in from the tool tree, set each step's parameters in the tool form, and link a
|
|
111
|
+
step's inputs to an earlier step's output. Each input links to the latest earlier step that makes what
|
|
112
|
+
it needs, for example the checked lines from **Check Seed Lines** into **Centerline**, and the
|
|
113
|
+
links are reset whenever a step is added, moved or removed. Pipelines save as `.json` files and also
|
|
114
|
+
run without the GUI:
|
|
115
|
+
|
|
116
|
+
```bash
|
|
117
|
+
$ python -m beratools.core.pipeline my_pipeline.json --processes 4
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
See [Building a pipeline](https://beratool.github.io/beratools/user_guide/#building-a-pipeline).
|
|
121
|
+
|
|
106
122
|
## BERA Tools Guide
|
|
107
123
|
|
|
108
124
|
Check the online [BERA Tools Guide](https://beratool.github.io/beratools/) for user, developer and technical guides.
|
|
@@ -22,7 +22,7 @@ Here are the ways to install BERA Tools:
|
|
|
22
22
|
|
|
23
23
|
### Windows Installer
|
|
24
24
|
|
|
25
|
-
Windows installer is provided with releases. Check the [latest release](https://github.com/BERATool/beratools/releases/latest).
|
|
25
|
+
Windows installer is provided with releases. Check the [latest release](https://github.com/BERATool/beratools/releases/latest). Signing is currently off by default; unsigned assets are named `beratools-installer-x.y.z-unsigned.exe` and identified as unsigned in the release notes. Windows may display an unknown-publisher or SmartScreen warning. Maintainers can enable SignPath per run according to the BERA Tools [Code signing policy](CODE_SIGNING_POLICY.md).
|
|
26
26
|
|
|
27
27
|
### Install with Anaconda
|
|
28
28
|
|
|
@@ -51,6 +51,21 @@ Install with Anaconda works on Windows, macOS, and Linux.
|
|
|
51
51
|
|
|
52
52
|
For more information about installation, check the [BERA Tools Installation](https://beratool.github.io/beratools/user/installation/).
|
|
53
53
|
|
|
54
|
+
## Pipelines
|
|
55
|
+
|
|
56
|
+
The **Pipeline** tab, next to **Tool history** in the GUI, chains tools into steps that run one after
|
|
57
|
+
another. Drag tools in from the tool tree, set each step's parameters in the tool form, and link a
|
|
58
|
+
step's inputs to an earlier step's output. Each input links to the latest earlier step that makes what
|
|
59
|
+
it needs, for example the checked lines from **Check Seed Lines** into **Centerline**, and the
|
|
60
|
+
links are reset whenever a step is added, moved or removed. Pipelines save as `.json` files and also
|
|
61
|
+
run without the GUI:
|
|
62
|
+
|
|
63
|
+
```bash
|
|
64
|
+
$ python -m beratools.core.pipeline my_pipeline.json --processes 4
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
See [Building a pipeline](https://beratool.github.io/beratools/user_guide/#building-a-pipeline).
|
|
68
|
+
|
|
54
69
|
## BERA Tools Guide
|
|
55
70
|
|
|
56
71
|
Check the online [BERA Tools Guide](https://beratool.github.io/beratools/) for user, developer and technical guides.
|
|
@@ -201,6 +201,7 @@ def _resolve_tool_function(tool_api: str):
|
|
|
201
201
|
def run_tool_command(args: argparse.Namespace) -> int:
|
|
202
202
|
from beratools.utility import env_checks
|
|
203
203
|
|
|
204
|
+
env_checks.configure_gdal_proj_environment()
|
|
204
205
|
env_checks.warn_gdal_proj_env()
|
|
205
206
|
|
|
206
207
|
tool_api = args.tool_api
|
|
@@ -238,11 +238,15 @@ def snap_end_to_end(in_line, line_reference, max_snap_dist=None,cid=None):
|
|
|
238
238
|
forward = False
|
|
239
239
|
|
|
240
240
|
if forward:
|
|
241
|
-
|
|
242
|
-
coords[-1] = seed_end.coords[0]
|
|
241
|
+
snap_start, snap_end = seed_start, seed_end
|
|
243
242
|
else:
|
|
244
|
-
|
|
245
|
-
|
|
243
|
+
snap_start, snap_end = seed_end, seed_start
|
|
244
|
+
|
|
245
|
+
# Only snap ends within max_snap_dist of their target
|
|
246
|
+
if max_snap_dist is None or cl_start.distance(snap_start) <= max_snap_dist:
|
|
247
|
+
coords[0] = snap_start.coords[0]
|
|
248
|
+
if max_snap_dist is None or cl_end.distance(snap_end) <= max_snap_dist:
|
|
249
|
+
coords[-1] = snap_end.coords[0]
|
|
246
250
|
#
|
|
247
251
|
# Safety check
|
|
248
252
|
#
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Copyright (C) 2025 Applied Geospatial Research Group.
|
|
3
|
+
|
|
4
|
+
This script is licensed under the GNU General Public License v3.0.
|
|
5
|
+
See <https://gnu.org/licenses/gpl-3.0> for full license details.
|
|
6
|
+
|
|
7
|
+
Description:
|
|
8
|
+
This script is part of the BERA Tools.
|
|
9
|
+
Webpage: https://github.com/BERATool/beratools
|
|
10
|
+
|
|
11
|
+
The purpose of this script is to provide user-built pipelines: an ordered list of tool steps,
|
|
12
|
+
saved as JSON and run one after another. A step's input can be linked to an earlier step's
|
|
13
|
+
output, so the pipeline can be rerun on other data by changing only the first step's input.
|
|
14
|
+
|
|
15
|
+
Which inputs can be linked to which outputs comes from beratools.json, so a new tool takes part
|
|
16
|
+
in pipelines without changes here:
|
|
17
|
+
- an input parameter with "accepts": "<kind>" can be linked;
|
|
18
|
+
- an output parameter with "produces": {"<kind>": [patterns]} provides that kind. An empty
|
|
19
|
+
list means the output itself (e.g. a vector file|layer); otherwise the first
|
|
20
|
+
pattern that matches files in the output folder is used, newest file first.
|
|
21
|
+
|
|
22
|
+
Usage:
|
|
23
|
+
python -m beratools.core.pipeline my_pipeline.json [--processes 4]
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
import argparse
|
|
27
|
+
import functools
|
|
28
|
+
import json
|
|
29
|
+
import os
|
|
30
|
+
import subprocess
|
|
31
|
+
import sys
|
|
32
|
+
import time
|
|
33
|
+
import uuid
|
|
34
|
+
from pathlib import Path
|
|
35
|
+
|
|
36
|
+
PIPELINE_FORMAT = 2
|
|
37
|
+
TOOLS_DIR = Path(__file__).resolve().parents[1] / "tools"
|
|
38
|
+
TOOLS_JSON = Path(__file__).resolve().parents[1] / "gui" / "assets" / "beratools.json"
|
|
39
|
+
PACKAGE_ROOT = Path(__file__).resolve().parents[2]
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@functools.lru_cache(maxsize=None)
|
|
43
|
+
def _tool_params():
|
|
44
|
+
with open(TOOLS_JSON, "r") as tool_json_file:
|
|
45
|
+
bera_tools = json.load(tool_json_file)
|
|
46
|
+
return {
|
|
47
|
+
tool["tool_api"]: tool.get("parameters", [])
|
|
48
|
+
for toolbox in bera_tools["toolbox"]
|
|
49
|
+
for tool in toolbox["tools"]
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def linkable_inputs(tool_api):
|
|
54
|
+
"""{input variable: kind} for the tool's inputs that can come from an earlier step."""
|
|
55
|
+
return {p["variable"]: p["accepts"] for p in _tool_params().get(tool_api, []) if p.get("accepts")}
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def produced_kinds(tool_api):
|
|
59
|
+
"""{kind: (output variable, file patterns)} for what the tool writes."""
|
|
60
|
+
kinds = {}
|
|
61
|
+
for p in _tool_params().get(tool_api, []):
|
|
62
|
+
for kind, patterns in p.get("produces", {}).items():
|
|
63
|
+
kinds[kind] = (p["variable"], patterns)
|
|
64
|
+
return kinds
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def new_step(tool_api, args=None, links=None, step_id=None):
|
|
68
|
+
return {
|
|
69
|
+
"id": step_id or uuid.uuid4().hex[:8],
|
|
70
|
+
"tool_api": tool_api,
|
|
71
|
+
"args": dict(args or {}),
|
|
72
|
+
"links": dict(links or {}),
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def step_index(steps, step_id):
|
|
77
|
+
for i, step in enumerate(steps):
|
|
78
|
+
if step["id"] == step_id:
|
|
79
|
+
return i
|
|
80
|
+
return None
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def link_sources(steps, kind):
|
|
84
|
+
"""Steps (from the given list) that produce kind."""
|
|
85
|
+
return [step for step in steps if kind in produced_kinds(step["tool_api"])]
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def default_links(steps, tool_api):
|
|
89
|
+
"""Link each of the tool's linkable inputs to the latest step (from steps) that produces its kind."""
|
|
90
|
+
links = {}
|
|
91
|
+
for variable, kind in linkable_inputs(tool_api).items():
|
|
92
|
+
sources = link_sources(steps, kind)
|
|
93
|
+
if sources:
|
|
94
|
+
links[variable] = sources[-1]["id"]
|
|
95
|
+
return links
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def relink(steps):
|
|
99
|
+
"""Reset every step's links to the defaults for its place in the list.
|
|
100
|
+
|
|
101
|
+
Called after adding, moving or removing a step.
|
|
102
|
+
"""
|
|
103
|
+
for i, step in enumerate(steps):
|
|
104
|
+
step["links"] = default_links(steps[:i], step["tool_api"])
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def validate_pipeline(steps):
|
|
108
|
+
"""Return a list of problems with the step links; empty when the pipeline can run."""
|
|
109
|
+
errors = []
|
|
110
|
+
if not steps:
|
|
111
|
+
errors.append("The pipeline has no steps.")
|
|
112
|
+
for n, step in enumerate(steps, 1):
|
|
113
|
+
accepts = linkable_inputs(step["tool_api"])
|
|
114
|
+
for variable, source_id in step["links"].items():
|
|
115
|
+
i = step_index(steps, source_id)
|
|
116
|
+
if variable not in accepts:
|
|
117
|
+
errors.append(f"Step {n}: '{variable}' can't be linked to another step.")
|
|
118
|
+
elif i is None:
|
|
119
|
+
errors.append(f"Step {n}: the step '{variable}' reads from no longer exists.")
|
|
120
|
+
elif i >= n - 1:
|
|
121
|
+
errors.append(f"Step {n}: '{variable}' reads the output of step {i + 1}, which runs after it.")
|
|
122
|
+
elif accepts[variable] not in produced_kinds(steps[i]["tool_api"]):
|
|
123
|
+
errors.append(f"Step {n}: step {i + 1} doesn't produce the {accepts[variable]} that '{variable}' needs.")
|
|
124
|
+
else:
|
|
125
|
+
out_variable, _ = produced_kinds(steps[i]["tool_api"])[accepts[variable]]
|
|
126
|
+
if not steps[i]["args"].get(out_variable):
|
|
127
|
+
errors.append(f"Step {n}: step {i + 1} has no '{out_variable}' set.")
|
|
128
|
+
return errors
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
def missing_inputs(steps):
|
|
132
|
+
"""Return a problem for each input file or folder a step reads that doesn't exist.
|
|
133
|
+
|
|
134
|
+
Linked inputs are skipped, since an earlier step writes them while the pipeline runs.
|
|
135
|
+
"""
|
|
136
|
+
errors = []
|
|
137
|
+
for n, step in enumerate(steps, 1):
|
|
138
|
+
for p in _tool_params().get(step["tool_api"], []):
|
|
139
|
+
variable = p.get("variable")
|
|
140
|
+
if p.get("type") not in ("file", "directory") or p.get("output") or variable in step["links"]:
|
|
141
|
+
continue
|
|
142
|
+
value = step["args"].get(variable)
|
|
143
|
+
if not value or not isinstance(value, str):
|
|
144
|
+
continue
|
|
145
|
+
path = value.rsplit("|", 1)[0] # vector inputs are stored as path|layer
|
|
146
|
+
if not Path(path).exists():
|
|
147
|
+
label = p.get("label", variable).rstrip(".")
|
|
148
|
+
errors.append(f"Step {n}: {label} not found: {path}")
|
|
149
|
+
return errors
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def describe_link(steps, step, variable):
|
|
153
|
+
"""What a linked input will read, e.g. 'D:/rasters/Merged_*_DTM.tif', for showing in the GUI."""
|
|
154
|
+
source = steps[step_index(steps, step["links"][variable])]
|
|
155
|
+
out_variable, patterns = produced_kinds(source["tool_api"])[linkable_inputs(step["tool_api"])[variable]]
|
|
156
|
+
folder = source["args"].get(out_variable, "")
|
|
157
|
+
return Path(folder, patterns[0]).as_posix() if patterns and folder else folder
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def resolve_link(steps, step, variable):
|
|
161
|
+
"""The path a linked input reads; call it after the source step has run."""
|
|
162
|
+
source_index = step_index(steps, step["links"][variable])
|
|
163
|
+
source = steps[source_index]
|
|
164
|
+
out_variable, patterns = produced_kinds(source["tool_api"])[linkable_inputs(step["tool_api"])[variable]]
|
|
165
|
+
folder = source["args"][out_variable]
|
|
166
|
+
if not patterns:
|
|
167
|
+
return folder
|
|
168
|
+
|
|
169
|
+
for pattern in patterns:
|
|
170
|
+
matches = sorted(Path(folder).glob(pattern), key=lambda f: f.stat().st_mtime, reverse=True)
|
|
171
|
+
if matches:
|
|
172
|
+
return matches[0].as_posix()
|
|
173
|
+
raise FileNotFoundError(
|
|
174
|
+
f"Step {source_index + 1} wrote no file matching {' or '.join(patterns)} in {folder} for '{variable}'"
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def resolve_step_args(steps, index):
|
|
179
|
+
"""Step arguments with the linked inputs filled in from the earlier steps' outputs."""
|
|
180
|
+
step = steps[index]
|
|
181
|
+
args = dict(step["args"])
|
|
182
|
+
for variable in step["links"]:
|
|
183
|
+
args[variable] = resolve_link(steps, step, variable)
|
|
184
|
+
return args
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
def stringify_args(args):
|
|
188
|
+
# Same conversion as the GUI's Run button: the tools validate and convert string values.
|
|
189
|
+
return {key: value if isinstance(value, str) else str(value) for key, value in args.items()}
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
def save_pipeline(path, steps):
|
|
193
|
+
with open(path, "w") as pipeline_file:
|
|
194
|
+
json.dump({"format": PIPELINE_FORMAT, "steps": steps}, pipeline_file, indent=4)
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def load_pipeline(path):
|
|
198
|
+
with open(path, "r") as pipeline_file:
|
|
199
|
+
data = json.load(pipeline_file)
|
|
200
|
+
file_format = data.get("format")
|
|
201
|
+
if file_format != PIPELINE_FORMAT:
|
|
202
|
+
raise ValueError(f"Unsupported pipeline format: {file_format}")
|
|
203
|
+
|
|
204
|
+
return [new_step(s["tool_api"], s.get("args"), s.get("links"), s.get("id")) for s in data.get("steps", [])]
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def tool_command(tool_api, args, processes, call_mode="CLI"):
|
|
208
|
+
"""Arguments to run one tool script in its own process, as the GUI does."""
|
|
209
|
+
return [
|
|
210
|
+
(TOOLS_DIR / f"{tool_api}.py").as_posix(),
|
|
211
|
+
"-i", json.dumps(stringify_args(args)),
|
|
212
|
+
"-p", str(processes),
|
|
213
|
+
"-c", call_mode,
|
|
214
|
+
"-l", "INFO",
|
|
215
|
+
] # fmt: skip
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
def run_pipeline(steps, processes=0):
|
|
219
|
+
errors = validate_pipeline(steps) + missing_inputs(steps)
|
|
220
|
+
if errors:
|
|
221
|
+
raise ValueError("\n".join(errors))
|
|
222
|
+
|
|
223
|
+
# the tool scripts import beratools, which may be run from a source checkout rather than installed
|
|
224
|
+
env = dict(os.environ)
|
|
225
|
+
env["PYTHONPATH"] = os.pathsep.join(filter(None, [PACKAGE_ROOT.as_posix(), env.get("PYTHONPATH")]))
|
|
226
|
+
|
|
227
|
+
for i, step in enumerate(steps):
|
|
228
|
+
print("\n" + "-" * 50)
|
|
229
|
+
print(f"Step {i + 1}/{len(steps)}: {step['tool_api']}")
|
|
230
|
+
print("-" * 50, flush=True)
|
|
231
|
+
args = resolve_step_args(steps, i)
|
|
232
|
+
result = subprocess.run([sys.executable] + tool_command(step["tool_api"], args, processes), env=env)
|
|
233
|
+
if result.returncode != 0:
|
|
234
|
+
raise RuntimeError(f"Step {i + 1} ({step['tool_api']}) failed with exit code {result.returncode}")
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def main():
|
|
238
|
+
parser = argparse.ArgumentParser(description="Run a pipeline saved from the BERA Tools GUI.")
|
|
239
|
+
parser.add_argument("pipeline_file", help="pipeline .json file")
|
|
240
|
+
parser.add_argument("--processes", type=int, default=0, help="worker count (0 = all CPU cores)")
|
|
241
|
+
args = parser.parse_args()
|
|
242
|
+
|
|
243
|
+
start_time = time.time()
|
|
244
|
+
run_pipeline(load_pipeline(args.pipeline_file), args.processes)
|
|
245
|
+
print(f"Pipeline done in {round(time.time() - start_time, 1)} seconds")
|
|
246
|
+
|
|
247
|
+
|
|
248
|
+
if __name__ == "__main__":
|
|
249
|
+
main()
|
|
@@ -14,6 +14,7 @@
|
|
|
14
14
|
"parameters": [
|
|
15
15
|
{
|
|
16
16
|
"variable": "in_line",
|
|
17
|
+
"accepts": "line",
|
|
17
18
|
"label": "Seed Line",
|
|
18
19
|
"description": "Input seed line file.",
|
|
19
20
|
"type": "file",
|
|
@@ -52,6 +53,9 @@
|
|
|
52
53
|
},
|
|
53
54
|
{
|
|
54
55
|
"variable": "out_line",
|
|
56
|
+
"produces": {
|
|
57
|
+
"line": []
|
|
58
|
+
},
|
|
55
59
|
"label": "Output Seed Line",
|
|
56
60
|
"description": "Output Seed Line file.",
|
|
57
61
|
"type": "file",
|
|
@@ -336,6 +340,7 @@
|
|
|
336
340
|
"parameters": [
|
|
337
341
|
{
|
|
338
342
|
"variable": "in_line",
|
|
343
|
+
"accepts": "line",
|
|
339
344
|
"label": "Input Line",
|
|
340
345
|
"description": "Input lines to optimize.",
|
|
341
346
|
"type": "file",
|
|
@@ -387,6 +392,9 @@
|
|
|
387
392
|
},
|
|
388
393
|
{
|
|
389
394
|
"variable": "out_line",
|
|
395
|
+
"produces": {
|
|
396
|
+
"line": []
|
|
397
|
+
},
|
|
390
398
|
"label": "Optimized Line",
|
|
391
399
|
"description": "Output optimized line file.",
|
|
392
400
|
"type": "file",
|
|
@@ -407,6 +415,7 @@
|
|
|
407
415
|
"parameters": [
|
|
408
416
|
{
|
|
409
417
|
"variable": "in_line",
|
|
418
|
+
"accepts": "line",
|
|
410
419
|
"label": "Seed Line",
|
|
411
420
|
"description": "Input seed lines.",
|
|
412
421
|
"type": "file",
|
|
@@ -667,6 +676,9 @@
|
|
|
667
676
|
},
|
|
668
677
|
{
|
|
669
678
|
"variable": "out_line",
|
|
679
|
+
"produces": {
|
|
680
|
+
"line": []
|
|
681
|
+
},
|
|
670
682
|
"label": "Output Centerline",
|
|
671
683
|
"description": "Output centerline file.",
|
|
672
684
|
"type": "file",
|
|
@@ -687,6 +699,7 @@
|
|
|
687
699
|
"parameters": [
|
|
688
700
|
{
|
|
689
701
|
"variable": "in_line",
|
|
702
|
+
"accepts": "line",
|
|
690
703
|
"label": "Centerline",
|
|
691
704
|
"description": "Input centerline file.",
|
|
692
705
|
"type": "file",
|
|
@@ -879,6 +892,9 @@
|
|
|
879
892
|
},
|
|
880
893
|
{
|
|
881
894
|
"variable": "out_footprint",
|
|
895
|
+
"produces": {
|
|
896
|
+
"footprint": []
|
|
897
|
+
},
|
|
882
898
|
"label": "Output Footprint",
|
|
883
899
|
"description": "Output footprint polygon file.",
|
|
884
900
|
"type": "file",
|
|
@@ -899,6 +915,7 @@
|
|
|
899
915
|
"parameters": [
|
|
900
916
|
{
|
|
901
917
|
"variable": "in_line",
|
|
918
|
+
"accepts": "line",
|
|
902
919
|
"label": "Centerline",
|
|
903
920
|
"description": "Input line file.",
|
|
904
921
|
"type": "file",
|
|
@@ -939,6 +956,9 @@
|
|
|
939
956
|
},
|
|
940
957
|
{
|
|
941
958
|
"variable": "out_footprint",
|
|
959
|
+
"produces": {
|
|
960
|
+
"footprint": []
|
|
961
|
+
},
|
|
942
962
|
"label": "Output Footprint",
|
|
943
963
|
"description": "Output footprint polygons.",
|
|
944
964
|
"type": "file",
|
|
@@ -949,6 +969,9 @@
|
|
|
949
969
|
},
|
|
950
970
|
{
|
|
951
971
|
"variable": "out_centerline",
|
|
972
|
+
"produces": {
|
|
973
|
+
"line": []
|
|
974
|
+
},
|
|
952
975
|
"label": "Output Centerline",
|
|
953
976
|
"description": "Output centerline polygons.",
|
|
954
977
|
"type": "file",
|
|
@@ -1040,6 +1063,7 @@
|
|
|
1040
1063
|
"parameters": [
|
|
1041
1064
|
{
|
|
1042
1065
|
"variable": "in_line",
|
|
1066
|
+
"accepts": "line",
|
|
1043
1067
|
"label": "Centerline",
|
|
1044
1068
|
"description": "Input centerline file.",
|
|
1045
1069
|
"type": "file",
|
|
@@ -1050,6 +1074,7 @@
|
|
|
1050
1074
|
},
|
|
1051
1075
|
{
|
|
1052
1076
|
"variable": "in_footprint",
|
|
1077
|
+
"accepts": "footprint",
|
|
1053
1078
|
"label": "Input Footprint",
|
|
1054
1079
|
"description": "Input footprint.",
|
|
1055
1080
|
"type": "file",
|
|
@@ -1091,6 +1116,9 @@
|
|
|
1091
1116
|
},
|
|
1092
1117
|
{
|
|
1093
1118
|
"variable": "out_footprint",
|
|
1119
|
+
"produces": {
|
|
1120
|
+
"footprint": []
|
|
1121
|
+
},
|
|
1094
1122
|
"label": "Output Ground Footprint",
|
|
1095
1123
|
"description": "Output fixed width ground footprint file.",
|
|
1096
1124
|
"type": "file",
|
|
@@ -749,7 +749,10 @@ class BTData(object):
|
|
|
749
749
|
def _set_param_type_for_output(self, single_param, param):
|
|
750
750
|
# Assume subtype is already a list
|
|
751
751
|
subtypes = param["subtype"]
|
|
752
|
-
|
|
752
|
+
if param["type"] == "directory":
|
|
753
|
+
single_param["parameter_type"] = {"Directory": subtypes}
|
|
754
|
+
else:
|
|
755
|
+
single_param["parameter_type"] = {"NewFile": subtypes}
|
|
753
756
|
|
|
754
757
|
def _find_tool_params(self, tool_name):
|
|
755
758
|
"""
|
|
@@ -810,6 +813,9 @@ class BTData(object):
|
|
|
810
813
|
def _validate_number(self, value, param_def):
|
|
811
814
|
"""Validate and convert numeric types (int, float)."""
|
|
812
815
|
subtype = param_def.get("subtype", "")
|
|
816
|
+
if isinstance(subtype, list):
|
|
817
|
+
subtype = subtype[0] if subtype else ""
|
|
818
|
+
subtype = str(subtype).strip().lower()
|
|
813
819
|
variable = param_def.get("variable", "unknown")
|
|
814
820
|
|
|
815
821
|
if subtype == "int":
|
|
@@ -914,6 +920,9 @@ class BTData(object):
|
|
|
914
920
|
def _validate_list(self, value, param_def):
|
|
915
921
|
"""Validate list parameter with allowed values and type conversion."""
|
|
916
922
|
subtype = param_def.get("subtype", "")
|
|
923
|
+
if isinstance(subtype, list):
|
|
924
|
+
subtype = subtype[0] if subtype else ""
|
|
925
|
+
subtype = str(subtype).strip().lower()
|
|
917
926
|
variable = param_def.get("variable", "unknown")
|
|
918
927
|
allowed_values = param_def.get("data", [])
|
|
919
928
|
|