Dear Jeff & ALL,
Attached is the latest version of my Basemap embedded in wxPython
sample application. I have added a check menu option that allows one
to toggle the overlay of the Blue Marble image on and off the Basemap
figure. Everything works well -- except that the Blue Marble image is
plotted upside down! I could not figure out the cause of this, say,
rather bizarre behaviour. Any hints?
Thanks in advance for any assistance you can provide.
Best regards,
PS This version has another known bug, to be eventually fixed -- if
the user has plotted a point coordinate file, the points are erased if
the Blue Marble overlay is requested because the PlotMap() routine
calls ax.cla() at the start.
--
Dr. Mauro J. Cavalcanti
Ecoinformatics Studio
P.O. Box 46521, CEP 20551-970
Rio de Janeiro, RJ, BRASIL
E-mail: mauro...@gmail.com
Web: http://studio.infobio.net
Linux Registered User #473524 * Ubuntu User #22717
"Life is complex. It consists of real and imaginary parts."
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# generated by wxGlade 0.6.3 on Thu Nov 20 17:06:15 2008
# This sample program shows how to read
# geographic coordinates stored in comma-delimited files
# and plot them in a Matplotlib Basemap class object.
# Data files should be formatted as below:
# cities,latitude,longitude
# "Boulder, CO",40.02,-105.16
# "San Diego, CA",32.73,-117.16
# "Washington, DC",38.55,-77.00
# "Whitefish, MT",48.25,-114.21
# "Belize City, Belize",17.29,-88.10
import os
import csv
import wx
from matplotlib.backends.backend_wx import FigureCanvasWx as FigureCanvas
from matplotlib.backends.backend_wx import NavigationToolbar2Wx
from matplotlib.figure import Figure
from mpl_toolkits.basemap import Basemap
fields = {'cities':0,
'latitude':1,
'longitude':2}
# begin wxGlade: extracode
# end wxGlade
class TMainForm(wx.Frame):
def __init__(self, *args, **kwds):
# begin wxGlade: TMainForm.__init__
kwds["style"] = wx.ICONIZE|wx.CAPTION|wx.MINIMIZE|wx.CLOSE_BOX
wx.Frame.__init__(self, *args, **kwds)
self.Splitter = wx.SplitterWindow(self, -1, style=wx.SP_3D|wx.SP_BORDER)
self.PlotPanel = wx.Panel(self.Splitter, -1)
self.FilePanel = wx.Panel(self.Splitter, -1)
self.Notebook = wx.Notebook(self.FilePanel, -1, style=0)
self.ReportPage = wx.Panel(self.Notebook, -1)
self.FilePage = wx.Panel(self.Notebook, -1)
# Menu Bar
self.MainMenu = wx.MenuBar()
self.FileMenu = wx.Menu()
self.FileOpenItem = wx.MenuItem(self.FileMenu, 102, "&Open...\tCtrl+O", "Open an existing file", wx.ITEM_NORMAL)
self.FileMenu.AppendItem(self.FileOpenItem)
self.FileMenu.AppendSeparator()
self.FileQuitItem = wx.MenuItem(self.FileMenu, wx.ID_EXIT, "&Quit\tCtrl+Q", "Quit the program", wx.ITEM_NORMAL)
self.FileMenu.AppendItem(self.FileQuitItem)
self.MainMenu.Append(self.FileMenu, "&File")
self.OptionsMenu = wx.Menu()
self.OptionsBlueMarbleItem = wx.MenuItem(self.OptionsMenu, 201, "&Blue Marble", "Overlay Blue Marble image", wx.ITEM_CHECK)
self.OptionsMenu.AppendItem(self.OptionsBlueMarbleItem)
self.MainMenu.Append(self.OptionsMenu, "&Options")
self.HelpMenu = wx.Menu()
self.HelpAboutItem = wx.MenuItem(self.HelpMenu, 301, "&About...", "Display general information about the program", wx.ITEM_NORMAL)
self.HelpMenu.AppendItem(self.HelpAboutItem)
self.MainMenu.Append(self.HelpMenu, "&Help")
self.SetMenuBar(self.MainMenu)
# Menu Bar end
self.StatusBar = self.CreateStatusBar(2, wx.ST_SIZEGRIP)
self.FileList = wx.ListBox(self.FilePage, -1, choices=[])
self.ReportText = wx.TextCtrl(self.ReportPage, -1, "", style=wx.TE_MULTILINE|wx.TE_READONLY)
self.__set_properties()
self.__do_layout()
self.Bind(wx.EVT_MENU, self.OnFileOpen, self.FileOpenItem)
self.Bind(wx.EVT_MENU, self.OnFileQuit, self.FileQuitItem)
self.Bind(wx.EVT_MENU, self.OnOptionsOverlay, self.OptionsBlueMarbleItem)
self.Bind(wx.EVT_MENU, self.OnHelpAbout, self.HelpAboutItem)
self.Bind(wx.EVT_LISTBOX, self.OnSelect, self.FileList)
# end wxGlade
self.Bind(wx.EVT_CHECKLISTBOX, self.EvtCheckListBox, self.FileList)
self.Bind(wx.EVT_SIZE, self.OnSize)
def __set_properties(self):
# begin wxGlade: TMainForm.__set_properties
self.SetTitle("Gaia")
self.SetSize((800, 600))
self.SetFocus()
self.StatusBar.SetStatusWidths([-1, -1])
# statusbar fields
StatusBar_fields = ["Ready", ""]
for i in range(len(StatusBar_fields)):
self.StatusBar.SetStatusText(StatusBar_fields[i], i)
self.FileList.SetMinSize((680, 545))
self.ReportText.SetMinSize((680, 545))
# end wxGlade
self.plot_list = list()
self.figure = Figure(figsize=(5,4), dpi=100)
self.canvas = FigureCanvas(self.PlotPanel, -1, self.figure)
self.ax = self.figure.add_axes([0,0,1,1])
self.SetColor( (255,255,255) )
def __do_layout(self):
# begin wxGlade: TMainForm.__do_layout
sizer_5 = wx.BoxSizer(wx.HORIZONTAL)
sizer_7 = wx.BoxSizer(wx.VERTICAL)
sizer_6 = wx.BoxSizer(wx.VERTICAL)
sizer_8 = wx.BoxSizer(wx.HORIZONTAL)
sizer_9 = wx.BoxSizer(wx.HORIZONTAL)
sizer_9.Add(self.FileList, 0, wx.EXPAND, 0)
self.FilePage.SetSizer(sizer_9)
sizer_8.Add(self.ReportText, 0, wx.EXPAND, 0)
self.ReportPage.SetSizer(sizer_8)
self.Notebook.AddPage(self.FilePage, "Files")
self.Notebook.AddPage(self.ReportPage, "Report")
sizer_6.Add(self.Notebook, 1, wx.EXPAND, 0)
self.FilePanel.SetSizer(sizer_6)
self.PlotPanel.SetSizer(sizer_7)
self.Splitter.SplitVertically(self.FilePanel, self.PlotPanel, 240)
sizer_5.Add(self.Splitter, 1, wx.EXPAND|wx.FIXED_MINSIZE, 0)
self.SetSizer(sizer_5)
self.Layout()
self.Centre()
# end wxGlade
self.Splitter.SetMinimumPaneSize(200)
sizer_7.Add(self.canvas, 1, wx.EXPAND|wx.RIGHT, 0)
self.SetSizer(sizer_7)
self.toolbar = NavigationToolbar2Wx(self.canvas)
self.toolbar.Realize()
tw, th = self.toolbar.GetSizeTuple()
fw, fh = self.canvas.GetSizeTuple()
self.toolbar.SetSize(wx.Size(fw, th))
sizer_7.Add(self.toolbar, 0, wx.GROW)
self.toolbar.update()
self.PlotMap()
def OnFileOpen(self, event): # wxGlade: TMainForm.<event_handler>
dlg = wx.FileDialog(self, "Open", os.getcwd(), "", "Comma-Separated Values files (*.csv)|*.csv", wx.OPEN)
if dlg.ShowModal() == wx.ID_OK:
fileName = dlg.GetFilename()
dirName = dlg.GetDirectory()
self.SetTitle("Gaia" + " - [" + fileName + "]")
self.FileList.Append(fileName)
dlg.Destroy()
def OnFileQuit(self, event): # wxGlade: TMainForm.<event_handler>
self.Close()
def OnHelpAbout(self, event): # wxGlade: TMainForm.<event_handler>
dlg = wx.MessageDialog(self, "This sample program shows how to read\n"
"geographic coordinates stored in comma-delimited files\n"
"and plot them in a Matplotlib Basemap class object,",
"About Gaia", wx.OK|wx.ICON_INFORMATION)
dlg.ShowModal()
dlg.Destroy()
def OnSelect(self, event): # wxGlade: TMainForm.<event_handler>
dataFile = open(self.FileList.GetStringSelection(), "r")
try:
reader = csv.reader(dataFile, delimiter = ',', quoting=csv.QUOTE_ALL)
longs = []
lats = []
reader.next() # Skip header line
i = 0
for row in reader:
locality = row[fields['cities']]
longitude = float(row[fields['longitude']])
latitude = float(row[fields['latitude']])
longs.append(longitude)
lats.append(latitude)
i = i + 1
finally:
dataFile.close()
self.StatusBar.SetStatusText(str(i) + " record(s) processed", 1)
# Store points in the plot list
index = self.FileList.GetSelection()
self.plot_list.append(self.map.plot(longs,lats,'o'))
plot = self.plot_list[index]
plot[0].set_visible(False)
self.Splitter.SendSizeEvent()
def EvtCheckListBox(self, event):
index = event.GetSelection()
plot = self.plot_list[index]
if self.FileList.IsChecked(index):
plot[0].set_visible(True)
else:
plot[0].set_visible(False)
self.Splitter.SendSizeEvent()
self.FileList.SetSelection(index) # so that (un)checking also selects (moves the highlight)
def SetColor(self, rgbtuple=None):
"""Set figure and canvas colours to be the same."""
if rgbtuple is None:
rgbtuple = wx.SystemSettings.GetColour(wx.SYS_COLOUR_BTNFACE).Get()
clr = [c/255. for c in rgbtuple]
self.figure.set_facecolor(clr)
self.figure.set_edgecolor(clr)
self.canvas.SetBackgroundColour(wx.Colour(*rgbtuple))
def PlotMap(self, overlay=False):
"""Plot basemap."""
self.PlotPanel.SetCursor(wx.StockCursor(wx.CURSOR_WAIT))
self.ax.cla()
self.map = Basemap(ax=self.ax)
if overlay:
self.map.bluemarble()
self.map.drawcoastlines(color='#777799')
self.map.drawcountries(color='#ccccee')
self.map.drawmapboundary()
else:
self.map.drawcoastlines()
self.map.drawcountries()
self.map.fillcontinents(color="lime", lake_color="aqua")
self.map.drawmapboundary(fill_color="aqua")
self.figure.canvas.draw()
self.PlotPanel.SetCursor(wx.StockCursor(wx.CURSOR_ARROW))
def OnSize(self, event):
self.Splitter.SendSizeEvent()
def OnOptionsOverlay(self, event): # wxGlade: TMainForm.<event_handler>
if self.OptionsBlueMarbleItem.IsChecked():
self.PlotMap(overlay=True)
else:
self.PlotMap()
self.Splitter.SendSizeEvent()
# end of class TMainForm
class TApplication(wx.App):
def OnInit(self):
wx.InitAllImageHandlers()
MainForm = TMainForm(None, -1, "")
self.SetTopWindow(MainForm)
MainForm.Show()
return True
# end of class TApplication
if __name__ == "__main__":
Application = TApplication(0)
Application.MainLoop()
------------------------------------------------------------------------------
SF.Net email is Sponsored by MIX09, March 18-20, 2009 in Las Vegas, Nevada.
The future of the web can't happen without you. Join us at MIX09 to help
pave the way to the Next Web now. Learn more and register at
http://ad.doubleclick.net/clk;208669438;13503038;i?http://2009.visitmix.com/
_______________________________________________
Matplotlib-users mailing list
Matplotlib-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/matplotlib-users