编程实现Google Earth和ArcGIS的联动[demo]
这个DEMO实现的功能就是在google earth中用鼠标获取地理坐标,然后自动定位到ARCGIS对应的区域影像中。前段时间因项目引导,在师兄的启发下,做了一个这样的Demo,后来因为各种原因一直没有进行下去。现在感觉还是有些用处的,现在抽空整理一下。
Google Earth是一款优秀的Map Explorer,它的优秀在于使用普及和方便,虽然DigitalGlobe影像数据更新速度慢,但分辨率是还是很高的,最高可达到0.5m。如果忽略其误差,可以利用Google Earth的影像资源进行定位参考、解译参考、数字化等。
Google Earth继Google Map API之后推出的Google Earth COM API ,提供的功能相对比较弱,一些功能据说似乎还存在着Bug。网址:http://earth.google.com/comapi/index.html。google earth的二次开发有两种方法:
(1)使用windows API控制两个进程的调用,利用google earth api实现核心功能。
(2)使用做好的务功能重点放在GE API开发上。
这个demo使用的是GEVContorl(GEVC),它是一个完全COM的控件,对截止目前所有版本的GoogleEarth都支持,具有很高的兼容性和可用性,能够将GE视图(地球视图)集成到开发人员的应用系统中,并且支持滚轮功能。下载地址:http://www.gis9.com/download.jsp
一、安装完GE后,系统会自动拷贝一个EARTHLib.dll,添加Google Earth 1.0 Type Library到库应用.
二、.NET中新建Arcgis command类。将生成一个dll,可以被ARCGIS自动添加到组件库中。
三、部分代码,主窗口中:
Imports EARTHLib2
Imports ESRI.ArcGIS.Controls3
Imports ESRI.ArcGIS.Geometry4
Imports ESRI.ArcGIS.Carto5
Imports ESRI.ArcGIS.SystemUI6
Imports ESRI.ArcGIS.ArcMapUI7
Imports ESRI.ArcGIS.esriSystem8
Imports ESRI.ArcGIS.Framework9
Imports ESRI.ArcGIS.Geodatabase10
Imports ESRI.ArcGIS.Display11
Imports System.Windows.Forms.Cursor12
Imports System.Drawing13
Imports System.Drawing.Drawing2D14

15

16
Public Class Form217
Public g_GeHelper As EARTHLib.ApplicationGE 'GE的主应用API18
Public hookhelper As IHookHelper 'hookhelper19
Public longitude As Double '经度20
Public latitude As Double '维度21
Dim pApp As IApplication22
Dim pEnable As Boolean23
Dim pDoc As IMxDocument24
Dim pWorkspace As IWorkspace25
Dim pMap As IMap26
Dim pLayer As ILayer27
Dim pWorkE As IWorkspaceEdit28
Dim pFeaLayer As IFeatureLayer29
Private Structure POINTAPI30
Dim x As Double31
Dim y As Double32
End Structure33

34
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click35
'初始化应用GE36
g_GeHelper = New EARTHLib.ApplicationGE37
AxGEViewer1.HookGE(g_GeHelper.GetMainHwnd, g_GeHelper.GetRenderHwnd)38
End Sub39

40

41
Private Sub AxGEViewer1_MouseDownEvent(ByVal sender As System.Object, ByVal e As AxGEVControl.__GEViewer_MouseDownEvent) Handles AxGEViewer1.MouseDownEvent42
If CheckBox1.Checked = True Then43
Dim GePt As PointOnTerrainGE44
Dim pt As POINTAPI45
'屏幕坐标到GE屏幕坐标的转换46
pt.x = e.evtArgs.X * 2 / Me.Width - 147
pt.y = -e.evtArgs.Y * 2 / Me.Height + 148
'GE屏幕坐标到地理坐标的转换49
GePt = g_GeHelper.GetPointOnTerrainFromScreenCoords(CDbl(pt.X), CDbl(pt.Y))50

51
MsgBox("点击屏幕坐标:" & pt.x & " , " & pt.y & "获得ge坐标:" & GePt.Longitude & " , " & GePt.Latitude)52
longitude = GePt.Longitude53
latitude = GePt.Latitude54
pMap = hookhelper.ActiveView55

