Select to view content in your preferred language

A little help with some python code please.

851
5
Jump to solution
05-09-2012 12:27 PM
TonyAlmeida
MVP Regular Contributor
I have tried run this script but i get the following error,
<type 'exceptions.IndexError'>: list index out of range
Failed to execute (Script).

I would like to enter a Parcel number and have it select and zoom to that parcel. I would like to make a script tool out of this.
ACCTEXT is the filed i am searching in are are follows. P12345, P12346, P98745 etc..
TAXPARCELS is they layer i am searching in.

import arcpy arcpy.AddMessage("Starting") pu = arcpy.GetParameterAsText(0) arcpy.AddMessage(pu) mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Main Map")[0] lyr = arcpy.mapping.ListLayers(mxd, "TAXPARCELS", df)[0] arcpy.AddMessage(lyr.name) expression = "ACCTEXT = '"+p+"'" arcpy.AddMessage(expression) arcpy.SelectLayerByAttribute_management(lyr,"NEW_SELECTION",expression) df.extent = lyr.getSelectedExtent() df.scale = df.scale*1.1 for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "P"):     if elm.name == "P":         elm.text = (p) arcpy.SelectLayerByAttribute_management(lyr,"CLEAR_SELECTION") arcpy.RefreshActiveView() arcpy.AddMessage("Completed")
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MathewCoyle
Honored Contributor
Hey Tony, sorry forgot about getting back to you. You can do something like this.

import arcpy  arcpy.AddMessage("Starting")  acc_val = arcpy.GetParameterAsText(0) arcpy.AddMessage(acc_val) df_name = "Main Map" lyr_name = "TAXPARCELS" field_name = "ACCTEXT"  mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, df_name)[0] lyr = arcpy.mapping.ListLayers(mxd, lyr_name, df)[0] arcpy.AddMessage(lyr.name)  datasource = lyr.workspacePath field_delim = arcpy.AddFieldDelimiters(datasource,field_name) expression = "{0} = '{1}'".format(field_name,acc_val) arcpy.AddMessage(expression)  arcpy.SelectLayerByAttribute_management(lyr,"NEW_SELECTION",expression) df.extent = lyr.getSelectedExtent() df.scale = df.scale*1.1  for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "P"):     if elm.name == "P":         elm.text = (acc_val) arcpy.SelectLayerByAttribute_management(lyr,"CLEAR_SELECTION") arcpy.RefreshActiveView()  arcpy.AddMessage("Completed")

View solution in original post

0 Kudos
5 Replies
DarrenWiens2
MVP Alum
expression = "ACCTEXT = '"+p+"'"
This probably isn't the problem, but is the above 'p' supposed to be 'pu'? I assume the problem is either there is no data frame called "Main Map" or there is no layer called "TAXPARCELS". Which line is throwing the error?
0 Kudos
MathewCoyle
Honored Contributor
Hey Tony, sorry forgot about getting back to you. You can do something like this.

import arcpy  arcpy.AddMessage("Starting")  acc_val = arcpy.GetParameterAsText(0) arcpy.AddMessage(acc_val) df_name = "Main Map" lyr_name = "TAXPARCELS" field_name = "ACCTEXT"  mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, df_name)[0] lyr = arcpy.mapping.ListLayers(mxd, lyr_name, df)[0] arcpy.AddMessage(lyr.name)  datasource = lyr.workspacePath field_delim = arcpy.AddFieldDelimiters(datasource,field_name) expression = "{0} = '{1}'".format(field_name,acc_val) arcpy.AddMessage(expression)  arcpy.SelectLayerByAttribute_management(lyr,"NEW_SELECTION",expression) df.extent = lyr.getSelectedExtent() df.scale = df.scale*1.1  for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "P"):     if elm.name == "P":         elm.text = (acc_val) arcpy.SelectLayerByAttribute_management(lyr,"CLEAR_SELECTION") arcpy.RefreshActiveView()  arcpy.AddMessage("Completed")
0 Kudos
TonyAlmeida
MVP Regular Contributor
dkwiens you were right my data frame was "Layers".

mzcoyle thank you very much for that code.
I changed the
df_name = "Main Map"
to
df_name = "Layers"

and it zoomed to it.

My next questions how do you get to highlight they feature you searched for.
0 Kudos
MathewCoyle
Honored Contributor
dkwiens you were right my data frame was "Layers".

mzcoyle thank you very much for that code.
I changed the
df_name = "Main Map"
to
df_name = "Layers"

and it zoomed to it.

My next questions how do you get to highlight they feature you searched for.


Highlighting is cleared at the end, remove this line to retain highlighting.
arcpy.SelectLayerByAttribute_management(lyr,"CLEAR_SELECTION")
0 Kudos
TonyAlmeida
MVP Regular Contributor
Thank you!
0 Kudos