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).

-- 
Glynn Clements <[email protected]>
_______________________________________________
grass-user mailing list
[email protected]
http://lists.osgeo.org/mailman/listinfo/grass-user

Reply via email to