Select to view content in your preferred language

Zoom to Point

866
2
06-19-2012 02:00 PM
WilliamIde
Emerging Contributor
I am trying to figure out how to zoom to a point in arcpy.  My client would like to hand me a shape file with points in it.  Each point represents the center of an area that they want to create a PDF of.  So given a point how can I set the extent of my dataframe  and panToExtent.  The radius of each extent is no more that 500m.  So what I think will happen is I will iterate through the Points one by one. Zoom to the point set the extern to R = 500m and ExportToPDF move on to the next point.  Any help?  Thanks.
Tags (2)
0 Kudos
2 Replies
JoshuaChisholm
Frequent Contributor
Hello William. Looks to me like you're trying to re-invent the wheel.
If you are using ArcMap 10, check out data driven pages:
1) Load points feature class into a MXD
2) Customize>Toolbars>Data Driven Pages
3) Click "Data Driven Pages Setup..." button. Enable DDP and customize your settings
If the points have an associated scale, you can enter it into the settings.
4) Go to layout view and use arrows to cycle through pages. When exporting to PDF, check out the "Pages" tab for multiple PDFs/pages.
With DDP, you can also use the area polygons your client has instead of the center points they are providing you with. It may work better with the polygons.

If you are not using ArcMap 10, I can think of two options.
1) Use 'MapBook'. It was the precursor to Data Driven Pages, and works similarly. http://arcscripts.esri.com/details.asp?dbid=16037
2) Use python. I wrote a short script that should do what you need:
import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0] #Change the [number] to whichever dataframe you want to use (0 is the first dataframe, 1 in the second, etc.)

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.name=="Points": #Change to the name of the point layer in your table of contents
        pointsLyr=lyr
        break #this will only get the first occurance of the given name in the TOC

rows = arcpy.SearchCursor(pointsLyr.dataSource)

for row in rows:
    arcpy.SelectLayerByAttribute_management(pointsLyr, "NEW_SELECTION", '"FID" = '+str(row.FID))
    df.zoomToSelectedFeatures()
    arcpy.SelectLayerByAttribute_management(pointsLyr, "CLEAR_SELECTION") #clears selected so that the exported PDF does not have that selected blue colouring
    df.scale=50000 #set scale as desired. If there is an attribute for scale use this: df.scale=row.Scale
    arcpy.RefreshActiveView()
    arcpy.RefreshTOC()
    arcpy.mapping.ExportToPDF(mxd, "C:\\Users\\jchishol\\Desktop\\TEMPzoompoint\\Output"+str(row.FID)+".pdf") #exports to PDFs. The files have the 'row.FID' in them so they do not all have the same name
    
del mxd,df,lyr,pointsLyr,row,rows


Let me know how it goes. Good Luck!
~Josh
0 Kudos
WilliamIde
Emerging Contributor
DDP won't do it for this one I think.  Here is what I came up with.  It works.

import arcpy

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0] #Change the [number] to whichever dataframe you want to use (0 is the first dataframe, 1 in the second, etc.)

rows = arcpy.SearchCursor(pointsLyr.dataSource)
cols = arcpy.ListFields(pointsLyr.dataSource)
for row in rows:
    for col in cols:
        if col.name == "LATITUDE":
           latstr = row.getValue(col.name)
        if col.name == "LONGITUDE":
           lonstr = row.getValue(col.name)

        if col.name == "OBJECTID":
               objstr = str(row.getValue(col.name))
               arcpy.SelectLayerByAttribute_management(pointsLyr.dataSource,"NEW_SELECTION", ("%s = %s" % (col.name, objstr)
    newextent = df.extent
    xmin = float(lonstr) - .005
    ymin = float(latstr) - .005
    xmax = float(lonstr) + .005
    ymax = float(latstr) + .005

    newextent.XMin, newextent.YMin newextent.XMax newextent.YMax = xmin, ymin, xmax, ymax
    df.extent = newextent
    df.panToExtent(df.extent)
    arcpy.RefreshActiveView()  



It seems to work just fine.  The .005 number is basically the size.  So this could be define elsewhere and passed in as a parameter.

Thank for the hint.
0 Kudos