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?
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.