0

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')
  • aprx.listMaps('Map')[0] the zero index is to fetch first listed map. Are you sure all your projects have maps? Which line of code give you the error? – BERA Jun 20 at 14:46
  • The line of code where I get my error is at the "arcpy.mp.createWebLayerSDDraft" and it's because of my list, I believe. All my layers are in the same map. I've used this script several times, sometimes I'll get this error and sometimes I don't. I could just ignore the list part and use my "m" variable, but I don't want to convert all my layers in my map. – Trey Jun 20 at 15:34
  • What is the length of lyrs after all of the appends? Is it always 4? – smiller Jun 21 at 18:26
0

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))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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