Python Ctypes Examples: Difference between revisions

From GRASS-Wiki
Jump to navigation Jump to search
No edit summary
Line 6: Line 6:


<source lang="python">
<source lang="python">
#!/usr/bin/python
import sys
import sys


Line 28: Line 30:
nlines = Vect_get_num_lines(map_info)
nlines = Vect_get_num_lines(map_info)


for line in range(1, nlines):
for line in range(1, nlines + 1):
     ltype = Vect_read_line(map_info, points, cats, line)
     ltype = Vect_read_line(map_info, points, cats, line)
     if line % 100 == 0:
     if line % 100 == 0:
Line 41: Line 43:
                       points, cats)
                       points, cats)
     line += 1
     line += 1
sys.stderr.write("\r")


Vect_destroy_line_struct(points)
Vect_destroy_line_struct(points)
Vect_destroy_cats_struct(cats)
Vect_destroy_cats_struct(cats)


Vect_build_partial(map_info, GV_BUILD_NONE)
Vect_build(map_info)
Vect_close(map_info)
Vect_close(map_info)
</source>
</source>


{{Python}}
{{Python}}

Revision as of 19:04, 20 January 2011

See GRASS and Python for more information.

Accessing feature geometry

This script switches X, Y coordinates and multiple them by -1.

#!/usr/bin/python

import sys

import grass.script as grass

from grass.lib.vector import *

if len(sys.argv) < 2:
    sys.exit("Usage: %s: vector" % sys.argv[0])

name = sys.argv[1]

map_info = pointer(Map_info())
points = Vect_new_line_struct()
cats = Vect_new_cats_struct()
c_points = points.contents

level = Vect_open_update(map_info, name, "")
if level < 2:
    sys.exit("Topology not available")

nlines = Vect_get_num_lines(map_info)

for line in range(1, nlines + 1):
    ltype = Vect_read_line(map_info, points, cats, line)
    if line % 100 == 0:
        sys.stderr.write("\r%d" % line)
    
    for i in range(c_points.n_points):
        x = c_points.x[i]
        c_points.x[i] = -1 * c_points.y[i]
        c_points.y[i] = -1 * x
    
    Vect_rewrite_line(map_info, line, ltype,
                      points, cats)
    line += 1

sys.stderr.write("\r")

Vect_destroy_line_struct(points)
Vect_destroy_cats_struct(cats)

Vect_build_partial(map_info, GV_BUILD_NONE)
Vect_build(map_info)
Vect_close(map_info)