pygrass_raster.rst 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. .. _raster-label:
  2. Introduction to Raster classes
  3. ==============================
  4. Details about the GRASS GIS raster architecture can be found in the
  5. `GRASS GIS 7 Programmer's Manual: GRASS Raster Library
  6. <http://grass.osgeo.org/programming7/rasterlib.html>`_.
  7. PyGRASS uses 3 different raster classes, that respect the 3 different
  8. approaches of GRASS-C API. The classes use a standardized interface to
  9. keep methods consistent between them. The read access is row wise for
  10. :ref:`RasterRow-label` and :ref:`RasterRowIO-label` and additionally
  11. cached in the RowIO class. Only the first class writes sequentially.
  12. RowIO is row cached, :ref:`RasterSegment-label` is tile cached for reading and
  13. writing; therefore, random access is possible. Hence RasterRow and
  14. RasterRowIO should be used for fast (cached) row read access and
  15. RasterRow for fast sequential writing. RasterSegment should be used
  16. for random access. The classes are part of the :mod:`~pygrass.raster`
  17. module.
  18. ========================== ======================= ======== ============
  19. Class Name C library Read Write
  20. ========================== ======================= ======== ============
  21. :ref:`RasterRow-label` `Raster library`_ randomly sequentially
  22. :ref:`RasterRowIO-label` `RowIO library`_ cached no
  23. :ref:`RasterSegment-label` `Segmentation library`_ cached randomly
  24. ========================== ======================= ======== ============
  25. These classes share common methods and attributes to address
  26. common tasks as rename, remove, open, close, exist, is_open.
  27. In the following example we instantiate a RasterRow object. ::
  28. >>> from grass.pygrass import raster
  29. >>> elev = raster.RasterRow('elevation')
  30. >>> elev.name
  31. 'elevation'
  32. >>> print(elev)
  33. elevation@PERMANENT
  34. >>> elev.exist()
  35. True
  36. >>> elev.is_open()
  37. False
  38. >>> new = raster.RasterRow('new')
  39. >>> new.exist()
  40. False
  41. >>> new.is_open()
  42. False
  43. We can rename the map: ::
  44. >>> # setting the attribute
  45. >>> new.name = 'new_map'
  46. >>> print(new)
  47. new_map
  48. >>> # or using the rename methods
  49. >>> new.rename('new')
  50. >>> print(new)
  51. new
  52. .. _RasterRow-label:
  53. RasterRow
  54. ---------
  55. The PyGRASS :class:`~pygrass.raster.RasterRow` class allow user to open maps row
  56. by row in either read or write mode using the `Raster library`_. Reading and writing
  57. to the same map at the same time is not supported. For this functionality,
  58. please see the :ref:`RasterSegment-label` class.
  59. The RasterRow class allows map rows to be read in any order, but map rows can
  60. only be written in sequential order. Therefore, each row written to a map is
  61. added to the file as the last row. ::
  62. >>> raster = reload(raster)
  63. >>> elev = raster.RasterRow('elevation')
  64. >>> # the private _cols attribute is set from the current region only when the map is open
  65. >>> # the .info.cols attribute is set to total number of map cols only when the map is open
  66. >>> # cols in .info.cols equals the number reported by r.info module
  67. >>> elev._cols
  68. >>> elev.info.cols
  69. 0
  70. >>> elev.open()
  71. >>> elev.is_open()
  72. True
  73. >>> elev._cols
  74. 200
  75. >>> elev.info.cols
  76. 1500
  77. >>> elev._rows
  78. 300
  79. >>> # number of available rows/cols also can be determined by len()
  80. >>> len(elev)
  81. 300
  82. >>> len(elev[0])
  83. 200
  84. >>> # we can read the elevation map, row by row
  85. >>> for row in elev[:5]: print(row[:3])
  86. [ 141.99613953 141.27848816 141.37904358]
  87. [ 142.90461731 142.39450073 142.68611145]
  88. [ 143.81854248 143.54707336 143.83972168]
  89. [ 144.56524658 144.58493042 144.86477661]
  90. [ 144.99488831 145.22894287 145.57142639]
  91. >>> # we can open a new map in write mode
  92. >>> new = raster.RasterRow('new')
  93. >>> new.open('w', 'CELL')
  94. >>> # for each elev row we can perform computation, and write the result into
  95. >>> # the new map
  96. >>> for row in elev:
  97. ... new.put_row(row < 144)
  98. ...
  99. >>> # close the maps
  100. >>> new.close()
  101. >>> elev.close()
  102. >>> # check if the map exist
  103. >>> new.exist()
  104. True
  105. >>> # we can open the map in read mode
  106. >>> new.open('r')
  107. >>> for row in new[:5]: print(row[:3])
  108. [1 1 1]
  109. [1 1 1]
  110. [1 1 1]
  111. [0 0 0]
  112. [0 0 0]
  113. >>> new.close()
  114. >>> new.remove()
  115. >>> new.exist()
  116. False
  117. .. _RasterRowIO-label:
  118. RasterRowIO
  119. -----------
  120. The :class:`~pygrass.raster.RasterRowIO` class uses the GRASS `RowIO library`_, and implements a row
  121. cache. The RasterRowIO class only supports reading rasters; because raster rows
  122. can only be written in sequential order, writing by row id is not
  123. supported by design. Hence, the RowIO lib can only be used to cache rows
  124. for reading, and any write access should use the :ref:`RasterRow-label` class. ::
  125. >>> raster = reload(raster)
  126. >>> elev = raster.RasterRowIO('elevation')
  127. >>> elev.open('r')
  128. >>> for row in elev[:5]: print(row[:3])
  129. [ 141.99613953 141.27848816 141.37904358]
  130. [ 142.90461731 142.39450073 142.68611145]
  131. [ 143.81854248 143.54707336 143.83972168]
  132. [ 144.56524658 144.58493042 144.86477661]
  133. [ 144.99488831 145.22894287 145.57142639]
  134. >>> elev.close()
  135. .. _RasterSegment-label:
  136. RasterSegment
  137. -------------
  138. The :class:`~pygrass.raster.RasterSegment` class uses the GRASS `Segmentation library`_. The class divides
  139. a raster map into small tiles stored on disk. Initialization of this class is
  140. therefore intensive. However, this class has lower memory requirements, as GRASS
  141. loads only currently-accessed tiles into memory. The segment library allow
  142. opening maps in a read-write mode. ::
  143. >>> raster = reload(raster)
  144. >>> elev = raster.RasterSegment('elevation')
  145. >>> elev.open()
  146. >>> for row in elev[:5]: print(row[:3])
  147. [ 141.99613953 141.27848816 141.37904358]
  148. [ 142.90461731 142.39450073 142.68611145]
  149. [ 143.81854248 143.54707336 143.83972168]
  150. [ 144.56524658 144.58493042 144.86477661]
  151. [ 144.99488831 145.22894287 145.57142639]
  152. >>> new = raster.RasterSegment('new')
  153. >>> new.open('w', 'CELL')
  154. >>> for irow, row in enumerate(elev):
  155. ... new[irow] = row < 144
  156. ...
  157. >>> for row in new[:5]: print(row[:3])
  158. [1 1 1]
  159. [1 1 1]
  160. [1 1 1]
  161. [0 0 0]
  162. [0 0 0]
  163. Due to the unique behavior of this class, the RasterSegment class defines two
  164. methods to read a map:
  165. * ``get_row`` calls the C function ``Segment_get_row()`` and returns a buffer
  166. object with the row. ::
  167. >>> # call explicitly the method
  168. >>> elev_row0 = elev.get_row(0)
  169. >>> # call implicitly the method
  170. >>> elev_row0 = elev[0]
  171. * ``get`` calls the C function ``Segment_get()`` and returns the value of the
  172. map cell. ::
  173. >>> # call explicitly the method
  174. >>> elev_val_0_0 = elev.get(0, 0)
  175. >>> # call implicitly the method
  176. >>> elev_val_0_0 = elev[0, 0]
  177. Similarly, writing to a map uses two methods: ``put_row()`` to write a row and
  178. ``put()`` to write a single value to the map. ::
  179. >>> # compare the cell value get using the ``get`` method, and take the first
  180. >>> # value of the row with the ``get_row`` method
  181. >>> # the methods are used internally by the index operators
  182. >>> elev[0, 0] == elev[0][0]
  183. True
  184. >>> # write a new value to a cell,
  185. >>> new[0, 0] = 10 # ``put`` is used internally by the index operators
  186. >>> new[0, 0]
  187. 10
  188. >>> new.close()
  189. >>> new.exist()
  190. True
  191. >>> new.remove()
  192. >>> elev.close()
  193. >>> elev.remove()
  194. .. _Raster library: http://grass.osgeo.org/programming7/rasterlib.html
  195. .. _RowIO library: http://grass.osgeo.org/programming7/rowiolib.html
  196. .. _Segmentation library: http://grass.osgeo.org/programming7/segmentlib.html