Tuesday, September 20, 2016

Going from Zero to "How did I ever get along before that" in three minutes

The other day I heard about a little indicator app ( aka bookmarks-indicator ) for Ubuntu that sits nicely on the task bar and allows you to drill down through the disk hierarchy by simply mousing over the icon.  It displays the folder names as you move through the system, auto expanding as you pause over a file name, showing individual files in each folder and opening them when you click.  One minute I didn't even know I wanted this ability and three minutes later I wondered how I ever survived without it.  The fact that is was just a couple dozen easy to read lines of python was even funner.  

Jump ahead to this morning and I came across a bunch of lines that just needed to have one particular point removed from them.  Long story, but on this particular job it happens on a fairly regular basis.  Well I've always had a program to straighten a line, removing all points between to identified locations, but I just needed to delete one point, and doing it with a single click would just be that much easier.  Granted EditVertex set to delete is 95% of the way to what I want but honestly sometimes you just need to step outside for a break to grab that last 5% especially if you can do so with a few lines of code.  And let's be honest it's funner than working anyway.

Hence dellpt.py

print 'dellpt.py modified 9:06 AM 9/20/2016'
# Delete Line Point
'''
Copyright 2016 Dennis Shimer
Vr Mapping copyright Cardinal Systems, LLC
No warranties as to safety or usability expressed or implied.
Free to use, copy, modify, distribute with author credit.

Deletes a single point identified on a line.
'''

Ws=PyVrWs()
Line=PyVrLine()
while Line.Id() != -1:
    Ws.UndoBegin(WsNum,"dellpt")
    PntNum=Line.GetIdPoint ()
    Line.DelPoint (PntNum)
    Line.ReRec()
    Line.Plot()
    PyVrGr().Replot()
    Ws.UndoEnd(WsNum)

Thursday, June 09, 2016

Simple tools for simple tasks (an appreciation for....)

Maybe because I am fairly simple minded, I have always had a deep appreciation for "The Unix Philosophy" which to my thinking involves the idea that if you just need to solve a simple little problem quickly you just throw together a simple little tool to do it but at the same time make sure that there is a way to chain these little tools together in a way that can make the aggregate a truly useful and maybe powerful solution. The integration of this idea into the programs I interact with minute by minute all day every day (and the fact that the first versions were on Unix) make Mike Kitaif one of my heroes.  Yes almost every function, option, toolset has an easy, GUI, comprehensive way of dealing with it but at the same time almost all have a simple command equivalent with arguments that can be built upon using function keys, macros, or incredibly the python interpreter. When you combine this with the fact that rather than a monolithic database for hanging on to all the settings they are usually stored in tiny easily accessible (sometimes even text) files you get the opportunity to make the system do almost anything you can dream.

A great example of all this came up recently when I realized that under certain conditions I wanted to under certain conditions be able to view a particular vertical slice of LiDAR data and set the coloring based on elevation sub-slices.  There is of course a way to do this using the GUI for the poidis command.

There are also fairly straightforward ways to set all these things from the command line.
http://www.vrmapping.net/help5/index.html?point_display.htm

However, what I really wanted was the ability to input a minimal amount of information (base elevation and total slice thickness) and then have it display exactly what I wanted with the colors set to the sub slices (thickness divided by total number of available slices, or 20 currently) and here is where the python comes in. Granted the programming is about at the level of an 8 year old with a Raspberry Pi but you get the idea.

print 'viewsl.py modified 6:06 AM 5/6/2016'
# Views a slice of lidar with color by slice elevation.
'''
Copyright 2016 Dennis Shimer
Vr Mapping copyright Cardinal Systems, LLC
No warranties as to safety or usability expressed or implied.
Free to use, copy, modify, distribute with author credit.

A little more user friendly option to the Vr alternative which uses an
external parameter file which can be referenced by other programs
like bumvsl

'''
import os
import man
Ws=PyVrWs()
WsNum=Ws.Aws()
Gui=PyVrGui()

MinZ = Ws.GetMinMax (WsNum)[2]
MaxZ = Ws.GetMinMax (WsNum)[5]
AverageZ = (MaxZ+MinZ)/2.0

#In case the parameter file was never created before or can't be read for some reason
BaseZ=AverageZ
DelZMin=MinZ-AverageZ
DelZMax=MaxZ-AverageZ
Thickness=MaxZ-MinZ

ParFileName=VrCfg().GetVrHomeDir()+'/hostdir/viewslice.par'
if not os.path.isfile(ParFileName):
     open(ParFileName,'w').write('{:.2f} {:.2f} {:.2f} {:.2f}'.format(BaseZ,DelZMin,DelZMax,Thickness))
elif not os.path.getsize(ParFileName):
     open(ParFileName,'w').write('{:.2f} {:.2f} {:.2f} {:.2f}'.format(BaseZ,DelZMin,DelZMax,Thickness))
Params=open(ParFileName,'r').read().split()
BaseZ=float(Params[0])
Thickness=float(Params[3])

#In case the user passes in the numbers from the command line.
if VrArgs:
      BaseZ=float(VrArgs[0])
      Thickness=float(VrArgs[1])

PromBox = VrPromBox ("Set Z Slice", 30, 1)
PromBox.SetFocus()
PromBox.AddDouble('BaseZ',BaseZ,2)
PromBox.AddDouble('Thickness',Thickness,2)

if not VrArgs:
      if PromBox.Display(1)==0:
          BaseZ=PromBox.GetDoubleByPrompt('BaseZ')
          Thickness=PromBox.GetDoubleByPrompt('Thickness')
open(ParFileName,'w').write('{:.2f} {:.2f} {:.2f} {:.2f}'.format(BaseZ,DelZMin,DelZMax,Thickness))

