[Gfoss] [GRASS 7] Grass 7 e python script

Ciao ho installato da snv i nuovi sorgenti, effetivamente lo
script di test è cambiato adesso è il seguente (per
chiarezza):

--- ini script
#!/usr/bin/env python

"""
Run within GRASS session
Run this before starting python to append module search
path:

@code
export
PYTHONPATH=$PYTHONPATH:/usr/local/grass-7.0.svn/etc/python/
@endcode

Check with "import sys; sys.path"
or:

@code
sys.path.append("/usr/local/grass-7.0.svn/etc/python")
@endcode
"""

import os, sys
from grass.lib import grass
from grass.lib import raster as grassrast

if not os.environ.has_key("GISBASE"):
    print "You must be in GRASS GIS to run this program."
    sys.exit(1)

if len(sys.argv)==2:
  input = sys.argv[1]
else:
  input = raw_input("Raster Map Name? ")

# initialize
grass.G_gisinit('')

# find map in search path
mapset = grass.G_find_raster2(input, '')

# determine the inputmap type (CELL/FCELL/DCELL) */
data_type = grassrast.Rast_map_type(input, mapset)

infd = grassrast.Rast_open_old(input, mapset)
inrast = grassrast.Rast_allocate_buf(data_type)

rown = 0
while True:
    myrow = grassrast.Rast_get_row(infd, inrast, rown,
data_type)
    print rown, myrow[0:10]
    rown += 1
    if rown == 476:
        break

grassrast.Rast_close(inrast)

--- fine script

anche io ho il medesimo errore "TypeError: 'int' object is
unsubscriptable"

ma questo penso sia dovuto al fatto che la funzione
Rast_get_row restituiscae 1, 0 o -1 e non un array
(http://download.osgeo.org/grass/grass7_progman/raster_2get__row_8c.html#ec254ec7e611f98dd42974e8b7051475)
risolto questo segnalo un successivo errore nella righa
grassrast.Rast_close(inrast) che si risolve chiudendo la
giusta risorsa cioè grassrast.Rast_close(infd).

Premetto che sono un ex utente arcgis passato con estrema
gioia a grass perciò sono ancora molto inesperto su questo
nuovo universo.
Dopo un discreto successo in termini di produttivita lato
user desktop vorrei imparare a fare qualche script... nulla
di faraonico...ma qualcosa del tipo:

apri un raster
crea un nuovo raster
leggi il valore della cella x,y e mettilo in a
leggi il valore della cella x-1,y e mettilo in b
se a>b scrivi 1 nella cella x,y dei un nuovo raster

Mi trovo solo un pò perplesso da non aver trovato troppe
risorse con esempi per quanto rigurada la programmazione di
script qualcuno sa suggerirmi qualche risorsa?

grazie mille maurizio

----- Original Message -----
Da : Markus Neteler <neteler@osgeo.org>
A : "junkhead@funbox.it" <maurizio.melani@gislab.it>
Cc: gfoss@faunalia.it
Oggetto : Re: [Gfoss] [GRASS 7] Grass 7 e python script
Data : Sat, 10 Oct 2009 18:25:04 +0200

2009/10/10 junkhead@funbox.it <maurizio.melani@gislab.it>:
> Salve ho installato grass 7 dal snv e devo dire che
> funziona tutto all
> 100%... a parte gli script python...
>
> Ad esempio se lancio lo script rasteraccess.py (fornito
> nella
> distribuzione) ho in uscita l'errore :
>
> File "./rasteraccess.py", line 40, in <module>
> data_type = grass.G_raster_map_type(input,

mapset)

> AttributeError: 'module' object has no attribute
> 'G_raster_map_type'
>
> Credo di aver fatto la giusta installazione visto che
> alcune righe
> funzionano... (tipo grass.G_find_cell2(input, '')..)

si, in GRASS 7 la rasterlib e la gislib sono finalmente
state separate. La funzione si chiama ora
G_find_raster2() e tutte G_raster_*() sono
Rast_*().

> confrontando le reference di grass 6
>
(http://download.osgeo.org/grass/grass6_progman/opencell_8
> c.html) con le
> nuove di grass 7
>
(http://download.osgeo.org/grass/grass7_progman/opencell_8
> c.html) ho
> notato che la proprietà ha cambiato nome
> G_raster_map_type in grass 7 si
> invoca con Rast_map_type .... però provando ancora non
> funziona...

Ho appena fatto (in SVN) gli aggiornamenti per riportarlo
allo stato di GRASS 7.
Mi da ancora un errore di "TypeError: 'int' object is
unsubscriptable" ma spero che sia una banalità che non

ho

visto.

Potresti aggiornare dal SVN e riprovare?
Magari stasera Martin Landa da un'occhiata.

ciao
Markus

Ciao:

2009/10/11 junkhead@funbox.it <maurizio.melani@gislab.it>:

Ciao ho installato da snv i nuovi sorgenti,

...

anche io ho il medesimo errore "TypeError: 'int' object is
unsubscriptable"

Viene discussiono nella grass-dev mailing list, ecco
una risposta spero utile:

On Mon, Oct 12, 2009 at 9:34 PM, Glynn Clements
<glynn@gclements.plus.com> wrote:

Try:
       import array
       inrast = array.array('i')
       inrast.extend([0] * gis.G_window_cols())

This is a good example of one of SWIG's main limitations. The only
thing it knows about Rast_allocate_buf() is that it returns a void*;
there's no way that it can generate a usable Python object from that
(it doesn't know the actual element type or the number of elements).

In general, it's easier to create objects in Python and pass them to C
than the other way around.

The only real solution is to code wrappers by hand; a C prototype
simply doesn't contain enough information to create a wrapper in
anything beyond the simplest cases.

--
Glynn Clements <glynn@gclements.plus.com>
_______________________________________________
grass-dev mailing list
grass-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/grass-dev

ciao
Markus