[GRASS-SVN] r45113 - grass/trunk/doc/python

svn_grass at osgeo.org svn_grass at osgeo.org
Thu Jan 20 14:39:13 EST 2011


Author: martinl
Date: 2011-01-20 11:39:13 -0800 (Thu, 20 Jan 2011)
New Revision: 45113

Modified:
   grass/trunk/doc/python/raster_example_ctypes.py
   grass/trunk/doc/python/vector_example_ctypes.py
Log:
update ctypes examples


Modified: grass/trunk/doc/python/raster_example_ctypes.py
===================================================================
--- grass/trunk/doc/python/raster_example_ctypes.py	2011-01-20 19:31:41 UTC (rev 45112)
+++ grass/trunk/doc/python/raster_example_ctypes.py	2011-01-20 19:39:13 UTC (rev 45113)
@@ -1,95 +1,80 @@
 #!/usr/bin/env python
 
 """
-This is an example of how to use "ctypes" to access GRASS's C API from
-within a Python script. See the GRASS Programmer's manual for details
-on the e.g. G_*() functions in the GRASS C libraries (libgis et al.).
+Sample Python script to access raster data using GRASS Ctypes
+interface
 
-USAGE:  example_ctypes.py [Raster map name]
-        If a raster map name is not given it will prompt you for one.
-	This raster map is then opened, read, and data values printed
-	to stdout. You may wish to use 'g.region' to set the rows x
-	columns to something small (say 10 x 5) to avoid overwhelming
-	yourself: `g.region rows=10 cols=5`
+You may wish to use 'g.region' to set the rows x columns to something
+small (say 10 x 5) to avoid overwhelming yourself: `g.region rows=10
+cols=5`
 """
 
-
 # FIXME: as an example it should make extensive use of code comments and document
 #  each and every step along the way.  (e.g. explain c_char_p().value memory pointer
 #  to string conversion for Python programmers not familar with C pointers)
 #
 #  FIXME: explain at a basic level what ctypes is & does.
 
-import os, sys, subprocess
-# actiavate ctypes
-from ctypes import *
+import os
+import sys
 
+from grass.lib.grass  import *
+from grass.lib.raster import *
+
 # check if GRASS is running or not
 if not os.environ.has_key("GISBASE"):
-    print "You must be in GRASS GIS to run this program."
-    sys.exit(1)
+    sys.exit("You must be in GRASS GIS to run this program")
 
-# load in the main GRASS C library
-grass = CDLL("libgrass_gis.so")
-rast = CDLL("libgrass_raster.so")
-
-# parse command line arguements, prompt user for a raster map name if one wasn't given
-if len(sys.argv)==2:
+# parse command line arguments, prompt user for a raster map name if one wasn't given
+if len(sys.argv) == 2:
   input = sys.argv[1]
 else:
-  input = raw_input("Raster Map Name? ")
+  input = raw_input("Name of raster map? ")
 
-# initialize the C library
-s = subprocess.Popen(['g.version','-r'], stdout=subprocess.PIPE).communicate()[0]
-for line in s.splitlines():
-    if line.startswith('Revision:'):
-        version = '$' + line + '$'
-grass.G__gisinit(version, '')
+# initialize GRASS library
+G_gisinit('')
 
-
 # find map in search path
-mapset = grass.G_find_raster2(input, '')
-mapset = c_char_p(mapset).value
-
+mapset = G_find_raster2(input, '')
 if not mapset:
-    print "Raster map <%s> not found." % input
-    sys.exit(1)
+    sys.exit("Raster map <%s> not found" % input)
 
-
 # determine the inputmap type (CELL/FCELL/DCELL)
-data_type = rast.Rast_map_type(input, mapset)
+data_type = Rast_map_type(input, mapset)
 
-if data_type == 0:
+if data_type == CELL_TYPE:
     ptype = POINTER(c_int)
     type_name = 'CELL'
-elif data_type == 1:
+elif data_type == FCELL_TYPE:
     ptype = POINTER(c_float)
     type_name = 'FCELL'
-elif data_type == 2:
+elif data_type == DCELL_TYPR:
     ptype = POINTER(c_double)
     type_name = 'DCELL'
 
 print "Raster map <%s> contains data type %s." % (input, type_name)
 
-in_fd = rast.Rast_open_old(input, mapset)
-in_rast = rast.Rast_allocate_buf(data_type)
+in_fd   = Rast_open_old(input, mapset)
+in_rast = Rast_allocate_buf(data_type)
 in_rast = cast(c_void_p(in_rast), ptype)
 
