Select to view content in your preferred language

Works in ArcGIS Pro Python Console but FUBAR with script tool

09-23-2019 07:12 AM
Occasional Contributor

I have an old script tool that I'm migrating to ArcGIS Pro.  The tool only has two parameters (Subbasin: string, Directory: folder)  and it is supposed export a PDF mapbook of the features for the subbasin number entered.

I wrote the code and tested it in the Python console window in Pro. It works fine. I have exported >20 mapbooks.

I configured it as a script tool and two things happen:

  1. Whatever subbasin is entered, the resulting section set for mapsheets is always the selection set that was desired on the previous run of the tool—seriously weird!
  2. The PDF export file is never created. The exportToPDF function seems to run, but no file is generated and no exception is thrown.

The logic is as follows:

  1. Check the map for the required layers
  2. Apply the definition query for the desired subbasin
  3. Perform selections to get the desired mapsheet grid
    1. Select inspection results for the desired subbasin
    2. Select mapsheets for previously selected inspection results
  4. Apply definition query to based on mapsheet selection
    1. Get selection set for mapsheet
    2. Use selection set to make query string for definition query.
    3. Apply definition query to Inspection Mapsheets layer
  5. Change Layout text elements
  6. Refresh map series
  7. Export map series as PDF 

I added the mapbook derived output the last time i tried to get it to work.

0 Kudos
8 Replies
MVP Esteemed Contributor

That does seem mysterious. Just a wild guess: one issue has bit me in the past with geoprocessing is that if you provide a string "foobar", if you have a layer named "foobar" in the session and a dataset named "foobar" in the current workspace, you sometimes get one or the other (I think this layer gets precedence, but I'm not sure, and it may depend on context). My fix for this is has been to make sure when I reference an input if I want the dataset (not the layer), I convert it to a full path.

0 Kudos
Occasional Contributor

I gave the layers completely unique names as a quick test and the result is the same.

I think I may just rebuild it as an ArcGIS Pro add-in.

0 Kudos
MVP Esteemed Contributor

Whatever subbasin is entered, the resulting section set for mapsheets is always the selection set that was desired on the previous run of the tool

What happens on the first run, since there is no previous run?

0 Kudos
Occasional Contributor

I tested everything in the Python console before I moved it to the script tool.  So the first time it ran as a script tool the mapsheets/grid selection was same as the previous run in the console—not the desired result.

I created a new project, imported the maps and layouts, and created another script tool and the result is the same.  The mapsheet/grid selection result is what was desired in the last run in the other project.

0 Kudos
MVP Esteemed Contributor

Since you imported the maps and layouts into the new project, you must also be importing the selections on layers from the other project.  What if you clear all selections from the original project before importing maps and layouts into new project and running script tool?

0 Kudos
Occasional Contributor

That's the problem...There were no selections.  The selections are cleared each time.   And I'm deleting the display cache. 

I may try rebuilding the project from scratch.

0 Kudos
Occasional Contributor

Here's what is happening:

The SelectLayerByLocation_management function is using the subbasin layer— before the definition query is applied— as the selecting features input.

Whatever definition query is applied to the subbasin layer when the project is opened is the one used for the spatial selection. Even though I get the correct layer object and use it as a parameter in the selection (line 33).

In the image below there was no definition query applied to the subbasin layer before the tool ran, so the selecting features were all the subbasins.

I even had the tool sleep for 30 sec (line 30) while I manually refreshed the map to no effect (BTW, there is no refresh method in the Map class of the mp module).

Next step is to export the layer with the definition query applied then use that output as the selecting features in the spatial selection.

EDIT: CopyFeatures_management does the same thing as SelectLayerByLocation_management

This is only an issue when run as a GP tool.  No issue in the python console.

Sure would be nice if someone from Esri would comment on this...

import arcpy
from arcpy import env
import time

# Global variables
# ----------------
aprx ='CURRENT')
myMap = aprx.listMaps('Map')[0]
sbCode = arcpy.GetParameterAsText(0)
sbAIName = f'Subbasin {sbCode}'
sbDefQ = f'AREA_CODE = {sbCode}'
# Get the layer objects
mshLayer = myMap.listLayers('Mapsheets')[0]
mshAILayer = myMap.listLayers('AI Mapsheets')[0]
aiResultLayer = myMap.listLayers('AI Results')[0]
sbAILayer = myMap.listLayers('Subbasin *')


for sb in sbAILayer:
    arcpy.AddMessage(f'Layer name start of loop: {sb}')
    arcpy.AddMessage(f'Subbasin query string: {sbDefQ}')
    sb.definitionQuery = sbDefQ
    arcpy.AddMessage('Definition query applied to AI Subbasins.') = sbAIName
    arcpy.AddMessage(f'AI Subbasin layer name changed to {sbAIName}')
    arcpy.AddMessage(f'Layer name end of loop: {sb}')
    arcpy.AddMessage('Sleeping for 30 seconds...')
    time.sleep(30)  # Manually refresh the map during sleep

# Layer object again to ensure I have the correct one  for selecting feature below
sbSelection = myMap.listLayers(sbAIName)[0]

# Perform spatial selections
# --------------------------
# Select Acoustic Inspection results in desired subbasin
resultsSelect = arcpy.SelectLayerByLocation_management(aiResultLayer,

0 Kudos
MVP Esteemed Contributor

Sure would be nice if someone from Esri would comment on this...

Now that you have the problem so well defined, I would contact Esri support so they can reproduce and fix it!

0 Kudos