0

I am looking to download files that are saved in a server location with a file path that is informed by attribute information created in the graphical modeler.

The information that I would like to add to the file path is stored as values in each row of the attribute table under the field 'TILENAME'.

I would like to input these values (e.g. 'SK74NE' in the field 'TILENAME') to inform the file path:

'file:///X:/GIS/Location Map/OS Open Map Local Apr 2019/SK/SK74NE.tif'

I have tried various appropriates using select by attribute, extract by attribute and refactoring fields but haven't had any luck.

  • Are you asking how to create a field with the full file path in it? – csk Jul 18 at 14:53
  • Kind of. The path that I displayed is the path that I am after, except that I want the 'SK' and 'SK74NE' to be informed by values stored in a field called 'TILENAME'. The values that are stored, e.g. 'SK74NE' contain the information that is needed to inform this path but I can't seem to grab it as an expression. – R.B Jul 18 at 15:17
1

To assemble a full filepath, where part of the filepath is constant and part of it is stored in a field, concatenate the constant parts of the filepath (as strings) with the field name in double quotes.

These expressions will work in the Field Calculator, which automatically iterates through all the features of the current layer.

  • This concatenation method will deliver a null value if the "TILENAME" field has a null value: 'file:///X:/GIS/Location Map/OS Open Map Local Apr 2019/SK/' || "TILENAME" || '.tif'
  • This concatenation method will deliver 'file:///X:/GIS/Location Map/OS Open Map Local Apr 2019/SK/.tif' if the "TILENAME" field has a null value: concat('file:///X:/GIS/Location Map/OS Open Map Local Apr 2019/SK/',"TILENAME", '.tif')

In the Download File tool in the model builder, there's no "current layer." So you have to specify what layer and what feature you want to evaluate the expression for.

Instead of the field name ("TILENAME"), use the attribute(feature,attribute_name) function.

For the feature parameter of the attribute function, use one of these two functions:

  • get_feature(layer,attribute,value) Returns the first feature of a layer matching a given attribute value.
  • get_feature_by_id(layer,feature_id) Returns the feature with an id on a layer. Syntax

For example, to get the filepath for the feature whose ID is 0, use this expression:

'file:///X:/GIS/Location Map/OS Open Map Local Apr 2019/SK/' || attribute(get_feature_by_id('layername',0)) || '.tif'

If you add the ID to the model as a Number input, you should be able to substitute it for the ID value in the expression above.

A note about batch processing / downloading multiple files:

Unfortunately, the QGIS model builder doesn't have an iterator yet. So it's only possible to download one file per instance of the Download file algorithm.

The Download file algorithm does have a batch processing mode outside the model builder, but it doesn't accept an expression as input. So you have to enter the exact file path for every download.

One workaround would be to use the Field Calculator (either inside or outside the modeler) to add the filepaths to a new field. Then copy and paste the filepaths from the attribute table into the Download file algorithm.

Another workaround is creating a custom script to download all the files. As script-writing goes, it's probably a fairly simple task. Here's an example: Simplify repetitive tasks in QGIS Graphical Modeler

  • Unfortunately I am receiving the same error using both methods: 'Download failed: Error opening X:/GIS/Location Map/OS Open Map Local Apr 2019/SK/[TILENAME].tif: The system cannot find the path specified.' It looks like the field name is not recognised. – R.B Jul 18 at 15:29
  • Where are you using the expressions? They have to go into the QGIS expression builder to be evaluated to complete filepaths. – csk Jul 18 at 15:30
  • I am using the expression in the expression dialogue that is located in the Download file algorithm. – R.B Jul 18 at 15:33
  • Oh, I see. Those expressions would work in the Field Calculator, which would automatically evaluate them for every feature in a layer. In the model builder, use the attribute() function to get the attribute for a specific feature, and also somehow iterate through all the features in the layer. But it looks like the model builder doesn't have an iterator (yet). – csk Jul 18 at 15:44
  • I don't see any reasonable solution to your problem. The Download file tool has a batch processing mode outside the modeler, but it doesn't accept an expression. The best workaround (and it's not very good) I can think of is to use the Field Calculator to add a new field with the full filepath, then copy and paste the paths from that field into the batch mode of Download file. Otherwise you're looking at creating a custom script, as they did here: gis.stackexchange.com/q/176599/81764 – csk Jul 18 at 15:49
1

If you are having problems with the download you could use python, example:

from shutil import copyfile
from os.path import join, basename

download_folder = '/home/bera/GIS/test/downloads/'
filepath_field = 'filepath'

layer = iface.activeLayer()
for feature in layer.getFeatures():
    src = feature[filepath_field]
    dst = join(download_folder, basename(feature[filepath_field]))
    copyfile(src, dst)

enter image description here

  • 1
    Thanks BERA. I'll have a play around and see if it can get this to work for my application. – R.B Jul 22 at 11:55
  • Just to provide an update, the code works but it is not suitable for my example. The 'filepath' refers to a 6 character code (e.g. 'NJ00NE') but does not contain the file path as such. I then need this code to be added to the folder path 'X:/CAD/AutoCAD/Location Map/OS Open Map Local Apr 2019/AA/AA%%%%', where AA refers to the first two characters of the six character code 'NJ' and where AA%%%% refers to the full six characters. – R.B Jul 25 at 9:24

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.