vectoraccess.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #!/usr/bin/python
  2. # run within GRASS Spearfish session
  3. # run this before starting python to append module search path:
  4. # export PYTHONPATH=/usr/src/grass70/swig/python
  5. # check with "import sys; sys.path"
  6. # or:
  7. # sys.path.append("/usr/src/grass70/swig/python")
  8. # FIXME: install the grass bindings in $GISBASE/lib/ ?
  9. import os, sys
  10. from grass.lib import grass
  11. from grass.lib import vector as grassvect
  12. if not os.environ.has_key("GISBASE"):
  13. print "You must be in GRASS GIS to run this program."
  14. sys.exit(1)
  15. if len(sys.argv)==2:
  16. input = sys.argv[1]
  17. else:
  18. input = raw_input("Vector Map Name? ")
  19. # initialize
  20. grass.G_gisinit('')
  21. # find map in search path
  22. mapset = grass.G_find_vector2(input,'')
  23. # define map structure
  24. map = grassvect.Map_info()
  25. # define open level (level 2: topology)
  26. grassvect.Vect_set_open_level (2)
  27. # open existing map
  28. grassvect.Vect_open_old(map, input, mapset)
  29. # query
  30. print 'Vect map: ', input
  31. print 'Vect is 3D: ', grassvect.Vect_is_3d (map)
  32. print 'Vect DB links: ', grassvect.Vect_get_num_dblinks(map)
  33. print 'Map Scale: 1:', grassvect.Vect_get_scale(map)
  34. # vector box tests
  35. box = grassvect.bound_box()
  36. c_easting1 = 599505.0
  37. c_northing = 4921010.0
  38. c_easting2 = 4599505.0
  39. grassvect.Vect_get_map_box(map, box)
  40. print 'Position 1 in box? ', grassvect.Vect_point_in_box(c_easting1, c_northing, 0, box)
  41. print 'Position 2 in box? ', grassvect.Vect_point_in_box(c_easting2, c_northing, 0, box)
  42. print 'Vector line 2 in box? ', grassvect.Vect_get_line_box(map, 2, box)
  43. # misleading:
  44. # print 'Number of lines:', grassvect.Vect_get_num_lines(map)
  45. # how to access GV_POINT?
  46. # print 'Number of points: ', grassvect.Vect_get_num_primitives(map,GV_POINT)
  47. # confusing:
  48. #print 'Number of lines: ', grassvect.Vect_get_num_primitives(map,GV_LINE)
  49. #print 'Number of areas:', grassvect.Vect_get_num_primitives(map,GV_AREA)
  50. print 'Number of areas:', grassvect.Vect_get_num_areas(map)
  51. # close map
  52. grassvect.Vect_close(map)
  53. ## end of the python script