raster.rst 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. .. _raster-label:
  2. Introduction on Raster
  3. ===========================
  4. PyGRASS uses 4 different Raster classes, that respect the 4 different approaches
  5. of GRASS-C API.
  6. The read access is row wise for :ref:`RasterRow-label` and
  7. :ref:`RasterRowIO-label` and additionally
  8. cached in the RowIO class. Both classes write sequentially.
  9. RowIO is row cached, :ref:`RasterSegment-label` and :ref:`RasterNumpy-label`
  10. are tile cached for reading and writing therefore a randomly access is possible.
  11. Hence RasterRow and RasterRowIO should be used in case for fast (cached)
  12. row read access and RasterRow for fast sequential writing.
  13. Segment and Numpy should be used for random access, but Numpy only for files
  14. not larger than 2GB.
  15. ========================== ======================= ======== ============
  16. Class Name C library Read Write
  17. ========================== ======================= ======== ============
  18. :ref:`RasterRow-label` `Raster library`_ randomly sequentially
  19. :ref:`RasterRowIO-label` `RowIO library`_ cached no
  20. :ref:`RasterSegment-label` `Segmentation library`_ cached randomly
  21. :ref:`RasterNumpy-label` `numpy.memmap`_ cached randomly
  22. ========================== ======================= ======== ============
  23. All these classes share common methods and attributes, necessary to address
  24. common tasks as rename, remove, open, close, exist, is_open.
  25. In the next examples we instantiate a RasterRow object. ::
  26. >>> from pygrass import raster
  27. >>> elev = raster.RasterRow('elevation')
  28. >>> elev.name
  29. 'elevation'
  30. >>> print(elev)
  31. elevation@PERMANENT
  32. >>> elev.exist()
  33. True
  34. >>> elev.is_open()
  35. False
  36. >>> new = raster.RasterRow('new')
  37. >>> new.exist()
  38. False
  39. >>> new.is_open()
  40. False
  41. We can rename the map: ::
  42. >>> # setting the attribute
  43. >>> new.name = 'new_map'
  44. >>> print(new)
  45. new_map
  46. >>> # or using the rename methods
  47. >>> new.rename('new')
  48. >>> print(new)
  49. new
  50. .. _RasterRow-label:
  51. RastRow
  52. -------
  53. PyGrass allow user to open the maps, in read and write mode,
  54. row by row using the `Raster library`_, there is not support to read and write
  55. to the same map at the same time, for this functionality, please see the
  56. :ref:`RasterSegment-label` and :ref:`RasterNumpy-label` classes.
  57. The RasterRow class allow to read in a randomly order the row from a map, but
  58. it is only possible to write the map using only a sequence order, therefore every
  59. time you are writing a new map, the row is add to the file as the last row. ::
  60. >>> raster = reload(raster)
  61. >>> elev = raster.RasterRow('elevation')
  62. >>> # the cols attribute is set from the current region only when the map is open
  63. >>> elev.cols
  64. >>> elev.open()
  65. >>> elev.is_open()
  66. True
  67. >>> elev.cols
  68. 1500
  69. >>> # we can read the elevation map, row by row
  70. >>> for row in elev[:5]: print(row[:3])
  71. [ 141.99613953 141.27848816 141.37904358]
  72. [ 142.90461731 142.39450073 142.68611145]
  73. [ 143.81854248 143.54707336 143.83972168]
  74. [ 144.56524658 144.58493042 144.86477661]
  75. [ 144.99488831 145.22894287 145.57142639]
  76. >>> # we can open a new map in write mode
  77. >>> new = raster.RasterRow('new')
  78. >>> new.open('w', 'CELL')
  79. >>> # for each elev row we can perform computation, and write the result into
  80. >>> # the new map
  81. >>> for row in elev:
  82. ... new.put_row(row < 144)
  83. ...
  84. >>> # close the maps
  85. >>> new.close()
  86. >>> elev.close()
  87. >>> # check if the map exist
  88. >>> new.exist()
  89. True
  90. >>> # we can open the map in read mode
  91. >>> new.open('r')
  92. >>> for row in new[:5]: print(row[:3])
  93. [1 1 1]
  94. [1 1 1]
  95. [1 1 1]
  96. [0 0 0]
  97. [0 0 0]
  98. >>> new.close()
  99. >>> new.remove()
  100. >>> new.exist()
  101. False
  102. .. autoclass:: pygrass.raster.RasterRow
  103. :members:
  104. .. _RasterRowIO-label:
  105. RasterRowIO
  106. -----------
  107. The RasterRowIO class use the grass `RowIO library`_, and implement a row
  108. cache. The RasterRowIO class support only reading the raster, because the
  109. raster rows can only be written in sequential order, writing by row id is not
  110. supported by design. Hence, we should use the rowio lib only for caching rows
  111. for reading and use the default row write access as in the RasterRow class. ::
  112. >>> raster = reload(raster)
  113. >>> elev = raster.RasterRowIO('elevation')
  114. >>> elev.open('r')
  115. >>> for row in elev[:5]: print(row[:3])
  116. [ 141.99613953 141.27848816 141.37904358]
  117. [ 142.90461731 142.39450073 142.68611145]
  118. [ 143.81854248 143.54707336 143.83972168]
  119. [ 144.56524658 144.58493042 144.86477661]
  120. [ 144.99488831 145.22894287 145.57142639]
  121. >>> elev.close()
  122. .. autoclass:: pygrass.raster.RasterRowIO
  123. :members:
  124. .. _RasterSegment-label:
  125. RastSegment
  126. -----------
  127. The RasterSegment class use the grass `Segmentation library`_, it work dividing
  128. the raster map into small different files, that grass read load into the memory
  129. and write to the hardisk.
  130. The segment library allow to open a map in a read-write mode. ::
  131. >>> raster = reload(raster)
  132. >>> elev = raster.RasterSegment('elevation')
  133. >>> elev.open()
  134. >>> for row in elev[:5]: print(row[:3])
  135. [ 141.99613953 141.27848816 141.37904358]
  136. [ 142.90461731 142.39450073 142.68611145]
  137. [ 143.81854248 143.54707336 143.83972168]
  138. [ 144.56524658 144.58493042 144.86477661]
  139. [ 144.99488831 145.22894287 145.57142639]
  140. >>> new = raster.RasterSegment('new')
  141. >>> new.open('w', 'CELL')
  142. >>> for irow in xrange(elev.rows):
  143. ... new[irow] = elev[irow] < 144
  144. ...
  145. >>> for row in new[:5]: print(row[:3])
  146. [1 1 1]
  147. [1 1 1]
  148. [1 1 1]
  149. [0 0 0]
  150. [0 0 0]
  151. The RasterSegment class define two methods to read and write the map:
  152. * ``get_row`` that return the buffer object with the row that call the
  153. C function ``segment_get_row``. ::
  154. >>> # call explicity the method
  155. >>> elev_row0 = elev.get_row(0)
  156. >>> # call implicity the method
  157. >>> elev_row0 = elev[0]
  158. * ``get`` that return the value of the call map that call the
  159. C function ``segment_get``. ::
  160. >>> # call explicity the method
  161. >>> elev_val_0_0 = elev.get(0, 0)
  162. >>> # call implicity the method
  163. >>> elev_val_0_0 = elev[0, 0]
  164. Similarly to write the map, with ``put_row``, to write a row and with ``put``
  165. to write a single value to the map. ::
  166. >>> # compare the cell value get using the ``get`` method, and take the first
  167. >>> # value of the row with the ``get_row`` method
  168. >>> elev[0, 0] == elev[0][0]
  169. True
  170. >>> # write a new value to a cell,
  171. >>> new[0, 0] = 10
  172. >>> new[0, 0]
  173. 10
  174. >>> new.close()
  175. >>> new.exist()
  176. True
  177. >>> new.remove()
  178. >>> elev.close()
  179. >>> elev.remove()
  180. .. autoclass:: pygrass.raster.RasterSegment
  181. :members:
  182. .. _RasterNumpy-label:
  183. RasterNumpy
  184. -----------
  185. The RasterNumpy class, is based on the `numpy.memmap`_ class If you open an
  186. existing map, the map will be copied on a binary format, and read to avoid
  187. to load all the map in memory. ::
  188. >>> raster = reload(raster)
  189. >>> elev = raster.RasterNumpy('elevation', 'PERMANENT')
  190. >>> elev.open('r')
  191. >>> # in this case RasterNumpy is an extention of the numpy class
  192. >>> # therefore you may use all the fancy things of numpy.
  193. >>> elev[:5, :3]
  194. RasterNumpy([[ 141.99613953, 141.27848816, 141.37904358],
  195. [ 142.90461731, 142.39450073, 142.68611145],
  196. [ 143.81854248, 143.54707336, 143.83972168],
  197. [ 144.56524658, 144.58493042, 144.86477661],
  198. [ 144.99488831, 145.22894287, 145.57142639]], dtype=float32)
  199. >>> el = elev < 144
  200. >>> el[:5, :3]
  201. RasterNumpy([[1, 1, 1],
  202. [1, 1, 1],
  203. [1, 1, 1],
  204. [0, 0, 0],
  205. [0, 0, 0]], dtype=int32)
  206. >>> el.name == None
  207. True
  208. >>> # give a name to the new map
  209. >>> el.name = 'new'
  210. >>> el.exist()
  211. False
  212. >>> el.close()
  213. >>> el.exist()
  214. True
  215. >>> el.remove()
  216. .. autoclass:: pygrass.raster.RasterNumpy
  217. :members:
  218. .. _Raster library: http://grass.osgeo.org/programming7/rasterlib.html
  219. .. _RowIO library: http://grass.osgeo.org/programming7/rowiolib.html
  220. .. _Segmentation library: http://grass.osgeo.org/programming7/segmentlib.html
  221. .. _numpy.memmap: http://docs.scipy.org/doc/numpy/reference/generated/numpy.memmap.html