Select to view content in your preferred language

Definition query is not running correctly when the path of the aprx file is given.

457
4
12-09-2024 05:48 AM
mohanalogakrishnan
Emerging Contributor

Hi everyone,

I am newbie to arcpy scripting. I have written the following script based on referring to previous posts. This script remove definition query for a layer in a map.

I run this in the python console of ArcMap.

The query works fine when giving path of the aprx as CURRENT. It removes the existing definition query.

aprx = arcpy.mp.ArcGISProject("CURRENT")
 

The same query also runs when the path of the aprx is given. But it doest remove definition query

aprx = arcpy.mp.ArcGISProject(r'C:\08_GIS\Arc\MyProject9_Test.aprx')

    # Specify the path to your ArcGIS project
    #aprx = arcpy.mp.ArcGISProject("CURRENT")
    aprx = arcpy.mp.ArcGISProject(r'C:\08_GIS\Arc\99_temp_work\MyProject9_Test.aprx')

 

## This script removes all definition query in Map layers in Apro
import arcpy
import os

def remove_definition_query(layer_name, my_map=None):
    # Specify the path to your ArcGIS project
    #aprx = arcpy.mp.ArcGISProject("CURRENT")
    aprx = arcpy.mp.ArcGISProject(r'C:\08_GIS\Arc\99_temp_work\MyProject9_Test.aprx')
    
    # Iterate through maps in the project
    for map in aprx.listMaps():
        # Check if a specific map is provided
        if my_map and map.name != my_map:
            continue
        
        print(f"Checking map: {map.name}")  # Debugging line
        
        # Iterate through layers in the map
        for layer in map.listLayers():
            print(f"Found layer: {layer.name}")  # Debugging line
            if layer.name == layer_name:
                try:
                    # Remove the definition query by setting it to None
                    layer.definitionQuery = None
                    print(f"Removed definition query for layer: {layer_name} in map: {map.name}")
                
                except Exception as e:
                    print(f"Error removing definition query for layer {layer_name} in map {map.name}: {e}")

remove_definition_query("LFS_Replenishment_2_Dissolve1", 'TestMap1')

 Query Output:

Checking map: TestMap1
Found layer: LFS`1
Found layer: LFS_Replenishment_2_Dissolve1
Removed definition query for layer: LFS_Replenishment_2_Dissolve1 in map: TestMap1
Found layer: MapGeniePremiumITM

 

Can someone tell me why this is query is not working correctly when given the path. I appreciate your help on this.

matter. FYI: The path file is correct. 

Tags (2)
0 Kudos
4 Replies
DanPatterson
MVP Esteemed Contributor

In Pro example 2, they save the aprx when they are done, give it a try

Layer—ArcGIS Pro | Documentation


... sort of retired...
0 Kudos
mohanalogakrishnan
Emerging Contributor

Thank you for your time Dan,

I did tried saving to a new layer file. It saves but the saved the file is similar to that of input ones. It didn't remove definition query.

0 Kudos
DanPatterson
MVP Esteemed Contributor

it wasnt the layer being saved, but the project


... sort of retired...
0 Kudos
Luke_Pinner
MVP Regular Contributor
  1.  as @DanPatterson  notes, you need to save the aprx. e.g. aprx.save()
  2. if are you trying to run that code in ArcGIS Pro with that aprx open, that won't work. When you open a project in Pro, it reads it into memory. Any changes you make to the underlying .aprx on disk will not be reflected in the current Pro session. Have you tried running the code, then opening the aprx in a new Pro session?
0 Kudos