GRASS GIS – How to Execute GRASS Module Directly on Geodata File via Command Line

grass-gis

I have a geopackage of lines that I want to run v.clean on. I have GRASS 7.8 installed:

me@server:~/grass_proc$ grass78 -v
GRASS GIS 7.8.2

Geographic Resources Analysis Support System (GRASS) is Copyright,
1999-2019 by the GRASS Development Team, and licensed under terms of the
GNU General Public License (GPL) version >=2.

This GRASS GIS 7.8.2 release is coordinated and produced by
the GRASS Development Team with contributions from all over the world.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

I am working on a remove server via SSH. I can figure out how to do interactively, by following these steps:

  1. Run grass78
  2. Create a workspace
  3. Close the GUI (which somehow is able to be used via SSH)
  4. Run these commands, in this order:

.

v.import input=network_to_clean.gpkg output=network_to_clean
v.clean input=network_to_clean output=cleaned_network tool=break
v.out.ogr input=cleaned_network output=cleaned_network.gpkg format=GPKG

It works fine. But I would like to do this all automatically, without having to create a workspace beforehand, literally just run a single script which only takes network_to_clean.gpkg. It could be a shell script, I don't really care. But how can I do this without having to use the GUI to set the workspace? And can it be cleaned up automatically (i.e. delete the workspace afterwards)?

Best Answer

THis is certainly possible, and built into GRASS by taking advantage of an envivironment variable GRASS_BATCH_JOB. The procedure is explained quite well in the GRASS Wiki.

In short, you create a temporary GRASS Location, choosing the coordinate reference system from the existing geopackage file for reference. Then you define the GRASS_BATCH_JOB environ variable, pointing it to the script to be run. Then start GRASS in that temp Location. The script will run, doing whatever commands you entered into the batch script, then GRASS exists. And finally just remove that environ variable (and the temp location).

Related Question