pysaga-cmd
PySAGA-cmd is a simple way of running SAGA GIS tools using Python.
Science Score: 67.0%
This score indicates how likely this project is to be science-related based on various indicators:
-
✓CITATION.cff file
Found CITATION.cff file -
✓codemeta.json file
Found codemeta.json file -
✓.zenodo.json file
Found .zenodo.json file -
✓DOI references
Found 1 DOI reference(s) in README -
✓Academic publication links
Links to: zenodo.org -
○Academic email domains
-
○Institutional organization owner
-
○JOSS paper metadata
-
○Scientific vocabulary similarity
Low similarity (12.8%) to scientific vocabulary
Keywords
Repository
PySAGA-cmd is a simple way of running SAGA GIS tools using Python.
Basic Info
Statistics
- Stars: 9
- Watchers: 1
- Forks: 1
- Open Issues: 1
- Releases: 18
Topics
Metadata Files
README.md
PySAGA-cmd
PySAGA-cmd is a simple way of running SAGA GIS tools using Python.
How to download
Binary installers for the latest released version are available at the Python Package Index (PyPI). If you want to have access to extra features (like plotting) you can download the extras as shown in the second command.
sh
pip install PySAGA-cmd
pip install PySAGA-cmd[extras]
How to use the package
Choosing tools
Before you can use this package, you need to locate the saga_cmd in your system. For linux, it can be found somewhere in the /usr/bin/ directory. For Windows, it is usually located in C:\Program Files\SAGA.
Accesing tools can be done with the truediv operator (the forward slash /), like in the example below.
```python from PySAGAcmd import ( SAGA, getsample_dem )
saga = SAGA('/usr/bin/saga_cmd')
Choosing libraries.
preprocessor = saga / 'ta_preprocessor'
Choosing tools.
routedetection = preprocessor / 'Sink Drainage Route Detection' sinkremoval = preprocessor / 'Sink Removal' flowaccumulation = saga / 'tahydrology' / 'Flow Accumulation (Parallelizable)' ```
Executing
Executing an object is straight forward and is done using the execute method. For the SAGA and Library objects no keyword arguments are required. For tools, just provide the required keyword arguments.
```python
Executing the SAGA object. Useful when you want to see the available libraries.
sagaoutput = saga.execute() print(sagaoutput.stdout)
Executing the Library object. Useful when you want to see the available tools.
preprocessoroutput = preprocessor.execute() print(preprocessoroutput.stdout)
Executing a Tool object.
dem = getsampledem() output = 'path/to/output.sdat' output = route_detection.execute(verbose=True, elevation=dem, sinkroute=output) print(output.stdout) ```
Using flags
You can provide flags for SAGA, Library and Tool objects. To see what kind of flags can be used, look at the output of the following.
python
saga.flag = 'help'
print(saga.execute().stdout)
Chaining commands
Chaining commands can be done with PySAGA-cmd with the or operator (the vertical line |). Consider the following example where the goal is to get a hydrologically preprocessed DEM and use that as input for the Flow Accumulation (Parallelizable) tool.
python
pipe = (
route_detection(elevation=dem, sinkroute='temp.sdat') |
sink_removal(dem=route_detection.elevation,
sinkroute=route_detection.sinkroute,
dem_preproc='temp.sdat') |
flow_accumulation(dem=sink_removal.dem_preproc, flow=output)
)
outputs = pipe.execute(verbose=True)
Notice the use of the or operator operator. Also, notice how we can create temporary intermediate files by using temp as the path. This is useful because we didn't care about the sinkroute and dem_preproc grids and we didn't want to save them, we only wanted to use them as input for other tools.
To visualize the temporary files, access the temp_files attribute of SAGA.
python
print(saga.temp_dir)
print(saga.temp_files)
After you are done, don't forget to clean up the temporary folder (if you used temporary files).
python
saga.temp_dir_cleanup()
Plotting
After the execution of a Tool, we can use the returned Output object to plot the results.
The below example can be accessed here.
```python
If you set a flag to the SAGA object that would stop the tool
from working (like 'help'), make sure to remove it before accesing
the tools, like so:
saga.flag = None
import matplotlib.pyplot as plt from matplotlib import gridspec
Defining tools.
slopeaspectcurvature = saga / 'tamorphometry' / 0 # We can also use tool indices to access tools. shading = saga / 'talighting' / 'Analytical Hillshading'
Executing tools.
output1 = slopeaspectcurvature.execute(verbose=True, elevation=dem, slope='temp.sdat') elevation = output1.rasters['elevation'] slope = output1.rasters['slope']
output2 = shading.execute(verbose=True, elevation=dem, shade='temp.sdat', method='5') shading = output2.rasters['shade']
fig = plt.figure(figsize=(15, 10))
gs = gridspec.GridSpec(2, 2, heightratios=[1.5, 1]) ax1 = fig.addsubplot(gs[0]) ax2 = fig.addsubplot(gs[1]) ax3 = fig.addsubplot(gs[2]) ax4 = fig.add_subplot(gs[3])
Maps
elevation.plot(ax=ax1, cmap='terrain', cbarkwargs=dict(label='Elevation (meters)')) slope.plot(ax=ax2, cmap='rainbow', cbarkwargs=dict(label='Radians')) shading.plot(ax=ax1, cbar=False, alpha=0.45) ax1.settitle('Elevation map') ax2.settitle('Slope map')
Histograms
histkwargs = { 'bins': 15, 'alpha': 0.65, 'facecolor': '#2ab0ff', 'edgecolor': '#169acf', 'linewidth': 0.5 } elevation.hist(ax=ax3, **histkwargs) ax3.setylabel('Count') ax3.setxlabel('Elevation') slope.hist(ax=ax4, **histkwargs) ax4.setylabel('Count') ax4.set_xlabel('Radians')
plt.tight_layout() plt.show()
fig.savefig('../media/plot1.png', dpi=300, bbox_inches='tight')
saga.tempdircleanup()
```

For extra information on how to use the package, you can also look at the notebooks inside the examples folder on the Github page.
TODOs
- [x] Implement recursive search for the saga_cmd file.
- [x] Improve the verbose behaviour of tool execution (add a progress bar?).
- [ ] Improve flags.
- [ ] Support the creation of toolchains?
- [x] Add "elapsed time" to progress bar.
Owner
- Login: alecsandrei
- Kind: user
- Repositories: 1
- Profile: https://github.com/alecsandrei
Citation (CITATION.cff)
cff-version: 1.2.0 message: "If you use this software, please cite it as below." authors: - family-names: "Cuvuliuc" given-names: "Alex-Andrei" orcid: "https://orcid.org/0009-0004-7575-2036" title: "PySAGA-cmd" version: 1.0.6 date-released: 2024-02-18 url: "https://github.com/alecsandrei/PySAGA-cmd"
GitHub Events
Total
- Issues event: 2
- Watch event: 4
- Issue comment event: 2
- Fork event: 1
Last Year
- Issues event: 2
- Watch event: 4
- Issue comment event: 2
- Fork event: 1
Packages
- Total packages: 1
-
Total downloads:
- pypi 189 last-month
- Total dependent packages: 0
- Total dependent repositories: 0
- Total versions: 26
- Total maintainers: 1
pypi.org: pysaga-cmd
A package that allows you to run SAGA GIS tools using Python.
- Homepage: https://github.com/alecsandrei/PySAGA-cmd
- Documentation: https://pysaga-cmd.readthedocs.io/
- License: MIT
-
Latest release: 1.2.6
published over 1 year ago