Files
agent-profiles/suites-help/开发者指南/脚本开发指南.md
T

56 lines
1.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 脚本开发指南
## 脚本约定
`scripts/run.py` 通过环境变量获取参数,通过 stdout 输出 JSON 结果。
### 接收参数
```python
import json, os
params_file = os.environ.get("PARAMS_FILE", "/tmp/params.json")
with open(params_file) as f:
params = json.load(f)
expression = params.get("expression", "1+1")
precision = int(params.get("precision", 2))
```
### 输出结果
```python
result = {"status": "ok", "result": 42, "output_path": "/tmp/output/result.geojson"}
print(json.dumps(result))
```
stdout 的 JSON 会被执行器捕获并作为步骤结果。
### 文件输出
- 输出文件写入 `/tmp/output/`(步骤间共享)
- 执行结束后脚本目录自动清理
## 执行环境
- **基础镜像**`gis-base:latest`(从 Gitea Packages 加载 tar.gz
- **包含**Python 3.11, GDAL, Shapely, GeoPandas, Fiona, PyProj, Rasterio, numpy
- **脚本路径**`/tmp/scripts/run.py`(只读)
- **工作目录**`/tmp/output`(读写,步骤间共享)
- **参数文件**`/tmp/params.json`
## 错误处理
退出码非 0 表示失败:
```python
print(json.dumps({"status": "error", "message": "文件不存在"}))
sys.exit(1)
```
## 开发建议
1. **输出明确**:结果 JSON 包含关键信息
2. **错误友好**:失败信息写清楚原因
3. **使用 GDAL**:GIS 文件处理优先使用 GDAL 命令行工具