r.import.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: r.import
  5. #
  6. # AUTHOR(S): Markus Metz
  7. #
  8. # PURPOSE: Import and reproject on the fly
  9. #
  10. # COPYRIGHT: (C) 2015 GRASS development team
  11. #
  12. # This program is free software under the GNU General
  13. # Public License (>=v2). Read the file COPYING that
  14. # comes with GRASS for details.
  15. #
  16. #############################################################################
  17. #%module
  18. #% description: Imports raster data into a GRASS raster map using GDAL library and reprojects on the fly.
  19. #% keyword: raster
  20. #% keyword: import
  21. #% keyword: projection
  22. #%end
  23. #%option G_OPT_F_INPUT
  24. #% description: Name of GDAL dataset to be imported
  25. #% guisection: Input
  26. #%end
  27. #%option
  28. #% key: band
  29. #% type: integer
  30. #% required: no
  31. #% multiple: yes
  32. #% description: Input band(s) to select (default is all bands)
  33. #% guisection: Input
  34. #%end
  35. #%option
  36. #% key: memory
  37. #% type: integer
  38. #% required: no
  39. #% multiple: no
  40. #% options: 0-2047
  41. #% label: Maximum memory to be used (in MB)
  42. #% description: Cache size for raster rows
  43. #% answer: 300
  44. #%end
  45. #%option G_OPT_R_OUTPUT
  46. #% description: Name for output raster map
  47. #% guisection: Output
  48. #%end
  49. #%option
  50. #% key: resample
  51. #% type: string
  52. #% required: no
  53. #% multiple: no
  54. #% options: nearest,bilinear,bicubic,lanczos,bilinear_f,bicubic_f,lanczos_f
  55. #% description: Resampling method to use for reprojection
  56. #% descriptions: nearest;nearest neighbor;bilinear;bilinear interpolation;bicubic;bicubic interpolation;lanczos;lanczos filter;bilinear_f;bilinear interpolation with fallback;bicubic_f;bicubic interpolation with fallback;lanczos_f;lanczos filter with fallback
  57. #% answer: nearest
  58. #% guisection: Output
  59. #%end
  60. #%option
  61. #% key: extent
  62. #% type: string
  63. #% required: no
  64. #% multiple: no
  65. #% options: input,region
  66. #% answer: input
  67. #% description: Output raster map extent
  68. #% descriptions: region;extent of current region;input;extent of input map
  69. #% guisection: Output
  70. #%end
  71. #%option
  72. #% key: resolution
  73. #% type: string
  74. #% required: no
  75. #% multiple: no
  76. #% answer: estimated
  77. #% options: estimated,value,region
  78. #% description: Resolution of output raster map (default: estimated)
  79. #% descriptions: estimated;estimated resolution;value;user-specified resolution;region;current region resolution
  80. #% guisection: Output
  81. #%end
  82. #%option
  83. #% key: resolution_value
  84. #% type: double
  85. #% required: no
  86. #% multiple: no
  87. #% description: Resolution of output raster map (use with option resolution=value)
  88. #% guisection: Output
  89. #%end
  90. #%flag
  91. #% key: e
  92. #% description: Estimate resolution only
  93. #% guisection: Optional
  94. #%end
  95. #%flag
  96. #% key: n
  97. #% description: Do not perform region cropping optimization
  98. #% guisection: Optional
  99. #%end
  100. #%flag
  101. #% key: l
  102. #% description: Force Lat/Lon maps to fit into geographic coordinates (90N,S; 180E,W)
  103. #%end
  104. import sys
  105. import os
  106. import atexit
  107. import math
  108. import grass.script as grass
  109. from grass.exceptions import CalledModuleError
  110. # initialize global vars
  111. TMPLOC = None
  112. SRCGISRC = None
  113. GISDBASE = None
  114. TMP_REG_NAME = None
  115. def cleanup():
  116. # remove temp location
  117. if TMPLOC:
  118. grass.try_rmdir(os.path.join(GISDBASE, TMPLOC))
  119. if SRCGISRC:
  120. grass.try_remove(SRCGISRC)
  121. if TMP_REG_NAME:
  122. grass.run_command('g.remove', type='vector', name=TMP_REG_NAME,
  123. flags='f', quiet=True)
  124. def main():
  125. global TMPLOC, SRCGISRC, GISDBASE, TMP_REG_NAME
  126. GDALdatasource = options['input']
  127. output = options['output']
  128. method = options['resample']
  129. memory = options['memory']
  130. bands = options['band']
  131. tgtres = options['resolution']
  132. if options['resolution_value']:
  133. if tgtres != 'value':
  134. grass.fatal(_("To set custom resolution value, select 'value' in resolution option"))
  135. tgtres_value = float(options['resolution_value'])
  136. if tgtres_value <= 0:
  137. grass.fatal(_("Resolution value can't be smaller than 0"))
  138. elif tgtres == 'value':
  139. grass.fatal(_("Please provide the resolution for the imported dataset or change to 'estimated' resolution"))
  140. grassenv = grass.gisenv()
  141. tgtloc = grassenv['LOCATION_NAME']
  142. tgtmapset = grassenv['MAPSET']
  143. GISDBASE = grassenv['GISDBASE']
  144. tgtgisrc = os.environ['GISRC']
  145. SRCGISRC = grass.tempfile()
  146. TMPLOC = 'temp_import_location_' + str(os.getpid())
  147. f = open(SRCGISRC, 'w')
  148. f.write('MAPSET: PERMANENT\n')
  149. f.write('GISDBASE: %s\n' % GISDBASE)
  150. f.write('LOCATION_NAME: %s\n' % TMPLOC)
  151. f.write('GUI: text\n')
  152. f.close()
  153. tgtsrs = grass.read_command('g.proj', flags='j', quiet=True)
  154. # create temp location from input without import
  155. grass.verbose(_("Creating temporary location for <%s>...") % GDALdatasource)
  156. parameters = dict(input=GDALdatasource, output=output,
  157. memory=memory, flags='c',
  158. location=TMPLOC, quiet=True)
  159. if bands:
  160. parameters['band'] = bands
  161. try:
  162. grass.run_command('r.in.gdal', **parameters)
  163. except CalledModuleError:
  164. grass.fatal(_("Unable to read GDAL dataset <%s>") % GDALdatasource)
  165. # switch to temp location
  166. os.environ['GISRC'] = str(SRCGISRC)
  167. # switch to target location
  168. os.environ['GISRC'] = str(tgtgisrc)
  169. # try r.in.gdal directly first
  170. additional_flags = 'l' if flags['l'] else ''
  171. if grass.run_command('r.in.gdal', input=GDALdatasource, flags='j',
  172. errors='status', quiet=True) == 0:
  173. parameters = dict(input=GDALdatasource, output=output,
  174. memory=memory, flags='k' + additional_flags)
  175. if bands:
  176. parameters['band'] = bands
  177. try:
  178. grass.run_command('r.in.gdal', **parameters)
  179. grass.verbose(_("Input <%s> successfully imported without reprojection") % GDALdatasource)
  180. return 0
  181. except CalledModuleError as e:
  182. grass.fatal(_("Unable to import GDAL dataset <%s>") % GDALdatasource)
  183. # make sure target is not xy
  184. if grass.parse_command('g.proj', flags='g')['name'] == 'xy_location_unprojected':
  185. grass.fatal(_("Coordinate reference system not available for current location <%s>") % tgtloc)
  186. # switch to temp location
  187. os.environ['GISRC'] = str(SRCGISRC)
  188. # make sure input is not xy
  189. if grass.parse_command('g.proj', flags='g')['name'] == 'xy_location_unprojected':
  190. grass.fatal(_("Coordinate reference system not available for input <%s>") % GDALdatasource)
  191. # import into temp location
  192. grass.verbose(_("Importing <%s> to temporary location...") % GDALdatasource)
  193. parameters = dict(input=GDALdatasource, output=output,
  194. memory=memory, flags='k' + additional_flags)
  195. if bands:
  196. parameters['band'] = bands
  197. try:
  198. grass.run_command('r.in.gdal', **parameters)
  199. except CalledModuleError:
  200. grass.fatal(_("Unable to import GDAL dataset <%s>") % GDALdatasource)
  201. outfiles = grass.list_grouped('raster')['PERMANENT']
  202. # is output a group?
  203. group = False
  204. path = os.path.join(GISDBASE, TMPLOC, 'group', output)
  205. if os.path.exists(path):
  206. group = True
  207. path = os.path.join(GISDBASE, TMPLOC, 'group', output, 'POINTS')
  208. if os.path.exists(path):
  209. grass.fatal(_("Input contains GCPs, rectification is required"))
  210. # switch to target location
  211. os.environ['GISRC'] = str(tgtgisrc)
  212. region = grass.region()
  213. rflags = None
  214. if flags['n']:
  215. rflags = 'n'
  216. for outfile in outfiles:
  217. n = region['n']
  218. s = region['s']
  219. e = region['e']
  220. w = region['w']
  221. grass.use_temp_region()
  222. if options['extent'] == 'input':
  223. # r.proj -g
  224. try:
  225. tgtextents = grass.read_command('r.proj', location=TMPLOC,
  226. mapset='PERMANENT',
  227. input=outfile, flags='g',
  228. memory=memory, quiet=True)
  229. except CalledModuleError:
  230. grass.fatal(_("Unable to get reprojected map extent"))
  231. try:
  232. srcregion = grass.parse_key_val(tgtextents, val_type=float, vsep=' ')
  233. n = srcregion['n']
  234. s = srcregion['s']
  235. e = srcregion['e']
  236. w = srcregion['w']
  237. except ValueError: # import into latlong, expect 53:39:06.894826N
  238. srcregion = grass.parse_key_val(tgtextents, vsep=' ')
  239. n = grass.float_or_dms(srcregion['n'][:-1]) * \
  240. (-1 if srcregion['n'][-1] == 'S' else 1)
  241. s = grass.float_or_dms(srcregion['s'][:-1]) * \
  242. (-1 if srcregion['s'][-1] == 'S' else 1)
  243. e = grass.float_or_dms(srcregion['e'][:-1]) * \
  244. (-1 if srcregion['e'][-1] == 'W' else 1)
  245. w = grass.float_or_dms(srcregion['w'][:-1]) * \
  246. (-1 if srcregion['w'][-1] == 'W' else 1)
  247. grass.run_command('g.region', n=n, s=s, e=e, w=w)
  248. # v.in.region in tgt
  249. vreg = TMP_REG_NAME = 'vreg_tmp_' + str(os.getpid())
  250. grass.run_command('v.in.region', output=vreg, quiet=True)
  251. grass.del_temp_region()
  252. # reproject to src
  253. # switch to temp location
  254. os.environ['GISRC'] = str(SRCGISRC)
  255. try:
  256. grass.run_command('v.proj', input=vreg, output=vreg,
  257. location=tgtloc, mapset=tgtmapset, quiet=True)
  258. except CalledModuleError:
  259. grass.fatal(_("Unable to reproject to source location"))
  260. # set region from region vector
  261. grass.run_command('g.region', raster=outfile)
  262. grass.run_command('g.region', vector=vreg)
  263. # align to first band
  264. grass.run_command('g.region', align=outfile)
  265. # get number of cells
  266. cells = grass.region()['cells']
  267. estres = math.sqrt((n - s) * (e - w) / cells)
  268. # remove from source location for multi bands import
  269. grass.run_command('g.remove', type='vector', name=vreg,
  270. flags='f', quiet=True)
  271. os.environ['GISRC'] = str(tgtgisrc)
  272. grass.run_command('g.remove', type='vector', name=vreg,
  273. flags='f', quiet=True)
  274. grass.message(_("Estimated target resolution for input band <{out}>: {res}").format(out=outfile, res=estres))
  275. if flags['e']:
  276. continue
  277. if options['extent'] == 'input':
  278. grass.use_temp_region()
  279. grass.run_command('g.region', n=n, s=s, e=e, w=w)
  280. res = None
  281. if tgtres == 'estimated':
  282. res = estres
  283. elif tgtres == 'value':
  284. res = tgtres_value
  285. grass.message(_("Using given resolution for input band <{out}>: {res}").format(out=outfile, res=res))
  286. # align to requested resolution
  287. grass.run_command('g.region', res=res, flags='a')
  288. else:
  289. curr_reg = grass.region()
  290. grass.message(_("Using current region resolution for input band "
  291. "<{out}>: nsres={ns}, ewres={ew}").format(out=outfile, ns=curr_reg['nsres'],
  292. ew=curr_reg['ewres']))
  293. # r.proj
  294. grass.message(_("Reprojecting <%s>...") % outfile)
  295. try:
  296. grass.run_command('r.proj', location=TMPLOC,
  297. mapset='PERMANENT', input=outfile,
  298. method=method, resolution=res,
  299. memory=memory, flags=rflags, quiet=True)
  300. except CalledModuleError:
  301. grass.fatal(_("Unable to to reproject raster <%s>") % outfile)
  302. if grass.raster_info(outfile)['min'] is None:
  303. grass.fatal(_("The reprojected raster <%s> is empty") % outfile)
  304. if options['extent'] == 'input':
  305. grass.del_temp_region()
  306. if flags['e']:
  307. return 0
  308. if group:
  309. grass.run_command('i.group', group=output, input=','.join(outfiles))
  310. return 0
  311. if __name__ == "__main__":
  312. options, flags = grass.parser()
  313. atexit.register(cleanup)
  314. sys.exit(main())