Python Scripting [3D]: Layer Fractional Segments for NZ Rail Maps 4

Continuing from our last post, we now want to find tile segments that need world files written for them and generate those files. 
The code to do this (from the steps outlined previously is:
  1. Strip the extension off the base file (92XJ7-92MKF.jgw) so that we get 92XJ7-92MKF:
    baseNameBase = os.path.splitext(baseName)[0]
  2. Split the filename at the hyphen so we get two pieces, baseColDescriptor and baseRowDescriptor respectively being 92XJ7 and 92MKF in this example:
    baseNameSplit = baseNameBase.split(“-“)
    baseColDescriptor = baseNameSplit[0]
    baseRowDescriptor = baseNameSplit[1]
  3. Set gridRowDescriptor to be a concatenation of baseRowDescriptor and gridY e.g. 92XJ7x4.1
    gridRowDescriptor = baseRowDescriptor + gridY
  4. Set gridColDescriptor to be a concatenation of baseColDescriptor and gridX e.g. 92MKFx4.3
    gridColDescriptor = baseColDescriptor + gridX
  5. Set gridFilename to be gridColDescriptor + “-” + gridRowDescriptor + “.jpg”
    gridFileName = gridColDescriptor + “-” + gridRowDescriptor + “.jpg”
  6. Search for a filename that ends with gridFileName (it will start with something else like K1977full-)
    rootFilesList = os.listdir(rootPath)
    for rootFile in rootFilesList:
           if rootFile.endswith(gridFileName):
  7. If the file name exists then change the full name into “.jgw” extension and write the 6 lines needed in the file:
    rootNameBase = os.path.splitext(rootFile)[0]
    segmentName = rootNameBase + “.jgw”
    segmentFileName = rootPath + segmentName
    segmentFile = open(segmentFileName, “w+”)
    segmentFile.write(str(pixelSize) + “n”)
    segmentFile.write(str(baseSkewX) + “n”)
    segmentFile.write(str(baseSkewY) + “n”)
    segmentFile.write(“-” + str(pixelSize) + “n”)
    segmentFile.write(str(segmentX) + “n”)
    segmentFile.write(str(segmentY) + “n”)
    segmentFile.close()
Testing so far indicates that the jgw files that the script wrote so far in testing are correct. Only one layer has been tested so far but the segment tile was presented in the right place relative to the base tile.
After looking at the workflow it would be ideal to provide the .xml and .jpg.aux.xml files for each jpeg file and have the script copy these automatically for each jgw file that it writes, so that all four files needed for each segment are complete. I expect to write a piece of code to perform that function and then present the complete script in the next part. So far the script is 72 lines total.

Tags: