الاثنين، ٣١ مارس ٢٠٠٨

ArcMap and Google Earth Integration


The movie below shows how to build a tool in ArcMap that will sycronize the current ArcMap extent with Google Earth extent this code is working with wg84 projections. To use this code you should have GE installed on your PC


copy the code below to a mousedown event on UIToolControl on ArcMap VBA

Dim pScreenDisplay As IScreenDisplay
Dim pActiveView As IActiveView
Dim pMxDoc As IMxDocument

Set pMxDoc = Application.Document
Set pActiveView = pMxDoc.FocusMap
Set pScreenDisplay = pActiveView.ScreenDisplay
pScreenDisplay.TrackPan

Dim pMap As IMap
Dim pMapsActiveView As IActiveView
Dim pEnvelope As IEnvelope
Dim pCenterPt As IPoint
Dim pEnvSpatRef As ISpatialReference
Dim pSRI As ISpatialReferenceInfo
Dim pPCS As IProjectedCoordinateSystem
Dim dMetersPerUnit As Double
Dim viewAlt As Double

'Google Earth Parameters

Dim GE As IApplicationGE
Set GE = New ApplicationGE

'Set the ArcMap values
Set pMxDoc = Application.Document
Set pMap = pMxDoc.FocusMap
Set pMapsActiveView = pMap
Set pEnvelope = pMapsActiveView.Extent
Set pCenterPt = New Point
Set pEnvSpatRef = pEnvelope.SpatialReference

'Get current meters per map unit
If pEnvSpatRef Is Nothing Then
MsgBox "Please set a projection for your Map", vbOKOnly, "No Projection Set"
Exit Sub
ElseIf TypeOf pEnvSpatRef Is IUnknownCoordinateSystem Then
MsgBox "Please set a projection for your Map", vbOKOnly, "Projection Unknown"
Exit Sub
ElseIf TypeOf pEnvSpatRef Is IProjectedCoordinateSystem Then
Set pPCS = pEnvSpatRef
dMetersPerUnit = pPCS.CoordinateUnit.MetersPerUnit
Else
dMetersPerUnit = 1
End If

'Set view altitude the same as the width (seems to zoom enough)
'Get width, then convert it to meters using the metersperunit value
viewAlt = Round((pEnvelope.LowerRight.x - pEnvelope.LowerLeft.x) * dMetersPerUnit)

'Create a WGS84 spatial reference for LatLong in Google
Dim pSpRef2 As ISpatialReference
Dim pSpRFc As SpatialReferenceEnvironment
Dim pGCS As IGeographicCoordinateSystem
Set pSpRFc = New SpatialReferenceEnvironment
Set pGCS = pSpRFc.CreateGeographicCoordinateSystem(esriSRGeoCS_WGS1984)
Set pSpRef2 = pGCS
pSpRef2.SetFalseOriginAndUnits -180, -90, 1000000

'Project the envelope to WGS84
pEnvelope.Project pSpRef2

'Get the Center Point: ((XMin + XMax) / 2, (YMin + YMax) / 2)
pCenterPt.PutCoords (pEnvelope.LowerLeft.x + pEnvelope.LowerRight.x) / 2, _
(pEnvelope.LowerLeft.y + pEnvelope.UpperRight.y) / 2

'Send the parameters to Google
'Note: The altitude mode - RelativeToGroundAltitudeGE = 1, AbsoluteAltitudeGE = 2
'If set the speed to 5 or more it snaps to the site (no transition), a lower number
'(i.e. 3.5) helps show movement has taken place

While (GE.IsInitialized = 0)
'In theory this waits for Google Earth to load if it isn't open already
Wend
GE.SetCameraParams pCenterPt.y, pCenterPt.x, 0, 1, viewAlt, 0, 0.01, 3.5

You can find this tool in http://arcscripts.esri.com/ i have added some minor coding to allow this tool to work with mousedown event.

All the best