r_proj.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. """
  2. ***************************************************************************
  3. r_proj.py
  4. ---------
  5. Date : October 2017
  6. Copyright : (C) 2017 by Médéric Ribreux
  7. Email : medspx at medspx dot fr
  8. ***************************************************************************
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License as published by *
  12. * the Free Software Foundation; either version 2 of the License, or *
  13. * (at your option) any later version. *
  14. * *
  15. ***************************************************************************
  16. """
  17. __author__ = 'Médéric Ribreux'
  18. __date__ = 'October 2017'
  19. __copyright__ = '(C) 2017, Médéric Ribreux'
  20. from qgis.core import QgsProcessingParameterString
  21. from processing.tools.system import isWindows
  22. from grassprovider.Grass7Utils import Grass7Utils
  23. def processInputs(alg, parameters, context, feedback):
  24. # Grab the projection from the input vector layer
  25. layer = alg.parameterAsLayer(parameters, 'input', context)
  26. # Creates a new location with this Crs
  27. wkt_file_name = Grass7Utils.exportCrsWktToFile(layer.crs(), context)
  28. newLocation = 'newProj{}'.format(alg.uniqueSuffix)
  29. alg.commands.append('g.proj wkt="{}" location={}'.format(
  30. wkt_file_name, newLocation))
  31. # Go to the newly created location
  32. alg.commands.append('g.mapset mapset=PERMANENT location={}'.format(
  33. newLocation))
  34. # Import the layer
  35. alg.loadRasterLayerFromParameter(
  36. 'input', parameters, context, False)
  37. # Go back to default location
  38. alg.commands.append('g.mapset mapset=PERMANENT location=temp_location')
  39. # Grab the projected Crs
  40. crs = alg.parameterAsCrs(parameters, 'crs', context)
  41. wkt_file_name = Grass7Utils.exportCrsWktToFile(crs, context)
  42. alg.commands.append('g.proj -c wkt="{}"'.format(wkt_file_name))
  43. # Remove crs parameter
  44. alg.removeParameter('crs')
  45. # Add the location parameter with proper value
  46. location = QgsProcessingParameterString(
  47. 'location',
  48. 'new location',
  49. 'newProj{}'.format(alg.uniqueSuffix)
  50. )
  51. alg.addParameter(location)
  52. # And set the region
  53. grassName = alg.exportedLayers['input']
  54. # We use the shell to capture the results from r.proj -g
  55. if isWindows():
  56. # TODO: make some tests under a non POSIX shell
  57. alg.commands.append('set regVar=')
  58. alg.commands.append('for /f "delims=" %%a in (\'r.proj -g input^="{}" location^="{}"\') do @set regVar=%%a'.format(
  59. grassName, newLocation))
  60. alg.commands.append('g.region -a %regVar%')
  61. else:
  62. alg.commands.append('g.region -a $(r.proj -g input="{}" location="{}")'.format(
  63. grassName, newLocation))