PYTHON GIS - How to modify and/or create a Definition Query to a layer within several ArcGIS files (ArcMap / MXD)

0

 



In this article you will learn how to set and/or redefine a SQL query expression to a layer that is stored in several MXD files using Python.


You may need to limit the spatial information displayed by a layer with respect to the totality it has to emphasize one element or another or several elements, it may also happen (as it has happened to me) that at the beginning of your project you choose to use a feature class to represent one thing in your graphical outputs, and over time you had to include other geometric objects to that feature class, therefore these new objects are also represented in your previous graphical outputs, showing inconsistent information when re-exporting them.


Whatever the case may be, your traditional solution would be the following:


How to modify and/or create a SQL expression an ArcGIS / ArcMap layer?


1) Open the MXD file. Suppose we want to show only feature “1” and hide feature “2”. 


Imagen ilustrativa de dos features, un rectángulo rojo (1) y un rectángulo verde (2)

2) Double click on the layer to which you want to modify and/or create the SQL expression (It is the same as right click [a] and press the “Properties...” button [b]), go to the “Properties...” button [c]), go to the “Properties...” [d] and click on “Properties...” [e]. [b]), go to the Definition Query tab (c). Click on the “Query Builder...” button (d).


Muestra como acceder a la pestaña Definition Query de una capa





3) Write the SQL expression. In this case the “Name” field will be used as a discriminator, when the field is text type the numeric values must be used between apostrophes, instead of simply writing 1, you must write '1'. As long as the expression makes sense, it can be as complex as required.

Muestra un ejemplo de como elaborar una consulta SQL


4) Click on the “OK” button. The result will be as follows:


Resultado de la aplicación de la Definition Query. Solo muestra el rectángulo rojo (1)


5) Save MXD file.


This procedure has to be repeated with this same layer in each of the MXD files, which can be quite time consuming, so I will show you how it can be much more efficient using Python code (Don't worry you don't need to know how to program to do it).


How to modify the SQL console or batch Definition Query of an ArcGIS / ArcMap layer using Python?


Step 1: Open a blank MXD file.


Step 2: Open the Python window in the Standard toolbar.


Muestra la ubicación de la ventana Python dentro de la barra de herramientas Standard (estándar)



Step 3: Backup the original MXD files.


Step 4: Copy the following code into a notepad and in line #02 of the code replace the path C:\EJEMPLO\EJEMPLO with the path where the MXD files to be modified are stored.



import arcpy, os                                             #01
arcpy.env.workspace = ws = r"C:\EJEMPLO\EJEMPLO"             #02
mxd_list = arcpy.ListFiles("*.mxd")                          #03
for mxd in mxd_list:                                         #04
    mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))   #05
    df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]      #06
    for lyr in arcpy.mapping.ListLayers(mxd, "Layer", df):   #07
        if lyr.supports("DEFINITIONQUERY"):                  #08
            lyr.definitionQuery = "Name = 1"                 #09
    mxd.save()                                               #10
                                                             #11
del mxd_list                                                 #12
                                                                     


Step 5: In line #06 of the code replace “Layers” with the name of the dataframe containing the layer to be modified (It is a good practice that if there is more than one dataframe within an mxd file, to name each one uniquely).


Step 6: In line #07 of the code replace "Layer" with the name of the layer to modify.


Step 7: In line #09 of the code replace "Name = 1" by the SQL expression to use.


Step 8: Copy the modified code into the Python window and press the Enter key twice.


After a few seconds you will see how the modification date of the ArcMap files starts to update, and when you open them you will notice the application of the SQL expression to the layer mentioned in step 6.


I hope you enjoyed this article, soon I will upload a video explaining the procedure that you can consult in this blog. Save my blog among your favorite links, I will be uploading many more tricks of this style, remember that you can send me your concerns in the contact page or leave your comment, I will be attentive to respond.


You may like these posts

No comments