raster.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. .. _raster-label:
  2. Raster
  3. ======
  4. PyGrass use 4 different Raster classes, that respect the 4 different approaches
  5. of C grass 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. Booth 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 to 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. .. _RasterRow-label:
  115. RastRow
  116. -------
  117. PyGrass allow user to open the maps, in read and write mode,
  118. row by row using the `Raster library`_, there is not support to read and write
  119. to the same map at the same time, for this functionality, please see the
  120. :ref:`RasterSegment-label` and :ref:`RasterNumpy-label` classes.
  121. The RasterRow class allow to read in a randomly order the row from a map, but
  122. it is only possible to write the map using only a sequence order, therefore every
  123. time you are writing a new map, the row is add to the file as the last row. ::
  124. >>> raster = reload(raster)
  125. >>> elev = raster.RasterRow('elevation')
  126. >>> # the cols attribute is set from the current region only when the map is open
  127. >>> elev.cols
  128. >>> elev.open()
  129. >>> elev.is_open()
  130. True
  131. >>> elev.cols
  132. 1500
  133. >>> # we can read the elevation map, row by row
  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. >>> # we can open a new map in write mode
  141. >>> new = raster.RasterRow('new')
  142. >>> new.open('w', 'CELL')
  143. >>> # for each elev row we can perform computation, and write the result into
  144. >>> # the new map
  145. >>> for row in elev:
  146. ... new.put_row(row < 144)
  147. ...
  148. >>> # close the maps
  149. >>> new.close()
  150. >>> elev.close()
  151. >>> # check if the map exist
  152. >>> new.exist()
  153. True
  154. >>> # we can open the map in read mode
  155. >>> new.open('r')
  156. >>> for row in new[:5]: print(row[:3])
  157. [1 1 1]
  158. [1 1 1]
  159. [1 1 1]
  160. [0 0 0]
  161. [0 0 0]
  162. >>> new.close()
  163. >>> new.remove()
  164. >>> new.exist()
  165. False
  166. .. _RasterRowIO-label:
  167. RasterRowIO
  168. -----------
  169. The RasterRowIO class use the grass `RowIO library`_, and implement a row
  170. cache. The RasterRowIO class support only reading the raster, because the
  171. raster rows can only be written in sequential order, writing by row id is not
  172. supported by design. Hence, we should use the rowio lib only for caching rows
  173. for reading and use the default row write access as in the RasterRow class. ::
  174. >>> raster = reload(raster)
  175. >>> elev = raster.RasterRowIO('elevation')
  176. >>> elev.open('r')
  177. >>> for row in elev[:5]: print(row[:3])
  178. [ 141.99613953 141.27848816 141.37904358]
  179. [ 142.90461731 142.39450073 142.68611145]
  180. [ 143.81854248 143.54707336 143.83972168]
  181. [ 144.56524658 144.58493042 144.86477661]
  182. [ 144.99488831 145.22894287 145.57142639]
  183. >>> elev.close()
  184. .. _RasterSegment-label:
  185. RastSegment
  186. -----------
  187. The RasterSegment class use the grass `Segmentation library`_, it work dividing
  188. the raster map into small different files, that grass read load into the memory
  189. and write to the hardisk.
  190. The segment library allow to open a map in a read-write mode. ::
  191. >>> raster = reload(raster)
  192. >>> elev = raster.RasterSegment('elevation')
  193. >>> elev.open()
  194. >>> for row in elev[:5]: print(row[:3])
  195. [ 141.99613953 141.27848816 141.37904358]
  196. [ 142.90461731 142.39450073 142.68611145]
  197. [ 143.81854248 143.54707336 143.83972168]
  198. [ 144.56524658 144.58493042 144.86477661]
  199. [ 144.99488831 145.22894287 145.57142639]
  200. >>> new = raster.RasterSegment('new')
  201. >>> new.open('w', 'CELL')
  202. >>> for irow in xrange(elev.rows):
  203. ... new[irow] = elev[irow] < 144
  204. ...
  205. >>> for row in new[:5]: print(row[:3])
  206. [1 1 1]
  207. [1 1 1]
  208. [1 1 1]
  209. [0 0 0]
  210. [0 0 0]
  211. The RasterSegment class define two methods to read and write the map:
  212. * ``get_row`` that return the buffer object with the row that call the
  213. C function ``segment_get_row``. ::
  214. >>> # call explicity the method
  215. >>> elev_row0 = elev.get_row(0)
  216. >>> # call implicity the method
  217. >>> elev_row0 = elev[0]
  218. * ``get`` that return the value of the call map that call the
  219. C function ``segment_get``. ::
  220. >>> # call explicity the method
  221. >>> elev_val_0_0 = elev.get(0, 0)
  222. >>> # call implicity the method
  223. >>> elev_val_0_0 = elev[0, 0]
  224. Similarly to write the map, with ``put_row``, to write a row and with ``put``
  225. to write a single value to the map. ::
  226. >>> # compare the cell value get using the ``get`` method, and take the first
  227. >>> # value of the row with the ``get_row`` method
  228. >>> elev[0, 0] == elev[0][0]
  229. True
  230. >>> # write a new value to a cell,
  231. >>> new[0, 0] = 10
  232. >>> new[0, 0]
  233. 10
  234. >>> new.close()
  235. >>> new.exist()
  236. True
  237. >>> new.remove()
  238. >>> elev.close()
  239. >>> elev.remove()
  240. .. _RasterNumpy-label:
  241. RasterNumpy
  242. -----------
  243. The RasterNumpy class, is based on the `numpy.memmap`_ class If you open an
  244. existing map, the map will be copied on a binary format, and read to avoid
  245. to load all the map in memory. ::
  246. >>> raster = reload(raster)
  247. >>> elev = raster.RasterNumpy('elevation', 'PERMANENT')
  248. >>> elev.open('r')
  249. >>> # in this case RasterNumpy is an extention of the numpy class
  250. >>> # therefore you may use all the fancy things of numpy.
  251. >>> elev[:5, :3]
  252. RasterNumpy([[ 141.99613953, 141.27848816, 141.37904358],
  253. [ 142.90461731, 142.39450073, 142.68611145],
  254. [ 143.81854248, 143.54707336, 143.83972168],
  255. [ 144.56524658, 144.58493042, 144.86477661],
  256. [ 144.99488831, 145.22894287, 145.57142639]], dtype=float32)
  257. >>> el = elev < 144
  258. >>> el[:5, :3]
  259. RasterNumpy([[1, 1, 1],
  260. [1, 1, 1],
  261. [1, 1, 1],
  262. [0, 0, 0],
  263. [0, 0, 0]], dtype=int32)
  264. >>> el.name == None
  265. True
  266. >>> # give a name to the new map
  267. >>> el.name = 'new'
  268. >>> el.exist()
  269. False
  270. >>> el.close()
  271. >>> el.exist()
  272. True
  273. >>> el.remove()
  274. .. _Buffer-label:
  275. Buffer
  276. ------
  277. The buffer class is used to interact with a memory buffer of a map like a
  278. raster row. The buffer class is based on the `numpy.ndarray`_ class. Therefore
  279. all the nice feature of the ndarray are allowed.
  280. .. autoclass:: pygrass.raster.buffer.Buffer
  281. :members:
  282. .. _numpy.ndarray: http://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.html
  283. .. _RowIO-label:
  284. RowIO
  285. ------
  286. .. autoclass:: pygrass.raster.rowio.RowIO
  287. :members:
  288. .. _Segment-label:
  289. Segment
  290. -------
  291. .. autoclass:: pygrass.raster.segment.Segment
  292. :members:
  293. .. _History-label:
  294. History
  295. --------
  296. .. autoclass:: pygrass.raster.history.History
  297. :members:
  298. .. _Category-label:
  299. Category
  300. --------
  301. .. autoclass:: pygrass.raster.category.Category
  302. :members:
  303. .. _Raster library: http://grass.osgeo.org/programming7/rasterlib.html/
  304. .. _RowIO library: http://grass.osgeo.org/programming7/rowiolib.html
  305. .. _Segmentation library: http://grass.osgeo.org/programming7/segmentlib.html
  306. .. _numpy.memmap: http://docs.scipy.org/doc/numpy/reference/generated/numpy.memmap.html