-rows = rast.Rast_window_rows()
-cols = rast.Rast_window_cols()
-print "Current region is %d rows x %d columns.\n" % (rows, cols)
+rows = Rast_window_rows()
+cols = Rast_window_cols()
+print "Current region is %d rows x %d columns" % (rows, cols)
 
 # iterate through map rows
 print "Map data:"
 for row_n in xrange(rows):
     # read a row of raster data into memory, then print it
-    rast.Rast_get_row(in_fd, in_rast, row_n, data_type)
+    Rast_get_row(in_fd, in_rast, row_n, data_type)
     print row_n, in_rast[0:cols]
+    # TODO check for NULL
 
-#TODO: NULL -> NaN
-
-
 # closed map and cleanup memory allocation
-rast.Rast_close(in_fd)
-grass.G_free(in_rast)
+Rast_close(in_fd)
+G_free(in_rast)
 
+def check_null(value):
+    if math.isnan(value):
+        return 'null'
+    return value

Modified: grass/trunk/doc/python/vector_example_ctypes.py
===================================================================
--- grass/trunk/doc/python/vector_example_ctypes.py	2011-01-20 19:31:41 UTC (rev 45112)
+++ grass/trunk/doc/python/vector_example_ctypes.py	2011-01-20 19:39:13 UTC (rev 45113)
@@ -1,14 +1,8 @@
 #!/usr/bin/python
 
 """
-Sample Python script to access vector dada using GRASS Ctypes interface
-
-Run this before starting python to append module search path:
-   export PYTHONPATH=$PYTHONPATH:/usr/local/grass-7.0.svn/etc/python
-check with "import sys; sys.path"
-
-or
-   sys.path.append("/usr/local/grass-7.0.svn/etc/python")
+Sample Python script to access vector data using GRASS Ctypes
+interface
 """
 
 import os, sys
@@ -22,7 +16,7 @@
 if len(sys.argv) == 2:
   input = sys.argv[1]
 else:
-  input = raw_input("Vector Map Name? ")
+  input = raw_input("Name of vector map? ")
 
 # initialize GRASS library
 G_gisinit('')
@@ -33,19 +27,19 @@
     sys.exit("Vector map <%s> not found" % input)
 
 # define map structure
-map = Map_info()
+map_info = pointer(Map_info())
 
 # define open level (level 2: topology)
 Vect_set_open_level(2)
 
 # open existing vector map
-Vect_open_old(byref(map), input, mapset)
+Vect_open_old(map_info, input, mapset)
 
 # query
-print 'Vector map     :', Vect_get_full_name(byref(map))
-print 'Vector is 3D   :', Vect_is_3d(byref(map))
-print 'Vector DB links:', Vect_get_num_dblinks(byref(map))
-print 'Map Scale:  1  :', Vect_get_scale(byref(map))
+print 'Vector map        :', Vect_get_full_name(map_info)
+print 'Vector is 3D      :', Vect_is_3d(map_info)
+print 'Vector DB links   :', Vect_get_num_dblinks(map_info)
+print 'Map Scale         : 1:%d' % Vect_get_scale(map_info)
 
 # vector box tests
 box = bound_box()
@@ -53,14 +47,14 @@
 c_northing  = 4921010.0
 c_easting2  = 4599505.0
 
-Vect_get_map_box(byref(map), byref(box))
-print 'Position 1 in box? ', Vect_point_in_box(c_easting1, c_northing, 0, byref(box))
-print 'Position 2 in box? ', Vect_point_in_box(c_easting2, c_northing, 0, byref(box))
+Vect_get_map_box(map_info, byref(box))
+print 'Position 1 in box ?', Vect_point_in_box(c_easting1, c_northing, 0, byref(box))
+print 'Position 2 in box ?', Vect_point_in_box(c_easting2, c_northing, 0, byref(box))
 
-print 'Number of features:', Vect_get_num_lines(byref(map))
-print 'Number of points  :', Vect_get_num_primitives(byref(map), GV_POINT)
-print 'Number of lines   :', Vect_get_num_primitives(byref(map), GV_LINE)
-print 'Number of areas   :', Vect_get_num_areas(byref(map))
+print 'Number of features:', Vect_get_num_lines(map_info)
+print 'Number of points  :', Vect_get_num_primitives(map_info, GV_POINT)
+print 'Number of lines   :', Vect_get_num_primitives(map_info, GV_LINE)
+print 'Number of areas   :', Vect_get_num_areas(map_info)
 
 # close map
-Vect_close(byref(map))
+Vect_close(map_info)



More information about the grass-commit mailing list