Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

Zoom or Pan to Selected Features, But not All of Them!

$
0
0
I get lots of these really simple maps, which take 10 minutes to make with a template, but the boss wants them in one minute! So I was told to write a tool to do this. I chose a Python Toolbox tool so I get to know this new 10.1 feature, and practice my Python a bit, at which I am a beginner--at best.
My code, upon some manual input of some parameters, selects a parcel, reads the address, then intersects this parcel with a couple of layers, reads some attributes from them, and writes everything to the map's title block, but I ran into a brick wall now.
Everything is fine, except that my code zooms not only to the subject parcel, but also to the selected features in the intersecting layers from which I need to read the attributes that relate to my parcel. I've tried (and tried) a couple of few methods to avoid zooming to the ancillary layers, but I cannot get it to work.
In any case, here's the code, and I have entered some of the error messages as comments. So this is my desperate call for help to the Arcpy Brotherhood, because I'm drowning in Python.

Code:


import arcpy, sys, os

class Toolbox(object):
    def __init__(self):
        """Define the toolbox (the name of the toolbox is the name of the
        .pyt file)."""
        self.label = "Location Map Toolbox"
        self.alias = "Location Map"

        # List of tool classes associated with this toolbox
        self.tools = [FillTitleBlock]

       
class FillTitleBlock(object):
    def __init__(self):
        """Define the tool (tool name is the name of the class)."""
        self.label = "Fill Title Block"
        self.description = "Fill Title Block"
        self.canRunInBackground = False


    # user project data input
    def getParameterInfo(self):
        """Define parameter definitions"""
        #params = None
        #return params

        ProjectNumber= arcpy.Parameter(
        displayName= "Project Number",
        name= "ProjectNumber",
        datatype= "GPString",
        parameterType= "Required",
        direction= "Input")

        ProjectTitle= arcpy.Parameter(
        displayName= "Project Title",
        name= "ProjectTitle",
        datatype= "GPString",
        parameterType= "Required",
        direction= "Input")

        ParcelID= arcpy.Parameter(
        displayName= "Parcel ID",
        name= "ParcelID",
        datatype= "GPString",
        parameterType= "Required",
        direction= "Input")

        parameters= [ProjectNumber, ProjectTitle, ParcelID]
       
        return parameters


    def isLicensed(self):
        """Set whether tool is licensed to execute."""
        return True

    def updateParameters(self, parameters):
        """Modify the values and properties of parameters before internal
        validation is performed.  This method is called whenever a parameter
        has been changed."""

        return

    def updateMessages(self, parameters):
        """Modify the messages created by internal validation for each tool
        parameter.  This method is called after internal validation."""
        return

    def execute(self, parameters, messages):
        """The source code of the tool."""

        #Layout and Table of Contents variables
        mxd= arcpy.mapping.MapDocument(r"G:\PCM_Jorge\LOCATION_MAPS\0-MAPS\LocationMap.mxd")
        df = arcpy.mapping.ListDataFrames(mxd, "Base Map")[0]
        lyr= "GIS.GISADMIN.parcels"
        TAZ= "GIS.GISADMIN.TAZ_LRTP2009_2035_371"
        DIS= "CommissionDistrict"

        #Parameters
        ProjNum= parameters[0].valueAsText
        ProjTit= parameters[1].valueAsText
        ParcelID= parameters[2].valueAsText

        #Input title block data
        for txtelem in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
            if txtelem.name == "PRJ_NUM":
                txtelem.text = ProjNum
            elif txtelem.name == "PRJ_TIT":
                txtelem.text = ProjTit
            elif txtelem.name == "PID":
                txtelem.text = ParcelID

        #Select parcel and zoom to it
        selPID= "\"VPARCEL\" = '" + ParcelID + "'"
        arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", selPID)

        #Here comes the problem
        #df.zoomToSelectedFeatures(lyr) #option 1
        #without lyr, it zooms to all selected layers (below). With this argument, I get TypeError: zoomToSelectedFeatures() takes exactly 1 argument (2 given)

        df.panToExtent(lyr.getSelectedExtent()) #option 2
        # I get AttributeError: 'str' object has no attribute 'getSelectedExtent'
        df.scale = df.scale * 1.25

        arcpy.RefreshActiveView() #This used to be at the end of the script, but I moved it here hoping the layer selections below would not get zoomed to: FAIL

       
        #Input address, TAZ, and commission district
        arcpy.MakeFeatureLayer_management(lyr, "ParSel", selPID)
        parLyr= arcpy.SelectLayerByLocation_management(lyr,"ARE_IDENTICAL_TO","ParSel")
        rows= arcpy.SearchCursor(parLyr)
        row= rows.next()
        lblAddress= arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "PRJ_ADD")[0]

        if row.isNull("PHYSADDR") == True:
            lblAddress.text= "None found."
        else:
            lblAddress.text= row.getValue("PHYSADDR")
     
        #The ancillary layers
        TAZLyr= arcpy.SelectLayerByLocation_management(TAZ,"INTERSECT","ParSel")
        rows= arcpy.SearchCursor(TAZLyr)
        row= rows.next()
        lblTAZ= arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "TAZ")[0]
        lblTAZ.text= row.getValue("ctyTAZ")

        DISLyr= arcpy.SelectLayerByLocation_management(DIS,"INTERSECT","ParSel")
        rows= arcpy.SearchCursor(DISLyr)
        row= rows.next()
        lblDIS= arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "DIS")[0]
        lblDIS.text= row.getValue("COMMISSION")
               
       
               
##      mxd.saveACopy(r"C:\TEMP\\" + ProjNum + ".mxd") #directory must exist
##      arcpy.mapping.ExportToPDF(mxd, r"C:\TEMP\\" + ProjNum + ".pdf")

##      mxd.save() ##this will overwrite the template
       
        del mxd, row, rows, lblAddress, lblTAZ, lblDIS,
     
        return


Viewing all articles
Browse latest Browse all 2485

Trending Articles