Select to view content in your preferred language

Ability to identify a Query Layer in ArcPy & Modify them

939
1
06-06-2016 01:01 PM
Status: Open
Labels (1)
RonnieRichards
Regular Contributor

ArcPy functionality using and detecting Query Layers is very limited.

 

  • Identification - In the arcpy.mapping module there is no proper way to identify a query layer. There should be a method or property to identify the layer in question is a query layer.
  • Update Query - In the arcpy.mapping module there is no ability to modify the query portion of the query layer.
  • repathDataSource - This method does not work properly for Query Layers especially if the database name has changed. Using SQL Server the database name is part of the query and this method does not update the database inside the query layer properties. This could be dealt with if updating the query was possible but currently it is not so repathing a map document using query layers with differing databases is not an easy task or nearly impossible.
1 Comment
JeffBarrette

@RonnieRichards this should be possible using Python CIM Access

Here is a code snippet that could be used to find if a layer is a query layer.

 

aprx = arcpy.mp.ArcGISProject('current')
m = aprx.listMaps()[0]
qlyr = m.listLayers('MyQueryLayer')[0]
cim_qlyr = qlyr.getDefinition("V3")
if type(cim_qlyr.featureTable.dataConnection).__name__ == 'CIMSqlQueryDataConnection':
    print('Ureka, its a query layer')

 

 

Next you could continue with modifying other CIM property, like the SQLQuery

 

l_cim.featureTable.dataConnection.sqlQuery = "select OBJECTID,Shape,STATE_NAME,STATE_FIPS,SUB_REGION,STATE_ABBR,POP1990,POP2000,POP90_SQMI from ARCPYMAPPING_01.APM_PRO_states WHERE SUB_REGION = 'Mtn'"
qlyr.setDefinition(cim_qlyr)

 

 

Data connection info is also there. Does this work for you?

Jeff - Layout and arcpy.mp teams