TransWikia.com

Using GRASS i.segment in Python 3 stand alone script on Windows10

Geographic Information Systems Asked on December 13, 2020

I installed GRASS GIS 7.8.1 on Windows 10 using a OSGeo4W.

I would like to use in a stand alone Python 3 script the i.segment GRASS tool.

I saw some related questions (e.g., Using GRASS GIS in Python standalone scripts and call grass modules via python from outside) but they all relevant to Python 2.7, or Linux, or some old versions of GRASS or asking you to install GRASS (which is already installed on my computer via OSGeo4W). I don’t mind using subprocess to exceute the i.segment function or to use it "fully" in my script.

Any easy and straightforward way to achieve that?

One Answer

Standalone Python scripts which run GRASS GIS commands

The "grass-session" Python library is an easy way to control GRASS GIS from "outside"-Python. To install the current version, run:

pip install grass-session

A usage example is also found in the GRASS GIS Wiki, Section:

Python: GRASS GIS 7 with an external library: grass-session

Example for usage of i.segment in a Python script

A Python 3 script (addon) which uses i.segment is i.segment.uspo (Python code) which you may look at for inspiration.

How to write Python GRASS GIS 7 addons

An alternative to "grass-session" is to write a proper Python GRASS GIS 7 addon and then run it through the --exec interface of the GRASS GIS start program grass7 --exec ... like a batch job. The advantage is that GRASS offers a real parser to handle flags and parameters which also generates help texts, the GUI instructions, offers JSON output for REST API and much more.

See for example the tutorial "How to write a Python GRASS GIS 7 addon" for details: https://github.com/wenzeslaus/python-grass-addon

Answered by markusN on December 13, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP