#Importing Modules import arcpy #Declaring Variables ID = arcpy.GetParameterAsText(0) OutputLocation = arcpy.GetParameterAsText(1) #Assembling Map - Selecting MXD, Dataframe, and Selecting Layer by Attribute #Creating a Map Document Object within python, referencing the .mxd you want to work with. mxd = arcpy.mapping.MapDocument(r"put filepath to your map here") #Once you have a map document object in python, you can access its various elements, now we need to access the dataframe the layer you are using is in. #The line of code below makes a list of all the dataframes in the map document object we made. The [0] returns the first dataframe in our map document, #if you need to access additional dataframes, you would use a different index, we now have made a dataframe object in python and can access the layers in it df = arcpy.mapping.ListDataFrames(mxd,"*")[0] #Now to Create a Layer Object, the line of code below returns a list of all the layers in the dataframe, with this name. The [0] returns the first layer with that name instead of the whole list Layer = arcpy.mapping.ListLayers(mxd, "Your Layer Name here", df)[0] #Selecting by Attribute, Zooming to Feature, and Setting Scale #This line performs a select by attribute on our Layer object, using a new selection, with an SQL query of ID = the ID variable we created using getparameterAsText(0) arcpy.SelectLayerByAttribute_management(Layer, "NEW_SELECTION" , "ID = " + ID) scales = [scale 1 here , scale 2 here, scale 3 here] for scale in scales: #Selecting by Attribute, Zooming to Feature, and Setting Scale #This line performs a select by attribute on our Layer object, using a new selection, with an SQL query of ID = the ID variable we created using getparameterAsText(0) arcpy.SelectLayerByAttribute_management(Layer, "NEW_SELECTION" , "ID = " + ID) #This zooms the dataframe to the selected feature df.zoomToSelectedFeatures() df.scale = scale #Clearing selected features so they do not show up selected on map arcpy.SelectLayerByAttribute_management(Layer, "CLEAR_SELECTION") #Exporting map dataframe view to pdf, I have it set up to save arcpy.mapping.ExportToPDF(mxd, OutputLocation + ID + scale + ".pdf", "Page_Layout", 640, 480) #Cleaning out memory by deleting the python objects del mxd; del df, del Layer
Ian, I appreciate what you've done for me so far but I'm still very lost.
I've tried reading the Arc help pages, plugging in parameters, running the script in Python and in Arc (and sometimes crashing it!) but I'm just not quite getting it.
Basically all I believe that is working properly is the mxd file path. I've been able to set the parameters to return a list of layers so I can select the one where my points/table is located that I want to search but I don't know how to set up the script or the parameters to be able to search it.
Attached is a lame test file I made with random Lat/Long coordinates and other columns. I'd be trying to search for numbers in the "ID" column to zoom to and print out.
Do I need to change the .py file for every ID that I want to search for? That's what the SQL field is for in the parameters GUI, right? Ugh. So frustrating - it just hasn't clicked yet. Any more help please?
I apologize about the first script tool I loaded, I forgot to save the script I was working on as a .py, I left it as a .txt, so it couldn't be editted from the catalog.
From your last response you seem to want to iterate over all the ID fields in a layer, and then export PDFs of each of those IDs at 3 pre-defined zooms.
I fixed my script tool, and made it take 7 parameters, that way you don't need to modify the script at all(unless you want to change how they name when they export, I've made the script export with the mxd name, then ID Number, then the Scale Number, so they are all differnt names on export. Currently the tool, will let you export them by ID one at a time, but if you need to iterate all ID's in a Layer and export them all at the same scale, then the script could be changed to do them all at once, and should be faster that way.
I've tested this script tool this time(was in a bit of a rush last time), so it should be out of the box useable, just unzip, navigate to the toolbox in ArcCatalog, open the tool, fill in the parameters and let it run. To fill in the Output Location, you can either navigate to it from the tool, or click and drag it to the input, same for the MXD parameter. For the Layer Name, make sure you copy the name exactly as it shows up in the table of contents in your map document.
Hope this does what you need, if you want it editted to do all ID's in a Layer, it should be a fairly easy adjustment.
Edit: To run the tool as is, it took my 5 year old computer about 42 seconds, hopefully yours goes a bit quicker.
When I unzip the "Toolbox2" file, I get a "Toolbox2.tbx" and that's it. Putting it into Arc, there's no script file in the toolbox. Posting the Python code up here will work- I can add it to a toolbox as a script like I did the previous one. Sorry to be a pain!
*edit* Yes, I'd like to just input one ID at a time for now. I may want to be able to enter multiple ID's in the future but never all of them at the same time. Thanks!
Hello Again,
I just unzipped a new copy of the toolbox, and the script was in there(see attached)
I can go ahead and post my code, and I'll put a screenshot of how the parameters were set up as well if you want. When you open the tbx in ArcCataog it should show a script which you can double clcik and run(incidentally, what version of ArcGIS are you running? That can effect toolboxes).
#Importing Modules import arcpy #Declaring Variables ID = arcpy.GetParameterAsText(0) OutputLocation = arcpy.GetParameterAsText(1) #Assembling Map - Selecting MXD, Dataframe, and Selecting Layer by Attribute #Creating a Map Document Object within python, referencing the .mxd you want to work with. mxdpath = arcpy.GetParameterAsText(2) mxd = arcpy.mapping.MapDocument(mxdpath) #Once you have a map document object in python, you can access its various elements, now we need to access the dataframe the layer you are using is in. #The line of code below makes a list of all the dataframes in the map document object we made. The [0] returns the first dataframe in our map document, #if you need to access additional dataframes, you would use a different index, we now have made a dataframe object in python and can access the layers in it df = arcpy.mapping.ListDataFrames(mxd,"*")[0] #Now to Create a Layer Object, the line of code below returns a list of all the layers in the dataframe, with this name. The [0] returns the first layer with that name instead of the whole list LayerName = arcpy.GetParameterAsText(3) Layer = arcpy.mapping.ListLayers(mxd, LayerName, df)[0] #Selecting by Attribute, Zooming to Feature, and Setting Scale #This line performs a select by attribute on our Layer object, using a new selection, with an SQL query of ID = the ID variable we created using getparameterAsText(0) arcpy.SelectLayerByAttribute_management(Layer, "NEW_SELECTION" , "ID = " + ID) scale1 = arcpy.GetParameterAsText(4) scale2 = arcpy.GetParameterAsText(5) scale3 = arcpy.GetParameterAsText(6) scales = [scale1, scale2, scale3] for scale in scales: #Selecting by Attribute, Zooming to Feature, and Setting Scale #This line performs a select by attribute on our Layer object, using a new selection, with an SQL query of ID = the ID variable we created using getparameterAsText(0) arcpy.SelectLayerByAttribute_management(Layer, "NEW_SELECTION" , "ID = " + ID) #This zooms the dataframe to the selected feature df.zoomToSelectedFeatures() df.scale = scale #Clearing selected features so they do not show up selected on map arcpy.SelectLayerByAttribute_management(Layer, "CLEAR_SELECTION") mxdname = mxd.filePath.split("\\")[-1] mxdname = mxdname.replace(".mxd" , "") #Exporing map dataframe view to pdf, I have it set up to save arcpy.mapping.ExportToPDF(mxd, OutputLocation + "/" + mxdname + "ID_" + ID + scale + ".pdf", "Page_Layout", 640, 480)
I sent a new copy of the toolbox, the two images, and the actual python script file, so you don't have to copy the code posted.
I am using Arc 10.1.
Getting closer.... I had to convert the x,y data to a Shapefile so that there were Object ID's and it's happy about that now. I don't want to use the OID field to select the point though, I want to use the Target "ID" field in the excel file. I am getting the following error now:
Executing: Zoomto %1% "Z:\Bulldog\Junk\Script Test1" Z:\Bulldog\BaseMaps\ArcScriptTest.mxd ArcTests_01 10000 5000 1000
Start Time: Thu Jul 10 10:30:45 2014
Running script Zoomto...
<type 'exceptions.IndexError'>: list index out of range
Failed to execute (Zoomto).
Failed at Thu Jul 10 10:30:47 2014 (Elapsed Time: 2.00 seconds)
I'm not sure which is out of range (zoom levels don't seem to make a difference). I've tried to enter in my ID number in multiple ways, TAR101 or just 101 or %TAR101%, %101%, etc....
Is there any reason why you chose to type in the layer name vs. having a pull-down list (Parameters: Layer)?
Do any of the Parameter Property values need to be changed? I see how to make the Scales to a list with Filter:Value List. That could come in handy later...
Oh wait, "List index out of range" must mean there's something wrong with my layer name... maybe? I only have one data frame so that shouldn't be the "list" issue. Thinking aloud here...
*edit* Nope.... lol. New layer name didn't help.