1

I am a novice in writing scripts.

I have a line geometry. Let's say line.shp. I want to smooth it with v.generalize, e.g.

v.generalize input=line output=line_chaiken method=chaiken \
threshold=1 error=roads_chaiken_leftover

What do I have to do to get the things done? Do I somehow have to do in three steps, like

  • Load into GRASS
  • v.generalize
  • Write output

How would these steps fit into a script?

4

In bash, you could do something like the following. I don't have your shapefile so I can't test that these are the exact commands that will work, but the approximate workflow may help you.

# create the region based on the shapefile, and execute script w/ commands
grass70 -c ./line.shp MyMap --exec sh script.sh
# the line above creates MyMap region (and folder on disk).
# to access it in the future, you would launch grass like this:
# grass70 ./MyMap/PERMANENT

In a separate script (named script.sh, with executable bit set) put whatever commands you want:

#!/bin/bash

# load the shapefile
v.in.ogr input=line.shp output=line
# smooth
v.generalize input=line output=line_chaiken method=chaiken
# write output
v.out.ogr input=line_chaiken output=line_chaiken.shp

See https://grass.osgeo.org/grass72/manuals/grass7.html#batch-jobs-with-the-exec-interface (and comment from @markusN)

  • Thanks. Will try it at home. And if I not need the region and the folder after excecuting the processing; would it be possible to tell GRASS to delete them (within the script)? – hoge6b01 Jan 4 '17 at 14:03
  • To simplify even more, you can put all this into a script and run from the grass72 start script as a batch job, see: grass.osgeo.org/grass72/manuals/… – markusN Jan 8 '17 at 14:48
2

I ran your command and it needs more parameters. However, in your Linux system, you don't need to load the layer into GRASS. Assuming that a dataset has been created, first, you have to prepare your system to run adequately the script.

I have in my .bashrc the following lines (in this case for GRASS 6):

export GISBASE="/usr/lib/grass64"
export PATH="$PATH:$GISBASE/bin:$GISBASE/script:$GISBASE/lib"
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$GISBASE/lib"
export GIS_LOCK=$$
export GISRC="$HOME/.grassrc6" 

In my home user, I created a new version of your script (by using a lot of parameters with chaiken algorithm; where my input line was route and my output line test).

v.generalize input=route layer=1 type=line -c type=line method=chaiken threshold=1.0 look_ahead=7 reduction=50 slide=0.5 angle_thresh=3 degree_thresh=0 closeness_thresh=0 betweeness_thresh=0 alpha=1.0 beta=1.0 iterations=1 output=test

For doing executable your script you need:

chmod +x script_name

and for running it:

./script_name

Finally, I have a .grassrc6 file with next lines:

GISDBASE: /home/zeito/datagrass
LOCATION_NAME: utah
MAPSET: jose
GRASS_GUI: wxpython

After running the script at the bash console, resulting layer could be visualized at Map Canvas of QGIS:

enter image description here

It worked.

  • How does you bash script look like now? The parameters given for the algorithm derived from your personal experience? Do I have to make .bashrc and .grassrc6 look like yours? Maybe GRASS7 works like suggested by mankoff? – hoge6b01 Jan 4 '17 at 14:34
  • No, it won't work (mankoff suggestion has an exactly copy of your command ) because I tried out your script in my system and it didn't. I have Debian stable and I should compile GRASS 7 in my system. For this reason, you only have to adapt my GRASS 6 suggestion to your needs. – xunilk Jan 4 '17 at 17:13
  • Thanks. I'm on Mint18 (Ubuntu 16.04) and it seems not to be possible to install a GRASS 6 on it. Could you also provide a GRASS 7 solution, please? – hoge6b01 Jan 5 '17 at 13:07
  • 1
    ... GRASS GIS 7 is much recommended over GRASS 6 anyway (faster, more reliable, more features etc etc). – markusN Jan 8 '17 at 14:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.