Monday, 21 January 2019

Python Scripting [1A]: Scripting to copy map aerial layers 1

In my arsenal of computers I have one low spec one that still runs Windows. Even with all the stuff I have for Linux, there are still a handful of tasks that use Windows only software, although this has diminished to the point that this computer often doesn't get turned on for weeks at a time. The reason it has been on a lot lately is solely to copy the map aerial layers. The way this works is simple. Because there is so much aerial photography that I have to download (the amount that I have so far for maybe a quarter of the maps amounts to hundreds of gigs) then I have to sift through it and pick out just the layers I need, then that list of layers gets saved into a file. The script then reads the layer list, copies just the layers needed, and then they get put into the maps project in Qgis.

The Powershell script is very simple because it just works with a text list of layer names, which are assumed to be in the hard coded directory path, and copies them to another hard coded directory path. The actual paths are network paths from the shares that the Windows computer has access to, which are hosted on one of the Linux computers. The actual script itself is stored on the same network share. Here is the script:

$layers = Get-Content "S:\CopyFiles\CopyFiles.txt"
ForEach($layer in $layers)
{
    Write-Host $layer
    $source = "S:\CopyFiles\Source\" + $layer + ".*"
    Write-Host $source
    Copy-Item -Path $source -Destination "S:\CopyFiles\Dest\"
}

Basically the first line is loading that layer list from a hardcoded file reference. The rest of the script very simply echoes each line of the script to the screen and then assembles the full file path and then performs the copy operation.

I did look at extracting layer names from the source XML file, but ended up performing this step manually each time. It will be looked at again with the new script. This is expected to be in Python, the key issue being ability to run it easily from a command line or whatever in Linux.

So, a little progress to report. After playing about with IDEs I have naturally settled for KDevelop, which of course is the native development environment for KDE, since I already run KDE on most of my Linux computers. I am using a tutorial from the below website to aid my knowledge in developing the script needed for this. It is a pretty simple script task and there is enough capability built in to Python to use XML extraction this time, result is a more sophisticated script that can automate more of the required task. So I am taking a break from the maps for the next day or two while I build and test this script, which will speed up and simplify this process.