vector.rst 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. .. _vector-label:
  2. Introduction to Vector classes
  3. ==============================
  4. Details about the architecture can be found in the `GRASS GIS 7 Programmer's Manual: GRASS Vector Library <http://grass.osgeo.org/programming7/vectorlib.html>`_
  5. Instantiation and basic interaction. ::
  6. >>> from pygrass.vector import VectTopo
  7. >>> municip = VectTopo('boundary_municp_sqlite')
  8. >>> municip.is_open()
  9. False
  10. >>> municip.mapset
  11. ''
  12. >>> municip.exist() # check if exist, and if True set mapset
  13. True
  14. >>> municip.mapset
  15. 'user1'
  16. Open the map with topology: ::
  17. >>> municip.open()
  18. get the number of primitive:
  19. >>> municip.num_primitive_of('line')
  20. 0
  21. >>> municip.num_primitive_of('centroid')
  22. 3579
  23. >>> municip.num_primitive_of('boundary')
  24. 5128
  25. ask for other feature in the vector map: ::
  26. >>> municip.number_of("areas")
  27. 3579
  28. >>> municip.number_of("islands")
  29. 2629
  30. >>> municip.number_of("holes")
  31. 0
  32. >>> municip.number_of("lines")
  33. 8707
  34. >>> municip.number_of("nodes")
  35. 4178
  36. >>> municip.number_of("pizza") # doctest: +ELLIPSIS +NORMALIZE_WHITESPACE
  37. Traceback (most recent call last):
  38. ...
  39. ValueError: vtype not supported, use one of: 'areas', ..., 'volumes'
  40. Suppose that we want to select all and only the areas that have an
  41. area bigger than 10000m2: ::
  42. >>> big = [area for area in municip.viter('areas')
  43. ... if area.alive() and area.area >= 10000]
  44. it's pretty easy, isn't it?!? :-)
  45. the method "viter" return an iterator object of the vector features,
  46. in this way no memory is wasted... User can choose on which
  47. vector features want to iterate...
  48. then you can go on with python stuff like, sort by area dimension: ::
  49. >>> from operator import methodcaller as method
  50. >>> big.sort(key = method('area'), reverse = True) # sort the list
  51. >>> for area in big[:3]:
  52. ... print area, area.area()
  53. Area(3102) 697521857.848
  54. Area(2682) 320224369.66
  55. Area(2552) 298356117.948
  56. or sort for the number of isles that are contained inside: ::
  57. >>> big.sort(key = lambda x: x.isles.__len__(), reverse = True)
  58. >>> for area in big[:3]:
  59. ... print area, area.isles.__len__()
  60. ...
  61. Area(2682) 68
  62. Area(2753) 45
  63. Area(872) 42
  64. or you may have only the list of the areas that contain isles inside, with: ::
  65. >>> area_with_isles = [area for area in big if area.isles]
  66. >>> area_with_isles # doctest: +ELLIPSIS
  67. [Area(...), ..., Area(...)]
  68. Of course is still possible work only with a specific area, with: ::
  69. >>> from pygrass.vector.geometry import Area
  70. >>> area = Area(v_id=1859, c_mapinfo=municip.c_mapinfo)
  71. >>> area.area()
  72. 39486.05401495844
  73. >>> area.bbox() # north, south, east, west
  74. Bbox(175711.718494, 175393.514494, 460344.093986, 460115.281986)
  75. >>> area.isles
  76. Isles([])
  77. Now, find an area with an island inside... ::
  78. >>> area = Area(v_id=2972, c_mapinfo=municip.c_mapinfo)
  79. >>> area.isles # doctest: +ELLIPSIS
  80. Isles([Isle(1538), Isle(1542), Isle(1543), ..., Isle(2571)])
  81. >>> isle = area.isles[0]
  82. >>> isle.bbox()
  83. Bbox(199947.296494, 199280.969494, 754920.623987, 754351.812986)
  84. Vector
  85. ----------
  86. .. autoclass:: pygrass.vector.Vector
  87. :members:
  88. VectorTopo
  89. ----------
  90. .. autoclass:: pygrass.vector.VectorTopo
  91. :members: