v.rast.stats.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. #!/usr/bin/env python3
  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-2016 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. #% keyword: sampling
  26. #% keyword: querying
  27. #%end
  28. #%flag
  29. #% key: c
  30. #% description: Continue if upload column(s) already exist
  31. #%end
  32. #%flag
  33. #% key: d
  34. #% label: Create densified lines (default: thin lines)
  35. #% description: All cells touched by the line will be set, not only those on the render path
  36. #%end
  37. #%option G_OPT_V_MAP
  38. #%end
  39. #%option G_OPT_V_FIELD
  40. #%end
  41. #%option G_OPT_V_TYPE
  42. #%end
  43. #%option G_OPT_DB_WHERE
  44. #%end
  45. #%option G_OPT_R_INPUTS
  46. #% key: raster
  47. #% description: Name of input raster map to calculate statistics from
  48. #%end
  49. #%option
  50. #% key: column_prefix
  51. #% type: string
  52. #% description: Column prefix for new attribute columns
  53. #% required : yes
  54. #% multiple: yes
  55. #%end
  56. #%option
  57. #% key: method
  58. #% type: string
  59. #% description: The methods to use
  60. #% required: no
  61. #% multiple: yes
  62. #% options: number,null_cells,minimum,maximum,range,average,stddev,variance,coeff_var,sum,first_quartile,median,third_quartile,percentile
  63. #% answer: number,null_cells,minimum,maximum,range,average,stddev,variance,coeff_var,sum,first_quartile,median,third_quartile,percentile
  64. #%end
  65. #%option
  66. #% key: percentile
  67. #% type: integer
  68. #% description: Percentile to calculate
  69. #% options: 0-100
  70. #% answer: 90
  71. #% required : no
  72. #%end
  73. import sys
  74. import os
  75. import atexit
  76. import grass.script as grass
  77. from grass.script.utils import decode
  78. from grass.exceptions import CalledModuleError
  79. def cleanup():
  80. if rastertmp:
  81. grass.run_command('g.remove', flags='f', type='raster',
  82. name=rastertmp, quiet=True)
  83. # for f in [tmp, tmpname, sqltmp]:
  84. # grass.try_remove(f)
  85. def main():
  86. global tmp, sqltmp, tmpname, nuldev, vector, rastertmp
  87. rastertmp = False
  88. # setup temporary files
  89. tmp = grass.tempfile()
  90. sqltmp = tmp + ".sql"
  91. # we need a random name
  92. tmpname = grass.basename(tmp)
  93. nuldev = open(os.devnull, 'w')
  94. rasters = options['raster'].split(',')
  95. colprefixes = options['column_prefix'].split(',')
  96. vector = options['map']
  97. layer = options['layer']
  98. vtypes = options['type']
  99. where = options['where']
  100. percentile = options['percentile']
  101. basecols = options['method'].split(',')
  102. ### setup enviro vars ###
  103. env = grass.gisenv()
  104. mapset = env['MAPSET']
  105. vs = vector.split('@')
  106. if len(vs) > 1:
  107. vect_mapset = vs[1]
  108. else:
  109. vect_mapset = mapset
  110. # does map exist in CURRENT mapset?
  111. if vect_mapset != mapset or not grass.find_file(vector, 'vector', mapset)['file']:
  112. grass.fatal(_("Vector map <%s> not found in current mapset") % vector)
  113. # check if DBF driver used, in this case cut to 10 chars col names:
  114. try:
  115. fi = grass.vector_db(map=vector)[int(layer)]
  116. except KeyError:
  117. grass.fatal(
  118. _('There is no table connected to this map. Run v.db.connect or v.db.addtable first.'))
  119. # we need this for non-DBF driver:
  120. dbfdriver = fi['driver'] == 'dbf'
  121. # colprefix for every raster map?
  122. if len(colprefixes) != len(rasters):
  123. grass.fatal(_("Number of raster maps ({0}) different from \
  124. number of column prefixes ({1})". format(len(rasters),
  125. len(colprefixes))))
  126. vector = vs[0]
  127. rastertmp = "%s_%s" % (vector, tmpname)
  128. for raster in rasters:
  129. # check the input raster map
  130. if not grass.find_file(raster, 'cell')['file']:
  131. grass.fatal(_("Raster map <%s> not found") % raster)
  132. # save current settings:
  133. grass.use_temp_region()
  134. # Temporarily aligning region resolution to $RASTER resolution
  135. # keep boundary settings
  136. grass.run_command('g.region', align=rasters[0])
  137. # prepare base raster for zonal statistics
  138. try:
  139. nlines = grass.vector_info_topo(vector)['lines']
  140. kwargs = {}
  141. if where:
  142. kwargs['where'] = where
  143. # Create densified lines rather than thin lines
  144. if flags['d'] and nlines > 0:
  145. kwargs['flags'] = 'd'
  146. grass.run_command('v.to.rast', input=vector, layer=layer, output=rastertmp,
  147. use='cat', type=vtypes, quiet=True, **kwargs)
  148. except CalledModuleError:
  149. grass.fatal(_("An error occurred while converting vector to raster"))
  150. # dump cats to file to avoid "too many argument" problem:
  151. p = grass.pipe_command('r.category', map=rastertmp, sep=';', quiet=True)
  152. cats = []
  153. for line in p.stdout:
  154. line = decode(line)
  155. cats.append(line.rstrip('\r\n').split(';')[0])
  156. p.wait()
  157. number = len(cats)
  158. if number < 1:
  159. grass.fatal(_("No categories found in raster map"))
  160. # Check if all categories got converted
  161. # Report categories from vector map
  162. vect_cats = grass.read_command('v.category', input=vector, option='report',
  163. flags='g').rstrip('\n').split('\n')
  164. # get number of all categories in selected layer
  165. for vcl in vect_cats:
  166. if vcl.split(' ')[0] == layer and vcl.split(' ')[1] == 'all':
  167. vect_cats_n = int(vcl.split(' ')[2])
  168. if vect_cats_n != number:
  169. grass.warning(_("Not all vector categories converted to raster. \
  170. Converted {0} of {1}.".format(number, vect_cats_n)))
  171. # check if DBF driver used, in this case cut to 10 chars col names:
  172. try:
  173. fi = grass.vector_db(map=vector)[int(layer)]
  174. except KeyError:
  175. grass.fatal(
  176. _('There is no table connected to this map. Run v.db.connect or v.db.addtable first.'))
  177. # we need this for non-DBF driver:
  178. dbfdriver = fi['driver'] == 'dbf'
  179. # Find out which table is linked to the vector map on the given layer
  180. if not fi['table']:
  181. grass.fatal(
  182. _('There is no table connected to this map. Run v.db.connect or v.db.addtable first.'))
  183. # replaced by user choiche
  184. #basecols = ['n', 'min', 'max', 'range', 'mean', 'stddev', 'variance', 'cf_var', 'sum']
  185. for i in range(len(rasters)):
  186. raster = rasters[i]
  187. colprefix = colprefixes[i]
  188. # we need at least three chars to distinguish [mea]n from [med]ian
  189. # so colprefix can't be longer than 6 chars with DBF driver
  190. if dbfdriver:
  191. colprefix = colprefix[:6]
  192. variables_dbf = {}
  193. # by default perccol variable is used only for "variables" variable
  194. perccol = "percentile"
  195. perc = None
  196. for b in basecols:
  197. if b.startswith('p'):
  198. perc = b
  199. if perc:
  200. # namespace is limited in DBF but the % value is important
  201. if dbfdriver:
  202. perccol = "per" + percentile
  203. else:
  204. perccol = "percentile_" + percentile
  205. percindex = basecols.index(perc)
  206. basecols[percindex] = perccol
  207. # dictionary with name of methods and position in "r.univar -gt" output
  208. variables = {'number': 2, 'null_cells': 3, 'minimum': 4, 'maximum': 5, 'range': 6,
  209. 'average': 7, 'stddev': 9, 'variance': 10, 'coeff_var': 11,
  210. 'sum': 12, 'first_quartile': 14, 'median': 15,
  211. 'third_quartile': 16, perccol: 17}
  212. # this list is used to set the 'e' flag for r.univar
  213. extracols = ['first_quartile', 'median', 'third_quartile', perccol]
  214. addcols = []
  215. colnames = []
  216. extstat = ""
  217. for i in basecols:
  218. # this check the complete name of out input that should be truncated
  219. for k in variables.keys():
  220. if i in k:
  221. i = k
  222. break
  223. if i in extracols:
  224. extstat = 'e'
  225. # check if column already present
  226. currcolumn = ("%s_%s" % (colprefix, i))
  227. if dbfdriver:
  228. currcolumn = currcolumn[:10]
  229. variables_dbf[currcolumn.replace("%s_" % colprefix, '')] = i
  230. colnames.append(currcolumn)
  231. if currcolumn in grass.vector_columns(vector, layer).keys():
  232. if not flags['c']:
  233. grass.fatal((_("Cannot create column <%s> (already present). ") % currcolumn) +
  234. _("Use -c flag to update values in this column."))
  235. else:
  236. if i == "n":
  237. coltype = "INTEGER"
  238. else:
  239. coltype = "DOUBLE PRECISION"
  240. addcols.append(currcolumn + ' ' + coltype)
  241. if addcols:
  242. grass.verbose(_("Adding columns '%s'") % addcols)
  243. try:
  244. grass.run_command('v.db.addcolumn', map=vector, columns=addcols,
  245. layer=layer)
  246. except CalledModuleError:
  247. grass.fatal(_("Adding columns failed. Exiting."))
  248. # calculate statistics:
  249. grass.message(_("Processing input data (%d categories)...") % number)
  250. # get rid of any earlier attempts
  251. grass.try_remove(sqltmp)
  252. f = open(sqltmp, 'w')
  253. # do the stats
  254. p = grass.pipe_command('r.univar', flags='t' + extstat, map=raster,
  255. zones=rastertmp, percentile=percentile, sep=';')
  256. first_line = 1
  257. f.write("{0}\n".format(grass.db_begin_transaction(fi['driver'])))
  258. for line in p.stdout:
  259. if first_line:
  260. first_line = 0
  261. continue
  262. vars = decode(line).rstrip('\r\n').split(';')
  263. f.write("UPDATE %s SET" % fi['table'])
  264. first_var = 1
  265. for colname in colnames:
  266. variable = colname.replace("%s_" % colprefix, '', 1)
  267. if dbfdriver:
  268. variable = variables_dbf[variable]
  269. i = variables[variable]
  270. value = vars[i]
  271. # convert nan, +nan, -nan, inf, +inf, -inf, Infinity, +Infinity,
  272. # -Infinity to NULL
  273. if value.lower().endswith('nan') or 'inf' in value.lower():
  274. value = 'NULL'
  275. if not first_var:
  276. f.write(" , ")
  277. else:
  278. first_var = 0
  279. f.write(" %s=%s" % (colname, value))
  280. f.write(" WHERE %s=%s;\n" % (fi['key'], vars[0]))
  281. f.write("{0}\n".format(grass.db_commit_transaction(fi['driver'])))
  282. p.wait()
  283. f.close()
  284. grass.message(_("Updating the database ..."))
  285. exitcode = 0
  286. try:
  287. grass.run_command('db.execute', input=sqltmp,
  288. database=fi['database'], driver=fi['driver'])
  289. grass.verbose((_("Statistics calculated from raster map <{raster}>"
  290. " and uploaded to attribute table"
  291. " of vector map <{vector}>."
  292. ).format(raster=raster, vector=vector)))
  293. except CalledModuleError:
  294. grass.warning(
  295. _("Failed to upload statistics to attribute table of vector map <%s>.") %
  296. vector)
  297. exitcode = 1
  298. sys.exit(exitcode)
  299. if __name__ == "__main__":
  300. options, flags = grass.parser()
  301. atexit.register(cleanup)
  302. main()