def processInputs(alg, parameters, context, feedback): # Grab the projection from the input vector layer layer = alg.parameterAsLayer(parameters, 'input', context) alg.setSessionProjectionFromLayer(layer) layerCrs = layer.crs().toProj() # Creates a new location with this Crs wkt_file_name = Grass7Utils.exportCrsWktToFile(layer.crs()) newLocation = 'newProj{}'.format(alg.uniqueSuffix) alg.commands.append('g.proj wkt="{}" location={}'.format( wkt_file_name, newLocation)) # Go to the newly created location alg.commands.append( 'g.mapset mapset=PERMANENT location={}'.format(newLocation)) # Import the layer alg.loadVectorLayerFromParameter('input', parameters, context, feedback, False) # Go back to default location alg.commands.append('g.mapset mapset=PERMANENT location=temp_location') # Grab the projected Crs crs = alg.parameterAsCrs(parameters, 'crs', context) wkt_file_name = Grass7Utils.exportCrsWktToFile(crs) alg.commands.append('g.proj -c wkt="{}"'.format(wkt_file_name)) # Remove crs parameter alg.removeParameter('crs') # Add the location parameter with proper value location = QgsProcessingParameterString( 'location', 'new location', 'newProj{}'.format(alg.uniqueSuffix)) alg.addParameter(location)
def processInputs(alg, parameters, context, feedback): # Grab the projection from the input vector layer layer = alg.parameterAsLayer(parameters, 'input', context) # Creates a new location with this Crs wkt_file_name = Grass7Utils.exportCrsWktToFile(layer.crs()) newLocation = 'newProj{}'.format(alg.uniqueSuffix) alg.commands.append('g.proj wkt="{}" location={}'.format( wkt_file_name, newLocation)) # Go to the newly created location alg.commands.append('g.mapset mapset=PERMANENT location={}'.format( newLocation)) # Import the layer alg.loadRasterLayerFromParameter( 'input', parameters, context, False) # Go back to default location alg.commands.append('g.mapset mapset=PERMANENT location=temp_location') # Grab the projected Crs crs = alg.parameterAsCrs(parameters, 'crs', context) wkt_file_name = Grass7Utils.exportCrsWktToFile(crs) alg.commands.append('g.proj -c wkt="{}"'.format(wkt_file_name)) # Remove crs parameter alg.removeParameter('crs') # Add the location parameter with proper value location = QgsProcessingParameterString( 'location', 'new location', 'newProj{}'.format(alg.uniqueSuffix) ) alg.addParameter(location) # And set the region grassName = alg.exportedLayers['input'] # We use the shell to capture the results from r.proj -g if isWindows(): # TODO: make some tests under a non POSIX shell alg.commands.append('set regVar=') alg.commands.append('for /f "delims=" %%a in (\'r.proj -g input^="{}" location^="{}"\') do @set regVar=%%a'.format( grassName, newLocation)) alg.commands.append('g.region -a %regVar%') else: alg.commands.append('g.region -a $(r.proj -g input="{}" location="{}")'.format( grassName, newLocation))
def processOutputs(alg, parameters, context, feedback): crs = alg.parameterAsCrs(parameters, 'sourceproj', context) wkt_file_name = Grass7Utils.exportCrsWktToFile(crs) alg.commands.insert(0, 'g.proj -c wkt="{}"'.format(wkt_file_name))