56
'arcmap中绘制点57
Dim point As IPoint58
point = New ESRI.ArcGIS.Geometry.Point59
point.PutCoords(longitude, latitude)60

61
Dim pMarkerElement As IMarkerElement62
pMarkerElement = New MarkerElement63

64
Dim pMarkerSymbol As ESRI.ArcGIS.Display.ISimpleMarkerSymbol65
pMarkerSymbol = New ESRI.ArcGIS.Display.SimpleMarkerSymbol66
pMarkerSymbol.Size = 367
pMarkerSymbol.Style = ESRI.ArcGIS.Display.esriSimpleMarkerStyle.esriSMSDiamond68

69
Dim pElement As IElement70
pElement = pMarkerElement71
pElement.Geometry = point72
pMarkerElement.Symbol = pMarkerSymbol73

74
Dim pGraphicsContainer As IGraphicsContainer75
Dim pActiveView As IActiveView76
pActiveView = pMap77
'pActiveView.Extent.CenterAt(point)78

79
'arcmap中点的定位80
Dim pEnvelop As IEnvelope81
pEnvelop = pActiveView.Extent82
pEnvelop.CenterAt(point)83
pActiveView.Extent = pEnvelop84
pActiveView.Refresh()85
pGraphicsContainer = pMap86
pGraphicsContainer.AddElement(pMarkerElement, 0)87
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, Nothing, Nothing)88
End If89

90
End Sub91
Public WriteOnly Property hook() As IHookHelper92
Set(ByVal value As IHookHelper)93
hookhelper = value94
End Set95
End Property96

97
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load98

99
End Sub100
End Class
command.cs中实现组件注册:
63
Private m_hookHelper As IHookHelper64

65

66
' A creatable COM class must have a Public Sub New() 67
' with no parameters, otherwise, the class will not be 68
' registered in the COM registry and cannot be created 69
' via CreateObject.70
Public Sub New()71
MyBase.New()72

73
' TODO: Define values for the public properties74
MyBase.m_category = "test" 'localizable text 75
MyBase.m_caption = "tool1" 'localizable text 76
MyBase.m_message = "This should work in ArcMap/MapControl/PageLayoutControl" 'localizable text 77
MyBase.m_toolTip = "" 'localizable text 78
MyBase.m_name = "" 'unique id, non-localizable (e.g. "MyCategory_MyCommand")79
Try80
'TODO: change bitmap name if necessary81
Dim bitmapResourceName As String = Me.GetType().Name + ".bmp"82
MyBase.m_bitmap = New Bitmap(Me.GetType(), bitmapResourceName)83
Catch ex As Exception84
System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap")85
End Try86

87

88
End Sub89

90

91
Public Overrides Sub OnCreate(ByVal hook As Object)92
If m_hookHelper Is Nothing Then m_hookHelper = New HookHelperClass93

94
If Not hook Is Nothing Then95
Try96
m_hookHelper.Hook = hook97
If m_hookHelper.ActiveView Is Nothing Then m_hookHelper = Nothing98
Catch99
m_hookHelper = Nothing100
End Try101

102
'Disable if hook fails103
If m_hookHelper Is Nothing Then104
MyBase.m_enabled = False105
Else106
MyBase.m_enabled = True107
End If108

109
'TODO: Add other initialization code110
End If111
End Sub112

113
Public Overrides Sub OnClick()114
'TODO: Add Command1.OnClick implementation115
Dim ce As IEnvelope = m_hookHelper.ActiveView.Extent116
ce.Expand(0.5, 0.5, True)117
m_hookHelper.ActiveView.Extent = ce118
m_hookHelper.ActiveView.Refresh()119
MsgBox("asdasd")120
Dim frm As Form2121
frm = New Form2122
frm.hookhelper = m_hookHelper123
frm.Show()124
End Sub125

126
End Class127

生成dll后,即可直接在arcmap中添加自定义的comand按钮进行调用了。

浙公网安备 33010602011771号