基于 Excel VBA的Google earth应用开发

时间:2022-10-20 01:10:02

基于 Excel VBA的Google earth应用开发

摘要: 本文主要开展了基于Excel VBA的Google earth开发探讨,实现了Excel与Google Earth的实时动态交互,提高了工作效率。

关键词:KML;VBA;Google earth

中图分类号:C35文献标识码: A

The Application development of Google Earth base on Excel VBA

姓名:YiPei Shan Wanting Zhao

(单位):Surveying & Mapping Institute of Dalian(Geomatics Center of Dalian)

Abstract: This paper discuss the Application development of Google Earth base on Excel VBA, Achieved Real-time dynamic interaction between Excel and Google Earth which improved the working efficiency。

Key words: Keyhole Markup Language, Visual Basic for Applications, Google earth

0 引言

随着Google Earth应用的不断扩展,原始的手工输入已不能满足现行相关工作要求,为了将其更好的应用在工作中,实现界面友好化,操作简洁化,高效化,实用化,提出Excel VBA +Google Earth 联动的技术路线。

1 Google Earth简介

Google Earth是由Google公司开发的一款虚拟地球软件,它把航拍照片、卫星图像和GIS数据整合在一起,形成一个地球的三维模型。Google Earth的诞生,改变了传统GIS观念,为普通用户提供高清晰的影像数据,并支持与用户的交互和数据的共享。

Google公司在2008年4月14日放弃了对其KML/KMZ的控制,以便使Google Earth服务和Google map服务中使用的这种语言成为一个国际标准。OGC宣布已经完成了KML的标准化进程。KML已经成为了一个OGC标准。

2 Google Earth 开发简介

虽然Google Earth各功能平台已能满足日常使用,但是,当应用比较复杂时,用户需要逐步与这些功能交互,工作会比较繁琐,甚至有些功能Google Earth目前还没有提供,所以,为适应用户的不同需求,Google Earth为用户提供了两种接口,一种是KML/KMZ文件形式,另一种是组件(COM)API形式。

2.1 Google Earth Com API

Google Earth COM API是Google Earth的基于组件技术的应用程序开发接口,用户可以基于不同的平台使用这些接口来完成特定任务。COM即组件技术,API即应用编程接口。Google公司为用户提供了Google Earth COM API类库,其意义在于用户可以在各种外部程序中调用Google Earth的功能。Google Earth COM API是一个包括11个类的类库,这11个类中IApplicationGE类最为重要。Google Earth COM API类库中的其他类的使用都会涉及IApplicationGE类中的属性和函数。以下对这11个类进行简单的介绍。

表1 Google Earth API中11个类的介绍表

IApplicationGE 入口类,通过该类,用户进一步调用其他类。

ICameraInfoGE 相机类,通过该类,用户可以调整观看当前视图的方式。

IFeatureGE 要素类,通过该类,用户可以控制要素的属性。

IFeatureCollectionGE 要素集合类,通过该类,用户可以进一步获取要素。

IPointOnTerrainGE 地理坐标点类,通过该类,用户获取屏幕点的地理坐标。

IViewExtentsGE 视口类,通过该类,用户可以控制当前视口。

ISearchControllerGE Search面板类,通过该类,用户可以完成相应的搜索功能。

ITourControllerGE Tour面板类,通过该类,用户可以动态播放当前的要素。

IAnimationControllerGE Animation面板类,通过该类,用户可以动态播放当前的时间要素。

ITimeGE 时间类,通过该类,用户可以获取和设置要素的时间属性。

ITimeIntervalGE 时间间隔类,通过该类,用户可以获取要素的时间间隔属性。

2.2 Google Earth(Map)开发整合方式

GIS数据与Google Earth整合的方式主要有以下四种:

GIS数据KML/KMZGoogle Earth

GIS数据WMS ServerGoogle Earth

GIS数据Image PyramidGoogle Earth

Google Earth ImageGeoreferenced ImageGIS

