Python Scripting [6B]: Layer Sidecar Duplication & Renaming for NZ Rail Maps 2

Since yesterday the script has been completed and tested OK. Here is the complete script. It is similar to the segments script but a lot less complex with a total of 77 lines.

 # declarations
import glob

import argparse
import os
import shutil
import sys

rootPath = os.getcwd()

# set up command line argument parser
parser = argparse.ArgumentParser(prog=’duplicate’)
parser.add_argument(‘-s’, ‘–source’, required=True)
parser.add_argument(‘-d’, ‘–dest’, required=True)
parser.add_argument(‘-m’, ‘–multisuffix’, type=str, default=”x2″)
parser.add_argument(‘-p’, ‘–pixelsize’, type=float, default=0.15)

# get arguments
argList = sys.argv[1:]                              # drop the script name parameter
args = parser.parse_args(argList)
dests = args.dest.split(” “)                   # multiple dests supported
source = args.source
multiSuffix = args.multisuffix
pixelSize = args.pixelsize

This initial section is basically declarations and initialisations including setting up the command line input parser and receiving the input. I mentioned the command input in the previous post, and how the way to run the script is from the directory that contains all of the input files and receives the output files. To achieve this, rootPath is set near the top to be the current working directory, instead of being a fixed path as has been the case in other scripts. However the previous scripts were much easier to use for their particular purpose with specially designated hard coded directories instead of specifying a long path containing spaces for each of the source and destination parameters. So there is no right or wrong way concerning paths; there is just whatever is the easiest one to use for the particular situation.

# search for source 
filessourcePath = os.path.normpath(rootPath + “/” + source + “*.jpg”)
for file in glob.glob(sourcePath):
    fileName = os.path.basename(file)
    fileNameParts = os.path.splitext(fileName)
    fileNameBase = fileNameParts[0]
    fileNameExt = fileNameParts[1]
    fileNameSects = fileNameBase.split(“-“)
   
    # loop through each possible destination
    for destName in dests:
        fileNameNew = destName + fileNameSects[1] + multiSuffix + “-” + fileNameSects[2] + multiSuffix
        FNNJpg = fileNameNew + “.jpg”
        FilePathNew = os.path.normpath(rootPath + “/” + FNNJpg)
        if os.path.isfile(FilePathNew):

This section contains code for finding source files and creating destination file specs. It initialises two processing loops. The first loop is initialised by getting a list of all jpg files in the directory and then slicing each file name into components (separated by a dash delimiter). The loop processes each source file name found. The second loop simply processes in turn each possible destination filename prefix. It uses this information to create a jpg file name by combining the components of the previous file name with the destination filename prefix and the multiplier suffix. It then enters an if statement loop which tests for the existence of a layer that has that file name (that was created by exporting from a Gimp mosaic).

            # File copying / writing loop
            print fileName + ” ^^ ” + FNNJpg
           
            # read world file
            worldFileName = fileNameBase + “.jgw”
            worldFilePath = os.path.normpath(rootPath + “/” + worldFileName)
            worldFile = open(worldFilePath, “r”)
            worldFileLines = worldFile.readlines()
            worldFile.close()
            WFNNew = fileNameNew + “.jgw”
            WFPNew = os.path.normpath(rootPath + “/” + WFNNew)
            print worldFileName + ” -> ” + WFNNew
           
            # write new world file
            worldFile = open(WFPNew, “w+”)
            worldFile.write(str(pixelSize) + “n”)
            worldFile.write(worldFileLines[1] + “n”)
            worldFile.write(worldFileLines[2] + “n”)
            worldFile.write(“-” + str(pixelSize) + “n”)
            worldFile.write(worldFileLines[4] + “n”)
            worldFile.write(worldFileLines[5] + “n”)
            worldFile.close()

This section of code is concerned with using the destination file spec to create the world file for the destination layer. It accomplishes this by reading the world file for the original source layer, and copying all of the data except for the two lines which specify the pixel size of the destination layer. It also prints status messages to show what is happening (at the top is the message showing a matching destination layer has been found, and in about the middle is the message showing a world file is being copied).


            # copy xml files            
            auxFileName = fileNameBase + “.jpg.aux.xml”
            auxFilePath = os.path.normpath(rootPath + “/” + auxFileName)
            AFNNew = fileNameNew + “.jpg.aux.xml”
            AFPNew = os.path.normpath(rootPath + “/” + AFNNew)
            print auxFileName + ” -> ” + AFNNew
            shutil.copyfile(auxFilePath,AFPNew)
            auxFileName = fileNameBase + “.xml”
            auxFilePath = os.path.normpath(rootPath + “/” + auxFileName)
            AFNNew = fileNameNew + “.xml”
            AFPNew = os.path.normpath(rootPath + “/” + AFNNew)
            print auxFileName + ” -> ” + AFNNew
            shutil.copyfile(auxFilePath,AFPNew)

The script is concluded by a simple block of code to directly copy the xml sidecars without modifying them in any way. It writes a status message for each of the two files.

The script has been tested for real world situations and is performing a stellar job.


Tags: