A map in my ArcGIS Pro 3.0.3 project has a layer with a Query Feature Class as its source. I can see the SQL that defines the Query Feature Class by looking at its Layer Properties. That SQL is based on tables in an SQL Server Enterprise Geodatabase.
I can get at many properties of the layer using the test code below:
import arcpy
aprx = arcpy.mp.ArcGISProject(r"\\testfolder\testproject.aprx")
mapx = aprx.listMaps("Test Map")[0]
lyrx = mapx.listLayers("Test Layer")[0]
arcpy.AddMessage(f"Layer Name: {lyrx.name}")
arcpy.AddMessage(f"Data Source: {lyrx.dataSource}")
arcpy.AddMessage(f"Definition Query: {lyrx.definitionQuery}")
arcpy.AddMessage(f"Connection Properties: {lyrx.connectionProperties}")
# definition_queries = lyrx.listDefinitionQueries
arcpy.AddMessage(f"Definition Queries: {lyrx.listDefinitionQueries()}")
# Create a Describe object from the feature layer.
#
desc = arcpy.Describe(lyrx)
# Print some properties of the feature layer, and its featureclass.
#
arcpy.AddMessage("Name String: " + desc.nameString)
arcpy.AddMessage("Where Clause: " + desc.whereClause)
arcpy.AddMessage("Feature class type: " + desc.featureClass.featureType)
desc = arcpy.da.Describe(lyrx)
for k, v in desc.items():
arcpy.AddMessage(f"{k}: {v}")
None of these are able to access the SQL code that defines the Query Feature Class. The last part of the code iterates all of the Describe properties of the Layer object and fails to find it.
Is there any way to bring the SQL code that defines the Query Feature Class into a Python variable to work with?
Describe
properties and it's not there, you might need to dump it to a layer file and scan the file as a byte stream of UTF-16 characters (sort of like anod -a
)od -a
) "? I think that may be beyond me as a Python-only programmer..lyr
files were binary, with a mixture of float, int, and string. I looked for sequences of UTF-16 characters 0x00 + ASCII/ASCII + 0x00 longer than four bytes. It was among the uglier blocks of code I had written for a customer in 35 years. If.lyrx
is JSON, then this is now dirt easy, with ajson.loads()
on the UTF-16 character stream from the file (probably little-endian). I'll check this out when I fire up my laptop in a few hours. Might even blog it in Community.