Shape From Shadeing
sfsding_dislin_PIL.py
<< sfsding_dislin_PIL declarations >>
imgtyp
elevation_line
class ViewerFrame
__init__
get_img
OnCloseWindow
EvtChoice
EvtChoice2
Picture_Open
Picture_Save
EvtText
EvtThreeDText
EvtThreeDChar
Picture_Exit
Picture_Refresh_Button_Press
ThreeD_Refresh_Button_Press
dis_img
dis_img2
dis_img3
class MyApp
<< class MyApp declarations >>
OnInit
globalDEMV.py
<< globalDEMV declarations >>
imgtyp
get_init_data
class Global_tile
<< class Global_tile declarations >>
__init__
OnMouseEvent
OnPaint
ConvertEventCoords
class ViewerFrame
__init__
OnCloseWindow
Picture_Open
EvtChoice
OnRSpin
OnUSpin
OnDSpin
EvtLatitudeText
EvtLongitudeText
Picture_Save
EvtText
EvtHeightText
Picture_Exit
ThreeD_Refresh_Button_Press
dis_img
dis_img2
dis_img3
class MyApp
OnInit
@path c:\python22\tom
@root sfsding_dislin_PIL.py
<< sfsding_dislin_PIL declarations >>
<< sfsding_dislin_PIL methods >>
app = MyApp(0) # Create an instance of the application class
app.MainLoop() # Tell it to start processing events
@code
"""Shape from Shading
A method for determining the shape of a surface from its image
"Shape from shading" (also known as photoclinometry) is a method for determining
the shape of a surface from its image. For a surface of constant albedo, the brightness at
a point (x,y) in the image is related to the gradients (p,q) by the following expression:
i(x,y) = a R[p(x,y),q(x,y)]
where R is the reflectance map, p = dz/dx and q = dz/dy are the partial derivatives
of the surface in the x- and y-directions, and a is a constant that depends on the albedo,
the gain of the imaging system and other factors. The above expression also assumes that any
additive offsets, for example, because of atmospheric scattering, have been removed.
A variety of methods have been developed for inverting the above equation (see Horn 1990).
The next section describes a simple method that provides satisfactory results in many
planetary imaging scenarios. It is based on some early ideas described by Horn (1977).
. If the image is rotated so that the vector that points to the sun is in the x-z plane,
it can be shown that
i(x,y) ~ a [sin(s) p(x,y) + cos(s)]
where s is the zenith angle of the sun.
The constant scale factor a is difficult to determine directly without ground truth
(that is, ground targets with known albedo and slope).
However, because in most images the gradients are more-or-less
uniformly distributed in all directions, the expected value of the gradient
in the x-direction E[p] ~ 0 and so the average image brightness
E[i] ~ a cos(s).
This then allows us to estimate the scale factor
a = E[i] / cos(s).
The elevation map z(x,y) can be obtained iteratively, row-by-row as
z(x,y) = z(x-1,y) + [i(x,y) - a cos(s)] / a sin(s)
z(x) = z(x-1) + [i(x) - a cos(s)] / a sin(s)
where z(0,y) are the boundary values. If the boundary values z(0,y) are unknown,
we can minimize the mean-squared elevation difference between rows by subtracting
the average row elevation from the elevations in the row."""
import os
import Image
import ImageChops
from wxPython.wx import *
from wxPython.lib.imagebrowser import *
from math import *
import dislin
import StringIO
import image_view
wxInitAllImageHandlers()
<< sfsding_dislin_PIL methods >>=
def imgtyp(file_nm):#returns wx image typ
fl_fld = os.path.splitext(file_nm)
ext = fl_fld[1]
ext = string.lower(ext[1:])
if ext == 'bmp':
image = wxBITMAP_TYPE_BMP
elif ext == 'gif':
image = wxBITMAP_TYPE_GIF
elif ext == 'png':
image = wxBITMAP_TYPE_PNG
elif ext == 'jpg':
image = wxBITMAP_TYPE_JPEG
return image
<< sfsding_dislin_PIL methods >>=
def elevation_line(line_data, zenith_angle, scale_factor):#calculates elivation data
output_line = []
z = 0.
z2 = 0.
avg = 0.
hi = 0
lo = 255
for i in line_data:
avg = avg + i
avg = avg / len(line_data)
z = avg + ( - scale_factor * cos(zenith_angle)) / (scale_factor * sin(zenith_angle))
for i in line_data:
z2 = z + ( i - scale_factor * cos(zenith_angle)) / (scale_factor * sin(zenith_angle))
if z2 > hi:
hi = int(z2)
if z2 < lo:
lo = int(z2)
output_line.append(z2)
return output_line, max(output_line),min(output_line) #hi, lo
## Create a new frame class, derived from the wxPython Frame.
<< sfsding_dislin_PIL methods >>=
class ViewerFrame(wxFrame):
<< class ViewerFrame methods >>
<< class ViewerFrame methods >>=
def __init__(self, parent, id, title):
# First, call the base class' __init__ method to create the frame
wxFrame.__init__(self, parent, id, title,wxDefaultPosition,wxDefaultSize)
self.Imagetest = Image.new.__init__('RGB',(853,603))
self.rotationangle = 0
self.raster_flag = false
self.zenith_angle = 30
self.scale_factor = 2.5
self.displaychoice = 'shade'
self.filterselect = Image.NEAREST
self.new_line = []
self.hi = 255
self.lo = 0
TypeList2 = ['Nearest', 'Bilinear', 'Bicubic']
TypeList = ['shade', 'contour', 'grid']
self.globdir = os.path.join(os.getcwd(),'glob.png')
self.splochdir = os.path.join(os.getcwd(),'sploch.png')
self.mainmenu = wxMenuBar() # Create menu bar.
menu=wxMenu()# Make a menu (will be the Open menu)
exitID=wxNewId() # Make a new ID for a menu entry.
menu.Append(exitID, '&Open', 'Open Picture') # Name the ID by adding it to the menu.
EVT_MENU(self, exitID, self.Picture_Open)
exitID=wxNewId()# Make a new ID for a menu entry.
menu.Append(exitID, '&Save', 'Save Projection')
EVT_MENU(self, exitID, self.Picture_Save)
menu.Append(exitID, 'E&xit', 'Exit program')
EVT_MENU(self, exitID, self.Picture_Exit)
self.mainmenu.Append (menu, '&File') # Add the File menu to the menu bar.
self.SetMenuBar (self.mainmenu) # Attach the menu bar to the window.
#Sizers
self.sub_Picture_button_sizer = wxBoxSizer(wxVERTICAL)
self.sub_Picture_button_text_sizer = wxBoxSizer(wxVERTICAL)
self.Picture_button_sizer = wxBoxSizer(wxHORIZONTAL)
self.Picture_sizer = wxBoxSizer(wxVERTICAL)
self.AandS_sizer = wxBoxSizer(wxVERTICAL)
self.AandS_lable_sizer = wxBoxSizer(wxVERTICAL)
self.sub_Picture_button_text_sizer = wxBoxSizer(wxVERTICAL)
self.camera_sizer = wxBoxSizer(wxVERTICAL)
self.camera_text_sizer = wxBoxSizer(wxVERTICAL)
self.ThreeD_button_sizer = wxBoxSizer(wxHORIZONTAL)
self.ThreeD_sizer = wxBoxSizer(wxVERTICAL)
#Controll ID's
self.PictureWindowID = wxNewId()
self.ThreeD_slider_ID = wxNewId()
self.ThreeD_slider_IDa = wxNewId()
self.ThreeDWindowID = wxNewId()
self.Picture_Refresh_Button_ID = wxNewId()
self.Picture_slider_ID = wxNewId()
self.ThreeD_viewID = wxNewId()
self.ThreeD_Refresh_Button_ID = wxNewId()
self.RAngle_ID = wxNewId()
exitID=wxNewId()
self.UAngle_ID = wxNewId()
self.DAngle_ID = wxNewId()
#Controlls
self.Splitter = wxSplitterWindow(self, -1)
self.Picture_view = wxPanel(self.Splitter, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL)
self.PictureWindow = wxScrolledWindow(self.Picture_view, self.PictureWindowID, wxDefaultPosition, wxDefaultSize, wxSUNKEN_BORDER)
self.ThreeD_view = wxPanel(self.Splitter, self.ThreeD_viewID, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL)
#self.ThreeDWindow = wxScrolledWindow(self.ThreeD_view, self.ThreeDWindowID, wxDefaultPosition, wxDefaultSize, wxSUNKEN_BORDER)
self.ThreeD_Refresh_Button = wxButton(self.ThreeD_view, self.ThreeD_Refresh_Button_ID, 'Refresh', wxDefaultPosition)
self.Picture_Refresh_Button = wxButton(self.Picture_view, self.Picture_Refresh_Button_ID, 'Refresh', wxDefaultPosition)
self.choice2 = wxChoice(self.Picture_view, 80, (80, 50), choices = TypeList2)
self.Picture_slider = wxTextCtrl(self.Picture_view, self.Picture_slider_ID, "0", size=(125, -1))
self.choice = wxChoice(self.ThreeD_view, 40, (80, 50), choices = TypeList) #self.ThreeDWindow = wxScrolledWindow(self.ThreeD_view, self.ThreeDWindowID, wxDefaultPosition, wxDefaultSize, wxSUNKEN_BORDER)
self.RAngle = wxSpinCtrl(self.ThreeD_view, self.RAngle_ID, "", wxPoint(30, 50), wxSize(80, -1))
self.ThreeD_slidera = wxTextCtrl(self.ThreeD_view, self.ThreeD_slider_IDa, "30", size=(125, -1))
self.ThreeD_slider = wxTextCtrl(self.ThreeD_view, self.ThreeD_slider_ID, "2.5", size=(125, -1))
self.UAngle = wxSpinCtrl(self.ThreeD_view, self.UAngle_ID, "", wxPoint(30, 50), wxSize(80, -1))
self.DAngle = wxSpinCtrl(self.ThreeD_view, self.DAngle_ID, "", wxPoint(30, 50), wxSize(80, -1))
#lables
l1 = wxStaticText(self.Picture_view, -1, "Rotation Angle",wxPoint(30, 50), wxSize(80, -1),wxALIGN_CENTRE )
l2 = wxStaticText(self.ThreeD_view, -1, "Scale",wxPoint(30, 50), wxSize(80, -1),wxALIGN_CENTRE )
l4 = wxStaticText(self.Picture_view, -1, "Method",wxPoint(30, 50), wxSize(80, -1),wxALIGN_CENTRE )
l5 = wxStaticText(self.ThreeD_view, -1, "Azimuth",wxPoint(30, 50), wxSize(80, -1),wxALIGN_CENTRE )
l6 = wxStaticText(self.ThreeD_view, -1, "Display",wxPoint(30, 50), wxSize(80, -1),wxALIGN_CENTRE )
l7 = wxStaticText(self.ThreeD_view, -1, "R Angle",wxPoint(30, 50), wxSize(80, -1),wxALIGN_CENTRE )
l8 = wxStaticText(self.ThreeD_view, -1, "Up Angle",wxPoint(30, 50), wxSize(80, -1),wxALIGN_CENTRE )
l9 = wxStaticText(self.ThreeD_view, -1, "Distance",wxPoint(30, 50), wxSize(80, -1),wxALIGN_CENTRE )
#Controll Settings
self.choice2.SetStringSelection('Nearest')
self.Picture_slider.SetInsertionPoint(0)
self.ThreeD_slidera.SetInsertionPoint(0)
self.ThreeD_slider.SetInsertionPoint(0)
self.choice.SetStringSelection('shade')
self.RAngle.SetRange(0,359)
self.RAngle.SetValue(190)
self.UAngle.SetRange(0,359)
self.UAngle.SetValue(25)
self.DAngle.SetRange(0,5000)
self.DAngle.SetValue(800)
self.Splitter.SetMinimumPaneSize(20)
self.Splitter.SplitVertically(self.ThreeD_view, self.Picture_view)
self.Splitter.SetSashPosition(300)
#Class Events
EVT_TEXT(self, self.Picture_slider_ID, self.EvtText)
EVT_CHOICE(self, 40 , self.EvtChoice)
EVT_TEXT(self, self.ThreeD_slider_ID, self.EvtThreeDChar)
EVT_BUTTON(self, self.Picture_Refresh_Button_ID, self.Picture_Refresh_Button_Press)
EVT_TEXT(self, self.ThreeD_slider_IDa, self.EvtThreeDText)
EVT_BUTTON(self, self.ThreeD_Refresh_Button_ID, self.ThreeD_Refresh_Button_Press)
EVT_CHOICE(self, 80 , self.EvtChoice2)
self.glob_thing = image_view.ImageView(self.ThreeD_view, -1)
self.sub_Picture_button_text_sizer.Add(l1, 1, wxEXPAND)
self.sub_Picture_button_text_sizer.Add(l4, 1, wxEXPAND)
self.sub_Picture_button_sizer.Add(self.Picture_slider, 1, wxEXPAND)
self.sub_Picture_button_sizer.Add(self.choice2, 1, wxEXPAND)
self.Picture_button_sizer.Add(self.sub_Picture_button_text_sizer, 1, wxEXPAND)
self.Picture_button_sizer.Add(self.sub_Picture_button_sizer, 1, wxEXPAND)
self.Picture_button_sizer.Add(self.Picture_Refresh_Button, 1, wxEXPAND)
self.Picture_sizer.Add(self.Picture_button_sizer, 1, wxEXPAND)
self.Picture_sizer.Add(self.PictureWindow, 10, wxEXPAND)
self.Picture_view.SetSizer(self.Picture_sizer)
self.Picture_view.SetAutoLayout(1)
self.Picture_sizer.Fit(self.Picture_view)
self.AandS_lable_sizer.Add(l5, 1, wxEXPAND)
self.AandS_lable_sizer.Add(l2, 1, wxEXPAND)
self.AandS_lable_sizer.Add(l6, 1, wxEXPAND)
self.AandS_sizer.Add(self.ThreeD_slidera, 1, wxEXPAND)
self.AandS_sizer.Add(self.ThreeD_slider, 1, wxEXPAND)
self.AandS_sizer.Add(self.choice, 1, wxEXPAND)
self.camera_text_sizer.Add(l7, 1, wxEXPAND)
self.camera_text_sizer.Add(l8, 1, wxEXPAND)
self.camera_text_sizer.Add(l9, 1, wxEXPAND)
self.camera_sizer.Add(self.RAngle, 1, wxEXPAND)
self.camera_sizer.Add(self.UAngle, 1, wxEXPAND)
self.camera_sizer.Add(self.DAngle, 1, wxEXPAND)
self.ThreeD_button_sizer.Add(self.ThreeD_Refresh_Button, 1, wxEXPAND)
self.ThreeD_button_sizer.Add(self.AandS_lable_sizer, 2, wxEXPAND)
self.ThreeD_button_sizer.Add(self.AandS_sizer, 1, wxEXPAND)
self.ThreeD_button_sizer.Add(self.camera_text_sizer, 2, wxEXPAND)
self.ThreeD_button_sizer.Add(self.camera_sizer, 1, wxEXPAND)
self.ThreeD_sizer.Add(self.ThreeD_button_sizer, 1, wxEXPAND)
self.ThreeD_sizer.Add(self.glob_thing, 10, wxEXPAND)
self.ThreeD_view.SetSizer(self.ThreeD_sizer)
self.ThreeD_view.SetAutoLayout(1)
self.ThreeD_sizer.Fit(self.ThreeD_view)
self.imgfile = 'Unnamed'
if len(sys.argv) > 1:
self.imgfile = sys.argv[1]
elif self.imgfile == 'Unnamed':
self.imgfile = self.get_img()
if self.imgfile == 'Unnamed':
sys.exit()
print self.imgfile
self.PIL_bitmap = Image.open(str(self.imgfile))
self.PIL_bitmap = self.PIL_bitmap.convert("L")
new_box = int(sqrt((self.PIL_bitmap.size[0]**2)+(self.PIL_bitmap.size[1]**2)))
new_image = Image.new("L",(new_box,new_box))
up_left = (int((new_box - self.PIL_bitmap.size[0])/2),int((new_box - self.PIL_bitmap.size[1])/2))
new_image.paste(self.PIL_bitmap,up_left)
self.PIL_bitmap = new_image
self.PIL_bitmap.save(self.splochdir)
self.view_bitmap = wxBitmap(self.splochdir,wxBITMAP_TYPE_PNG)
self.bitmapID = wxNewId()
self.bitmap2ID = wxNewId()
#self.glob_thing = wxStaticBitmap(self.ThreeDWindow, self.bitmap2ID,self.view_bitmap, wxPoint(0,0), wxSize(853, 603))
#view = ImageView(win, -1)
#self.glob_thing.set_image(WXToPIL(self.view_bitmap))
#self.glob_thing.set_image(Image.open(None))
self.static_thing = wxStaticBitmap(self.PictureWindow, self.bitmapID, self.view_bitmap, wxDefaultPosition, wxSize(self.view_bitmap.GetWidth(), self.view_bitmap.GetHeight()))
#self.ThreeDWindow.SetScrollbars(1, 1, 853, 603)
self.PictureWindow.SetScrollbars(1, 1, self.PIL_bitmap.size[0],self.PIL_bitmap.size[1])
<< class ViewerFrame methods >>=
def get_img(self): #gets the image file name
dlg = wxFileDialog(self, 'Image File Name', 'C:\\Documents and Settings\\Owner\\My Documents\\My Pictures\\mars\\', '', 'BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif|PNG files (*.png)|*.png|And many more|*.*', wxOPEN)
is_open = dlg.ShowModal()
if is_open == wxID_OK:
return dlg.GetPath()
elif is_open == wxID_CANCEL:
return self.imgfile
dlg.Destroy()
wxSafeYield()
<< class ViewerFrame methods >>=
def OnCloseWindow(self, event):
# tell the window to kill itself
self.Destroy()
<< class ViewerFrame methods >>=
def EvtChoice(self, event):
self.displaychoice = event.GetString()
<< class ViewerFrame methods >>=
def EvtChoice2(self, event):
if event.GetString() == 'Bilinear':
self.filterselect = Image.BILINEAR
elif event.GetString() == 'Bicubit':
self.filterselect = Image.BICUBIC
else:
self.filterselect = Image.NEAREST
<< class ViewerFrame methods >>=
def Picture_Open(self, event):#self.splochdir
tempimgfile = self.get_img()
if tempimgfile <> 'Unnamed':
self.raster_flag = false
self.imgfile = tempimgfile
self.PIL_bitmap = Image.open(str(self.imgfile)).convert("L")
new_box = int(sqrt((self.PIL_bitmap.size[0]**2)+(self.PIL_bitmap.size[1]**2)))
new_image = Image.new("L",(new_box,new_box))
up_left = (int((new_box - self.PIL_bitmap.size[0])/2),int((new_box - self.PIL_bitmap.size[1])/2))
new_image.paste(self.PIL_bitmap,up_left)
self.PIL_bitmap = new_image
self.PIL_bitmap.save(self.splochdir)
self.view_bitmap = wxBitmap(self.splochdir,wxBITMAP_TYPE_PNG)
self.static_thing = wxStaticBitmap(self.PictureWindow, self.bitmapID, self.view_bitmap, wxDefaultPosition, wxSize(self.view_bitmap.GetWidth(), self.view_bitmap.GetHeight()))
self.PictureWindow.SetScrollbars(1, 1, self.PIL_bitmap.size[0],self.PIL_bitmap.size[1],0,0)
<< class ViewerFrame methods >>=
def Picture_Save(self, event):
dlg = wxFileDialog(self, "Save Shading Map", ".", "","BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif|Jpeg files (*.jpg)|*.jpg|PNG files (*.png)|*.png|All files (*.*)|*.*", wxSAVE)
if dlg.ShowModal() == wxID_OK:
path = dlg.GetPath()
newimage = self.glob_thing.get_image()
#newimage.SaveFile(path,imgtyp(path))
#pilimage = Image.open(StringIO.StringIO(newimage))
#pilimage = image_view.WXToPIL(newimage, 'RGB')
newimage.save(path)
else:
pass
dlg.Destroy()
<< class ViewerFrame methods >>=
def EvtText(self, event):
try:
self.rotationangle = int(event.GetString())
except ValueError:
self.rotationangle = 0
<< class ViewerFrame methods >>=
def EvtThreeDText(self, event):
self.raster_flag = false
try:
self.zenith_angle = int(event.GetString())
except ValueError:
self.zenith_angle = 30
<< class ViewerFrame methods >>=
def EvtThreeDChar(self, event):
self.raster_flag = false
try:
self.scale_factor = float(event.GetString())
except ValueError:
self.scale_factor = 2.5
<< class ViewerFrame methods >>=
def Picture_Exit(self, event):
self.Destroy()
<< class ViewerFrame methods >>=
def Picture_Refresh_Button_Press(self, event):
self.raster_flag = false
tempimg = self.PIL_bitmap.rotate(self.rotationangle,self.filterselect)
self.PIL_bitmap = tempimg
tempimg.save(self.splochdir)
self.view_bitmap = wxBitmap(self.splochdir,wxBITMAP_TYPE_PNG)
self.static_thing.SetBitmap(self.view_bitmap)
self.PictureWindow.SetScrollbars(1, 1, self.PIL_bitmap.size[0],self.PIL_bitmap.size[1])
<< class ViewerFrame methods >>=
def ThreeD_Refresh_Button_Press(self, event):
New_Bitmap2 = self.PIL_bitmap.rotate(-90)
New_Bitmap = ImageChops.invert(New_Bitmap2)
line_data = New_Bitmap.getdata()
if not(self.raster_flag):
self.new_line, self.hi, self.lo = elevation_line(line_data, self.zenith_angle, self.scale_factor)
self.raster_flag = true
img_size = self.PIL_bitmap.size
if self.displaychoice == 'shade':
self.dis_img(self.new_line,img_size[1],img_size[0], self.lo, self.hi)
elif self.displaychoice == 'grid':
self.dis_img2(self.new_line,img_size[1],img_size[0], self.lo, self.hi)
elif self.displaychoice == 'contour':
self.dis_img3(self.new_line,img_size[1],img_size[0], self.lo, self.hi)
self.glob_thing.set_image(self.Imagetest)
<< class ViewerFrame methods >>=
def dis_img(self,zmat,m, n, zlo, zhi):
char = ''
max_bytes = 0
xray = range (n)
yray = range (m)
dislin.metafl ('VIRT')
dislin.setpag ('da4l')
#dislin.setfil (self.globdir)
dislin.disini ()
dislin.pagera ()
dislin.hwfont ()
dislin.ax3len (1400, 1400,1400)
dislin.autres(n, m)
dislin.shdmod ('poly', 'contur')
dislin.graf3 (0, n, 0, n/5, 0, m, 0, m/5, zlo, zhi, zlo, (zhi-zlo)/5)
dislin.crvmat (zmat, n, m, 1, 1)
dislin.title ()
char_buff = dislin.rbfpng(max_bytes)
max_bytes = int(char_buff[1])
char_buff = dislin.rbfpng(max_bytes)
char = char_buff[0]
self.Imagetest = Image.open(StringIO.StringIO(char))
dislin.disfin ()
<< class ViewerFrame methods >>=
def dis_img2(self, zmat, m, n, zlo, zhi):
char = ''
max_bytes = 0
RSpin = self.RAngle.GetValue()
USpin = self.UAngle.GetValue()
DSpin = self.DAngle.GetValue()
xray = range (n)
yray = range (m)
dislin.metafl ('VIRT')
dislin.setpag ('da4l')
#dislin.setfil (self.globdir)
dislin.disini ()
dislin.pagera ()
dislin.hwfont ()
dislin.axis3d(n, m, int(zhi-zlo))
dislin.view3d(RSpin, USpin, DSpin, 'ANGLE')
dislin.color('BLUE')
dislin.graf3d(0, n, 0, n/5, 0, m, 0, m/5, zlo, zhi, zlo, (zhi-zlo)/5)
dislin.surshd(xray, n, yray, m, zmat)
dislin.title ()
char_buff = dislin.rbfpng(max_bytes)
max_bytes = int(char_buff[1])
char_buff = dislin.rbfpng(max_bytes)
char = char_buff[0]
self.Imagetest = Image.open(StringIO.StringIO(char))
dislin.disfin ()
<< class ViewerFrame methods >>=
def dis_img3(self, zmat, n, m, zl, zh):
char = ''
max_bytes = 0
if zl == zh:
zl = 0
zh = 10
xray = range (n)
yray = range (m)
dislin.metafl('VIRT')
dislin.setpag('da4l')
#dislin.setfil(self.globdir)
dislin.disini()
dislin.pagera()
dislin.hwfont()
dislin.complx()
if (n * (1400 / m))< 2000:
dislin.axslen(n * (1400 / m), 1400)
else:
dislin.axslen(2000,m * (2000 / n))
dislin.graf(0, n, 0, n/5, 0, m, 0, m/5)
dislin.height (25)
for i in range (0, 9):
zlev = (i * ((zh-zl)/8)) + zl
dislin.labels ('FLOAT', 'CONTUR')
dislin.setclr ((i+1) * 23)
dislin.contur (xray, n, yray, m, zmat, zlev)
char_buff = dislin.rbfpng(max_bytes)
max_bytes = int(char_buff[1])
char_buff = dislin.rbfpng(max_bytes)
char = char_buff[0]
self.Imagetest = Image.open(StringIO.StringIO(char))
dislin.disfin()
<< sfsding_dislin_PIL methods >>=
class MyApp(wxApp):
<< class MyApp declarations >>
<< class MyApp methods >>
@code
# wxWindows calls this method to initialize the application
<< class MyApp methods >>=
def OnInit(self):
# Create an instance of our customized Frame class
frame = ViewerFrame(NULL, -1, "Shape from Shading")
frame.Show(true)# Tell wxWindows that this is our main window
self.SetTopWindow(frame) # Return a success flag
return true
@root globalDEMV.py
<< globalDEMV declarations >>
<< globalDEMV methods >>
app = MyApp(0) # Create an instance of the application class
app.MainLoop() # Tell it to start processing events
@code
import os
from wxPython.wx import *
import dislin
import string
import binascii
import Image
import image_view
import StringIO
# startup numbers (declares global variables) not sure if it is necessary
lat_num = 39.99583333333333
start_lat = 39.99583333333333
long_num = -139.99583333333334
start_long = -139.99583333333334
Latitude_Minimum = -10
Latitude_Maximum = 40
Longitude_Minimum = -140
Longitude_Maximum = -100
x_scale = 0.00833333333333
y_scale = 0.00833333333333
demdirectory = 'EMPTY'
RSpin = 10
USpin = 25
DSpin = 400
height_num = 1.0
width_num = 1.0
wxInitAllImageHandlers() # initialize wxPython image handlers
<< globalDEMV methods >>=
def imgtyp(file_nm):# returns wx image type
fl_fld = os.path.splitext(file_nm)
ext = fl_fld[1]
ext = string.lower(ext[1:])# get extension
if ext == 'bmp':
image = wxBITMAP_TYPE_BMP
elif ext == 'gif':
image = wxBITMAP_TYPE_GIF
elif ext == 'png':
image = wxBITMAP_TYPE_PNG
elif ext == 'jpg':
image = wxBITMAP_TYPE_JPEG
return image
<< globalDEMV methods >>=
def get_init_data(parent):# get data from the .hdr file
global tom,long_num,start_long,lat_num,start_lat,x_scale,y_scale,Latitude_Minimum,Latitude_Maximum ,Longitude_Minimum,Longitude_Maximum
dir = ''
dlg = wxFileDialog(parent, "Location of DEM file", ".", "", "*.dem", wxOPEN)
if dlg.ShowModal() == wxID_OK:
dir = dlg.GetPath()
dlg.Destroy()
tom = open(dir,'rb')
head_file_name = os.path.join(os.path.split(dir)[0],os.path.splitext(os.path.split(dir)[1])[0] + '.hdr')
temp_file = open(head_file_name,'r')
temp_file.readline()
temp_file.readline()
N_ROWS = int(string.split(temp_file.readline())[1])
N_COLS = int(string.split(temp_file.readline())[1])
temp_file.readline()
temp_file.readline()
temp_file.readline()
temp_file.readline()
temp_file.readline()
temp_file.readline()
long_num = float(string.split(temp_file.readline())[1])
start_long = long_num
lat_num = float(string.split(temp_file.readline())[1])
start_lat = lat_num
x_scale = float(string.split(temp_file.readline())[1])
y_scale = float(string.split(temp_file.readline())[1])
Latitude_Minimum = lat_num - (N_ROWS * y_scale)
Latitude_Maximum = lat_num + y_scale
Longitude_Minimum = long_num - x_scale
Longitude_Maximum = long_num + (N_COLS * x_scale)
temp_file.close()
return dir
<< globalDEMV methods >>=
class Global_tile(wxScrolledWindow):# the display window for giff from DEM package
<< class Global_tile declarations >>
<< class Global_tile methods >>
@code
# 8 to 1 ratio giff of data
# separate window so that I can catch the mouse events
# this is essentially taken from the demo
<< class Global_tile methods >>=
def __init__(self, parent, main_parent,id = -1, size = wxDefaultSize):
# object, parent objet (one side of split window) for wxPython, parent object
wxScrolledWindow.__init__(self, parent, id, wxPoint(0, 0), size, wxSUNKEN_BORDER)
self.parent = main_parent # make the parent object pointer passable to other procedures in the class
# scrolled window properties
self.lines = []
self.maxWidth = 1500
self.maxHeight = 1500
self.SetBackgroundColour(wxNamedColor("WHITE"))
self.SetCursor(wxStockCursor(wxCURSOR_CROSS))
temp_dir = os.path.join(os.path.split(demdirectory)[0],os.path.splitext(os.path.split(demdirectory)[1])[0] + '.gif')
bmp = wxBitmap(temp_dir,wxBITMAP_TYPE_GIF)
self.bmp = bmp
self.SetScrollbars(1, 1,600, 750)
# some class variables
self.coords = (0,0,1,1)# the box coordinates (probably redundant)
self.x, self.y = 0,0 # the mouse position
self.recstartx, self.recstarty = 0,0 # the rectangle start position
# class events
EVT_MOUSE_EVENTS(self, self.OnMouseEvent) # for when the mouse is used over the scrolled window
EVT_PAINT(self,self.OnPaint)# for when the window is move or resized
<< class Global_tile methods >>=
def OnMouseEvent(self,event):# the whole point of this class
if event.LeftDown():# mouse down
global lat_num, long_num # make sure I have access to my global variables
self.x, self.y = self.ConvertEventCoords(event)# get the mouse position
self.recstartx, self.recstarty = self.x, self.y # set the start of the rectangle mouse position
lat_num = start_lat - ((self.recstarty * 8) * y_scale)# calculate the latitude
long_num = start_long + ((self.recstartx * 8) * x_scale)# calculate the longitude
self.parent.Longitude.SetValue(str(long_num))# set the value of the Longitude text box
self.parent.Latitude.SetValue(str(lat_num))# set the value of the Latitude text box
self.CaptureMouse()# Capture all mouse events in class
elif event.Dragging():# dragging the mouse
dc = wxClientDC(self)
# make a device context, client because it is not in the OnPaint method
self.PrepareDC(dc)# link with class
dc.BeginDrawing()# draw to the DC
dc.DrawBitmap(self.bmp, 0, 0, 0)# draw the GIF from the DEM Package
self.x, self.y = self.ConvertEventCoords(event)# get the mouse position
dc.SetBrush(wxTRANSPARENT_BRUSH)# make sure the square is not filled
dc.SetPen(wxPen(wxNamedColour('RED'), 1))# set the box outline to red 1pixel
self.coords = (self.recstartx, self.recstarty) + ((self.x - self.recstartx), (self.y - self.recstarty))
# put the box in a tuple for the drawing function
apply(dc.DrawRectangle, self.coords)# draw the box
dc.EndDrawing()# move the drawing to the window
elif event.LeftUp():# let go of mouse button
global height_num,width_num# make sure I have access to my global variables
height_num = ((self.y - self.recstarty)* 8) * y_scale# calculate the height
width_num = ((self.x - self.recstartx) * 8) * x_scale# calculate the width
self.parent.Width.SetValue(str(width_num))# set the value of the Width text box
self.parent.Height.SetValue(str(height_num))# set the value of the Height text box
self.ReleaseMouse()# Release Mouse events
<< class Global_tile methods >>=
def OnPaint(self, event):# when the window is moved or uncovered
dc = wxPaintDC(self)
# make a device context, paint because it is in the OnPaint method
self.PrepareDC(dc)# link with class
dc.BeginDrawing()# draw to the DC
dc.DrawBitmap(self.bmp, 0, 0, 0)# draw the GIF from the DEM Package
dc.SetBrush(wxTRANSPARENT_BRUSH)# make sure the square is not filled
dc.SetPen(wxPen(wxNamedColour('RED'), 1))# set the box outline to red 1pixel
apply(dc.DrawRectangle, self.coords)# draw the box
dc.EndDrawing()# move the drawing to the window
<< class Global_tile methods >>=
def ConvertEventCoords(self, event):# gets x, y inside scrolled window
xView, yView = self.GetViewStart()
# Get the position at which the visible portion of the window starts.
xDelta, yDelta = self.GetScrollPixelsPerUnit()
# Get the number of pixels per scroll line made by SetScrollbars.
return (event.GetX() + (xView * xDelta), event.GetY() + (yView * yDelta))
# calculate and return x and y position
<< globalDEMV methods >>=
## Create a new frame class, derived from the wxPython Frame.
class ViewerFrame(wxFrame):
<< class ViewerFrame methods >>
<< class ViewerFrame methods >>=
def __init__(self, parent, id, title):
# First, call the base class' __init__ method to create the frame
wxFrame.__init__(self, parent, id, title,wxDefaultPosition,wxDefaultSize)
global demdirectory,tom,long_num,start_long,lat_num,start_lat,x_scale,y_scale,Latitude_Minimum,Latitude_Maximum ,Longitude_Minimum,Longitude_Maximum
self.Imagetest = Image.new.__init__('RGB',(853,603))
# make sure I have access to my global variables
demdirectory = get_init_data(self)
# get .DEM file location as well as set up data from .HDR file
self.displaychoice = 'shade'# start with shade plot
self.globdir = os.path.join(os.getcwd(),'glob.png')
# set the file name used for transferring the data from dislin in the current working directory
self.mainmenu = wxMenuBar() # Create menu bar.
menu=wxMenu()# Make a menu (will be the Open menu)
exitID=wxNewId() # Make a new ID for a menu entry.
menu.Append(exitID, '&Open', 'Open Picture') # Name the ID by adding it to the menu.
EVT_MENU(self, exitID, self.Picture_Open)# the menu event
exitID=wxNewId()# Make a new ID for a menu entry.
menu.Append(exitID, '&Save', 'Save Projection')# Name the ID by adding it to the menu.
EVT_MENU(self, exitID, self.Picture_Save)# the menu event
exitID=wxNewId()# Make a new ID for a menu entry.
menu.Append(exitID, 'E&xit', 'Exit program')# Name the ID by adding it to the menu.
EVT_MENU(self, exitID, self.Picture_Exit)# the menu event
self.mainmenu.Append (menu, '&File') # Add the File menu to the menu bar.
self.SetMenuBar (self.mainmenu) # Attach the menu bar to the window.
# make the main splitter window one side holds the dislin picture one for the DEM GIF
self.Splitter = wxSplitterWindow(self, -1)
# make sizers
self.Picture_button_sizer = wxBoxSizer(wxHORIZONTAL)
self.Picture_sizer = wxBoxSizer(wxVERTICAL)
self.sub_Picture_button_sizer = wxBoxSizer(wxVERTICAL)
self.sub_Picture_button_text_sizer = wxBoxSizer(wxVERTICAL)
self.sub_Picture_button_sizer2 = wxBoxSizer(wxVERTICAL)
self.sub_Picture_button_text_sizer2 = wxBoxSizer(wxVERTICAL)
self.ThreeD_button_sizer = wxBoxSizer(wxHORIZONTAL)
self.ThreeD_sizer = wxBoxSizer(wxVERTICAL)
self.choices_sizer = wxBoxSizer(wxVERTICAL)
self.choices_text_sizer = wxBoxSizer(wxVERTICAL)
self.camera_sizer = wxBoxSizer(wxVERTICAL)
self.camera_text_sizer = wxBoxSizer(wxVERTICAL)
# right splitter window
# panel for text and labels and scrolled window
self.Picture_view = wxPanel(self.Splitter, -1, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL)
# The DEM gif scrolled window
self.PictureWindow = Global_tile(self.Picture_view, self)
# long and lat boxes with labels
self.Longitude_ID = wxNewId()
self.Latitude_ID = wxNewId()
self.lattext = wxStaticText(self.Picture_view, -1, " Latitude")
self.longtext = wxStaticText(self.Picture_view, -1, " Longitude")
self.Latitude = wxTextCtrl(self.Picture_view, self.Latitude_ID, "0", size=(125, -1))
self.Latitude.SetInsertionPoint(0)
self.Latitude.SetValue(str(lat_num))
EVT_TEXT(self, self.Latitude_ID, self.EvtLatitudeText)
self.Longitude = wxTextCtrl(self.Picture_view, self.Longitude_ID, "0", size=(125, -1))
self.Longitude.SetInsertionPoint(0)
self.Longitude.SetValue(str(long_num))
EVT_TEXT(self, self.Longitude_ID, self.EvtLongitudeText)
#Height and Width boxes with lables
self.widthtext = wxStaticText(self.Picture_view, -1, " Width")
self.heighttext = wxStaticText(self.Picture_view, -1, " Height")
self.Width_ID = wxNewId()
self.Height_ID = wxNewId()
self.Width = wxTextCtrl(self.Picture_view, self.Width_ID, "0", size=(125, -1))
self.Width.SetInsertionPoint(0)
self.Width.SetValue(str(width_num))
EVT_TEXT(self, self.Width_ID, self.EvtText)
self.Height = wxTextCtrl(self.Picture_view, self.Height_ID, "0", size=(125, -1))
self.Height.SetInsertionPoint(0)
self.Height.SetValue(str(height_num))
EVT_TEXT(self, self.Height_ID, self.EvtHeightText)
# right splitter window
# panel for text and lables and scrolled window
self.ThreeD_viewID = wxNewId()
self.ThreeD_view = wxPanel(self.Splitter, self.ThreeD_viewID, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL)
# The Dislin PNG scrolled window
self.ThreeDWindowID = wxNewId()
#self.glob_thing = wxScrolledWindow(self.ThreeD_view, self.ThreeDWindowID, wxDefaultPosition, wxDefaultSize, wxSUNKEN_BORDER)
self.glob_thing = image_view.ImageView(self.ThreeD_view, -1, tools = [image_view.PanTool, image_view.ZoomTool, image_view.SharpTool , image_view.ColorTool , image_view.BrightTool , image_view.ContTool , image_view.RestoreTool])
# refresh button
self.ThreeD_Refresh_Button_ID = wxNewId()
self.ThreeD_Refresh_Button = wxButton(self.ThreeD_view, self.ThreeD_Refresh_Button_ID, 'Refresh', wxDefaultPosition)
EVT_BUTTON(self, self.ThreeD_Refresh_Button_ID, self.ThreeD_Refresh_Button_Press)
# A choice dialog for the display type
self.displaytext = wxStaticText(self.ThreeD_view, -1, " Display")
TypeList = ['shade', 'contour', 'grid']
self.choice = wxChoice(self.ThreeD_view, 40, (80, 50), choices = TypeList)
self.choice.SetStringSelection('shade')
EVT_CHOICE(self, 40 , self.EvtChoice)
# spin controls and lables for the contour lines, z debth, rotation angle, up angle and distance
self.conttext = wxStaticText(self.ThreeD_view, -1, " Cont. lines")
self.ztext = wxStaticText(self.ThreeD_view, -1, " Z depth")
self.Cont_linesID = wxNewId()
self.Cont_lines = wxSpinCtrl(self.ThreeD_view, self.Cont_linesID, "", wxPoint(30, 50), wxSize(80, -1))
self.Cont_lines.SetRange(1,25)
self.Cont_lines.SetValue(10)
self.Z_depthID = wxNewId()
self.Z_depth = wxSpinCtrl(self.ThreeD_view, self.Z_depthID, "", wxPoint(30, 50), wxSize(80, -1))
self.Z_depth.SetRange(1,150)
self.Z_depth.SetValue(10)
self.ratext = wxStaticText(self.ThreeD_view, -1, " R Angle")
self.uatext = wxStaticText(self.ThreeD_view, -1, " Up Angle")
self.disttext = wxStaticText(self.ThreeD_view, -1, " Distance")
self.RAngle_ID = wxNewId()
self.RAngle = wxSpinCtrl(self.ThreeD_view, self.RAngle_ID, "", wxPoint(30, 50), wxSize(80, -1))
self.RAngle.SetRange(0,359)
self.RAngle.SetValue(RSpin)
EVT_SPINCTRL(self, self.RAngle_ID, self.OnRSpin)
self.UAngle_ID = wxNewId()
self.UAngle = wxSpinCtrl(self.ThreeD_view, self.UAngle_ID, "", wxPoint(30, 50), wxSize(80, -1))
self.UAngle.SetRange(0,359)
self.UAngle.SetValue(USpin)
EVT_SPINCTRL(self, self.UAngle_ID, self.OnUSpin)
self.DAngle_ID = wxNewId()
self.DAngle = wxSpinCtrl(self.ThreeD_view, self.DAngle_ID, "", wxPoint(30, 50), wxSize(80, -1))
self.DAngle.SetRange(0,5000)
self.DAngle.SetValue(DSpin)
EVT_SPINCTRL(self, self.DAngle_ID, self.OnDSpin)
# set up splitter window
self.Splitter.SetMinimumPaneSize(20)
self.Splitter.SplitVertically(self.ThreeD_view, self.Picture_view)
self.Splitter.SetSashPosition(300)
# set up initial bitmaps for scrolled windows
temp_dir = os.path.join(os.path.split(demdirectory)[0],os.path.splitext(os.path.split(demdirectory)[1])[0] + '.gif')
self.view_bitmap = wxBitmap(temp_dir,wxBITMAP_TYPE_GIF)
self.bitmapID = wxNewId()
self.bitmap2ID = wxNewId()
#self.glob_thing = wxStaticBitmap(self.ThreeDWindow, self.bitmap2ID,self.view_bitmap, wxPoint(0,0), wxSize(853, 603))
# add everything to sizers
self.sub_Picture_button_text_sizer.Add(self.lattext, 1, wxEXPAND)
self.sub_Picture_button_text_sizer.Add(self.longtext, 1, wxEXPAND)
self.sub_Picture_button_sizer.Add(self.Latitude, 1, wxEXPAND)
self.sub_Picture_button_sizer.Add(self.Longitude, 1, wxEXPAND)
self.sub_Picture_button_text_sizer2.Add(self.heighttext, 1, wxEXPAND)
self.sub_Picture_button_text_sizer2.Add(self.widthtext, 1, wxEXPAND)
self.sub_Picture_button_sizer2.Add(self.Height, 1, wxEXPAND)
self.sub_Picture_button_sizer2.Add(self.Width, 1, wxEXPAND)
self.Picture_button_sizer.Add(self.sub_Picture_button_text_sizer, 1, wxEXPAND)
self.Picture_button_sizer.Add(self.sub_Picture_button_sizer, 1, wxEXPAND)
self.Picture_button_sizer.Add(self.sub_Picture_button_text_sizer2, 1, wxEXPAND)
self.Picture_button_sizer.Add(self.sub_Picture_button_sizer2, 1, wxEXPAND)
self.Picture_sizer.Add(self.Picture_button_sizer, 1, wxEXPAND)
self.Picture_sizer.Add(self.PictureWindow, 10, wxEXPAND)
self.Picture_view.SetSizer(self.Picture_sizer)
self.Picture_view.SetAutoLayout(1)
self.Picture_sizer.Fit(self.Picture_view)
self.camera_text_sizer.Add(self.ratext, 1, wxEXPAND)
self.camera_text_sizer.Add(self.uatext, 1, wxEXPAND)
self.camera_text_sizer.Add(self.disttext, 1, wxEXPAND)
self.camera_sizer.Add(self.RAngle, 1, wxEXPAND)
self.camera_sizer.Add(self.UAngle, 1, wxEXPAND)
self.camera_sizer.Add(self.DAngle, 1, wxEXPAND)
self.ThreeD_button_sizer.Add(self.ThreeD_Refresh_Button, 1, wxEXPAND)
self.ThreeD_button_sizer.Add(self.camera_text_sizer, 1, wxEXPAND)
self.ThreeD_button_sizer.Add(self.camera_sizer, 1, wxEXPAND)
self.choices_sizer.Add(self.choice, 1, wxEXPAND)
self.choices_sizer.Add(self.Cont_lines, 1, wxEXPAND)
self.choices_sizer.Add(self.Z_depth, 1, wxEXPAND)
self.choices_text_sizer.Add(self.displaytext, 1, wxEXPAND)
self.choices_text_sizer.Add(self.conttext, 1, wxEXPAND)
self.choices_text_sizer.Add(self.ztext, 1, wxEXPAND)
self.ThreeD_button_sizer.Add(self.choices_text_sizer, 1, wxEXPAND)
self.ThreeD_button_sizer.Add(self.choices_sizer, 1, wxEXPAND)
self.ThreeD_sizer.Add(self.ThreeD_button_sizer, 1, wxEXPAND)
self.ThreeD_sizer.Add(self.glob_thing, 10, wxEXPAND)
self.ThreeD_view.SetSizer(self.ThreeD_sizer)
self.ThreeD_view.SetAutoLayout(1)
self.ThreeD_sizer.Fit(self.ThreeD_view)
#make sure that the right controls are displayed
self.UAngle.Show(0)
self.DAngle.Show(0)
self.RAngle.Show(0)
self.ratext.Show(0)
self.uatext.Show(0)
self.disttext.Show(0)
self.conttext.Show(0)
self.ztext.Show(0)
self.Cont_lines.Show(0)
self.Z_depth.Show(0)
<< class ViewerFrame methods >>=
def OnCloseWindow(self, event):
# close the DEM file
tom.close()
# tell the window to kill itself
self.Destroy()
<< class ViewerFrame methods >>=
def Picture_Open(self, event):
# run the get_init_data function and reset bitmaps and text boxes in DEM gif scrolled window
global demdirectory
demdirectory = get_init_data(self)
temp_dir = os.path.join(os.path.split(demdirectory)[0],os.path.splitext(os.path.split(demdirectory)[1])[0] + '.gif')
bitmap = wxBitmap(temp_dir,wxBITMAP_TYPE_GIF)
self.PictureWindow.bmp = bitmap
self.PictureWindow.Refresh()
self.Longitude.SetValue(str(long_num))
self.Latitude.SetValue(str(lat_num))
self.Height.SetValue(str(height_num))
self.Width.SetValue(str(width_num))
<< class ViewerFrame methods >>=
def EvtChoice(self, event):# if the display choice is changed
self.displaychoice = event.GetString()
if self.displaychoice == 'shade':
self.UAngle.Show(0)# makeing sure only appropriate controls are displayed
self.DAngle.Show(0)
self.RAngle.Show(0)
self.ratext.Show(0)
self.uatext.Show(0)
self.disttext.Show(0)
self.conttext.Show(0)
self.ztext.Show(0)
self.Cont_lines.Show(0)
self.Z_depth.Show(0)
elif self.displaychoice == 'contour':
self.UAngle.Show(0)
self.DAngle.Show(0)
self.RAngle.Show(0)
self.ratext.Show(0)
self.uatext.Show(0)
self.disttext.Show(0)
self.conttext.Show(1)
self.ztext.Show(0)
self.Cont_lines.Show(1)
self.Z_depth.Show(0)
elif self.displaychoice == 'grid':
self.UAngle.Show(1)
self.DAngle.Show(1)
self.RAngle.Show(1)
self.ratext.Show(1)
self.uatext.Show(1)
self.disttext.Show(1)
self.conttext.Show(0)
self.ztext.Show(1)
self.Cont_lines.Show(0)
self.Z_depth.Show(1)
<< class ViewerFrame methods >>=
def OnRSpin(self, event):# rotation angle has changed
RSpin = self.RAngle.GetValue()
<< class ViewerFrame methods >>=
def OnUSpin(self, event):# up angle has changed
USpin = self.UAngle.GetValue()
<< class ViewerFrame methods >>=
def OnDSpin(self, event):# distance has changed
DSpin = self.DAngle.GetValue()
<< class ViewerFrame methods >>=
def EvtLatitudeText(self, event):# Latitude has changed
try: # while you type in a new number some of the characters
# don?t make since so we just pass till it makes since
my_lat_num = float(event.GetString())
my_height_num = float(self.Height.GetValue())
if my_lat_num > Latitude_Minimum and my_lat_num < Latitude_Maximum:# is it in scale
temp = int((-(my_lat_num - start_lat)/ y_scale)/ 8)# convert from lat to bitmap cooranants
if temp != self.PictureWindow.recstarty:# is it the same spot on the bitmap
# the same drawing procedure used in the Global_tile class
self.PictureWindow.recstarty = temp
self.PictureWindow.y = int(((my_height_num / y_scale) / 8) + self.PictureWindow.recstarty)
self.PictureWindow.coords = (self.PictureWindow.recstartx, self.PictureWindow.recstarty) + ((self.PictureWindow.x - self.PictureWindow.recstartx), (self.PictureWindow.y - self.PictureWindow.recstarty))
dc = wxClientDC(self.PictureWindow)
self.PictureWindow.PrepareDC(dc)
dc.BeginDrawing()
dc.DrawBitmap(self.PictureWindow.bmp, 0, 0, 0)
dc.SetBrush(wxTRANSPARENT_BRUSH)
dc.SetPen(wxPen(wxNamedColour('RED'), 1))
apply(dc.DrawRectangle, self.PictureWindow.coords)
dc.EndDrawing()
except ValueError:
pass
<< class ViewerFrame methods >>=
def EvtLongitudeText(self, event):# Longitude has changed
try:# while you type in a new number some of the characters
# don?t make since so we just pass till it makes since
my_long_num = float(event.GetString())
my_width_num = float(self.Width.GetValue())
if my_long_num > Longitude_Minimum and my_long_num < Longitude_Maximum:# is it in scale
temp = int(((my_long_num - start_long) / x_scale)/ 8)# convert from long to bitmap cooranants
if temp != self.PictureWindow.recstartx:# is it the same spot on the bitmap
self.PictureWindow.recstartx = temp
self.PictureWindow.x = int(((my_width_num / x_scale) / 8) + self.PictureWindow.recstartx)
self.PictureWindow.coords = (self.PictureWindow.recstartx, self.PictureWindow.recstarty) + ((self.PictureWindow.x - self.PictureWindow.recstartx), (self.PictureWindow.y - self.PictureWindow.recstarty))
dc = wxClientDC(self.PictureWindow)
self.PictureWindow.PrepareDC(dc)
dc.BeginDrawing()
dc.DrawBitmap(self.PictureWindow.bmp, 0, 0, 0)
dc.SetBrush(wxTRANSPARENT_BRUSH)
dc.SetPen(wxPen(wxNamedColour('RED'), 1))
apply(dc.DrawRectangle, self.PictureWindow.coords)
dc.EndDrawing()
except ValueError:
pass
<< class ViewerFrame methods >>=
def Picture_Save(self, event):
# save the dislin PNG as any PIL file type
path = os.path.join(os.getcwd(),'glob2.png')
dlg = wxFileDialog(self, "Save Shading Map", ".", "","BMP files (*.bmp)|*.bmp|GIF files (*.gif)|*.gif|Jpeg files (*.jpg)|*.jpg|PNG files (*.png)|*.png", wxSAVE)
if dlg.ShowModal() == wxID_OK:
path = dlg.GetPath()
newimage = self.glob_thing.get_image()
newimage.save(path)
else:
pass
dlg.Destroy()
<< class ViewerFrame methods >>=
def EvtText(self, event):
try:# while you type in a new number some of the characters
# don?t make since so we just pass till it makes since
my_width_num = float(event.GetString())
temp = int(((my_width_num / x_scale) / 8) + self.PictureWindow.recstartx)
if temp != self.PictureWindow.x:
self.PictureWindow.x = temp
self.PictureWindow.coords = (self.PictureWindow.recstartx, self.PictureWindow.recstarty) + ((self.PictureWindow.x - self.PictureWindow.recstartx), (self.PictureWindow.y - self.PictureWindow.recstarty))
dc = wxClientDC(self.PictureWindow)
self.PictureWindow.PrepareDC(dc)
dc.BeginDrawing()
dc.DrawBitmap(self.PictureWindow.bmp, 0, 0, 0)
dc.SetBrush(wxTRANSPARENT_BRUSH)
dc.SetPen(wxPen(wxNamedColour('RED'), 1))
apply(dc.DrawRectangle, self.PictureWindow.coords)
dc.EndDrawing()
except ValueError:
pass
<< class ViewerFrame methods >>=
def EvtHeightText(self, event):
try:# while you type in a new number some of the characters
# don?t make since so we just pass till it makes since
my_height_num = float(event.GetString())
temp = int(((my_height_num / y_scale) / 8) + self.PictureWindow.recstarty)
if temp != self.PictureWindow.y:
self.PictureWindow.y = temp
self.PictureWindow.coords = (self.PictureWindow.recstartx, self.PictureWindow.recstarty) + ((self.PictureWindow.x - self.PictureWindow.recstartx), (self.PictureWindow.y - self.PictureWindow.recstarty))
dc = wxClientDC(self.PictureWindow)
self.PictureWindow.PrepareDC(dc)
dc.BeginDrawing()
dc.DrawBitmap(self.PictureWindow.bmp, 0, 0, 0)
dc.SetBrush(wxTRANSPARENT_BRUSH)
dc.SetPen(wxPen(wxNamedColour('RED'), 1))
apply(dc.DrawRectangle, self.PictureWindow.coords)
dc.EndDrawing()
except ValueError:
pass
<< class ViewerFrame methods >>=
def Picture_Exit(self, event):
# close the DEM file
tom.close()
# tell the window to kill itself
self.Destroy()
<< class ViewerFrame methods >>=
def ThreeD_Refresh_Button_Press(self, event):
# where Everything is happening
#hi_num = 0# there?s a master list of elevation for each tile of the DEM data
#lo_num = 4328# so I could use some kind of look up table but this way is just as easy
new_line = []
data_char = ''
data_line_num = (self.PictureWindow.coords[2] * 16)
# width of box 8 to 1 data to gif ratio 16 bit variable in a string
for x in range((self.PictureWindow.coords[1] * 8),(self.PictureWindow.coords[1] + self.PictureWindow.coords[3]) * 8):
# iterate over height
data_start = (x * 9600) + (self.PictureWindow.coords[0] * 16)
# find start position of rectangle in file (the number 9600 bytes per line
# should probably be in a variable)
tom.seek(data_start)# move to proper position in file
data_char = tom.read(data_line_num)# read proper number of bytes
for i in range(0, len(data_char) - 1, 2):# iterate over the characters by 2s
# its a 16 bit signed integer which means that there has got to be
# a more elegant way of doing this but I was lazy
data_num = (ord(data_char[i]) << 8) | ord(data_char[i+1])
if data_num == 55537:#sea level
data_num = 0
if data_num >= 32767:
data_num = -(data_num ^ 65535)
new_line.append(data_num)
img_size = ((self.PictureWindow.coords[2] * 8) , (self.PictureWindow.coords[3] * 8) )
# Image size for dislin
# this could be an error spot on some systems
if self.displaychoice == 'shade':# call the shade routine
self.dis_img(new_line, img_size[1], img_size[0], min(new_line),max(new_line))
elif self.displaychoice == 'grid':# call the grid routine
self.dis_img2(new_line,img_size[1],img_size[0],min(new_line),max(new_line))
elif self.displaychoice == 'contour':# call the contour routine
self.dis_img3(new_line,img_size[1],img_size[0], min(new_line),max(new_line))
self.glob_thing.set_image(self.Imagetest)
<< class ViewerFrame methods >>=
def dis_img(self, zmat, n, m, zl, zh):# shade routine
char = ''
temp_Width = 1
temp_Height = 1
temp_lat = 1
temp_long = 1
newmat = []
max_bytes = 0
for i in range(m-1,-1,-1):# transform the rectangle
for j in range(n):
newmat.append(zmat[(j*m)+i])
newmat.reverse()
# get the lat, long, height and width for lables
try:
temp_Width = float(self.Width.GetValue())
temp_Height = float(self.Height.GetValue())
except ValueError:
temp_Width = 1
temp_Height = 1
try:
temp_lat = float(self.Latitude.GetValue())
temp_long = float(self.Longitude.GetValue())
except ValueError:
temp_lat = lat_num
temp_long = long_num
if zl == zh:
zl = 0
zh = 10
dislin.metafl('VIRT')# level 0,sets the file type
dislin.setpag('da4l')# page size and orientation
dislin.setfil(self.globdir)# file name
dislin.disini()# level 1, change to level 1
dislin.pagera()# plots a border around the page
dislin.hwfont()# sets default font
dislin.name ('Latitude', 'Y')
dislin.name ('Longitude', 'X')
dislin.name ('Meters', 'Z')
if (m * (1400 / n))< 2000:
dislin.ax3len(m * (1400 / n), 1400, 1400)
else:
dislin.ax3len(2000, n * (2000 / m), 1400)
dislin.shdmod('poly', 'contur')
dislin.autres(m, n)
# level 3
dislin.graf3(temp_long, temp_long + temp_Width, temp_long, temp_Width/5, temp_lat - temp_Height, temp_lat , temp_lat - temp_Height, temp_Height / 5, zl, zh, zl, (zh-zl)/10)
dislin.crvmat(newmat, m, n, 1, 1)
dislin.title()
char_buff = dislin.rbfpng(max_bytes)
max_bytes = int(char_buff[1])
char_buff = dislin.rbfpng(max_bytes)
char = char_buff[0]
self.Imagetest = Image.open(StringIO.StringIO(char))
dislin.disfin()
<< class ViewerFrame methods >>=
def dis_img2(self, zmat, n, m, zl, zh):
char = ''
max_bytes = 0
newmat = []
for i in range(m):
for j in range(n):
newmat.append(zmat[(j*m)+i])
newmat.reverse()
zmat = newmat
try:
temp_Width = float(self.Width.GetValue())
temp_Height = float(self.Height.GetValue())
except ValueError:
temp_Width = 1
temp_Height = 1
try:
temp_lat = float(self.Latitude.GetValue())
temp_long = float(self.Longitude.GetValue())
except ValueError:
temp_lat = lat_num
temp_long = long_num
if zl == zh:
zl = 0
zh = 10
xray = range (m)
yray = range (n)
for i in range(n):
yray[i] = temp_lat - ((temp_Height / n) * i)
for i in range(m):
xray[i] = temp_long + ((temp_Width / m) * i)
RSpin = self.RAngle.GetValue()
USpin = self.UAngle.GetValue()
DSpin = self.DAngle.GetValue()
z_debth = self.Z_depth.GetValue()
dislin.metafl ('VIRT')
dislin.setpag ('da4l')
dislin.setfil (self.globdir)
dislin.disini ()
dislin.pagera ()
dislin.hwfont ()
dislin.name ('Latitude', 'Y')
dislin.name ('Longitude', 'X')
dislin.axis3d(m, n, z_debth)
dislin.view3d(RSpin, USpin, DSpin, 'ANGLE')
dislin.shdmod('smooth', 'surface')
dislin.color('BLUE')
dislin.graf3d(temp_long, temp_long + temp_Width, temp_long, temp_Width/5, temp_lat - temp_Height, temp_lat , temp_lat - temp_Height, temp_Height / 5, zl, zh, zl, (zh-zl)/10)
#dislin.graf3d(temp_lat ,temp_lat + temp_Width , temp_lat , temp_Width/5, temp_long, temp_long + temp_Height, temp_long,temp_Height/5, zl, zh, zl, (zh-zl)/5)--temp_long, temp_long + temp_Height, temp_long, temp_Height/5, temp_lat - temp_Width, temp_lat , temp_lat - temp_Width, temp_Width/5,
dislin.surshd(xray, m, yray,n, zmat)
dislin.title()
char_buff = dislin.rbfpng(max_bytes)
max_bytes = int(char_buff[1])
char_buff = dislin.rbfpng(max_bytes)
char = char_buff[0]
self.Imagetest = Image.open(StringIO.StringIO(char))
dislin.disfin()
<< class ViewerFrame methods >>=
def dis_img3(self, zmat, n, m, zl, zh):
char = ''
max_bytes = 0
newmat = []
for i in range(m):
for j in range(n):
newmat.append(zmat[(j*m)+i])
try:
temp_Width = float(self.Width.GetValue())
temp_Height = float(self.Height.GetValue())
temp_lat = float(self.Latitude.GetValue())
temp_long = float(self.Longitude.GetValue())
except ValueError:
temp_lat = lat_num
temp_long = long_num
temp_Width = 1
temp_Height = 1
if zl == zh:
zl = 0
zh = 10
xray = range (m)
yray = range (n)
c_lines = self.Cont_lines.GetValue()
for i in range(n):
yray[i] = temp_lat - ((temp_Height / n) * i)
for i in range(m):
xray[i] = temp_long + ((temp_Width / m) * i)
dislin.metafl('VIRT')
dislin.setpag('da4l')
dislin.setfil(self.globdir)
dislin.disini()
dislin.pagera()
dislin.hwfont()
dislin.complx()
dislin.name ('Latitude', 'Y')
dislin.name ('Longitude', 'X')
if (m * (1400 / n))< 2000:
dislin.axslen(m * (1400 / n), 1400)
else:
dislin.axslen(2000,n * (2000 / m))
dislin.graf(temp_long, temp_long + temp_Width, temp_long, temp_Width/5, temp_lat - temp_Height, temp_lat , temp_lat - temp_Height, temp_Height/5)
#dislin.graf(temp_lat,temp_lat + temp_Width, temp_lat , temp_Width/5,temp_long, temp_long + temp_Height, temp_long,temp_Height/5)
dislin.height (25)
for i in range (c_lines -1 ):
zlev = (i * ((zh-zl) / c_lines)) + zl
dislin.labels ('FLOAT', 'CONTUR')
dislin.setclr ((i+1) * int(255/c_lines))
dislin.contur (xray, m, yray, n, newmat, zlev)
char_buff = dislin.rbfpng(max_bytes)
max_bytes = int(char_buff[1])
char_buff = dislin.rbfpng(max_bytes)
char = char_buff[0]
self.Imagetest = Image.open(StringIO.StringIO(char))
dislin.disfin()
<< globalDEMV methods >>=
# Every wxWindows application must have a class derived from wxApp
class MyApp(wxApp):
<< class MyApp methods >>
<< class MyApp methods >>=
def OnInit(self):
# Create an instance of our customized Frame class
frame = ViewerFrame(NULL, -1, "Digital Elevation Model Viewer")
frame.Show(true)# Tell wxWindows that this is our main window
self.SetTopWindow(frame) # Return a success flag
return true