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
try:
API = Dispatch("GoogleEarth.ApplicationGE")
if wait:
while API.IsInitialized() == 0:
sleep(0.1)
API.Login()
initflag = API.IsInitialized()
return initflag
except:
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)
MoveWindow(API.GetRenderHwnd(),0,0,width,height,True)
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
else:
system("taskkill /F /IM googlee*") # god this is an atrocious hack
def screenie(thisfile,qual=100,*args,**kwargs):
if init() > 0:
API.SaveScreenShot(thisfile,qual)
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"
else:
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]))
else:
a = init()
if a < 0:
fail("Unable to init")
if a == 0:
ok("Still initializing")
if a > 0:
ok("Initialized")
elif action == "l":
if len(arglist) > 2:
loadkml(str(arglist[0]),bool(arglist[1]),bool(arglist[2]))
ok("Asked load")
elif len(arglist) == 2:
loadkml(str(arglist[0]),bool(arglist[1]))
ok("Asked load")
elif len(arglist) == 1:
loadkml(arglist[0])
ok("Asked load")
else:
fail("Nothing to load")
elif action == "p":
if len(arglist) > 1:
loadkml(str(arglist[0]),int(arglist[1]))
ok("Asked screenshot")
elif len(arglist) > 0:
loadkml(arglist[0])
ok("Asked screenshot")
else:
fail("Nothing to save")
elif action == "c":
if len(arglist) > 2:
capture(int(arglist[0]),int(arglist[1]),int(arglist[2]))
ok("Captured render window")
else:
fail("Wrong number of parameters")
elif action == "r":
uncapture()
ok("Released render window")
elif action == "n":
if len(arglist) > 0:
nuke(arglist[0])
else:
nuke()
#ok("Nuked Google Earth") # taskkill does this for me
elif action == "g":
if init(False):
camget()
print "CAM:",camlat,camlon,camheight,camtilt
else:
fail("Not initialized")
elif action == "s":
if len(arglist) > 5:
camset(float(arglist[0]),float(arglist[1]),float(arglist[2]),float(arglist[3]),float(arglist[4]),float(arglist[5]))
ok("Camera set")
elif len(arglist) == 5:
camset(float(arglist[0]),float(arglist[1]),float(arglist[2]),float(arglist[3]),float(arglist[4]))
ok("Camera set")
elif len(arglist) == 4:
camset(float(arglist[0]),float(arglist[1]),float(arglist[2]),float(arglist[3]))
ok("Camera set")
else:
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"
else:
fail("No command")
if __name__ == "__main__":
parseparams()