[GRASS-SVN] r37595 -
grass/branches/releasebranch_6_4/swig/python/examples
svn_grass at osgeo.org
svn_grass at osgeo.org
Fri May 29 17:52:25 EDT 2009
Author: martinl
Date: 2009-05-29 17:52:24 -0400 (Fri, 29 May 2009)
New Revision: 37595
Modified:
grass/branches/releasebranch_6_4/swig/python/examples/rasteraccess.py
grass/branches/releasebranch_6_4/swig/python/examples/vectoraccess.py
Log:
synchronize examples with devbr6
Modified: grass/branches/releasebranch_6_4/swig/python/examples/rasteraccess.py
===================================================================
--- grass/branches/releasebranch_6_4/swig/python/examples/rasteraccess.py 2009-05-29 21:31:15 UTC (rev 37594)
+++ grass/branches/releasebranch_6_4/swig/python/examples/rasteraccess.py 2009-05-29 21:52:24 UTC (rev 37595)
@@ -1,28 +1,54 @@
-#!/usr/bin/python
+#!/usr/bin/env python
-# run within GRASS Spearfish session
+"""
+Run within GRASS session
+Run this before starting python to append module search path:
+ at code
+export PYTHONPATH=/usr/src/grass70/swig/python
+ at endcode
+
+Check with "import sys; sys.path"
+or:
+
+ at code
+sys.path.append("/usr/src/grass70/swig/python")
+ at endcode
+
+\todo install the grass bindings in $GISBASE/lib/ ?
+"""
+
import os, sys
-import python_grass6 as g6lib
+from grass.lib import grass
if not os.environ.has_key("GISBASE"):
print "You must be in GRASS GIS to run this program."
sys.exit(1)
-input = 'elevation.dem'
-mapset = 'PERMANENT'
+if len(sys.argv)==2:
+ input = sys.argv[1]
+else:
+ input = raw_input("Raster Map Name? ")
-g6lib.G_gisinit('')
-infd = g6lib.G_open_cell_old(input, mapset)
+# initialize
+grass.G_gisinit('')
-cell = g6lib.G_allocate_cell_buf()
+# find map in search path
+mapset = grass.G_find_cell2(input, '')
-rown=0
-while 1:
- myrow = g6lib.G_get_map_row_nomask(infd, cell, rown)
- print rown,myrow[0:10]
- rown = rown+1
- if rown==476:break
+# determine the inputmap type (CELL/FCELL/DCELL) */
+data_type = grass.G_raster_map_type(input, mapset)
-g6lib.G_close_cell(infd)
-g6lib.G_free(cell)
+infd = grass.G_open_cell_old(input, mapset)
+inrast = grass.G_allocate_raster_buf(data_type)
+
+rown = 0
+while True:
+ myrow = grass.G_get_raster_row(infd, inrast, rown, data_type)
+ print rown, myrow[0:10]
+ rown += 1
+ if rown == 476:
+ break
+
+grass.G_close_cell(inrast)
+grass.G_free(cell)
Modified: grass/branches/releasebranch_6_4/swig/python/examples/vectoraccess.py
===================================================================
--- grass/branches/releasebranch_6_4/swig/python/examples/vectoraccess.py 2009-05-29 21:31:15 UTC (rev 37594)
+++ grass/branches/releasebranch_6_4/swig/python/examples/vectoraccess.py 2009-05-29 21:52:24 UTC (rev 37595)
@@ -1,9 +1,16 @@
#!/usr/bin/python
# run within GRASS Spearfish session
+# run this before starting python to append module search path:
+# export PYTHONPATH=/usr/src/grass70/swig/python
+# check with "import sys; sys.path"
+# or:
+# sys.path.append("/usr/src/grass70/swig/python")
+# FIXME: install the grass bindings in $GISBASE/lib/ ?
import os, sys
-import python_grass6 as g6lib
+from grass.lib import grass
+from grass.lib import vector as grassvect
if not os.environ.has_key("GISBASE"):
print "You must be in GRASS GIS to run this program."
@@ -14,34 +21,36 @@
else:
input = raw_input("Vector Map Name? ")
-mapset = 'PERMANENT'
-
# initialize
-g6lib.G_gisinit('')
+grass.G_gisinit('')
+# find map in search path
+mapset = grass.G_find_vector2(input,'')
+
# define map structure
-map = g6lib.Map_info()
+map = grassvect.Map_info()
# define open level (level 2: topology)
-g6lib.Vect_set_open_level (2)
+grassvect.Vect_set_open_level (2)
# open existing map
-g6lib.Vect_open_old(map, input, mapset)
+grassvect.Vect_open_old(map, input, mapset)
# query
print 'Vect map: ', input
-print 'Vect is 3D: ', g6lib.Vect_is_3d (map)
-print 'Vect DB links: ', g6lib.Vect_get_num_dblinks(map)
-print 'Map Scale: 1:', g6lib.Vect_get_scale(map)
+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)
# misleading:
-# print 'Number of lines:', g6lib.Vect_get_num_lines(map)
-print 'Number of points: ', g6lib.Vect_get_num_primitives(map,g6lib.GV_POINT)
+# 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: ', g6lib.Vect_get_num_primitives(map,g6lib.GV_LINE)
-#print 'Number of areas:', g6lib.Vect_get_num_primitives(map,g6lib.GV_AREA)
-print 'Number of areas:', g6lib.Vect_get_num_areas(map)
+#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)
# close map
-g6lib.Vect_close(map)
+grassvect.Vect_close(map)
## end of the python script
More information about the grass-commit
mailing list