Sunday, March 22, 2015

It's all part of the process (or subprocess as the case may be)

On occasion I make the case for installing python in order to extend the usefulness of the environment in Vr, or at least getting hold of some of the most useful standard libraries.  Well here is another good reason.  This is just a snippet that I will use later but I figure this is as good a place as any to keep track of it.

Before too long I want to write a script that will depend on running an external process, then interacting with the results of the process (no spoilers yet).  In order to do that I'm going to make something similar to the following call to process some files leaving the results in the working directory. The research I did here was to make sure I had a way to not proceed until the original process was complete.  Later I'll need to make sure I can run a shell command and capture the resulting output but this will do for now.

import subprocess
process = subprocess.Popen('lasboundary -i *rgb1.las -otxt')
process.wait()
print process.returncode
print '\a'
the last line is just to beep so I knew when it was done.

Wednesday, March 11, 2015

Reminder snippets - liblas and reading laz files

The other thing that I used to do with this blog is post things that I wanted to remember.  At one point the Vr forums were a good place to do this because it also gave an opportunity to share and collaborate.  Since that doesn't really seem to be happening any more I suppose I could drop things here and then come back to them later if necessary.  Yes I could just do this in a document but who knows maybe a new opportunity for some kind of sharing community will pop up again.

In this case I just want to remind myself that if Vr never adopts the .LAZ LiDAR compression format for reading, it would still be possible using liblas. This is simple a little interactive session that shows it is theoretically possible using my own data.

There may indeed be other better ways, but this seems to work fine.

>>> import liblas
>>> f=liblas.file.File(r'c:\tmp\N1915250.laz',mode='r')
>>> f

>>> p=f.read(100)
>>> p.classification
5
>>> p.x
1919853.35
>>> f.header.compressed
True
>>> f.filename
'c:\\tmp\\N1915250.laz'

5 Minute Functions - 2

I haven't stopped coding, I've just stopped talking about it.  Again more a function of the fact that I doubt anybody really cares.  I noticed the other day that I have slightly over 300 python programs that have accumulated over the years.  Certainly there are the ones I am really proud of like the ones that translate a Vr file into a KML file or a TIN into  LandXML format because of the crucially useful functionality they add.  Then there are the ones I use multiple times an hour, like double points on a line, drive to points by user specified parameters, storing helpful window states, adding clamping points to DTM lines, and on it goes.  Lately I have noticed that I also just stop sometimes and write a quick program because as I mentioned in a previous post of a similar title, I can spend 5 minutes programming and save 15 minutes (and lots of wear and tear on the mouse clicking finger).  Not only is it worth it but it is also fun, keeps my brain lubricated, and gives me a base in case I find the task useful and want to come back later to add options, dialogs, or arguments that would make it more universal.

Here is an example, I have a few thousand points that got dumped into a file without regard to any distinction.  about 300 of them would be really helpful if they were in a certain layer and I don't want to miss any of them.  The only thing that makes them different is that there is a common string of text inside the feature code.  There may very well be an easy way to do this with Vr, but after a couple of tries I couldn't come up with a parameter that worked so I thought, "why not just write a quick script?".  In this instance I'll just hard code the search string and resulting changes. If I find it useful I may come back later and add a simple dialog to make it more agile.

print 'fcglob.py modified 6:23 AM 3/11/2015'
# Globally change something based on text in it's feature code
'''
Copyright 2015 Dennis Shimer
No warranties as to safety or usability expressed or implied.
Free to use, copy, modify, distribute with author credit.

Simple beginning of a function to change entities based on their
feature code.

Variables of interest:
    None
'''
Ws=PyVrWs()
Sym=PyVrSym()
WsNum=Ws.Aws()
Ws.UndoBegin(WsNum,'fcglob')
for EntNum in range ( Ws.GetSymCount(WsNum)):
    Sym.Load (WsNum, EntNum)
    if Sym.GetFc().lower().count(' fh'):
        print EntNum
        Sym.SetLayer(1042)
        Sym.SetGpoint(21)
        Sym.ReRec()
print ('\a')
Ws.UndoEnd(WsNum)

 It started out as about a half dozen lines in pyedi and doesn't amount to much more now. Nothing fancy, just something I can come back to if I need it again, and in this case it really helped.

I don't know, maybe I'll just start dumping more of these here. Doesn't hurt anything and though most of them aren't universally interesting it might give somebody an idea.

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.