r.import.py 12 KB

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