Google Earth(Map)开发最主要的是如何创建KML/KMZ以及其存储的方式――文件式存储/数据库管理。总的开发流程如下图:

图 2 Google earth(Map)的开发整合方式

3 应用研究

本文实现Excel与Google Earth的实时交互,程序界面如图3所示。

程序的关键步骤主要有以下几方面:

3.1注册Google Earth Com API

在命令行输入:"C:\Program Files\Google\Google Earth\client\googleearth.exe" /RegServer ,注册Google Earth COM API组件。其中的路径为本机Google Earth的安装路径,如果注册成功,Google Earth会自动启动。

3.2创建Google Earth 在Excel VBA中的类

Option Explicit

Private m_Error As Boolean

Private m_MsgError As String

Private m_GE As Object

Private m_HwndGE As Long

Private m_FcScale As Double

Private Sub Class_Initialize()

m_Error = False

Set m_GE = Nothing

Set m_GE = CreateObject("GoogleEarth.ApplicationGE")

m_HwndGE = m_GE.GetMainHwnd()

m_FcScale = 0.0035

End Sub

Private Sub Class_Terminate()

Set m_GE = Nothing

End Sub

Private Function StartGE() As Boolean

Dim timeStamp As Date, timeChange As Long, yourReturn

timeStamp = Now

timeChange = 0

While (m_GE.IsInitialized = 0)

timeChange = DateDiff("s", timeStamp, Now)

If timeChange > 120 Thenr

StartGE = False

Exit Function

End If

Wend

timeStamp = Now

timeChange = 0

While timeChange < 2

timeChange = DateDiff("s", timeStamp, Now)

Wend

StartGE = True

End Function

Public Sub Initialized()

If m_GE.IsInitialized = 0 Then

If StartGE() = False Then

m_Error = True

m_MsgError = "Google still waiting to load."

End If

End If

End Sub

此类在Google Earth中创建了Google earth Com API的对象,判断Google Eath 是否启动,如果未启动,则启动Google Earth。该类可以在此基础上进一步的拓展交互的功能。

3.2 Excel交互的实现方式

利用Excel VBA读取Excel 表格中的数据,根据KML语法规则生成KML,创建点、线、面,利用Google earth COM API 函数实时加载。

Public Sub OpenKMl(kmlfilepath As String)

Call m_GE.OpenKmlFile(kmlfilepath, True)

End Sub

同时可以编写函数并将kml文件、图片等相关文件一并压缩为ZIP文件,重命名为KMZ文件。这样就可以形成完整的数据成果进行,不依赖于文件的相互依赖路径限制和所在的工作平台。主要的压缩命令为:

Compress = "C:\Program Files\WinRAR\WinRAR.exe " & " A -ep " & File_rename & " " & File_sought & "*.png" & " " & File_sought & zipname & ".kml"

另外,通过调用Google earth Com API中的函数,进一步的拓展应用。

Public Sub GeSavePicture(Picture_Path As String, Quality_Num As Integer)

Call m_GE.SaveScreenShot(Picture_Path, Quality_Num)

End Sub

4 总结

本文主要介绍了利用Google Earth提供的Google Earth Com API以及Kml/Kmz语法规则,在Excel VBA中编写相通用和可扩展的类和函数,自动生成Kml/Kmz,调用API函数,实现相关点位在Google Earth上自动点位加载,实现excel VBA程序与Google Earth的集成。

参考文献:

[1] 马谦,等. Google Earth/Maps/KML核心开发技术揭秘[M].电子工业出版社. 2010.

[2] 江宽,等Google API 开发详解:Google Maps 与Google Earth 双剑合璧[M]. 电子工业出版社.2008.

[3] Excel VBA从入门到精通(实用案例视频版)[M]. 中国铁道出版社.2013

上一篇:水利工程堤防护岸施工技术解析 下一篇:大城西黄河公路大桥引桥50m连续梁上部结构设计...