import  sys
import  wx
import httplib, urlparse
import time

LICENSE = "Clear BSD" 

VERSION = "1.00"

class KMLPanel(wx.Panel):
    """Displays all the boxes and stuff."""
    def __init__(self, parent):
        wx.Panel.__init__(self, parent, -1)
        l1 = wx.StaticText(self, 100, "KML:")
        #
        space = 4
        sizer = wx.FlexGridSizer(cols=1, hgap=space, vgap=space)
        sizer.AddMany(
          [  l1 ]
          )
        border = wx.BoxSizer(wx.VERTICAL)
        border.Add(sizer, 0, wx.ALL, 25)
        self.SetSizer(border)
        self.SetAutoLayout(True)


class RunKMLApp(wx.App):
    """Built from wxPython examples"""
    def __init__(self, redirect=False, filename=None):
            wx.App.__init__(self, False, filename)
    def OnInit(self):
        #wx.Log_SetActiveTarget(wx.LogStderr())

        frame = wx.Frame(None, -1, "KMLTransform", pos=(50,50), size=(200,100),
                        style=wx.DEFAULT_FRAME_STYLE)
        frame.CreateStatusBar()

        menuBar = wx.MenuBar()
        menu = wx.Menu()
        item = menu.Append(-1, "E&xit\tCtrl-Q", "Exit demo")
        self.Bind(wx.EVT_MENU, self.OnExitApp, item)
        menuBar.Append(menu, "&File")

        frame.SetMenuBar(menuBar)
        frame.Show(True)

        win = KMLPanel(frame)

        # so set the frame to a good size for showing stuff
        win.SetFocus()
        self.window = win
        #ns['win'] = win
        frect = frame.GetRect()

        self.SetTopWindow(frame)
        self.frame = frame
        dlg = wx.FileDialog(
            frame, message="Choose a file", defaultDir=os.getcwd(), 
            defaultFile="", wildcard="KML Files (*.kml)|*.kml", style=wx.OPEN | wx.CHANGE_DIR
            )

        kml = None
        # Show the dialog and retrieve the user response. If it is the OK response, 
        # process the data.
        if dlg.ShowModal() == wx.ID_OK:
            # This returns a Python list of files that were selected.
            kml = dlg.GetPaths()[0]
            
        
        dlg = wx.FileDialog(
            frame, message="Choose a file", defaultDir=os.getcwd(), 
            defaultFile="", wildcard="Shape File (*.shp)|*.shp", style=wx.SAVE | wx.CHANGE_DIR
            )

        # Show the dialog and retrieve the user response. If it is the OK response, 
        # process the data.
        if dlg.ShowModal() == wx.ID_OK:
            # This returns a Python list of files that were selected.
            shape = dlg.GetPaths()[0]
        if kml and shape:
            from vectorformats.Formats.OGR import OGR
            from vectorformats.Formats.KML import KML 
            
            o = OGR(driver="ESRI Shapefile", dsname=shape, save_on_encode=True); k = KML()
            
            o.encode(k.decode(open(kml).read()))
        
        self.frame.Close(True)
        
        return True


    def OnExitApp(self, evt):
        self.frame.Close(True)

#---------------------------------------------------------------------------

if __name__ == '__main__':
    import sys,os
    app = RunKMLApp()
    app.MainLoop()
