listLayers isn't showing my XY To Line output

307
5
08-06-2021 05:36 AM
GabrielMarcus_C
New Contributor III

I'm using 2.8.2 but this issue is present in 2.8 and possibly before. 

I created a near table and produced an XY To Line feature in a python script. When attempting to use listLayers from a standalone python script OR from the python window in AGP, the XY Line doesn't appear in the returned list. WHY?

I have used export features and saved it as a different name in the defaultGeodatabase and homeFolder as a shp.  I shared it as a layer file and loaded it back in using LayerFile. I can return the name by using ListFeatureClasses but try as I have, I can't get it to be listed in listLayers. 

I'm trying to automate Overwrite Web Layer and the script demands a layer reference, not just a name. 

I'm hoping I'm just overlooking something. 

0 Kudos
5 Replies
DanPatterson
MVP Esteemed Contributor

are you using CIM access?

There is listLayers for LayerFiles  (aka *.lyrx) and listLayers in the Map class

Alphabetical list of arcpy.mp classes—ArcGIS Pro | Documentation

Your script might help narrow it down.  Accessing featureclasses and making a layer (temporary one) is often easier than navigating the CIM


... sort of retired...
0 Kudos
GabrielMarcus_C
New Contributor III

My script, you say? I thought you might ask for it. 

This script was largely taken from this page: ESRI's featuresharingdraft  documentation.
The goal is to overwrite an existing web layer that has lines of distance from facilities to wildfire origination points. I'm trying to automate this to run on a schedule and I can't. 

I've narrowed down the problem to my XY line "layer" not showing up when I call the m.listLayers() method.  I ran m.listLayers in the python window and confirmed that all of the other layers show up, but this one is strangely absent. 

Going into Properties>Source, everything in my project appears to be a "File Geodatabase Feature Class" and based on my skimming of this article, ESRI defines these all as layers and so they should all be listed when I run m.listLayers. 

I'm on a different computer than when I first posted this.  When I go into the python window and assign aprx to be the "CURRENT" project, the XY layer does show up after running m.listLayers, but when setting aprx to the path of the project, a different XY layer is listed that is NOT in the contents pane (I searched).

It's a previous layer from an earlier run of the script that creates the line, and is stored in the project GDB. I'm not doing a walk or other method that would attempt to search the GDB so I'm very confused regarding the behavior exhibited. 

 

import arcpy
import os
from datetime import datetime
from arcgis.gis import GIS
# Sign in to portal
arcpy.SignInToPortal("secret stuff deleted")

arcpy.env.workspace = r"C:\Users\gmarcus\ArcGIS\Projects\del_test2\del_test2.gdb"
homeGDB = r"C:\Users\gmarcus\ArcGIS\Projects\del_test2\del_test2.gdb"
now = datetime.now()
AMPM = ""
if now.hour - 12 > 0:
    AMPM = "PM"
else:
    AMPM = "AM"
prefix = AMPM + datetime.strftime(now,'%H_%d')

ag_xy = "F" + prefix + "_XY"
# Set output file names
outdir = r"C:\gisTEMP\Fire"
service_name = "Wildfire Map_WFL1"
sddraft_filename = service_name + ".sddraft"
sddraft_output_filename = os.path.join(outdir, sddraft_filename)
sd_filename = service_name + ".sd"
sd_output_filename = os.path.join(outdir, sd_filename)

# Reference map to publish

aprx = arcpy.mp.ArcGISProject(r"C:\Users\gmarcus\ArcGIS\Projects\del_test2\del_test2.aprx")
m = aprx.listMaps('Map')[0]

selected_layer = m.listLayers(ag_xy)
arcpy.AddMessage("Layer name: {lay} will be used".format(lay=selected_layer))
# Create FeatureSharingDraft and set overwrite property
server_type = "HOSTING_SERVER"
sddraft = m.getWebLayerSharingDraft(server_type, "FEATURE", service_name, selected_layer)
sddraft.overwriteExistingService = True
arcpy.AddMessage("Writing metadata...")
# Copied from the initial publish script
sddraft.credits = "Nobody"
sddraft.description = "Fire Distance"
sddraft.summary = "Fire distance from locations"
sddraft.tags = "wildfire"
sddraft.useLimitations = "This data is good"
sddraft.portalFolder = "Wildfire"
sddraft.allowExporting = True

arcpy.AddMessage("Creating SD Draft File....")
# Create Service Definition Draft file
sddraft.exportToSDDraft(sddraft_output_filename)

arcpy.AddMessage("Stage Service")
# Stage Service
print("Start Staging")
arcpy.StageService_server(sddraft_output_filename, sd_output_filename,102)

# Share to portal
print("Start Uploading")
arcpy.UploadServiceDefinition_server(sd_output_filename, server_type)

print("Finish Publishing")

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

Either run the script from an open project or use featureclasses and convert to layers as needed


... sort of retired...
0 Kudos
GabrielMarcus_C
New Contributor III

I'm trying to run this as a scheduled task, so the first option is out for me. 

Can you describe how to convert the string output from ListFeatureClasses to a layer object? I was trying to figure that out. 

0 Kudos
DanPatterson
MVP Esteemed Contributor

Make Feature Layer (Data Management)—ArcGIS Pro | Documentation

it will be temporary unless saved explicitly


... sort of retired...
0 Kudos