Re: [GRASS-user] using "not so common characters" in Python Script
Olá Ricardo :) Thanks for the feedback. Just one last question: is there any known limitation for using this encoding in Grass Python Scripts? ( # -*- coding: utf-8 -*- Thanks Antonio __ Information from ESET NOD32 Antivirus, version of virus signature database 6133 (20110518) __ The message was checked by ESET NOD32 Antivirus. http://www.eset.com ___ grass-user mailing list grass-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-user
Re: [GRASS-user] Re: Using temp files in Grass Script
Original-Nachricht > Datum: Thu, 19 May 2011 07:11:47 +0100 > Von: Glynn Clements > An: "Johannes Radinger" > CC: grass-user@lists.osgeo.org > Betreff: Re: [GRASS-user] Re: Using temp files in Grass Script > > Johannes Radinger wrote: > > > > Using an exit handler ensures that the temporary map gets removed > > > regardless of how the script terminates (e.g. if it terminates due to > > > an exception). > > > > Thank you for your nice examples how to do that with tmp files and the > exit handler. > > > > Just some questions: > > > > 1) is the PID also used on windows systems? so can it be integrated > > in a script which also windows users want to use? > > Yes. > > > Is it correct that the PID is the same value for running the whole > > script one time? > > Yes. > > > 2) what exactly does the "global tmpmap"? > > First, it should have been "global tmp_map", not "global tmpmap". > > "global" declares a variable as global, so that any assignment to that > variable within the function modifies the global variable rather than > creating a local variable with that name. > > In order to assign a global variable from within a function, it must > be explicitly declared "global". A "global" statement isn't required > to read a global variable. > > In this case, tmp_map must be global so that the name generated within > main() can be used from within cleanup(). > > > 3)I've got a lot of tmp-files which will be created during the > > process, so is there and option to tell the g.remove that all maps > > containing .tmp.%d' % os.getpid() in the end should be removed > > instead of typing all the tmp map files into the list of g.remove. > > You could use g.mremove to remove multiple maps based upon a pattern, > but I would recommend specifying all of the temporary maps explicitly. > > If an option for a GRASS command accepts multiple values (e.g. all of > g.remove's options), you can pass a Python list via > grass.run_command(), e.g.. > > grass.run_command('g.remove', rast = [tmp1, tmp2, tmp3]) > > > 4) so the whole thing works that in the end all the tmp maps are > > deleted after the processing of the script and after an exception > > etc. > > An exit handler registered using atexit.register() will be run when > the script terminates, whether due to reaching the end of the script, > calling sys.exit(), an uncaught exception, Ctrl-C/Ctrl-Z, etc. > > The only situation where it won't be run is if the Python interpreter > crashes (or is terminated with SIGKILL on Unix, etc), which can't > realistically be handled by any means. > > -- > Glynn Clements > ___ > grass-user mailing list > grass-user@lists.osgeo.org > http://lists.osgeo.org/mailman/listinfo/grass-user Okay I think I managed to do everything according to your really good explanation (Thank you!). Here how I implemented the tmp maps including the PID and deleting it via the cleanup and a list containing all tmp maps: tmp_map_rast = None tmp_map_vect = None def cleanup(): if (tmp_map_rast or tmp_map_vect) and not flags['a']: grass.run_command("g.remove", rast = tmp_map_rast, vect = tmp_map_vect, quiet = True) def main(): #global variables for cleanup global tmp_map_rast tmp_map_rast = [f + str(os.getpid()) for f in['rast1_tmp_', 'rast2_tmp_', 'rast3_tmp_']] global tmp_map_vect tmp_map_vect = [f + str(os.getpid()) for f in['vect1_tmp_', 'vect2_tmp_', 'vect3_tmp_']] if __name__ == "__main__": options, flags = grass.parser() atexit.register(cleanup) sys.exit(main()) I tried it once and it worked! I hope it a good way how i did it, but probably there is always something to make better. Anyway thanks a lot! /Johannes -- NEU: FreePhone - kostenlos mobil telefonieren! Jetzt informieren: http://www.gmx.net/de/go/freephone ___ grass-user mailing list grass-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-user
[GRASS-user] Installing two versions of grass
Hi all! Currently I have two GRASS Versions - 6.4.2 and 6.5 SVN installed. I download the SVN source code to my home folder and install it to the default location, i.e., /usr/local/. Now there are grass-6.4.2svn and grass-6.5.svn in my /usr/local folder and allied executables in /usr/local/bin as grass64 and grass65. Is it safe to have both of them in this way or will there be conflicts when using gdal or such libraries. So far I haven't noticed any conflicts. Thanks and regards, Chethan S. ___ grass-user mailing list grass-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-user
Re: [GRASS-user] Manual to own python script
Original-Nachricht > Datum: Mon, 16 May 2011 13:56:10 +0200 > Von: Markus Neteler > An: Johannes Radinger > CC: GRASS user list > Betreff: Re: [GRASS-user] Manual to own python script > On Mon, May 16, 2011 Markus Neteler: > >> On Thu, May 12, 2011 at 2:17 PM, Johannes Radinger > ... > >> If you use a Makefile to install it, the approach as used for the > >> existing Python > >> scripts should work. Of course your script must use the GRASS parser. > > > > But there is no makefile needed to "install" a python script. It is just > necessary to copy > > the *.py into the /bin folder and the script can be called with *.py. > > Sure. But then you don't get the advantage of getting the doc > autogenerated which > is done by the Makefile. > > > Anyhow how would and installation with an makefile work? > > Just deposit the html file into the same directory. > GRASS 6: name as "description.html" > GRASS 7: name as ".html" > > > the makefile of python scripts contain only little information, > > Yes, we tried to keep it minimalistic. > > > I tried to use the r.buffer makefile and adapt it to my script: > > > > 1 MODULE_TOPDIR = ../.. > > 2 > > 3 PGM = r.fidimo.py > > 4 > > 5 include $(MODULE_TOPDIR)/include/Make/Script.make > > 6 > > 7 default: script > > Perfect. > > > > but what now?? How is that connected to the .html file? > > The magic is in $(MODULE_TOPDIR)/include/Make/Script.make if you want to > see how it works. It discovers the HTML file and generates the manual page > when running > make > > > how do I have to install my script with the makefile (that is very new > to me)? > > I suspect that you are already done. > Please take a look at > > grass70/scripts// > > e.g. > http://trac.osgeo.org/grass/browser/grass/trunk/scripts/ > or in your local copy. Hello I am still not sure how to install the script. I've now a folder called MyScript including: * Makefile * MyScript.py * MyScript.py.html What should I do with this folder? I tried to put the folder into the bin directory but then I can't call the module anymore (nothing happens). Just for your information I work on Mac OSX and GRASS GIS 6.5SVN. I know it from the r.stream add-ons, I had to compile them and then put the add-ons manually into the bin folder and the html files into the html folder. But how to do it with my pyhton script? /johannes > > Markus > ___ > grass-user mailing list > grass-user@lists.osgeo.org > http://lists.osgeo.org/mailman/listinfo/grass-user -- NEU: FreePhone - kostenlos mobil telefonieren! Jetzt informieren: http://www.gmx.net/de/go/freephone ___ grass-user mailing list grass-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-user
[GRASS-user] Using a r.mapcalc expression in grass.mapcalc
I need to use this expression output=@input (input has labels and I want to export them so i need to do this). How can I do this in grass.mapcalc in Python? Does it accept @? Thanks ___ grass-user mailing list grass-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-user
Re: [GRASS-user] Using grass.run_commmand
Luisa Peña wrote: > I'm using grass.run_command and I have one question: besides setting flags, > parameters can I also get error and output from that function like > p=grass.run_command("r.out.xyz", parameters=X, flags="f...", stdout=output, > stderr=error) Use read_command or pipe_command. read_command returns the command's output (stdout) as a string. pipe_command returns the Popen object; read from its .stdout member. Call the .wait() method when you're done with it. For details of Popen objects, see: http://docs.python.org/library/subprocess.html#popen-objects The most general function is start_command, upon which all of the *_command functions (except for exec_command) are built. Again, this function returns the Popen object. Refer to the source code ($GISBASE/etc/python/grass/script/core.py) for more details. Most of those functions are only a few lines long. -- Glynn Clements ___ grass-user mailing list grass-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-user
Re: [GRASS-user] Installing two versions of grass
Chethan S. wrote: > Currently I have two GRASS Versions - 6.4.2 and 6.5 SVN installed. I > download the SVN source code to my home folder and install it to the default > location, i.e., /usr/local/. Now there are grass-6.4.2svn and grass-6.5.svn > in my /usr/local folder and allied executables in /usr/local/bin as grass64 > and grass65. Is it safe to have both of them in this way Yes. -- Glynn Clements ___ grass-user mailing list grass-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-user
[GRASS-user] Re: Is it possible to export labels in image?
Hi GIonanni Suggested me to use r.mapcalc new=@old_with_labels but I get a map with Nulls. Is there any way to export Labels? THanks On Fri, May 13, 2011 at 8:44 AM, Luis Lisboa wrote: > Greetings I have an raster file with labels (used r.reclass). Is it > possible to export (r.out.gdal in geotiff format) it with the labels? > Thanks > Luis > ___ grass-user mailing list grass-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-user
Re: [GRASS-user] Using a r.mapcalc expression in grass.mapcalc
Jenny Turner wrote: > I need to use this expression output=@input (input has labels and I want to > export them so i need to do this). How can I do this in grass.mapcalc in > Python? Does it accept @? grass.mapcalc() is just a wrapper around r.mapcalc, so it supports whatever r.mapcalc supports. This: grass.mapcalc('output = @input') will work, but you wouldn't normally hard-code map names in a script. grass.mapcalc() uses Python's string.Template class to simplify using variables in expressions. This: grass.mapcalc('$output = @$input', output = 'output', input = 'input') is equivalent to the previous expression, but you can easily replace the hard-coded names with variables, e.g.: grass.mapcalc('$output = @$input', output = output, input = input) Passing parameters which aren't used in the string is harmless, so you can use Python's "**" syntax to pass a dictionary of parameters, e.g. the "options" dictionary obtained from grass.parser(): grass.mapcalc('$output = @$input', **options) The values of the output= and input= options will be substituted into the expression. -- Glynn Clements ___ grass-user mailing list grass-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-user
Re: [GRASS-user] Re: Is it possible to export labels in image?
Hi Luis, > GIonanni Suggested me to use r.mapcalc new=@old_with_labels but I get > a map with Nulls. > Is there any way to export Labels? sorry, it works for me here. Any place where to download a sample of your rasters and give a try here? Cheers -- Giovanni -- ___ grass-user mailing list grass-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-user
[GRASS-user] RE: grass-user Digest, Vol 61, Issue 42 [SEC=UNCLASSIFIED]
> Date: Wed, 18 May 2011 14:53:25 +0200 > From: "Johannes Radinger" > Subject: Re: [GRASS-user] Re: Using temp files in Grass Script > To: Glynn Clements > Cc: grass-user@lists.osgeo.org > Message-ID: <20110518125325.274...@gmx.net> > Content-Type: text/plain; charset="utf-8" > > > Original-Nachricht > > Datum: Tue, 17 May 2011 13:11:41 +0100 > > Von: Glynn Clements > > An: "Johannes Radinger" > > CC: grass-user@lists.osgeo.org > > Betreff: Re: [GRASS-user] Re: Using temp files in Grass Script > > > > > Johannes Radinger wrote: > > > > > I found that thread about temporary maps... > > > > > > e.g in my case in my python script i use: > > > > > > #Convert river from vector to raster format > > > grass.run_command("v.to.rast", > > > input = "river_gen", > > > overwrite = True, > > > output = "river_raster", > > > use = "val", > > > value = res) > > > > > > #Thinning the rastarized river > > > grass.run_command("r.thin", > > > input = "river_raster", > > > overwrite = True, > > > output = "river_raster_thin") > > > > > > and here is the map river_raster only temporary for the thinning > > > process and the river_raster_thin is used for further calculations. > > > At the moment I have a g.remove to remove the river raster after > > > the thinning process but should I handle that with temporary files/maps > > > and if yes how is that exactly done in my python script? > > > > 1. In general, you should make an attempt at ensuring that temporary > > map names won't conflict with any existing map name. The usual > > approach is to include both ".tmp" and the PID in the map name, e.g.: > > > > import os > > ... > > global tmpmap > > tmp_map = 'river_raster.tmp.%d' % os.getpid() > > > > 2. Deletion should ideally be handled using an exit handler, e.g.: > > > > import os > > import atexit > > import grass.script as grass > > > > tmp_map = None > > > > def cleanup(): > > if tmp_map: > > grass.run_command('g.remove', rast = tmp_map, quiet = > True) > > > > def main(): > > global tmpmap > > tmp_map = 'river_raster.tmp.%d' % os.getpid() > > ... > > > > if __name__ == "__main__": > > options, flags = grass.parser() > > atexit.register(cleanup) > > main() > > > > Using an exit handler ensures that the temporary map gets removed > > regardless of how the script terminates (e.g. if it terminates due to > > an exception). > > Thank you for your nice examples how to do that with tmp files and the exit > handler. > > Just some questions: > 1) is the PID also used on windows systems? so can it be integrated in a > script > which also windows users want to use? Is it correct that the PID is the same > value for running the whole script one time? > > 2) what exactly does the "global tmpmap"? > > 3)I've got a lot of tmp-files which will be created during the process, so is > there and option to tell the g.remove that all maps containing .tmp.%d' % > os.getpid() in the end should be removed instead of typing all the tmp map > files into the list of g.remove. > > 4) so the whole thing works that in the end all the tmp maps are deleted > after the processing of the script and after an exception etc. Rather than use a simple global variable as per Glynn's example, use a list: tmp_map = [] Any time you create a temporary file name, append it to the list: tmp_fn = '...' tmp_map.append(tmp_fn) In your cleanup handler, scan the list: def cleanup(): while tmp_map: tfn = tmp_map.pop() grass.run_command('g.remove', rast = tfn, quiet = True) Using a list like this removes the need for the global statements, as you only manipulate the list rather than assign new objects to the global variable. -> "These thoughts are mine alone!" <- Andrew MacIntyre Operations Branch tel: +61 2 6219 5356 Communications Infrastructure Division fax: +61 2 6253 3277 Australian Communications & Media Authority email: andrew.macint...@acma.gov.auhttp://www.acma.gov.au/ NOTICE: This email message is for the sole use of the intended recipient(s) and may contain confidential and privileged information. Any unauthorized review, use, disclosure or distribution is prohibited. If you are not the intended recipient, please contact the sender by reply email and destroy all copies of the original message. ___ grass-user mailing list grass-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-user
[GRASS-user] grass standalone
how to run a custom grass viewer outside grass shell eg python gui_modules/render.py but it gives me error Initializing... Traceback (most recent call last): File "grass-qt/render.py", line 1308, in image = map.Render(force = True) File "grass-qt/render.py", line 865, in Render os.environ["GRASS_REGION"] = self.SetRegion(windres) File "grass-qt/render.py", line 697, in SetRegion region = self.AdjustRegion() File "grass-qt/render.py", line 484, in AdjustRegion mapwidth= abs(self.region["e"] - self.region["w"]) KeyError: 'e' Exception AttributeError: "'NoneType' object has no attribute 'GetCmdString'" in > ignored Exception AttributeError: "'NoneType' object has no attribute 'GetCmdString'" in > ignored -- Thanks && Regards Rashad ___ grass-user mailing list grass-user@lists.osgeo.org http://lists.osgeo.org/mailman/listinfo/grass-user