vectoraccess.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/usr/bin/python
  2. # run within GRASS Spearfish session
  3. # run this before starting python to append module search path:
  4. # export PYTHONPATH=$PYTHONPATH:/usr/local/grass-7.0.svn/etc/python
  5. # check with "import sys; sys.path"
  6. # or:
  7. # sys.path.append("/usr/local/grass-7.0.svn/etc/python")
  8. import os, sys
  9. from grass.lib import grass
  10. from grass.lib import vector as grassvect
  11. import grass.script as grassscript
  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. layer = 1
  52. tmp = grassscript.tempfile()
  53. tmpf = file(tmp, 'w')
  54. try:
  55. f = grassscript.vector_db(input)[int(layer)]
  56. except KeyError:
  57. grassscript.fatal("There is no table connected to this map. Run v.db.connect or v.db.addtable first.")
  58. table = f['table']
  59. database = f['database']
  60. driver = f['driver']
  61. # call GRASS command
  62. grassscript.run_command('v.db.select', flags = 'c', map = input,
  63. stdout = tmpf)
  64. tmpf.close()
  65. # check if result is empty
  66. tmpf = file(tmp)
  67. if tmpf.read(1) == '':
  68. grassscript.fatal("Table <%s> contains no data.", table)
  69. # print table to stdout
  70. for line in tmpf:
  71. print line
  72. tmpf.close()
  73. os.remove(tmp)
  74. # close map
  75. grassvect.Vect_close(map)
  76. ## end of the python script