#Just use the numbers to pass along arguments to the built in commands.
print BaseZ,DelZMin,DelZMax,Thickness
Gui.PushKeyin ('PoiFilZsl  On')
Gui.PushKeyin ('PoiFilZsl  BasZ {:.2f}'.format(BaseZ))
Gui.PushKeyin ('PoiFilZsl  MinDel {:.2f}'.format(0))
Gui.PushKeyin ('PoiFilZsl  MaxDel {:.2f}'.format(Thickness))

NumberOfSlices=20
SliceThickness = Thickness*1.08/NumberOfSlices #1.08 because it minimizes the top white slice.
print MinZ,MaxZ,SliceThickness,NumberOfSlices
Gui.PushKeyin ('PoiColSli  On')
Gui.PushKeyin ('PoiColSli  ResZ')
Gui.PushKeyin ('PoiColSli SliZ {:d} {:.2f} {:.2f}'.format(1,0,BaseZ))
CurrentElevation=BaseZ
for CurrentSliceNumber in range(2,NumberOfSlices):
    Gui.PushKeyin ('PoiColSli SliZ {:d} {:.2f} {:.2f}'.format(CurrentSliceNumber,CurrentElevation,CurrentElevation+SliceThickness))
    CurrentElevation+=SliceThickness
Gui.PushKeyin ('PoiColSli SliZ {:d} {:.2f} {:.2f}'.format(NumberOfSlices,CurrentElevation,1000000))

PyVrGr().Replot()

*Read the Wikipedia article if for no other reason than to better appreciate some of the men who helped make this all possible.

Wednesday, December 16, 2015

30 Second Functions

When is it faster to write the function than to even find out how to do it in the help.  Well ok, I did actually search and didn't find out how, but here is what would have been faster.

I use handwheels,  I wanted an easy way to switch the z input from the footdisk to the right handwheel and back.  There is probably a way to pass an argument into a macro but I couldn't find it so enter....

zhand.py

print 'z handwheel'
VrCfg().SetHwLocZ (2)
The beauty of VrCfg is that it modifies the environment in real time just like calling any of the functions that would do the same thing via a dialog box.

Saying anything else would just not have a point, except, always be thinking of ways to extend the usefulness of existing processes.

Tuesday, December 15, 2015

I Get By With A Little Help From My Friends

Talking to a friend the other day it occurred to me how fantastic it would be if Vr could import and display OpenStreetMap data.  Nothing fancy mind you but maybe create an image that you could use as a background for a quick check of the surrounding area or even flight prep.  Wow that sounds like a lot of work unless of course someone else has already done it.  One of the many beauties of Python is the number of fantastic libraries out there if you just spend a little time tracking them down.  If you take that a step farther and are willing to pass a little of the responsibility outside python using system calls or batch files the possibilities are nearly endless.  Let's just say I can now display an OpenStreetMap map image in Vr and it only took about 90 lines of python.  Here are a few of the major helpers besides the map site which is so worthy of support, contribution, and praise.

I know I have mentioned it before but I can't say enough about pyproj which I use to convert my local coordinates to geographic for sending out to various places.

Once I have the corners in Lat / Long the rest will be done by a batch file which is about the only thing my python script creates.  It could be done in other ways but I liked the idea of creating something that could stick around and be easily modified for use again.  So the batch file next makes a call to the openstreetmap.org API for grabbing the map data.

Once the OSM file is in house it will be converted to a bitmap using Maperitive.  I only use such a tiny fraction of the capabilities of this amazing program, but for my purposes using it's command line version with a custom generated script will fit the bill perfectly.

The original bitmap is created in Web Mercator and can also throw in a TFW and KML file for generic use or display by anybody if you want to send them along.  I however want to display it behind my map in a local coordinate system in whatever system I choose so the last step is to re-project the image using GDAL.

Is this the most advanced, professional, elegant way of doing it?  Of course not, you obviously don't know me very well.  As usual it is something you can hack together in just a short time with minimal duplication of effort that gets the job done.  Hooray for real programmers!!!

Don't forget to support your favorite projects with contributions or cash when possible.


Tuesday, December 01, 2015

Note to self: Step backward through LiDAR when removing, and repr ( round ( a number )) is awsome

First, apparently I never noticed this before but while trying to step though a bunch of LiDAR points to remove a certain class it apparently changes something dynamically (like the point number in the buffer).  I noticed this because I was doing a

    for PointNum in range(Punt.GetCount()):
        if Punt.Cla(PointNum) == Whatever:
            Punt.DelPunt(PointNum)
To get rid of a certain type of point, in this case based on class.  What I noticed though was that each time it ran there were a bunch of points left over (half??).  I decided that even though I wasn't doing a record until the loop was done, the point number in the buffer must change as points are deleted.  So what to do?  How about starting at the last point and removing them from the top end of the buffer so as you work backwards the previous point numbers would not have changed.  I came up with
for PointNum in range(Punt.GetCount(),-1,-1):
and it seems to work fine.

Second and totally unrelated I needed to rebuild a tile naming structure for a local dataset.  There is a tile scheme that is based on 5000 foot tiles that is easy enough to get the names of.  The problem is that there is also a subset of 1250 foot tiles that starts from the corner of each 5k block but the names are based on 1000 foot increments.  When you digitize a random point in a block you can't necessarily round down to the lower 1000 because they just truncate the thousands from a rounded 1250.  Ok hard to imagine but the corners would be 0, 1250, 2500, 3750 so a number 3749 would fall in a 2500 block and truncate to 2000 meaning the tile would have 2 in the name, not to mention dropping a million off one of the coordinates.  Now for the why I love python part!
repr(round(1773691/1250)*1250)[1:4]
yields '772' just like is necessary to build the proper tile name. 

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.