[GRASS-dev] bound_box in python

Can you tell me howto use bound_box in python. I am new in grass
developping and i use python. I need to use Vect_get_map_box and
bound_box* type is requiered.

___________________________________________________________________________
Yahoo! Mail réinvente le mail ! Découvrez le nouveau Yahoo! Mail et son interface révolutionnaire.
http://fr.mail.yahoo.com

On Sun, May 31, 2009 at 12:45 PM, Danho Fursy Rodelec Neuba
<danho_f@yahoo.fr> wrote:

Can you tell me howto use bound_box in python. I am new in grass
developping and i use python. I need to use Vect_get_map_box and
bound_box* type is requiered.

In theory it should be

import os, sys
from grass.lib import grass
from grass.lib import vector as grassvect
...
grassvect.Vect_get_map_box(map, box)

but I (also) failed to define

box = grassvect.bound_box()

I have added that locally to
swig/python/examples/vectoraccess.py

but I get
Traceback (most recent call last):
  File "examples/vectoraccess.py", line 45, in <module>
    print 'Map box:', grassvect.Vect_get_map_box(map, box)
TypeError: in method 'Vect_get_map_box', argument 2 of type 'BOUND_BOX *'

Using instead BOUND_BOX, it also fails:
Traceback (most recent call last):
  File "examples/vectoraccess.py", line 32, in <module>
    box = grassvect.BOUND_BOX()
AttributeError: 'module' object has no attribute 'BOUND_BOX'

?

@developers:
Looking at the GRASS includes, I see
include/vect/dig_structs.h:#define BOUND_BOX struct bound_box

Is there a strategic advantage to define 'struct bound_box' like this while
for example 'struct Map_info' isn't defined as MAP_INFO in the Vector
libs?

Markus

Markus Neteler wrote:

but I get
Traceback (most recent call last):
  File "examples/vectoraccess.py", line 45, in <module>
    print 'Map box:', grassvect.Vect_get_map_box(map, box)
TypeError: in method 'Vect_get_map_box', argument 2 of type 'BOUND_BOX *'

Using instead BOUND_BOX, it also fails:
Traceback (most recent call last):
  File "examples/vectoraccess.py", line 32, in <module>
    box = grassvect.BOUND_BOX()
AttributeError: 'module' object has no attribute 'BOUND_BOX'

Right. SWIG only generates wrappers for the "struct bound_box" type,
but requires that you pass a "BOUND_BOX *" to the function.

Like most things which deal with C, it gets confused by preprocessor
macros.

Looking at the GRASS includes, I see
include/vect/dig_structs.h:#define BOUND_BOX struct bound_box

Is there a strategic advantage to define 'struct bound_box' like this while
for example 'struct Map_info' isn't defined as MAP_INFO in the Vector
libs?

No. If you really want an alias, use typedef, e.g.:

  typedef struct bound_box BOUND_BOX;

But I would prefer to just use "struct bound_box" everywhere to make
it clear that it's a structure.

I intend to commit a fix once the test compilation has completed.

More generally: avoid using the preprocessor unless it's absolutely
essential. Any convenience which it appears to offer is usually
illusory; you get convenience in one aspect, and inconvenience in a
dozen others.

--
Glynn Clements <glynn@gclements.plus.com>

On Mon, Jun 1, 2009 at 6:59 AM, Glynn Clements <glynn@gclements.plus.com> wrote:

Markus Neteler wrote:

but I get
Traceback (most recent call last):
File "examples/vectoraccess.py", line 45, in <module>
print 'Map box:', grassvect.Vect_get_map_box(map, box)
TypeError: in method 'Vect_get_map_box', argument 2 of type 'BOUND_BOX *'

Using instead BOUND_BOX, it also fails:
Traceback (most recent call last):
File "examples/vectoraccess.py", line 32, in <module>
box = grassvect.BOUND_BOX()
AttributeError: 'module' object has no attribute 'BOUND_BOX'

Right. SWIG only generates wrappers for the "struct bound_box" type,
but requires that you pass a "BOUND_BOX *" to the function.

Like most things which deal with C, it gets confused by preprocessor
macros.

Looking at the GRASS includes, I see
include/vect/dig_structs.h:#define BOUND_BOX struct bound_box

Is there a strategic advantage to define 'struct bound_box' like this while
for example 'struct Map_info' isn't defined as MAP_INFO in the Vector
libs?

No. If you really want an alias, use typedef, e.g.:

   typedef struct bound\_box BOUND\_BOX;

But I would prefer to just use "struct bound_box" everywhere to make
it clear that it's a structure.

I intend to commit a fix once the test compilation has completed.

Thanks - testing the backport, will submit once SVN wakes up again.

Markus

On Sun, May 31, 2009 at 3:53 PM, Markus Neteler <neteler@osgeo.org> wrote:

On Sun, May 31, 2009 at 12:45 PM, Danho Fursy Rodelec Neuba
<danho_f@yahoo.fr> wrote:

Can you tell me howto use bound_box in python. I am new in grass
developping and i use python. I need to use Vect_get_map_box and
bound_box* type is requiered.

In theory it should be

import os, sys
from grass.lib import grass
from grass.lib import vector as grassvect
...
grassvect.Vect_get_map_box(map, box)

It has been fixed in SVN of 6.4, 6.5 and 7. Now
access to vector boxes is possible.

Here a simple example:
see also swig/python/examples/vectoraccess.py

# vector box tests
box = grassvect.bound_box()
c_easting1 = 599505.0
c_northing = 4921010.0
c_easting2 = 4599505.0

grassvect.Vect_get_map_box(map, box)
print 'Position 1 in box? ', grassvect.Vect_point_in_box(c_easting1,
c_northing, 0, box)
print 'Position 2 in box? ', grassvect.Vect_point_in_box(c_easting2,
c_northing, 0, box)
print 'Vector line 2 in box? ', grassvect.Vect_get_line_box(map, 2, box)

Markus