Using documentation here: https://grasswiki.osgeo.org/wiki/Working_with_GRASS_without_starting_it_explicitly#Python:_GRASS_GIS_7
I've figured out how to access GRASS modules in Python without explicityly starting GRASS; I set up the environment and run my script in IDLE and all goes well. I'm using GRASS 7 on Linux with Python 2.7.
What I haven't been able to figure out is how to capture the output that GRASS would generate and print to the command console. I would like to print that information to the screen so that I have an indication that things worked. For example, running this command from either the GRASS shell or gui:
gscript.run_command('v.in.ascii', input=pntfile, output='raster_pnts',
Would print this to the console and do the operation:
(Fri Jan 15 16:17:35 2016)
v.in.ascii --overwrite input=/grass_work/output/points_out.txt output=raster_pnts WARNING: Vector map already exists and will be overwritten Scanning input for column types... Number of columns: 3 Number of rows: 93 Importing points... Populating table... Building topology for vector map ... Registering primitives... 93 primitives registered 93 vertices registered Building areas... 0 areas built 0 isles built Attaching islands... Attaching centroids... Number of nodes: 0 Number of primitives: 93 Number of points: 93 Number of lines: 0 Number of boundaries: 0 Number of centroids: 0 Number of areas: 0 Number of isles: 0 (Fri Jan 15 16:17:36 2016) Command finished (0 sec)
When I execute my Python scripts I also want this to print to the screen, but I can't figure out how to do it. I know how to do this with certain commands where strings are returned as output, like:
region = gscript.parse_command('g.region', flags='p')
Would parse and print the region info for me. I know this is different, as I'm trying to capture error messages or progress and not the output itself. I have experimented with some of the examples here:
And have tried using
start_command instead of
run_command to try and capture and print output messages, but I can't get it right (have experimented and searched for several hours).
(I also tried using
subprocess.check_output - I've successfully used that for passing input from Python to the GDAL/OGR command line tools and getting the output messages back, but can't get it working here).