raster.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  1. .. _raster-label:
  2. 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. .. _RasterCategory-label:
  51. Categories
  52. ----------
  53. All the raster classes support raster categories and share commons methods
  54. to modify the raster category.
  55. It is possible to check if the map has or not the categories with the
  56. ``has_cats`` method. ::
  57. >>> elev.has_cats()
  58. False
  59. Opening a map that has category, for example the "landcove_1m" raster map
  60. from the North Carolina mapset. The ``has_cats`` method return True. ::
  61. >>> land = raster.RasterRow('landcover_1m')
  62. >>> land.has_cats()
  63. True
  64. Get and set the categories title, with: ::
  65. >>> land.cats_title
  66. 'Rural area: Landcover'
  67. >>> land.cats_title = 'Rural area: Landcover2'
  68. >>> land.cats_title
  69. 'Rural area: Landcover2'
  70. >>> land.cats_title = 'Rural area: Landcover'
  71. Get the number of categories of the map with: ::
  72. >>> land.num_cats()
  73. 11
  74. See all the categories with: ::
  75. >>> land.cats
  76. [('pond', 1, None),
  77. ('forest', 2, None),
  78. ('developed', 3, None),
  79. ('bare', 4, None),
  80. ('paved road', 5, None),
  81. ('dirt road', 6, None),
  82. ('vineyard', 7, None),
  83. ('agriculture', 8, None),
  84. ('wetland', 9, None),
  85. ('bare ground path', 10, None),
  86. ('grass', 11, None)]
  87. Access a single category, using Rast_get_ith_cat(), with: ::
  88. >>> land.cats[0]
  89. ('pond', 1, None)
  90. >>> land.cats['pond']
  91. ('pond', 1, None)
  92. >>> land.get_cat(0)
  93. ('pond', 1, None)
  94. >>> land.get_cat('pond')
  95. ('pond', 1, None)
  96. Add new or change existing categories: ::
  97. >>> land.set_cat('label', 1)
  98. >>> land.get_cat('label')
  99. ('label', 1, None)
  100. >>> land.set_cat('pond', 1, 1)
  101. Sort categories, with: ::
  102. >>> land.sort_cats()
  103. Copy categories from another raster map with: ::
  104. >>> land.copy_cats(elev)
  105. Read and Write: ::
  106. >>> land.read_cats()
  107. >>> #land.write_cats()
  108. Get a Category object or set from a Category object: ::
  109. >>> cats = land.get_cats()
  110. >>> land.set_cats(cats)
  111. Export and import from a file: ::
  112. >>> land.write_cats_rules('land_rules.csv', ';')
  113. >>> land.read_cats_rules('land_rules.csv', ';')
  114. .. autoclass:: pygrass.raster.category.Category
  115. :members:
  116. .. _RasterRow-label:
  117. RastRow
  118. -------
  119. PyGrass allow user to open the maps, in read and write mode,
  120. row by row using the `Raster library`_, there is not support to read and write
  121. to the same map at the same time, for this functionality, please see the
  122. :ref:`RasterSegment-label` and :ref:`RasterNumpy-label` classes.
  123. The RasterRow class allow to read in a randomly order the row from a map, but
  124. it is only possible to write the map using only a sequence order, therefore every
  125. time you are writing a new map, the row is add to the file as the last row. ::
  126. >>> raster = reload(raster)
  127. >>> elev = raster.RasterRow('elevation')
  128. >>> # the cols attribute is set from the current region only when the map is open
  129. >>> elev.cols
  130. >>> elev.open()
  131. >>> elev.is_open()
  132. True
  133. >>> elev.cols
  134. 1500
  135. >>> # we can read the elevation map, row by row
  136. >>> for row in elev[:5]: print(row[:3])
  137. [ 141.99613953 141.27848816 141.37904358]
  138. [ 142.90461731 142.39450073 142.68611145]
  139. [ 143.81854248 143.54707336 143.83972168]
  140. [ 144.56524658 144.58493042 144.86477661]
  141. [ 144.99488831 145.22894287 145.57142639]
  142. >>> # we can open a new map in write mode
  143. >>> new = raster.RasterRow('new')
  144. >>> new.open('w', 'CELL')
  145. >>> # for each elev row we can perform computation, and write the result into
  146. >>> # the new map
  147. >>> for row in elev:
  148. ... new.put_row(row < 144)
  149. ...
  150. >>> # close the maps
  151. >>> new.close()
  152. >>> elev.close()
  153. >>> # check if the map exist
  154. >>> new.exist()
  155. True
  156. >>> # we can open the map in read mode
  157. >>> new.open('r')
  158. >>> for row in new[:5]: print(row[:3])
  159. [1 1 1]
  160. [1 1 1]
  161. [1 1 1]
  162. [0 0 0]
  163. [0 0 0]
  164. >>> new.close()
  165. >>> new.remove()
  166. >>> new.exist()
  167. False
  168. .. autoclass:: pygrass.raster.RasterRow
  169. :members:
  170. .. _RasterRowIO-label:
  171. RasterRowIO
  172. -----------
  173. The RasterRowIO class use the grass `RowIO library`_, and implement a row
  174. cache. The RasterRowIO class support only reading the raster, because the
  175. raster rows can only be written in sequential order, writing by row id is not
  176. supported by design. Hence, we should use the rowio lib only for caching rows
  177. for reading and use the default row write access as in the RasterRow class. ::
  178. >>> raster = reload(raster)
  179. >>> elev = raster.RasterRowIO('elevation')
  180. >>> elev.open('r')
  181. >>> for row in elev[:5]: print(row[:3])
  182. [ 141.99613953 141.27848816 141.37904358]
  183. [ 142.90461731 142.39450073 142.68611145]
  184. [ 143.81854248 143.54707336 143.83972168]
  185. [ 144.56524658 144.58493042 144.86477661]
  186. [ 144.99488831 145.22894287 145.57142639]
  187. >>> elev.close()
  188. .. autoclass:: pygrass.raster.RasterRowIO
  189. :members:
  190. .. _RasterSegment-label:
  191. RastSegment
  192. -----------
  193. The RasterSegment class use the grass `Segmentation library`_, it work dividing
  194. the raster map into small different files, that grass read load into the memory
  195. and write to the hardisk.
  196. The segment library allow to open a map in a read-write mode. ::
  197. >>> raster = reload(raster)
  198. >>> elev = raster.RasterSegment('elevation')
  199. >>> elev.open()
  200. >>> for row in elev[:5]: print(row[:3])
  201. [ 141.99613953 141.27848816 141.37904358]
  202. [ 142.90461731 142.39450073 142.68611145]
  203. [ 143.81854248 143.54707336 143.83972168]
  204. [ 144.56524658 144.58493042 144.86477661]
  205. [ 144.99488831 145.22894287 145.57142639]
  206. >>> new = raster.RasterSegment('new')
  207. >>> new.open('w', 'CELL')
  208. >>> for irow in xrange(elev.rows):
  209. ... new[irow] = elev[irow] < 144
  210. ...
  211. >>> for row in new[:5]: print(row[:3])
  212. [1 1 1]
  213. [1 1 1]
  214. [1 1 1]
  215. [0 0 0]
  216. [0 0 0]
  217. The RasterSegment class define two methods to read and write the map:
  218. * ``get_row`` that return the buffer object with the row that call the
  219. C function ``segment_get_row``. ::
  220. >>> # call explicity the method
  221. >>> elev_row0 = elev.get_row(0)
  222. >>> # call implicity the method
  223. >>> elev_row0 = elev[0]
  224. * ``get`` that return the value of the call map that call the
  225. C function ``segment_get``. ::
  226. >>> # call explicity the method
  227. >>> elev_val_0_0 = elev.get(0, 0)
  228. >>> # call implicity the method
  229. >>> elev_val_0_0 = elev[0, 0]
  230. Similarly to write the map, with ``put_row``, to write a row and with ``put``
  231. to write a single value to the map. ::
  232. >>> # compare the cell value get using the ``get`` method, and take the first
  233. >>> # value of the row with the ``get_row`` method
  234. >>> elev[0, 0] == elev[0][0]
  235. True
  236. >>> # write a new value to a cell,
  237. >>> new[0, 0] = 10
  238. >>> new[0, 0]
  239. 10
  240. >>> new.close()
  241. >>> new.exist()
  242. True
  243. >>> new.remove()
  244. >>> elev.close()
  245. >>> elev.remove()
  246. .. autoclass:: pygrass.raster.RasterSegment
  247. :members:
  248. .. _RasterNumpy-label:
  249. RasterNumpy
  250. -----------
  251. The RasterNumpy class, is based on the `numpy.memmap`_ class If you open an
  252. existing map, the map will be copied on a binary format, and read to avoid
  253. to load all the map in memory. ::
  254. >>> raster = reload(raster)
  255. >>> elev = raster.RasterNumpy('elevation', 'PERMANENT')
  256. >>> elev.open('r')
  257. >>> # in this case RasterNumpy is an extention of the numpy class
  258. >>> # therefore you may use all the fancy things of numpy.
  259. >>> elev[:5, :3]
  260. RasterNumpy([[ 141.99613953, 141.27848816, 141.37904358],
  261. [ 142.90461731, 142.39450073, 142.68611145],
  262. [ 143.81854248, 143.54707336, 143.83972168],
  263. [ 144.56524658, 144.58493042, 144.86477661],
  264. [ 144.99488831, 145.22894287, 145.57142639]], dtype=float32)
  265. >>> el = elev < 144
  266. >>> el[:5, :3]
  267. RasterNumpy([[1, 1, 1],
  268. [1, 1, 1],
  269. [1, 1, 1],
  270. [0, 0, 0],
  271. [0, 0, 0]], dtype=int32)
  272. >>> el.name == None
  273. True
  274. >>> # give a name to the new map
  275. >>> el.name = 'new'
  276. >>> el.exist()
  277. False
  278. >>> el.close()
  279. >>> el.exist()
  280. True
  281. >>> el.remove()
  282. .. autoclass:: pygrass.raster.RasterNumpy
  283. :members:
  284. .. _Buffer-label:
  285. Buffer
  286. ------
  287. The buffer class is used to interact with a memory buffer of a map like a
  288. raster row. The buffer class is based on the `numpy.ndarray`_ class. Therefore
  289. all the nice feature of the ndarray are allowed.
  290. .. autoclass:: pygrass.raster.buffer.Buffer
  291. :members:
  292. .. _numpy.ndarray: http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.html
  293. .. _RowIO-label:
  294. RowIO
  295. ------
  296. .. autoclass:: pygrass.raster.rowio.RowIO
  297. :members:
  298. .. _Segment-label:
  299. Segment
  300. -------
  301. .. autoclass:: pygrass.raster.segment.Segment
  302. :members:
  303. .. _History-label:
  304. History
  305. --------
  306. .. autoclass:: pygrass.raster.history.History
  307. :members:
  308. .. _Raster library: http://grass.osgeo.org/programming7/rasterlib.html
  309. .. _RowIO library: http://grass.osgeo.org/programming7/rowiolib.html
  310. .. _Segmentation library: http://grass.osgeo.org/programming7/segmentlib.html
  311. .. _numpy.memmap: http://docs.scipy.org/doc/numpy/reference/generated/numpy.memmap.html