v_proj.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. """
  2. ***************************************************************************
  3. v_proj.py
  4. ---------
  5. Date : November 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__ = 'November 2017'
  19. __copyright__ = '(C) 2017, Médéric Ribreux'
  20. from qgis.core import QgsProcessingParameterString
  21. from grassprovider.Grass7Utils import Grass7Utils
  22. def processInputs(alg, parameters, context, feedback):
  23. # Grab the projection from the input vector layer
  24. layer = alg.parameterAsLayer(parameters, 'input', context)
  25. alg.setSessionProjectionFromLayer(layer, context)
  26. layerCrs = layer.crs().toProj()
  27. # Creates a new location with this Crs
  28. wkt_file_name = Grass7Utils.exportCrsWktToFile(layer.crs(), context)
  29. newLocation = 'newProj{}'.format(alg.uniqueSuffix)
  30. alg.commands.append('g.proj wkt="{}" location={}'.format(
  31. wkt_file_name, newLocation))
  32. # Go to the newly created location
  33. alg.commands.append('g.mapset mapset=PERMANENT location={}'.format(
  34. newLocation))
  35. # Import the layer
  36. alg.loadVectorLayerFromParameter(
  37. 'input', parameters, context, feedback, False)
  38. # Go back to default location
  39. alg.commands.append('g.mapset mapset=PERMANENT location=temp_location')
  40. # Grab the projected Crs
  41. crs = alg.parameterAsCrs(parameters, 'crs', context)
  42. wkt_file_name = Grass7Utils.exportCrsWktToFile(crs, context)
  43. alg.commands.append('g.proj -c wkt="{}"'.format(wkt_file_name))
  44. # Remove crs parameter
  45. alg.removeParameter('crs')
  46. # Add the location parameter with proper value
  47. location = QgsProcessingParameterString(
  48. 'location',
  49. 'new location',
  50. 'newProj{}'.format(alg.uniqueSuffix)
  51. )
  52. alg.addParameter(location)