2

After running a GRASS watershed algorithm I've got nine new layers:

enter image description here

Each one is in its own folder with a long hex name under a processing folder with a long hex name:

/tmp/processing_7dcdb54465034e139116032dc507384b/d6a3c2fc521146f792f01b0fc6c6acad/spi.tif
/tmp/processing_7dcdb54465034e139116032dc507384b/b7fe7e0b26934216b084c456f90f8873/drainage.tif

These are in a temporary folder (/tmp) so what I want to do is copy them to a less temporary folder (eg in my /home) and (ideally) update the QGIS layers to point to them in their new location.

I tried some plugins that do this kind of thing but for all layers, in order to make a transportable set of data and QGS project files:

  • QConsolidate3 : "Error: Parse error at line 1, column 1: error occurred while parsing element" on running it. Output folder empty.
  • QPackage : Install fail with "TypeError QVariant object is not subscriptable"
  • relocator : mentioned in another SO post, can't see it in my plugin dialog.

I could move that processing folder but there are also a few other folders in there with things I don't want. Selecting all the raster layers and using "Save As" or "Export" only saves the first layer.

I think (before I write a plugin to do this myself) I can create a layer definition file from the group, get the paths, and move the files manually. Luckily I've not set up any complex styles (yet) so losing the styles isn't too much trouble. But if I'm missing an easy way to do this I'd like to hear it...

Of course what I probably should have done is set the GRASS output folder to be where I wanted it in the first place....

0

Instead of moving the files, you could copy them over to a new destination, remove the currently loaded files and add the newly copied ones into the same group:

from shutil import copyfile

root = QgsProject.instance().layerTreeRoot()
main_group = root.findGroup('Hydrology')
my_folder = "/home/"

for layers in main_group.children():
    layer = layers.layer()
    name = layer.name()
    copyfile(layer.source(), my_folder + layer.name() + ".tif")
    QgsMapLayerRegistry.instance().removeMapLayers( [layer] )
    new_layer = QgsRasterLayer(my_folder + name + ".tif", name)
    QgsMapLayerRegistry.instance().addMapLayer(new_layer, False)
    main_group.insertChildNode(-1, QgsLayerTreeLayer(new_layer))

Edit:

You could copy the files to a new location and change the existing data source of the raster layers to point to the new path so that the existing style is preserved by using QgsRasterLayer::setDataSource():

from shutil import copyfile

root = QgsProject.instance().layerTreeRoot()
main_group = root.findGroup('Hydrology')
my_folder = "/home/"
options = QgsDataProvider.ProviderOptions()

for layers in main_group.children():
    layer = layers.layer()
    name = layer.name()
    new_path = my_folder + name + ".tif"
    copyfile(layer.source(), new_path)
    layer.setDataSource(new_path, name, 'gdal', options, False)
  • Would it be possible to loop over the layers, copy or move the file, and change the source of the layer (instead of creating new layers)? That would preserve the layer styles as well... – Spacedman May 31 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.