这个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自动添加到组件库中。
三、部分代码,主窗口中:
2Imports ESRI.ArcGIS.Controls
3Imports ESRI.ArcGIS.Geometry
4Imports ESRI.ArcGIS.Carto
5Imports ESRI.ArcGIS.SystemUI
6Imports ESRI.ArcGIS.ArcMapUI
7Imports ESRI.ArcGIS.esriSystem
8Imports ESRI.ArcGIS.Framework
9Imports ESRI.ArcGIS.Geodatabase
10Imports ESRI.ArcGIS.Display
11Imports System.Windows.Forms.Cursor
12Imports System.Drawing
13Imports System.Drawing.Drawing2D
14
15
16Public Class Form2
17 Public g_GeHelper As EARTHLib.ApplicationGE 'GE的主应用API
18 Public hookhelper As IHookHelper 'hookhelper
19 Public longitude As Double '经度
20 Public latitude As Double '维度
21 Dim pApp As IApplication
22 Dim pEnable As Boolean
23 Dim pDoc As IMxDocument
24 Dim pWorkspace As IWorkspace
25 Dim pMap As IMap
26 Dim pLayer As ILayer
27 Dim pWorkE As IWorkspaceEdit
28 Dim pFeaLayer As IFeatureLayer
29 Private Structure POINTAPI
30 Dim x As Double
31 Dim y As Double
32 End Structure
33
34 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
35 '初始化应用GE
36 g_GeHelper = New EARTHLib.ApplicationGE
37 AxGEViewer1.HookGE(g_GeHelper.GetMainHwnd, g_GeHelper.GetRenderHwnd)
38 End Sub
39
40
41 Private Sub AxGEViewer1_MouseDownEvent(ByVal sender As System.Object, ByVal e As AxGEVControl.__GEViewer_MouseDownEvent) Handles AxGEViewer1.MouseDownEvent
42 If CheckBox1.Checked = True Then
43 Dim GePt As PointOnTerrainGE
44 Dim pt As POINTAPI
45 '屏幕坐标到GE屏幕坐标的转换
46 pt.x = e.evtArgs.X * 2 / Me.Width - 1
47 pt.y = -e.evtArgs.Y * 2 / Me.Height + 1
48 '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.Longitude
53 latitude = GePt.Latitude
54 pMap = hookhelper.ActiveView
55
56 'arcmap中绘制点
57 Dim point As IPoint
58 point = New ESRI.ArcGIS.Geometry.Point
59 point.PutCoords(longitude, latitude)
60
61 Dim pMarkerElement As IMarkerElement
62 pMarkerElement = New MarkerElement
63
64 Dim pMarkerSymbol As ESRI.ArcGIS.Display.ISimpleMarkerSymbol
65 pMarkerSymbol = New ESRI.ArcGIS.Display.SimpleMarkerSymbol
66 pMarkerSymbol.Size = 3
67 pMarkerSymbol.Style = ESRI.ArcGIS.Display.esriSimpleMarkerStyle.esriSMSDiamond
68
69 Dim pElement As IElement
70 pElement = pMarkerElement
71 pElement.Geometry = point
72 pMarkerElement.Symbol = pMarkerSymbol
73
74 Dim pGraphicsContainer As IGraphicsContainer
75 Dim pActiveView As IActiveView
76 pActiveView = pMap
77 'pActiveView.Extent.CenterAt(point)
78
79 'arcmap中点的定位
80 Dim pEnvelop As IEnvelope
81 pEnvelop = pActiveView.Extent
82 pEnvelop.CenterAt(point)
83 pActiveView.Extent = pEnvelop
84 pActiveView.Refresh()
85 pGraphicsContainer = pMap
86 pGraphicsContainer.AddElement(pMarkerElement, 0)
87 pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, Nothing, Nothing)
88 End If
89
90 End Sub
91 Public WriteOnly Property hook() As IHookHelper
92 Set(ByVal value As IHookHelper)
93 hookhelper = value
94 End Set
95 End Property
96
97 Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
98
99 End Sub
100End Class
command.cs中实现组件注册:
63 Private m_hookHelper As IHookHelper
64
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 properties
74 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 Try
80 'TODO: change bitmap name if necessary
81 Dim bitmapResourceName As String = Me.GetType().Name + ".bmp"
82 MyBase.m_bitmap = New Bitmap(Me.GetType(), bitmapResourceName)
83 Catch ex As Exception
84 System.Diagnostics.Trace.WriteLine(ex.Message, "Invalid Bitmap")
85 End Try
86
87
88 End Sub
89
90
91 Public Overrides Sub OnCreate(ByVal hook As Object)
92 If m_hookHelper Is Nothing Then m_hookHelper = New HookHelperClass
93
94 If Not hook Is Nothing Then
95 Try
96 m_hookHelper.Hook = hook
97 If m_hookHelper.ActiveView Is Nothing Then m_hookHelper = Nothing
98 Catch
99 m_hookHelper = Nothing
100 End Try
101
102 'Disable if hook fails
103 If m_hookHelper Is Nothing Then
104 MyBase.m_enabled = False
105 Else
106 MyBase.m_enabled = True
107 End If
108
109 'TODO: Add other initialization code
110 End If
111 End Sub
112
113 Public Overrides Sub OnClick()
114 'TODO: Add Command1.OnClick implementation
115 Dim ce As IEnvelope = m_hookHelper.ActiveView.Extent
116 ce.Expand(0.5, 0.5, True)
117 m_hookHelper.ActiveView.Extent = ce
118 m_hookHelper.ActiveView.Refresh()
119 MsgBox("asdasd")
120 Dim frm As Form2
121 frm = New Form2
122 frm.hookhelper = m_hookHelper
123 frm.Show()
124 End Sub
125
126End Class
127
生成dll后,即可直接在arcmap中添加自定义的comand按钮进行调用了。