Friday, August 30, 2013

Lazy - Efficient, Potato - Potaaahto

I don't really tend to share my most complex and production related scripts because this is more of a tutorial and tip related blog.  In that same vane I have mentioned in the past that there are times that you do the same task several times or come up with a new procedure that you implement in several jobs that just begs for a simple script.  This is one of those times.

I have found over the years that having a simple command that saving the current states of things so you can recall them later is a big time saver.  For example save the current window display parameters so you can come back to the same settings later, or save the layer on/off states for easy recall.  There are ways to do stuff like this but I just want one two letter macro to save something and another to restore it.  Lately I have been working with 5-6 files at a time and if I jump out of the program or switch from VrOne to VrTwo I want to open the same files.  Hence StoVr.py, and GetVr.py to take a snapshot of open files and re-open them if I need to.

print 'stovr.py modified 9:08 AM Friday, August 30, 2013'
# Store Vr filenames of open files for later recall
'''
Copyright 2013 Dennis Shimer
No warranties as to safety or usability expressed or implied.
Free to use, copy, modify, distribute with author credit.

Echos the names  of currently open files to the command windows
and saves them for later recall.
'''

Ws=PyVrWs()
WorkSpaceCount=Ws.GetWsCount()
print 'File names saved'
if WorkSpaceCount:
    SaveFileNamesFile=open(VrCfg().GetVrHomeDir()+'\\hostdir\\SavedVrFileNames.txt','w')
    for WsNum in range(WorkSpaceCount):
        SaveFileNamesFile.write(Ws.GetFileName (WsNum)+'\n')
        print Ws.GetFileName (WsNum)
    SaveFileNamesFile.close()
else: print 'No workspaces open'

print 'getvr.py modified 9:08 AM Friday, August 30, 2013'
# Get Vr filenames previously stored
'''
Copyright 2013 Dennis Shimer
No warranties as to safety or usability expressed or implied.
Free to use, copy, modify, distribute with author credit.

Echos the previously saved names to the command window with
workspace numbers as they are opened.
'''

Ws=PyVrWs()
SaveFileNamesFile=open(VrCfg().GetVrHomeDir()+'\\hostdir\\SavedVrFileNames.txt','r')
FileNameDataList=SaveFileNamesFile.readlines()
for FileName in (FileNameDataList):
    PyVrGui().PushKeyin ('opevr '+FileName.rstrip())
    print Ws.GetWsCount(),FileName.rstrip()
SaveFileNamesFile.close()

Tuesday, August 27, 2013

Let me look that up in the dictionary for you.

There have been some changes to the VrPunt methods over time.  I have suspected that not everything is implemented exactly the way it is documented, and sometimes a name could be different or missing.  I have discovered though that the real magic is just in working with the dictionary which stores all the relevant LiDAR point attributes.  The LiDAR data provider that we most often use stores the individual flight line tag in the "SOU" tag, Vr however will only filter by the "FLT" tag.  Since FLT isn't used in our data, changing it to reflect the SOU value doesn't seem to have a downside.  Below is a simple program that runs through all the point data and copies SOU to FLT.  Now I can use the flight line filter to turn the points on and off.

print 'souflt.py modified 12:44 PM 8/27/2013'
# Copy LiDAR Source attribute to Flight attribute
'''
Copyright 2013 Dennis Shimer
No warranties as to safety or usability expressed or implied.
Free to use, copy, modify, distribute with author credit.

Reads the "Source" in a PuntA dictionary and sets the "Flight" to the same value.

'''


Ws=PyVrWs()
Punt=PyVrPunt()
Gui=PyVrGui()
WsNum=Ws.Aws()
PointBufferCount=Ws.GetPuntBufCount(WsNum)

Ws.UndoBegin(WsNum,"Source2Flight")
Gui.ProgInit('Points',Ws.GetPuntBufCount(WsNum))
for PointBufferNumber in range(PointBufferCount):
    Punt.Load(WsNum,PointBufferNumber)
    Gui.ProgSet(PointBufferNumber)
    for PointNum in range(Punt.GetCount()-1,-1,-1):
        PuntA=Punt.GetPuntA(PointNum)
        PuntA['Flt']=Punt.Sou(PointNum)
        Punt.ChgPuntA (PointNum, PuntA)
    Punt.ReRec()
Gui.ProgReset()
PyVrGr().Replot()
Ws.UndoEnd(WsNum)
 So the key is to load up the PuntA to make sure it is populated with the correct values, then modify the particular attribute and save it back to PuntA. When all the points are done re-record the entire Punt buffer.  As of this writting the dictionary is organized as follows.

"Lay"     = 1   - Layer                    (1-30001)
"Int"     = 0   - Intensity                (0-65535)
"Dsp"     = 1   - Display flag             (0-1)
"Ret"     = 0   - Return number            (1-5)
"Nre"     = 0   - Number of returns        (1-5)
"Sdf"     = 0   - Scan direction flag      (0-1)
"Edg"     = 0   - Edge of flight line flag (0-1)
"Cla"     = 0   - Classification           (0-31)
"Syn"     = 0   - Synthetic flag           (0-1)
"Key"     = 0   - Key-point flag           (0-1)
"Del"     = 0   - Delete flag              (0-1)
"Ang"     = 0   - Scan angle               (-90 - +90)
"Flt"     = 0   - Flight number            (0-255)
"Sou"     = 0   - Point source Id          (0-65535)
"Red"     = 255 - Red component color      (0-255)
"Green"   = 255 - Green component color    (0-255)
"Blue"    = 255 - Blue component color     (0-255)
"GpsTime" = 0.0 - GPS Time                 (Double precision number)

For anyone interested in trying VrPython for the first time or if you are early in the game, I suggest going to the earliest posts and working forward. I use VrPython every day for many wonderful things, needless to say it will change and could potentially damage a file. Any risk associated with using VrPython or any code or scripts mentioned here lies solely with the end user.

The "Personal VrPython page" in the link section will contain many code examples and an organized table of contents to this blog in a fairly un-attractive (for now) form.