2

I have a bunch of LAS files that have been delivered with different naming conventions. I want to rename the files to all have the same uniform naming - based on the easting and northing coordinates of each tiles centroid.
I have generated a text file with the old and new names, and am trying to piece together a python script - but am running into this error

Traceback (most recent call last):
  File "C:\LASTools\Test\ReName_Files.py", line 64, in <module>
    newName = newList[indexOldName]
NameError: name 'indexOldName' is not defined

Here's the script. I am very new to Python and so this could take me hours of research and trial and error (it already has).

import os    
folder = "C:\LASTools\Test\\"    
lookupTable = open(folder + "LookupTable.txt")
tableList = lookupTable.readlines()
lookupTable.close()

old = "TIFF"
new = "TILE"

newTable = []
for line in tableList:
     newLine = line.strip()  
     newerLine = newLine.strip(",")  
     newestLine = newerLine.strip("'")   
     almostFinalLine = newestLine.replace('"',"")  
     finalLine = almostFinalLine.split(",")
     newTable.append(finalLine)

header = newTable[0]
indexOld = header.index(old) - 1
indexNew = header.index(new) - 1

oldList = []
newList = []
for item in newTable:
    if item != newTable[0]:
        valueOld = item[indexOld]
        valueNew = item[indexNew]
        oldList.append(valueOld)
        newList.append(valueNew)

for file in os.listdir(folder):
    nameAndExt = os.path.splitext(file)
    oldName = nameAndExt[0]
    extension = nameAndExt[1]
    print oldName

if oldName in oldList:
    indexOldName = oldList.index(oldName)

newName = newList[indexOldName]

os.rename(folder + oldName + extension, folder + newName + extension)

closed as off-topic by Mike T, Stephen Lead, PolyGeo Sep 3 '14 at 3:58

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about software development are off-topic here unless they relate directly to Geographical Information Systems, but they can be asked on Stack Overflow." – Mike T, Stephen Lead, PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 4
    I don't really know how this is related to GIS as all you want to do is renaming files with a script. Better ask that on Stackoverflow – Curlew Aug 28 '14 at 18:59
2

I agree with Curlew, this is not a GIS question... however, you are using LasTools (it appears, you're not importing it!) which is a GIS lib.

Here's some code that might help - note it's in Esri arcpy objects but the Laspy you can use as a reference:

import sys
sys.path.append(r'C:\Python27\ArcGIS10.1\Lib\site-packages')
import os, string, laspy, arcpy
from laspy.file import File

if len(sys.argv) != 5:
    print "Not enough arguments!"
    sys.exit()

InFolder = sys.argv[1]
OutFolder = sys.argv[2]
OutCoSys = sys.argv[3]
IndexName = sys.argv[4]

if not os.path.exists(InFolder):
    print "In folder not found"
    sys.exit()
if not os.path.exists(OutFolder):
    print "Out folder not found"
    sys.exit()

OutShape = "%s\\%s.shp" % (OutFolder,IndexName)

if os.path.exists(OutShape):
    try:
        print "Removing old index"
        arcpy.Delete_management(OutShape)
    except:
        print "Unable to remove, please check for write"
        print "access and permissions"
        sys.exit()

arcpy.CreateFeatureclass_management(OutFolder,IndexName + ".shp","POLYGON","","DISABLED","DISABLED",OutCoSys)
arcpy.AddField_management(OutShape,"FileName","TEXT","","","125")
InsCur = arcpy.InsertCursor(OutShape,OutCoSys)

for ThisFile in os.listdir(InFolder):
    FileName,Ext = os.path.splitext(ThisFile)
    if Ext.lower() == ".las":
        LasOpened = False
        try:
            LASfile = File(InFolder + "\\" + ThisFile,mode = 'r')
            # This is the bit you should get interested in
            # The MinCo and MaxCo is a list/tuple in the order [[X],[Y]]
            MinCo = LASfile.header.get_min()
            MaxCo = LASfile.header.get_max()
            # the centroid is then
            # CenX = (MinCo[0] + MaxCo[0]) / 2
            # CenY = (MinCo[1] + MaxCo[1]) / 2
            # Make your file name from that and then use
            # os.rename(InFolder + "\\" + ThisFile,InFolder + "\\" + NewName) # to rename
            LASfile.close()
            LasOpened = True
        except:
            print "Unable to open %s with LasPY" % ThisFile
        if LasOpened:
            Xmin = int( MinCo[0] / 1000) * 1000
            Ymin = int( MinCo[1] / 1000) * 1000
            Xmax = (int( MaxCo[0] / 1000) * 1000) + 1000#Xmin + 1000#int( MaxCo[0] / 1000) * 1000#Xmin + 1000
            Ymax = (int( MaxCo[1] / 1000) * 1000) + 1000#Ymin + 1000#int( MaxCo[1] / 1000) * 1000#Ymin + 1000
            Xmin = MinCo[0]
            Ymin = MinCo[1]
            Xmax = MaxCo[0]
            Ymax = MaxCo[1]
            PArray = arcpy.Array()
            NewPol = InsCur.newRow()
            TPnt = arcpy.Point(Xmin,Ymin)
            PArray.add(TPnt)
            TPnt = arcpy.Point(Xmin,Ymax)
            PArray.add(TPnt)
            TPnt = arcpy.Point(Xmax,Ymax)
            PArray.add(TPnt)
            TPnt = arcpy.Point(Xmax,Ymin)
            PArray.add(TPnt)
            TPnt = arcpy.Point(Xmin,Ymin)
            PArray.add(TPnt)
            Poly = arcpy.Polygon(PArray,OutCoSys)
            NewPol.shape = Poly
            NewPol.setValue("FileName",ThisFile)
            InsCur.insertRow(NewPol)
            del PArray
            del NewPol
del InsCur

I've put some comments in there to direct your attention. For users of Esri this is a good way to get a shapefile of your coverage of LAS files. Note, I wrote this prior to 10.1 with the LASdataset so it's mostly redundant.

2
if oldName in oldList:
    indexOldName = oldList.index(oldName)
newName = newList[indexOldName]

The problem is when oldName is never in oldList then indexOldName never gets set to anything (defined), and the error is generated.

Make sure oldName is really in oldList, and set to indexOldName something, such as:

if oldName in oldList:
    indexOldName = oldList.index(oldName)
else:
    indexOldName = 0
newName = newList[indexOldName]

Not the answer you're looking for? Browse other questions tagged or ask your own question.