Geographic Information Systems Asked on November 12, 2021
In ArcGIS Pro python window:
I’m adding certain layers into a list. From that list, I am converting the list in a service definition draft. I use this same script all the time, sometimes I’ll get this “List out of range” and sometimes I don’t so I don’t why I get this error. I get this error when it’s trying to run the create web layer SSD function. I’ve tried to remove the “[0]” part (don’t know what this means), but I get a different error because of it.
import arcpy
arcpy.env.overwriteOutput = True
aprx = arcpy.mp.ArcGISProject('C:/naples/naples.aprx')
m = aprx.listMaps('Map')[0]
lyrs=[]
lyrs.append(m.listLayers('Installation')[0])
lyrs.append(m.listLayers('Building_Area')[0])
lyrs.append(m.listLayers('Storage Tank')[0])
lyrs.append(m.listLayers('Structure Area')[0])
arcpy.mp.CreateWebLayerSDDraft(lyrs, 'C:/naples/service_definition/naples_installation.sddraft', 'naples_installation', 'MY_HOSTED_SERVICES', 'FEATURE_ACCESS')
It's pretty difficult to provide an answer on "sometimes this errors, sometimes it doesn't" without seeing into your complete system/workflow.
The only advice I can offer is to add some debugging to your script which may highlight the error next time you attempt to run the workflow. As-is, your script expects very structured layer names. Any deviate from those names and it'll fail. The following code shows some more relaxed search patterns and debug techniques you could use.
aprx = arcpy.mp.ArcGISProject('C:/naples/naples.aprx')
m = aprx.listMaps('Map')[0]
lyrs=[]
### Example 1 - wildcards
# Search for "Installation" by using a wild card after a few matching characters
lyrs.append(m.listLayers('Inst*')[0])
# Search for "Storage Tanks" by searching for "Storage" and anything after it via a wild card
lyrs.append(m.listLayers('Storage *')[0])
### Example 2 - basic reporting
searcher = "Installation"
l = m.listLayers(searcher )[0]
if l:
lyrs.append(l)
else:
print("Tried to add {}, but didn't find it".format(searcher))
### Example 3 - bit more robust search/reporting
# Make a list of your layer names you want to match
matchLayers = ["Installation", "Building_Area", "Storage Tank", "Structure Area"]
# Make a list of layer names found in the map
listLayerNames = [x.name for x in m.listLayers()]
# Loop through your list of layer names
for searchLayers in matchLayers:
# If the name of your layer matches something in the map...
if searchLayers in listLayerNames:
# append that name to your lyrs variable
lyrs.append(m.listLayers(searchLayers)[0])
# and remove it from the matchLayers list
matchLayers.remove(searchLayers)
# And then at the end, print any layer not matched
print("The following layers were not found/matched:")
for p in matchLayers:
print(" > {}".format(p))
# And for debugging, print all the layers in the map so you can compare
print("These are the layers within the map:")
for p in listLayers:
print(" > {}".format(p))
Answered by KHibma on November 12, 2021
Get help from others!
Recent Questions
Recent Answers
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP