Wednesday, February 27, 2008

My first script...

Here is a script, written together with Moovida, in which I used three JGrass command in sequence to create 2 shapefiles, one withe the subbasins and the other with the numbered stream network.

In the new JGrass console editor it is possible to:
  1. declare variables
  2. use variables in the scripts
  3. put the result of a command in a variable
  4. use string like in Java
  5. ... much more that I don't know jet!! :-)
To declare variables just put the name and all the information needed and at the end of the statement put a ";". An example is below:

The string that identifies the name of the main basin:
basemap = "chiese";

The output folder for the shapefiles I will create:

outputfolder = "/home/moovida/TMP/tmp/";

The name of the input shapefile:
inputmonitoringpoints = "/home/moovida/data/hydrocareworkspace/featuredata/utm/pti_monitoraggio.shp";

To read some maps which are already saved in the location just type the name of these maps, in this case the strings are a little complicated but managing them is very easy in this way:

drain = basemap + "_drain";
net = basemap + "_net_chnum";
pit = "bacino_" + basemap + "_pit";
netflow = basemap + "_net_flow";
nethackstream = basemap + "_net_hstr";


The output variables are initialized with the string name, everything could be added here...

netnumber = basemap + "_netnumber";
basin = basemap + "_basin";
basinoutput = outputfolder + "bacino_" + basemap + ".shp";
netoutput = outputfolder + "rete_" + basemap + ".shp";

And now just use the varibles in the JGrass command lines:

jgrass {
h.netnumbering --mode 2 --igrass-flow $drain --igrass-net $net --ishapefile-pointshape $inputmonitoringpoints
--ograss-netnumber $netnumber --ograss-basin $basin
}



jgrass {
h.basinShape --igrass-pit $pit --igrass-basins $basin --oshapefile-basinshape $basinoutput
}



jgrass {
h.pfafstetter --mode 0 --igrass-flow $netflow --igrass-hacks $nethackstream --igrass-pit $pit --igrass-netnumber $netnumber --oshapefile-netshapeout $netoutput
}



Run the script with the specific RUN option in the right mouse button menu. In the console window you can see some messages to check the progress of the operations.

If someone has other interestig scripts it would be nice to put them here together.





No comments: