InsideDarkWeb.com

List index out of range when converting layers as a service definietion

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

Geographic Information Systems Asked on November 12, 2021

1 Answers

One Answer

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

Add your own answers!

Related Questions

Processing toolbox won’t open tools

0  Asked on April 18, 2021 by aadr

     

Difficulty with nested if statments in Survey123

0  Asked on April 18, 2021 by aoloughlen

 

Installing the Orfeo Toolbox in QGIS

7  Asked on April 18, 2021 by vascobnunes

     

Special characters changing to “?” using ArcMap

0  Asked on April 18, 2021 by hager-mohamed

       

QGIS forces multi-geometry?

1  Asked on April 18, 2021 by jarekj71

     

Renaming 1,200 rasters in File Geodatabase using ArcPy

0  Asked on April 18, 2021 by john-c-king

   

Install GDAL Python binding on mac

7  Asked on April 18, 2021 by strernd

 

USA CRS with Alaska and Hawaii in Bottom Left

0  Asked on April 17, 2021 by bryce-carrasco

     

Use Esri Portal Sign-in page for ASP.NET?

0  Asked on April 17, 2021 by ejobrien

     

Ask a Question

Get help from others!

© 2021 InsideDarkWeb.com. All rights reserved.