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

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Aug 21 22:46:41 EDT 2010


Author: hamish
Date: 2010-08-22 02:46:41 +0000 (Sun, 22 Aug 2010)
New Revision: 43185

Added:
   grass/trunk/doc/python/raster_example_ctypes.py
Log:
rename to make room for more examples (merge from 6.5svn)

Copied: grass/trunk/doc/python/raster_example_ctypes.py (from rev 43184, grass/trunk/doc/python/example_ctypes.py)
===================================================================
--- grass/trunk/doc/python/raster_example_ctypes.py	                        (rev 0)
+++ grass/trunk/doc/python/raster_example_ctypes.py	2010-08-22 02:46:41 UTC (rev 43185)
@@ -0,0 +1,50 @@
+#!/usr/bin/env python
+import os, sys, subprocess
+from ctypes import *
+grass = CDLL("libgrass_gis.so")
+rast = CDLL("libgrass_raster.so")
+
+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
+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, '')
+ 
+# find map in search path
+mapset = grass.G_find_raster2(input, '')
+mapset = c_char_p(mapset).value
+ 
+# determine the inputmap type (CELL/FCELL/DCELL) */
+data_type = rast.Rast_map_type(input, mapset)
+
+if data_type == 0:
+    ptype = POINTER(c_int)
+elif data_type == 1:
+    ptype = POINTER(c_float)
+elif data_type == 2:
+    ptype = POINTER(c_double)
+ 
+infd = rast.Rast_open_old(input, mapset)
+inrast = rast.Rast_allocate_buf(data_type)
+inrast = cast(c_void_p(inrast), ptype)
+
+rows = rast.Rast_window_rows()
+cols = rast.Rast_window_cols()
+
+for rown in xrange(rows):
+    rast.Rast_get_row(infd, inrast, rown, data_type)
+    print rown, inrast[0:cols]
+ 
+rast.Rast_close(infd)
+grass.G_free(inrast)
+



More information about the grass-commit mailing list