Tuesday, March 10, 2009

Simple tool for Integrating ESRI ArcMap with Google Earth

Thought you would like this.

I wrote a simple VBA tool (really short) that integrate ArcMap with Google Earth.

Add whatever data you want in ArcMap and then activate the tool. The tool will open a new internet explorer page with a Google earth plug in.

Now just browse your data in ArcMap (Zoom, Pan etc). You will see that Google Earth will fly to the same location. (Doesn't work with schematics for sure)

Why this is useful? You don't need to overwhelm your ArcMap with satellite images, you have a live source of up-to-date data from Google Earth as you browse your network.


Download the code from here.
Watch the demo here.

NOTE THIS CODE IS OBSOLETE, I HAVE ENHANCED IT INTO A COOL TOOL CALLED ARC2GOOGLE, DOWNLOAD FROM HERE

40 comments:

  1. Hi! Thanks for sharing this. I'm sure that a lot of people would be interested with it.

    ReplyDelete
  2. The tool works as advertised. I had a couple of miss-steps at first but it worked out great.

    ReplyDelete
  3. Thanks Ron,

    Glad it worked,

    Your feedback and enhancements are appreciated.

    ReplyDelete
  4. Hi,

    i Tried your tool, but i only got a empty map in the browser, when i change my Map in ArcMAP.
    Using German data, with a german projection.

    Any Idea ?

    ReplyDelete
  5. did you see Google Earth started in the browser? if not then you have to install Google Earth PlugIn for internet explorer.

    Any errors?

    ReplyDelete
  6. I am having trouble with the code (FYI - I am not a programmer). I am very excited to start using this tool. Please help. Thank you!

    ReplyDelete
  7. This comment has been removed by the author.

    ReplyDelete
  8. I dowloaded, but since I am using firefox, don´t know if it will work.
    Thanks anyway for sharing your knowledge.

    ReplyDelete
  9. Matt
    Just create a UIButton, then copy and paste the code into your VBA,

    that's it.
    Hope it will work :)

    I am going to upload a video into you tube how to do it.


    Carlos,
    If you have a windows it will sure work because internet explorer is already installed.
    even if you used another browser.

    hope it will work.

    ReplyDelete
  10. Hussein,

    Thank you for the instruction! I have the button set up in ArcMap. Here is what is happens when I click the button: IE opens for a few seconds then it closes and then Windows Explorer opens to the location of GE.htm. It almost seems like the procedures are out of order.

    Thanks in advance for your help on this!

    ReplyDelete
  11. This comment has been removed by the author.

    ReplyDelete
  12. @Matt
    This is weird,
    kindly send me an email on this address

    hmohamed (AT) gisadwea (DOT) (ae)

    @tacio70
    I would guess that you are using Vista maybe,
    I don't have a vista 9.3 environment,
    Once I compile the code at this environment I will get back to you

    ReplyDelete
  13. I copied the code as instructed and ran it successfully. But when i try to zoom-in in arcmap i get the following error:

    'Method Document IWebBrowser2 Failed'

    I also noticed that two browsers are opened when i run the code. is this normal? please help.

    thanks

    ReplyDelete
  14. @jack70
    What is your system specification?

    I would guess that you are using Vista maybe,
    I don't have a vista 9.3 environment,
    Once I compile the code at this environment I will get back to you

    No, one browser must be opened not two.

    I think am gonna compile the code into a VB6 environment at least.

    And reupload it again

    ReplyDelete
  15. Guys,
    use this path instead

    http://geshout.com/test/esrige.htm

    it appears using the file locally generates some problems.

    ReplyDelete
  16. Hi Hussein

    Is awesome, it works perfect. Thanks
    I don’t know VBA code, I use a button in Arcmap to see in google earth program the view, but I have to click every time, and your code is automatic, maybe you can fix these code to make it automatic.

    Thanks for your time


    code:
    Private Sub GoogleView_Click()
    'Sets the View in Google to [approximately] match the view in ArcMap
    'This uses the Google Earth SetCameraParams to jump to the location.
    'SetCameraParams zooms to a center point, the altitude value determines
    'how much area you will see. The program returns an altitude and center
    'point to approximately match the view extent of your map.
    'Works with 9.x basic ArcView license - tested with ArcGIS 9.1 and 9.3

    'Version 1.3a - 12/03/2008 - Moved GE Object creation (now after check projection, etc.)
    'Version 1.3 - 12/2/2008 - Corrected problem with Geographic projections
    'and changed how you set the view altitude (now does it after the projection).
    'Version 1.2 - Modified 9/10/2008 - Made a few corrections
    'Previously Modified 4/18/2007 - Added delay option when loading Google Earth
    'Optional: Set reference to the Google [Earth Type 1.0 Library]
    'Originally Created 03/22/2007 by Joel Stocker, UConn CES

    'ArcGIS map parameters
    Dim pMxDoc As IMxDocument
    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 viewAlt As Double

    '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

    'Check to make sure the map is projected
    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
    End If

    'Create a WGS84 spatial reference for LatLong in Google Earth
    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

    'Check if map has been properly set
    If Abs(pCenterPt.y) > 90 Or Abs(pCenterPt.x) > 180 Then
    MsgBox "The view center (" & pCenterPt.y & ", " & pCenterPt.x & ") is out of " & _
    "range for GEarth" & vbCrLf & _
    "You may need to check your projection or add a layer to the view.", _
    vbOKOnly, "Google View"
    Exit Sub
    End If

    'Method for calculating the view altitude - v1.3 modified 12/2/2008
    'You can tweak this part if you find a better way to set the elevation
    'for your area
    Dim wDeg As Double 'Extent width in degrees
    Dim wMeters As Double 'Extent width in meters at view center
    Dim pi As Double
    pi = 3.14159265358979
    wDeg = pEnvelope.LowerLeft.x - pEnvelope.LowerRight.x
    'Formula for converting the width from degrees to meters
    wMeters = wDeg * (Cos((pCenterPt.y) * (pi / 180)) * 111325)
    viewAlt = Abs(Round(wMeters)) 'Simply setting the view altitude to the width (in meters)

    ' 'If you want to use the extent height for elevation use this instead
    ' Dim hDeg As Double
    ' Dim hMeters As Double
    ' hDeg = pEnvelope.UpperLeft.Y - pEnvelope.LowerLeft.Y
    ' hMeters = hDeg * 111325 'latitude (y) is relatively constant across the globe
    ' viewAlt = Abs(Round(hMeters))

    'Google Earth Parameters
    'See http://earth.google.com/comapi/ for details
    'To show help tips while editing this code, uncomment the IApplication and
    'comment the Object references. Also, set Google [Earth Type 1.0 Library] in
    'Tools >> References (available if Google Earth is installed).

    'To show editing tips: Uncomment these two lines plus set GE reference...
    'Dim GE As IApplicationGE
    'Set GE = New ApplicationGE

    '...and Comment these three.
    Dim GE As Object
    Set GE = Nothing 'just in case still open
    Set GE = CreateObject("GoogleEarth.ApplicationGE")

    'Send the map values to Google Earth using GE.SetCameraParams
    'If you 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
    'Altitude modes: RelativeToGroundAltitudeGE = 1, AbsoluteAltitudeGE = 2

    Dim timeStamp As Date, timeChange As Long, yourReturn
    'I added this to provide a slight delay to wait for Google to 'really' finish loading
    'before sending the SetCameraParams command. An alternate is to wait for it to
    'respond that it is online, but sometimes you want to work offline
    'Note: This time delay option can be demanding on the processor, so comment it out if
    'it turns out to be a problem.

    If GE.IsInitialized = 0 Then 'If Google Earth not open then wait for it to open
    timeStamp = Now
    timeChange = 0
    While (GE.IsInitialized = 0)
    'In theory this waits for Google Earth to load if it isn't open already
    timeChange = DateDiff("s", timeStamp, Now) 'difference in seconds
    'Added this option to cancel 4/18/2007 (in case it get's stuck)
    If timeChange > 120 Then 'wait X number of seconds, then prompt user
    yourReturn = MsgBox("Google still waiting to load. Continue waiting?", vbYesNo, _
    "Loading Google Earth")
    If yourReturn = vbNo Then
    End 'cancel routine
    Else
    timeStamp = Now 'start again for loop
    timeChange = 0
    End If
    End If
    Wend

    timeStamp = Now
    timeChange = 0
    While timeChange < 2
    timeChange = DateDiff("s", timeStamp, Now) 'difference in seconds
    Wend
    'Now set the Google Earth view
    GE.SetCameraParams pCenterPt.y, pCenterPt.x, 0, 1, viewAlt, 0, 0.01, 3.5

    Else
    'If Google Earth already open, set the Google Earth view immediately
    GE.SetCameraParams pCenterPt.y, pCenterPt.x, 0, 1, viewAlt, 0, 0.01, 3.5
    End If

    End Sub

    Private Function GoogleView_ToolTip() As String
    'Information displayed when mouse over the button
    GoogleView_ToolTip = "Set Google Earth Extent to ArcMap Extent"
    End Function

    Private Function GoogleView_Message() As String
    GoogleView_Message = "Opens Google Earth to Current View Extent"
    End Function

    ReplyDelete
  17. Hi Carlos,
    just setup Your code to listen to the refresh event ..

    Leave the code as it is.


    Add a new UIButton, call it UIButtonControl1 and copy this code, right click on the button and click view source, you will see this


    Private Sub UIButtonControl1_Click()


    End Sub


    Add this code inside the sub so it will become like this

    Private Sub UIButtonControl1_Click()
    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    Set m_pMap = pMxDoc.FocusMap

    End Sub


    At the very top of the module copy this code..


    Private WithEvents m_pMap As Map


    Private Sub m_pMap_ViewRefreshed(ByVal view As esriCarto.IActiveView, ByVal phase As esriCarto.esriViewDrawPhase, ByVal Data As Variant, ByVal envelope As esriGeometry.IEnvelope)


    GoogleView_Click()

    End Sub



    This should work

    Email me at hmohamed "AT" gisadwea DOT "ae" if it didn't

    ReplyDelete
  18. Hi Hussein

    Thanks Master,, I Will try it.

    Regards

    Carlos

    ReplyDelete
  19. sorry but your explain are not enoug for set up this code.. for example i don't understand how can i set up the file's path.. can you give me further detail..thanks..fethi ata

    ReplyDelete
  20. @Fathi,
    Now problem

    Replace the file path to your ge.htm path
    or simply use this instead

    http://geshout.com/test/esrige.htm
    I'll update the ESRI site :)

    thanks for this insight.

    ReplyDelete
  21. Great program!! It works great!

    -Elise

    ReplyDelete
  22. Hia, Hussein. I have a problem whit de code, whean i put the code in the VB Editor and i tray to close a message opens that say "Compile Error: nvalid attribute in sub o Function". Here is a Screenshoot if yiou like to check: http://my.jetscreenshot.com/451/20090617-yw3f-347kb

    ReplyDelete
  23. @Omar_Baphomet
    When you open the vbeditor.
    Delete ALL THE CODE there
    and copy the vba.txt content

    you pasted function inside another function :)

    ReplyDelete
  24. Thanks Hussein, its a great tool congratulations for your work

    ReplyDelete
  25. @Omar
    Anytime bro, I hope it will be of use to you and everybody.

    ReplyDelete
  26. hi hussien i wanted to do like ur work but in flex with arcgis map service it works only with esri data when i pan in map it pan in google earth but the problem is it doesnot work with my data the earth rotate in incorrect way can u help plz

    ReplyDelete
  27. Hi Fatma,
    You need to project your data into the Google Earth Format ..

    this is the function that does the projection

    PointToLatLon .. in my script..

    Now I don't know of Flex.. but you must somehow converts the X/Y in ESRI Data into a Lat/Lon ,, once you done that.. the rest is easy ..

    ReplyDelete
  28. hi hussien thnx for reply
    i use this function in my script too
    but the problem i think in the projection i use Geographic coordinate system wgs 84 but it still dont work with my data

    and i have problem in running ur code too it give error in "WithEvents" says invlaid used only with objects

    ReplyDelete
  29. Anytime fatma,

    well, this code is pretty old,

    I have done a better code with .NET integrations ESRI ArcMap with Google Maps and google Earth

    Called it Arc2Google

    ReplyDelete
  30. This is the tool

    http://hnaser.blogspot.com/2009/07/arc2google.html

    ReplyDelete
  31. hi hussien ur code open arc 9.2 and i have 9.3
    when i try to debug the project it says u need to register and another issue my arc map is setted up on d:\

    ReplyDelete
  32. This shouldn't be a problem
    my code works in both 9.2 and 9.3 ..

    Download arc2google you will see two folders 2005 and 2008

    register the 2008 one.. (binary-> register.bat)

    ReplyDelete
  33. ok it worx now but it just like my problem in google earth when i pan my map the earth rotate in incorrect way can u tell me in what projection u projectedd ur data
    my data projected in Geographic Coordinate ststem WGS 1984

    ReplyDelete
  34. See the source code, i'm using the same projection

    maybe you are flipping the Lat/Lon positions?

    I mean maybe you placed the Latitude value in the Longtitde and vise versa ..

    try invereting them ..

    ReplyDelete
  35. i dont think so my code works correctly with sample data from esri but i cant understand now what is the problem anyway thanx so much for ur replies and ur time i appreciate ur help

    ReplyDelete
  36. hi Hussein,
    i hve 2 pb:
    1)i try this tool today, but an error,"compile error; user-defined type not defined". can u help me to fix it?
    2)is there a tool to convert from international coordinate system(ex(276372,873972)) to a gps coordinate system nd vice versa.
    thank you

    ReplyDelete
  37. Hi Nouri,

    This is an old version

    Try this one instead

    http://hnaser.blogspot.com/2009/07/arc2google.html


    Hope this helps

    ReplyDelete
  38. Hi Hussein,

    I use ArcGIS 9.3.1 and tried to integrate this uicontrol, but it did not work. This error showing up "Compile error - Ambiguous name detected: StorageAreaConnections"
    What is the problem? Can you help me.
    Thank you.

    ReplyDelete

Share your thoughts

Note: Only a member of this blog may post a comment.