GEHandler is a simple Python program that controls Google Earth in Windows. It can launch and kill Google Earth, load a kml, fly to a point, and so on. You can use it by itself as an executable or integrate the python script elsewhere.

usage: gehandler command parameters (use 1/0 for true/false)

c capture render window, parameters: new parent width height

g get camera info, output: lat lon height tilt

h help, you just saw it

i init, parameters: (wait for init)

l load kml, parameters: kmlfile (squelch errors) WARNING: Needs full path!

n nuke google earth (asynchronous)

p take picture(screenshot) parameters: filename (quality)

r release render window to GE parent

s set camera, parameters: lat lon height tilt (speed) (heading)

example: gehandler l c:\pytrac\logo.kml 1

try gehandler c 0 (your screen width) (your screen height) for a fairly interesting desktop wallpaper!

Binary download here and source code as follows (it's only one file, just check the imports before use):

 # -*- coding: cp1252 -*-
 class dummyobject:
   def IsInitialized(self): # emulates GE
       return 1
   def __init__(self, *args, **kwargs):
       "Ignore parameters."
       return None
   def __call__(self, *args, **kwargs):
       "Ignore method calls."
       return self
   def __getattr__(self, mname):
       "Ignore attribute requests."
       return self
   def __setattr__(self, name, value):
       "Ignore attribute setting."
       return self
   def __delattr__(self, name):
       "Ignore deleting attributes."
       return self
   def __repr__(self):
       "Return a string representation."
       return "<Null>"
   def __str__(self):
       "Convert to a string and return it."
       return "Null"
 from os import system
 from sys import argv
 from time import sleep
 from win32gui import GetClientRect, MoveWindow, ShowWindow, FindWindowEx
 from win32com.client import Dispatch
 from ctypes import windll
 user32 = windll.user32
 API = dummyobject()
 initflag = 0
 # used for sanity check of parameters
 typestr = type("aaa")
 typeint = type(1)
 typefloat = type(1.0)
 typebool = type(True)
 def init(wait = False,*args,**kwargs):
   global API, initflag
       API = Dispatch("GoogleEarth.ApplicationGE")
       if wait:
           while API.IsInitialized() == 0:
       initflag = API.IsInitialized()
       return initflag
       API = dummyobject()
       initflag = -1
       return -1
 def capture(whereto, width, height,*args,**kwargs):
       if init() > 0:
           tryme = user32.SetParent(API.GetRenderHwnd(), whereto)
           if tryme:
               ShowWindow(API.GetMainHwnd(), 3) #SW_MAXIMIZE)
               user32.ShowWindowAsync(API.GetMainHwnd(), 0)
 def uncapture(*args,**kwargs):
   if init() > 0:
       user32.ShowWindowAsync(API.GetMainHwnd(), 1)
       user32.SetParent(API.GetRenderHwnd(), API.GetMainHwnd())
 def loadkml(thisfile, sm=True, force=False,*args,**kwargs):
       if init() > 0 or force:
           pli = API.GetMyPlaces()
           if type(pli) <> type(None):
               pli.Visibility = 0
           API.OpenKmlFile(fileName=thisfile, suppressMessages=sm)
 def nuke(async=False,*args,**kwargs):
       if async:
           system("start /B /HIGH taskkill.exe /F /IM googlee*") # god this is an atrocious hack
           system("taskkill /F /IM googlee*") # god this is an atrocious hack
 def screenie(thisfile,qual=100,*args,**kwargs):
       if init() > 0:
 camtilt = 0.0
 camlat = 0.0
 camlon = 0.0
 camheight = 0.0
 def camget(considerTerrain=False,*args,**kwargs):
       global camtilt,camlat,camlon,camheight
       if init() > 0:
           localcam = API.GetCamera(considerTerrain)
           camtilt = localcam.Tilt
           camlat = localcam.FocusPointLatitude
           camlon = localcam.FocusPointLongitude
           camheight = localcam.Range
           return camlat,camlon,camheight,camtilt
 def symclamp(myfloat, clampval):
   a = abs(clampval)
   if myfloat < -a:
       return -a
   if myfloat > a:
       return a
   return myfloat 
 def camset(lat=0.0,lon=0.0,height=0.0,tilt=0.0,speed=1.0,heading=0.0,*args,**kwargs):
   if tilt > 90.0:
       tilt = 90.0
   if tilt < 0.0:
       tilt = 0.0
   if init() > 0:
           pli = API.GetMyPlaces()
           if type(pli) <> type(None):
               pli.Visibility = 0
           API.SetCameraParams( symclamp(lat,90.0), symclamp(lon,180.0), 0.0, 1, height, tilt, heading, speed)
 def fail(why=""):
   print "ERROR:",why
 def ok(what=""):
   global initflag
   if initflag == 1:
       print "SUCCESS:",what
   if initflag == 0:
       print "STANDBY: GE initializing"
   if initflag == -1:
       print "ERROR: GE init failure"
 def parseparams():
   arglist = argv[1:]
   if len(arglist) == 0:
       action = "h"
       action = argv[1].lower()
       arglist = argv[2:]
   #print action, arglist
   # if there are no arguments, look in a file
   if action == "x":
       ok("No action")
   elif action == "i":
       a = -1
       if len(arglist) > 0:
           a = init(int(arglist[0]))
           a = init()
       if a < 0:
           fail("Unable to init")
       if a == 0:
           ok("Still initializing")
       if a > 0:
   elif action == "l":
       if len(arglist) > 2:
           ok("Asked load")
       elif len(arglist) == 2:
           ok("Asked load")
       elif len(arglist) == 1:
           ok("Asked load")
           fail("Nothing to load")
   elif action == "p":
       if len(arglist) > 1:
           ok("Asked screenshot")
       elif len(arglist) > 0:
           ok("Asked screenshot")
           fail("Nothing to save")
   elif action == "c":
       if len(arglist) > 2:
           ok("Captured render window")
           fail("Wrong number of parameters")
   elif action == "r":
       ok("Released render window")
   elif action == "n":
       if len(arglist) > 0:
       #ok("Nuked Google Earth") # taskkill does this for me
   elif action == "g":
       if init(False):
           print "CAM:",camlat,camlon,camheight,camtilt
           fail("Not initialized")
   elif action == "s":
       if len(arglist) > 5:
           ok("Camera set")
       elif len(arglist) == 5:
           ok("Camera set")
       elif len(arglist) == 4:
           ok("Camera set")
           fail("Wrong number of parameters")
   elif action == "h":
       print "GEhandler 0.1"
       print "usage: gehandler command parameters (use 1/0 for true/false)n"
       print "c capture render window, parameters: new parent width height"
       print "g get camera info, output: lat lon height tilt"
       print "h help, you just saw it"
       print "i init, parameters: (wait for init)"
       print "l load kml, paramaters: kmlfile (squelch errors) WARNING: Needs full path!"
       print "n nuke google earth (asynchronous)"
       print "p take picture(screenshot) parameters: filename (quality)"
       print "r release render window to GE parent"
       print "s set camera, parameters: lat lon height tilt (speed) (heading)"
       print "nexample: gehandler l c:pytraclogo.kml 1"
       fail("No command")
 if __name__ == "__main__":
Page last modified on February 03, 2016, at 01:23 AM
Powered by PmWiki