3

I frequently use google maps to find locations, and paste the coordinates into the "go to XY" toolbar in decimal degrees. It's a bit repetitive to paste the latitude into one box, and the longitude into another. Is there a quicker way of zooming to a location with a string in the "google maps" coordinate format such as:

-29.948153, 146.864276

I was thinking I could make a python tool, but I can only find code for "zoom to selected features" (http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00s300000003000000)

I'm using ArcGIS Desktop 10.0.

2

Below is some code that should do the basics of this.

import arcpy

yxCoordString = arcpy.GetParameterAsText(0)
yStr,xStr = yxCoordString.split(",")
xFloat = float(xStr)
yFloat = float(yStr)

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
newExtent = df.extent
newExtent.XMin, newExtent.YMin = xFloat - 0.5, yFloat - 0.5
newExtent.XMax, newExtent.YMax = xFloat + 0.5, yFloat + 0.5
df.extent = newExtent

arcpy.RefreshActiveView()

You just need to Add Script it into a toolbox and then assign a parameter like below:

enter image description here

I have it zooming to a 1 x 1 degree extent by subtracting/adding 0.5 to the entered coordinates but you can set that to whatever you like.

This code takes no account of the coordinates entered being different to the coordinate system of the data frame.

2

I just got this script to do what you want:

def panToExtent(gmapsLonLat):
    mxd = arcpy.mapping.MapDocument('CURRENT')
    dataFrame = arcpy.mapping.ListDataFrames(mxd)[0]

    lat, lon = gmapsLonLat.split(',')

    lat = float(lat)
    lon = float(lon)

    extent = arcpy.Extent(lon, lat, lon, lat)
    dataFrame.panToExtent(extent)
    arcpy.RefreshActiveView()

Save it somewhere and then load it in the Python console window (right-click > Load).

Then you can simply call the function:

panToExtent('49.616170, 6.140333')

You have asked to enter it as a string, so I wrote it that way, but that also means that you have to paste your coordinates into a string, so do not forget to add the quotes!

To make it even more comfortable, I would not bother adding it as a string, and perhaps use the following code:

def panToExtent(lat,lon):
    mxd = arcpy.mapping.MapDocument('CURRENT')
    dataFrame = arcpy.mapping.ListDataFrames(mxd)[0]

    extent = arcpy.Extent(lon, lat, lon, lat)
    dataFrame.panToExtent(extent)
    arcpy.RefreshActiveView()

Now you can simply copy from Google Maps, and paste everything into your function when you call it, such as here:

panToExtent(50.850342, 4.363153)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.