0

I am trying to use the grid index features tool in ArcPy. However when I run it outside of the Python window in ArcGIS Pro, it freezes and crashes. When used within ArcGIS Pro it works fine. I am using version 2.9.

This is how the code looks:

import arcpy
import os

def gridIndex(outputLocation, siteBoundary, mapScale, mapWidth, mapHeight):
    """This definition will create a grid index for map series within ArcGIS Pro
       and add some custom fields to it.

    Arguments:
    outputLocation (string): The location of where the gridindex feature class has to be output.
    siteBoundary   (file): The site boundary feature class, to determine the border.
    mapScale       (integer): The scale in which the map series need to be displayed.
    mapWidth       (integer): The width of the index squares in cm.
    mapHeight      (integer): The height of the index squares in cm.

    Return:
    originalGridIndex (file: This is the feature class containing the grid
    """
    originalGridIndex = arcpy.cartography.GridIndexFeatures(os.path.join(outputLocation, "gridIndexs"), siteBoundary, "", "", mapScale, mapWidth, mapHeight)
    arcpy.management.AddField(originalGridIndex, "Scale", "LONG")
    arcpy.management.AddField(originalGridIndex, "FigureReference", "TEXT", 10) #SQL Query
    arcpy.management.AlterField(originalGridIndex, "PageName", "PageSort", "PageSort")
    arcpy.management.AddField(originalGridIndex, "PageName", "TEXT", 50)
    return originalGridIndex

def fillFields(gridIndexFC, mapScale):
    """This definition will take the grid index made in gridIndex and fill the fields
       that still need filling in.

       Arguments:
       gridIndexFC (file): This is the grid featureclass used for the map series.
       mapScale    (integer): This is the scale used for the map series.
       """
    arcpy.management.CalculateField(gridIndexFC, "Scale", mapScale)

def main():
    """This script will create a grid index within a site boundary.
       It will then add some custom fields to this feature class and fill some fields.
       This grid will be used for the map series within ArcGIS Pro.

       Arguments:
        outputLocation (string): The location of where the gridindex feature class has to be output.
        siteBoundary   (file): The site boundary feature class, to determine the border.
        mapScale       (integer): The scale in which the map series need to be displayed.
        mapWidth       (integer): The width of the index squares in cm.
        mapHeight      (integer): The height of the index squares in cm.

        Output:
        A grid index ready to be used in map series."""
    siteBoundary = arcpy.GetParameter(0)
    outputLocation = arcpy.GetParameterAsText(1)
    mapScale = arcpy.GetParameter(2)
    mapHeight = arcpy.GetParameter(3)
    mapWidth = arcpy.GetParameter(4)
    grid = gridIndex(outputLocation, siteBoundary, mapScale, mapWidth, mapHeight)
    fillFields(grid, mapScale)

if __name__ == '__main__':
    main()

Because it freezes and crashes, I can't find an error message and therefor I can't fix it. Any ideas?

6
  • Where “outside of the Python window in ArcGIS Pro” are you running this?
    – PolyGeo
    Commented Jan 12, 2023 at 19:06
  • An ArcGIS toolbox with the script imported.
    – Amidaz
    Commented Jan 16, 2023 at 9:44
  • What happens when you simplify your test tool to one that does no more than set the variables and print (arcpy.AddMessage) them?
    – PolyGeo
    Commented Jan 16, 2023 at 10:09
  • When I hash out the definitions and just add the add message for each variable, it prints them without issue.
    – Amidaz
    Commented Jan 16, 2023 at 14:59
  • 1
    originalGridIndex is a result object. So you are feeding a result object into the layer parameter of the AddField(). Typically you get the output stored in the result object using the getOutput() method. Suggest you read the help file.
    – Hornbydd
    Commented Jan 16, 2023 at 16:46

1 Answer 1

1

All your input parameters are set to the first input [GetParameter(0)]. They should look more like this:

siteBoundary = arcpy.GetParameterAsText(0)
outputLocation = arcpy.GetParameterAsText(1)
mapScale = arcpy.GetParameter(2)
mapHeight = arcpy.GetParameter(3)
mapWidth = arcpy.GetParameter(4)
1
  • Ah thank you, missed that one! Sadly this didn't resolve the issue though, it is still freezing and crashing.
    – Amidaz
    Commented Jan 16, 2023 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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