Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to create a button that will execute a Python script. I have limited Python experience. What we are trying to accomplish is to recreate these steps:

  1. Query on project Site and Radius Layers (both in an SDE)
  2. Zoom to the site
  3. Adjust scale to 10,500

I have pasted the code I wrote while trying to figure this all out, see below:

import arcpy
mxd = arcpy.mapping.MapDocument('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd, "Layers") [0]
df.scale = 10500

This works, but is not anything that I can't do with ArcMap tools.

share|improve this question
If it works, what is your question exactly? If it's step 1 that your question is really about, you will want to expand upon that point (what kind of query or queries need to be done, and what user interaction is required?). – blah238 May 15 '12 at 21:12
The user is putting definition queries on a point and polygon feature using the project number, which is in a field named "PID" within the attribute table. This will allow the user to select a layer and zoom to it using the zoom to layer tool. The code above simply zooms to a layer if the user manually defines the query and then selects the row. What would be the correct steps to incorporate the definition query and the selection of the row into a python script? Thanks for your help. – Colli May 15 '12 at 21:25
up vote 1 down vote accepted

Since user interaction is required (choose a layer, enter a number), I would suggest creating a script tool with parameters: one for the layer and another for the ID value. See this answer for a list of parameter types; I would suggest Feature Layer and Long but it depends on exactly what you want to do.

Then in your script, construct a SQL query expression (WHERE clause) from the chosen layer and specified ID value. Pass this, along with the layer name, into SelectLayerByAttribute_management and then continue with your script as-is.

A couple of tips for creating where clauses I always suggest are:

  • Use AddFieldDelimiters to automatically append the DBMS-specific field delimiters (e.g. "FIELD_NAME" for file geodatabases or [FIELD_NAME] for personal geodatabases)
  • Use Python string formatting (new or old styles) to more clearly format the SQL query.
  • If it is possible to enter multiple ID values, you might find the code in this answer helpful (also illustrates the other two suggestions).
share|improve this answer
This is indeed very helpful. Thank you for outlining this for me. – Colli May 16 '12 at 11:44

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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