Thursday, 9 May 2019

Python Scripting [6A]: Layer Sidecar Duplication & Renaming for NZ Rail Maps 1

Our new scripting project as of present is a script called duplicate.py which is specifically with the NZ Rail Maps project and it aims to achieve duplication and renaming of the sidecar files that are associated with raster layers.

Suppose that we have a base raster 4800x7200 pixels named in the following pattern:
  • Timbuctoo-930W8-92NN9.jpg
We have created a Gimp mosaic project in which these layers are stretched to double dimensions in both directions. The mosaic incorporates historical aerial imagery from 1942, 1961, 1974 and 1984. As a result we have exported the following files from Gimp:
  • T1942-930W8x2-92NN9x2.jpg
  • T1961-930W8x2-92NN9x2.jpg
  • T1974-930W8x2-92NN9x2.jpg
  • T1984-930W8x2-92NN9x2.jpg
The next steps are to find the sidecar files for the original base raster mentioned above (named Timbuctoo-) and copy these files and rename the copies so that there are four sets of them to match the four exported rasters from Gimp. You can see as the exports have had "x2" tagged onto the row and column names, this string has to be inserted at two places into the original filename. 

In addition to this straightforward copy exercise, the world file (xx.jgw) has to have its pixel size entries changed. These are on the first and fourth lines and these have to be halved to reflect the fact that the original file is now doubled in each dimension, so that Qgis will draw it to occupy the same space on the canvas as the original.

There is a complication in that one of the sidecar files is the xx.jpg.aux.xml file as the issue is that the extension in this case is considered to be .xml rather than .jpg.aux.xml and therefore this has to be taken account of when determining where to add the "x2" string into the original file name.

A new aspect of this script that is possible because of the way it works is to run it within the directory that contains all of these files so we cd into that directory and then invoke the script passing its full path. And here we can make life easier for ourselves by using the ln -s command to create a symlink to the Scripts directory. So that to invoke the script we only have to type

  • python ~/MapScripts/duplicate.py
Within the script itself we can use os.getcwd() to find out the directory we actually started in and then add that path to filenames when we do file operations. 
 
A typical invocation could look like 
 
  • python ~/MapScripts/duplicate.py -s Timbuctoo- -d "T1942- T1961- T1974- T1984-" -m x2 -p 0.15
 The parameter -s refers to the source filename prefix and the parameter -d refers to one or more destination filename prefixes, which can be placed within quotes if there is more than one. -m refers to the multiplier suffix (the string added to the row and column names signifying the resize factor) and -p to the new pixel size to be inserted in the world file. The -m and -p parameters can be omitted as they will default (in this case, to the values actually shown).
 
The script will search for source .jpg files whose names start with the source prefix and end with .jpg and then look for export files whose names start with each of the specified destination prefixes and end with .jpg. If such a file is found, the sidecar files for the source are copied/renamed/altered as mentioned above, to each destination file name.
 
By next posting I expect this relatively simple script will be completed.