v.rast.stats.py 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: v.rast.stats
  5. # AUTHOR(S): Markus Neteler
  6. # converted to Python by Glynn Clements
  7. # speed up by Markus Metz
  8. # add column choose by Luca Delucchi
  9. # PURPOSE: Calculates univariate statistics from a GRASS raster map
  10. # only for areas covered by vector objects on a per-category base
  11. # COPYRIGHT: (C) 2005-2010 by the GRASS Development Team
  12. #
  13. # This program is free software under the GNU General Public
  14. # License (>=v2). Read the file COPYING that comes with GRASS
  15. # for details.
  16. #
  17. #############################################################################
  18. #%module
  19. #% description: Calculates univariate statistics from a raster map based on a vector map and uploads statistics to new attribute columns.
  20. #% keyword: vector
  21. #% keyword: statistics
  22. #% keyword: raster
  23. #% keyword: univariate statistics
  24. #% keyword: zonal statistics
  25. #%end
  26. #%flag
  27. #% key: c
  28. #% description: Continue if upload column(s) already exist
  29. #%end
  30. #%option G_OPT_V_MAP
  31. #%end
  32. #%option G_OPT_V_FIELD
  33. #%end
  34. #%option G_OPT_R_INPUT
  35. #% key: raster
  36. #% description: Name of input raster map to calculate statistics from
  37. #%end
  38. #%option
  39. #% key: column_prefix
  40. #% type: string
  41. #% description: Column prefix for new attribute columns
  42. #% required : yes
  43. #%end
  44. #%option
  45. #% key: method
  46. #% type: string
  47. #% description: The methods to use
  48. #% required: no
  49. #% multiple: yes
  50. #% options: number,minimum,maximum,range,average,stddev,variance,coeff_var,sum,first_quartile,median,third_quartile,percentile
  51. #% answer: number,minimum,maximum,range,average,stddev,variance,coeff_var,sum,first_quartile,median,third_quartile,percentile
  52. #%end
  53. #%option
  54. #% key: percentile
  55. #% type: integer
  56. #% description: Percentile to calculate
  57. #% options: 0-100
  58. #% answer: 90
  59. #% required : no
  60. #%end
  61. import sys
  62. import os
  63. import atexit
  64. import grass.script as grass
  65. from grass.exceptions import CalledModuleError
  66. def cleanup():
  67. if rastertmp:
  68. grass.run_command('g.remove', flags='f', type='raster',
  69. name=rastertmp, quiet=True)
  70. grass.run_command('g.remove', flags='f', type='raster',
  71. name='MASK', quiet=True, stderr=nuldev)
  72. if mask_found:
  73. grass.message(_("Restoring previous MASK..."))
  74. grass.run_command('g.rename', raster=(tmpname + "_origmask", 'MASK'),
  75. quiet=True)
  76. # for f in [tmp, tmpname, sqltmp]:
  77. # grass.try_remove(f)
  78. def main():
  79. global tmp, sqltmp, tmpname, nuldev, vector, mask_found, rastertmp
  80. mask_found = False
  81. rastertmp = False
  82. #### setup temporary files
  83. tmp = grass.tempfile()
  84. sqltmp = tmp + ".sql"
  85. # we need a random name
  86. tmpname = grass.basename(tmp)
  87. nuldev = file(os.devnull, 'w')
  88. raster = options['raster']
  89. colprefix = options['column_prefix']
  90. vector = options['map']
  91. layer = options['layer']
  92. percentile = options['percentile']
  93. basecols = options['method'].split(',')
  94. ### setup enviro vars ###
  95. env = grass.gisenv()
  96. mapset = env['MAPSET']
  97. vs = vector.split('@')
  98. if len(vs) > 1:
  99. vect_mapset = vs[1]
  100. else:
  101. vect_mapset = mapset
  102. # does map exist in CURRENT mapset?
  103. if vect_mapset != mapset or not grass.find_file(vector, 'vector', mapset)['file']:
  104. grass.fatal(_("Vector map <%s> not found in current mapset") % vector)
  105. vector = vs[0]
  106. rastertmp = "%s_%s" % (vector, tmpname)
  107. # check the input raster map
  108. if not grass.find_file(raster, 'cell')['file']:
  109. grass.fatal(_("Raster map <%s> not found") % raster)
  110. # check presence of raster MASK, put it aside
  111. mask_found = bool(grass.find_file('MASK', 'cell', mapset)['file'])
  112. if mask_found:
  113. grass.message(_("Raster MASK found, temporarily disabled"))
  114. grass.run_command('g.rename', raster=('MASK', tmpname + "_origmask"),
  115. quiet=True)
  116. # save current settings:
  117. grass.use_temp_region()
  118. # Temporarily aligning region resolution to $RASTER resolution
  119. # keep boundary settings
  120. grass.run_command('g.region', align=raster)
  121. # prepare raster MASK
  122. try:
  123. grass.run_command('v.to.rast', input=vector, output=rastertmp,
  124. use='cat', quiet=True)
  125. except CalledModuleError:
  126. grass.fatal(_("An error occurred while converting vector to raster"))
  127. # dump cats to file to avoid "too many argument" problem:
  128. p = grass.pipe_command('r.category', map=rastertmp, sep=';', quiet=True)
  129. cats = []
  130. for line in p.stdout:
  131. cats.append(line.rstrip('\r\n').split(';')[0])
  132. p.wait()
  133. number = len(cats)
  134. if number < 1:
  135. grass.fatal(_("No categories found in raster map"))
  136. # check if DBF driver used, in this case cut to 10 chars col names:
  137. try:
  138. fi = grass.vector_db(map=vector)[int(layer)]
  139. except KeyError:
  140. grass.fatal(_('There is no table connected to this map. Run v.db.connect or v.db.addtable first.'))
  141. # we need this for non-DBF driver:
  142. dbfdriver = fi['driver'] == 'dbf'
  143. # Find out which table is linked to the vector map on the given layer
  144. if not fi['table']:
  145. grass.fatal(_('There is no table connected to this map. Run v.db.connect or v.db.addtable first.'))
  146. # replaced by user choiche
  147. #basecols = ['n', 'min', 'max', 'range', 'mean', 'stddev', 'variance', 'cf_var', 'sum']
  148. # we need at least three chars to distinguish [mea]n from [med]ian
  149. # so colprefix can't be longer than 6 chars with DBF driver
  150. if dbfdriver:
  151. colprefix = colprefix[:6]
  152. variables_dbf = {}
  153. # by default perccol variable is used only for "variables" variable
  154. perccol = "percentile"
  155. perc = None
  156. for b in basecols:
  157. if b.startswith('p'):
  158. perc = b
  159. if perc:
  160. # namespace is limited in DBF but the % value is important
  161. if dbfdriver:
  162. perccol = "per" + percentile
  163. else:
  164. perccol = "percentile_" + percentile
  165. percindex = basecols.index(perc)
  166. basecols[percindex] = perccol
  167. # dictionary with name of methods and position in "r.univar -gt" output
  168. variables = {'number': 2, 'minimum': 4, 'maximum': 5, 'range': 6,
  169. 'average': 7, 'stddev': 9, 'variance': 10, 'coeff_var': 11,
  170. 'sum': 12, 'first_quartile': 14, 'median': 15,
  171. 'third_quartile': 16, perccol: 17}
  172. # this list is used to set the 'e' flag for r.univar
  173. extracols = ['first_quartile', 'median', 'third_quartile', perccol]
  174. addcols = []
  175. colnames = []
  176. extstat = ""
  177. for i in basecols:
  178. # this check the complete name of out input that should be truncated
  179. for k in variables.keys():
  180. if i in k:
  181. i = k
  182. break
  183. if i in extracols:
  184. extstat = 'e'
  185. # check if column already present
  186. currcolumn = ("%s_%s" % (colprefix, i))
  187. if dbfdriver:
  188. currcolumn = currcolumn[:10]
  189. variables_dbf[currcolumn.replace("%s_" % colprefix, '')] = i
  190. colnames.append(currcolumn)
  191. if currcolumn in grass.vector_columns(vector, layer).keys():
  192. if not flags['c']:
  193. grass.fatal((_("Cannot create column <%s> (already present). ") % currcolumn) +
  194. _("Use -c flag to update values in this column."))
  195. else:
  196. if i == "n":
  197. coltype = "INTEGER"
  198. else:
  199. coltype = "DOUBLE PRECISION"
  200. addcols.append(currcolumn + ' ' + coltype)
  201. if addcols:
  202. grass.verbose(_("Adding columns '%s'") % addcols)
  203. try:
  204. grass.run_command('v.db.addcolumn', map=vector, columns=addcols,
  205. layer=layer)
  206. except CalledModuleError:
  207. grass.fatal(_("Adding columns failed. Exiting."))
  208. # calculate statistics:
  209. grass.message(_("Processing data (%d categories)...") % number)
  210. # get rid of any earlier attempts
  211. grass.try_remove(sqltmp)
  212. f = file(sqltmp, 'w')
  213. # do the stats
  214. p = grass.pipe_command('r.univar', flags='t' + extstat, map=raster,
  215. zones=rastertmp, percentile=percentile, sep=';')
  216. first_line = 1
  217. if not dbfdriver:
  218. f.write("BEGIN TRANSACTION\n")
  219. for line in p.stdout:
  220. if first_line:
  221. first_line = 0
  222. continue
  223. vars = line.rstrip('\r\n').split(';')
  224. f.write("UPDATE %s SET" % fi['table'])
  225. first_var = 1
  226. for colname in colnames:
  227. variable = colname.replace("%s_" % colprefix, '')
  228. if dbfdriver:
  229. variable = variables_dbf[variable]
  230. i = variables[variable]
  231. value = vars[i]
  232. # convert nan, +nan, -nan, inf, +inf, -inf, Infinity, +Infinity,
  233. # -Infinity to NULL
  234. if value.lower().endswith('nan') or 'inf' in value.lower():
  235. value = 'NULL'
  236. if not first_var:
  237. f.write(" , ")
  238. else:
  239. first_var = 0
  240. f.write(" %s=%s" % (colname, value))
  241. f.write(" WHERE %s=%s;\n" % (fi['key'], vars[0]))
  242. if not dbfdriver:
  243. f.write("COMMIT\n")
  244. p.wait()
  245. f.close()
  246. grass.message(_("Updating the database ..."))
  247. exitcode = 0
  248. try:
  249. grass.run_command('db.execute', input=sqltmp,
  250. database=fi['database'], driver=fi['driver'])
  251. grass.verbose((_("Statistics calculated from raster map <{raster}>"
  252. " and uploaded to attribute table"
  253. " of vector map <{vector}>."
  254. ).format(raster=raster, vector=vector)))
  255. except CalledModuleError:
  256. grass.warning(_("Failed to upload statistics to attribute table of vector map <%s>.") % vector)
  257. exitcode = 1
  258. finally:
  259. grass.run_command('g.remove', flags='f', type='raster',
  260. name='MASK', quiet=True, stderr=nuldev)
  261. sys.exit(exitcode)
  262. if __name__ == "__main__":
  263. options, flags = grass.parser()
  264. atexit.register(cleanup)
  265. main()