Python Scripting [1B]: Scripting to copy map aerial layers 2

So as I said at the end of the last post, I am learning to script in Python. Which is turning out to be breathtakingly simple and easy to do.
I remember that I was going to do this script originally in Python then ended up doing it in Powershell. Why I did that at the time, I really do not remember. I must have got my head messed up somehow trying to work out how to get started in Python because playing with simple scripts has been extremely easy to achieve.
So for today with really only a couple of hours work, here is a simple sample script that will actually get the data we are interested in, directly from the QLR layer definition file that Qgis produces with the list of layers we want to copy.
import xml.etree.ElementTree as ET
tree = ET.parse(‘/mnt/share/serverpc/sources/CopyFiles/Source/layers.qlr’)
root = tree.getroot()
for layername in root.iter(‘layer-tree-layer’):
So as you can see it hard codes where the file is and its name. Provided I remember to use that file name each time then it will be in that directory path which is also where the layers themselves are located. The code is basically invoking an XML parser library which it then uses to get what we need out of the source file.
The output from running that with our sample qlr file looks like this:
{‘source’: ‘./9311M-92N9W.jpg’, ‘checked’: ‘Qt::Checked’, ‘name’: ‘9311M-92N9W’, ‘providerKey’: ‘gdal’, ‘expanded’: ‘1’, ‘id’: ‘9311M_92N9W_a169c725_6030_42d2_a1b5_89fe3fb34a28’}
{‘source’: ‘./9311M-92N9X.jpg’, ‘checked’: ‘Qt::Checked’, ‘name’: ‘9311M-92N9X’, ‘providerKey’: ‘gdal’, ‘expanded’: ‘1’, ‘id’: ‘9311M_92N9X_544c6998_9038_4909_87c3_32e2870074b9’}
{‘source’: ‘./9311M-92N9Y.jpg’, ‘checked’: ‘Qt::Checked’, ‘name’: ‘9311M-92N9Y’, ‘providerKey’: ‘gdal’, ‘expanded’: ‘1’, ‘id’: ‘9311M_92N9Y_7ae2ee80_c3e0_46a2_8e12_be72776f96a9’}
{‘source’: ‘./9311M-92N9Z.jpg’, ‘checked’: ‘Qt::Checked’, ‘name’: ‘9311M-92N9Z’, ‘providerKey’: ‘gdal’, ‘expanded’: ‘1’, ‘id’: ‘9311M_92N9Z_f1e9d09b_91f2_4bb7_b928_68a290ac9331’}
{‘source’: ‘./9311N-92N9W.jpg’, ‘checked’: ‘Qt::Checked’, ‘name’: ‘9311N-92N9W’, ‘providerKey’: ‘gdal’, ‘expanded’: ‘1’, ‘id’: ‘9311N_92N9W_ebf8959b_6048_4436_8dce_c21c2332f286’}
{‘source’: ‘./9311N-92N9X.jpg’, ‘checked’: ‘Qt::Checked’, ‘name’: ‘9311N-92N9X’, ‘providerKey’: ‘gdal’, ‘expanded’: ‘1’, ‘id’: ‘9311N_92N9X_87f0e414_c64b_4952_81e7_66b2f723d34e’}
{‘source’: ‘./9311N-92N9Y.jpg’, ‘checked’: ‘Qt::Checked’, ‘name’: ‘9311N-92N9Y’, ‘providerKey’: ‘gdal’, ‘expanded’: ‘1’, ‘id’: ‘9311N_92N9Y_c0cc8080_10e0_4868_88b0_bb10baf9de58’}
{‘source’: ‘./9311N-92N9Z.jpg’, ‘checked’: ‘Qt::Checked’, ‘name’: ‘9311N-92N9Z’, ‘providerKey’: ‘gdal’, ‘expanded’: ‘1’, ‘id’: ‘9311N_92N9Z_9d9ae036_9ae9_4432_9a56_ecdc82775d5c’}
{‘source’: ‘./9311N-92NB0.jpg’, ‘checked’: ‘Qt::Checked’, ‘name’: ‘9311N-92NB0’, ‘providerKey’: ‘gdal’, ‘expanded’: ‘1’, ‘id’: ‘9311N_92NB0_fdc6a14e_e0c2_4e53_ad39_b564354a5a30’}
{‘source’: ‘./9311N-92NB1.jpg’, ‘checked’: ‘Qt::Checked’, ‘name’: ‘9311N-92NB1’, ‘providerKey’: ‘gdal’, ‘expanded’: ‘1’, ‘id’: ‘9311N_92NB1_4ab35f8d_caa2_4f1a_a36e_ae78b30bcf5c’}
{‘source’: ‘./9311N-92NB2.jpg’, ‘checked’: ‘Qt::Checked’, ‘name’: ‘9311N-92NB2’, ‘providerKey’: ‘gdal’, ‘expanded’: ‘1’, ‘id’: ‘9311N_92NB2_196987aa_ec76_4346_b896_b808b2cc5417’}
{‘source’: ‘./9311N-92NB3.jpg’, ‘checked’: ‘Qt::Checked’, ‘name’: ‘9311N-92NB3’, ‘providerKey’: ‘gdal’, ‘expanded’: ‘1’, ‘id’: ‘9311N_92NB3_bbf06581_e340_4d92_bed8_638bd2d18a3d’}
{‘source’: ‘./9311P-92N9V.jpg’, ‘checked’: ‘Qt::Checked’, ‘name’: ‘9311P-92N9V’, ‘providerKey’: ‘gdal’, ‘expanded’: ‘1’, ‘id’: ‘9311P_92N9V_91859b63_5399_47c1_9e33_1f3a5ea3c7bc’}
{‘source’: ‘./9311P-92N9W.jpg’, ‘checked’: ‘Qt::Checked’, ‘name’: ‘9311P-92N9W’, ‘providerKey’: ‘gdal’, ‘expanded’: ‘1’, ‘id’: ‘9311P_92N9W_2db6b1cd_ad46_4401_b832_3a03fbc3fb8b’}
{‘source’: ‘./9311P-92N9X.jpg’, ‘checked’: ‘Qt::Checked’, ‘name’: ‘9311P-92N9X’, ‘providerKey’: ‘gdal’, ‘expanded’: ‘1’, ‘id’: ‘9311P_92N9X_09a07b9e_dad9_4843_8a3c_8af6f4b68893’}
{‘source’: ‘./9311P-92N9Y.jpg’, ‘checked’: ‘Qt::Checked’, ‘name’: ‘9311P-92N9Y’, ‘providerKey’: ‘gdal’, ‘expanded’: ‘1’, ‘id’: ‘9311P_92N9Y_bc4b2eb8_be9a_4b22_9655_b77b205ad15d’}
{‘source’: ‘./9311P-92N9Z.jpg’, ‘checked’: ‘Qt::Checked’, ‘name’: ‘9311P-92N9Z’, ‘providerKey’: ‘gdal’, ‘expanded’: ‘1’, ‘id’: ‘9311P_92N9Z_4b00ea57_4ac3_455f_a32a_fee8866cfc88’}
{‘source’: ‘./9311P-92NB0.jpg’, ‘checked’: ‘Qt::Checked’, ‘name’: ‘9311P-92NB0’, ‘providerKey’: ‘gdal’, ‘expanded’: ‘1’, ‘id’: ‘9311P_92NB0_aef476ee_7ddd_4334_b5f3_66265650fde9’}
{‘source’: ‘./9311P-92NB1.jpg’, ‘checked’: ‘Qt::Checked’, ‘name’: ‘9311P-92NB1’, ‘providerKey’: ‘gdal’, ‘expanded’: ‘1’, ‘id’: ‘9311P_92NB1_718aabaa_2d59_4dbc_a709_64b05852e0a6’}
{‘source’: ‘./9311P-92NB2.jpg’, ‘checked’: ‘Qt::Checked’, ‘name’: ‘9311P-92NB2’, ‘providerKey’: ‘gdal’, ‘expanded’: ‘1’, ‘id’: ‘9311P_92NB2_0cb19185_927e_48ae_81f1_043db7f5ed06’}
{‘source’: ‘./9311P-92NB3.jpg’, ‘checked’: ‘Qt::Checked’, ‘name’: ‘9311P-92NB3’, ‘providerKey’: ‘gdal’, ‘expanded’: ‘1’, ‘id’: ‘9311P_92NB3_227e337c_2dbf_4e37_be7c_0c1381e6c2df’}
{‘source’: ‘./9311P-92NB5.jpg’, ‘checked’: ‘Qt::Checked’, ‘name’: ‘9311P-92NB5’, ‘providerKey’: ‘gdal’, ‘expanded’: ‘1’, ‘id’: ‘9311P_92NB5_6c0bddd0_b494_460a_bc52_afc923116898’}
{‘source’: ‘./9311Q-92N9V.jpg’, ‘checked’: ‘Qt::Checked’, ‘name’: ‘9311Q-92N9V’, ‘providerKey’: ‘gdal’, ‘expanded’: ‘1’, ‘id’: ‘9311Q_92N9V_c08280e1_597b_4509_aff4_4e78278b1e22’}
{‘source’: ‘./9311Q-92NB1.jpg’, ‘checked’: ‘Qt::Checked’, ‘name’: ‘9311Q-92NB1’, ‘providerKey’: ‘gdal’, ‘expanded’: ‘1’, ‘id’: ‘9311Q_92NB1_e6b90990_75e5_4c67_a74b_4fe86f4d4129’}
{‘source’: ‘./9311Q-92NB2.jpg’, ‘checked’: ‘Qt::Checked’, ‘name’: ‘9311Q-92NB2’, ‘providerKey’: ‘gdal’, ‘expanded’: ‘1’, ‘id’: ‘9311Q_92NB2_8c4891e9_990e_4180_80c6_a66bb0edb97e’}
{‘source’: ‘./9311Q-92NB3.jpg’, ‘checked’: ‘Qt::Checked’, ‘name’: ‘9311Q-92NB3’, ‘providerKey’: ‘gdal’, ‘expanded’: ‘1’, ‘id’: ‘9311Q_92NB3_4c2b5052_8468_483b_8116_249ac7fb82d9’}
So we are interested in the ‘name’ attribute of the layer-tree-layer element, which will give us the filename to copy, which is a wildcard copy. So as you can imagine it will be a fairly short step from here to actually copying the files.
This is where I have to leave things for today. Tomorrow I might be having a day off, and not coming back to this for a couple of days, so NZ Rail Maps will return after a short interlude.