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

svn_grass at osgeo.org svn_grass at osgeo.org
Sat Aug 21 22:54:42 EDT 2010


Author: hamish
Date: 2010-08-22 02:54:42 +0000 (Sun, 22 Aug 2010)
New Revision: 43187

Added:
   grass/trunk/doc/python/vector_example_ctypes.py
Removed:
   grass/trunk/doc/python/example_ctypes.py
   grass/trunk/doc/python/vectoraccess.py
Log:
rename to be in sync with other examples

Deleted: grass/trunk/doc/python/example_ctypes.py
===================================================================
--- grass/trunk/doc/python/example_ctypes.py	2010-08-22 02:53:48 UTC (rev 43186)
+++ grass/trunk/doc/python/example_ctypes.py	2010-08-22 02:54:42 UTC (rev 43187)
@@ -1,50 +0,0 @@
-#!/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)
-

Copied: grass/trunk/doc/python/vector_example_ctypes.py (from rev 43186, grass/trunk/doc/python/vectoraccess.py)
===================================================================
--- grass/trunk/doc/python/vector_example_ctypes.py	                        (rev 0)
+++ grass/trunk/doc/python/vector_example_ctypes.py	2010-08-22 02:54:42 UTC (rev 43187)
@@ -0,0 +1,96 @@
+#!/usr/bin/python
+
+# run within GRASS Spearfish session
+# 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")
+
+import os, sys
+from grass.lib import grass
+from grass.lib import vector as grassvect
+import grass.script as grassscript
+
+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("Vector Map Name? ")
+
+# initialize
+grass.G_gisinit('')
+
+# find map in search path
+mapset = grass.G_find_vector2(input,'')
+
+# define map structure
+map = grassvect.Map_info()
+
+# define open level (level 2: topology)
+grassvect.Vect_set_open_level (2)
+
+# open existing map
+grassvect.Vect_open_old(map, input, mapset)
+
+# query
+print 'Vect map: ', input
+print 'Vect is 3D: ', grassvect.Vect_is_3d (map)
+print 'Vect DB links: ', grassvect.Vect_get_num_dblinks(map)
+print 'Map Scale:  1:', grassvect.Vect_get_scale(map)
+
+# 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)
+# misleading:
+# print 'Number of lines:', grassvect.Vect_get_num_lines(map)
+# how to access GV_POINT?
+# print 'Number of points: ', grassvect.Vect_get_num_primitives(map,GV_POINT)
+# confusing:
+#print 'Number of lines: ', grassvect.Vect_get_num_primitives(map,GV_LINE)
+#print 'Number of areas:', grassvect.Vect_get_num_primitives(map,GV_AREA)
+print 'Number of areas:', grassvect.Vect_get_num_areas(map)
+
+layer = 1
+tmp = grassscript.tempfile()
+tmpf = file(tmp, 'w')
+
+try:
+    f = grassscript.vector_db(input)[int(layer)]
+except KeyError:
+    grassscript.fatal("There is no table connected to this map. Run v.db.connect or v.db.addtable first.")
+table = f['table']
+database = f['database']
+driver = f['driver']
+
+# call GRASS command
+grassscript.run_command('v.db.select', flags = 'c', map = input,
+                   stdout = tmpf)
+tmpf.close()
+
+# check if result is empty
+tmpf = file(tmp)
+if tmpf.read(1) == '':
+    grassscript.fatal("Table <%s> contains no data.", table)
+
+# print table to stdout
+for line in tmpf:
+    print line
+
+tmpf.close()
+os.remove(tmp)
+
+# close map
+grassvect.Vect_close(map)
+## end of the python script
+

Deleted: grass/trunk/doc/python/vectoraccess.py
===================================================================
--- grass/trunk/doc/python/vectoraccess.py	2010-08-22 02:53:48 UTC (rev 43186)
+++ grass/trunk/doc/python/vectoraccess.py	2010-08-22 02:54:42 UTC (rev 43187)
@@ -1,96 +0,0 @@
-#!/usr/bin/python
-
-# run within GRASS Spearfish session
-# 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")
-
-import os, sys
-from grass.lib import grass
-from grass.lib import vector as grassvect
-import grass.script as grassscript
-
-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("Vector Map Name? ")
-
-# initialize
-grass.G_gisinit('')
-
-# find map in search path
-mapset = grass.G_find_vector2(input,'')
-
-# define map structure
-map = grassvect.Map_info()
-
-# define open level (level 2: topology)
-grassvect.Vect_set_open_level (2)
-
-# open existing map
-grassvect.Vect_open_old(map, input, mapset)
-
-# query
-print 'Vect map: ', input
-print 'Vect is 3D: ', grassvect.Vect_is_3d (map)
-print 'Vect DB links: ', grassvect.Vect_get_num_dblinks(map)
-print 'Map Scale:  1:', grassvect.Vect_get_scale(map)
-
-# 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)
-# misleading:
-# print 'Number of lines:', grassvect.Vect_get_num_lines(map)
-# how to access GV_POINT?
-# print 'Number of points: ', grassvect.Vect_get_num_primitives(map,GV_POINT)
-# confusing:
-#print 'Number of lines: ', grassvect.Vect_get_num_primitives(map,GV_LINE)
-#print 'Number of areas:', grassvect.Vect_get_num_primitives(map,GV_AREA)
-print 'Number of areas:', grassvect.Vect_get_num_areas(map)
-
-layer = 1
-tmp = grassscript.tempfile()
-tmpf = file(tmp, 'w')
-
-try:
-    f = grassscript.vector_db(input)[int(layer)]
-except KeyError:
-    grassscript.fatal("There is no table connected to this map. Run v.db.connect or v.db.addtable first.")
-table = f['table']
-database = f['database']
-driver = f['driver']
-
-# call GRASS command
-grassscript.run_command('v.db.select', flags = 'c', map = input,
-                   stdout = tmpf)
-tmpf.close()
-
-# check if result is empty
-tmpf = file(tmp)
-if tmpf.read(1) == '':
-    grassscript.fatal("Table <%s> contains no data.", table)
-
-# print table to stdout
-for line in tmpf:
-    print line
-
-tmpf.close()
-os.remove(tmp)
-
-# close map
-grassvect.Vect_close(map)
-## end of the python script
-



More information about the grass-commit mailing list