PYTHON GIS - How to repair paths of multiple MXD files in bulk

0

 


In this article you will learn how to re-establish missing databases within various MXD files using Python.  


The loss of link between ArcMap files and spatial databases occurs for one of these reasons:  


(a) Deletion of information.
b) Damage to the spatial information itself.
c) Change of location in the folder containing the spatial information (GDB, Shapefile, among others).
d) Change in the name of the folder containing the spatial information.
e) Moved the complete project folder to another location (if the file was not specified to save relative paths).


The process shown here will help you in case scenarios c, d and e happen to you, and as a bonus I will show you how to avoid the same thing happening again as described in case e (It is worth mentioning that the link is not lost in case the MXD files folder is renamed); To recover the link of the spatial information in MXD files the normal procedure is as follows:

How to repair paths in an ArcGIS .MXD file?


1) Open the MXD file.


2) Go to the layer with the broken link, double right click on it, click on the Data item and click on the “Repair Data Source...” button.



Imagen que muestra la forma en la que se puede reparar la ruta de una capa en un archivo de ArcGIS


3) Find the Shapefile / Feature Class that corresponds to the layer in the new location.


4) Click on accept.


5) Save MXD file.


7) Optional step: If the information was stored in different databases or folders, it will be necessary to repeat the procedure for each layer or layers with different data sources.


If this procedure has to be repeated with each of the layers within the MXD file that have lost their data source, which can be quite time consuming; if in addition to several layers, there are several MXD's with this problem, this would become a colossal time expenditure. To save you all that time, 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 repair MXD (ArcMap) file data sources automatically?


Step 1: Open a blank ArcGIS 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: Identify the missing paths and the current paths to the data. To check the missing paths, right click on the layer (a), press the “Properties...” button (b) and go to the “Source” tab (c), in the “Location:” item (d) you will find the path where the spatial data was previously stored.


Muestra donde encontrar la ruta de una capa que perdió el enlace a su fuente de datos para un archivo de ArcGIS / ArcMap


Step 5: Copy the following code into a notepad and replace the text C:\EXAMPLE\MXD in line #02 with the path to the folder where you have stored the MXD files with the problem. 




import arcpy, os                                                                        #01
arcpy.env.workspace = ws = r"C:\EXAMPLE\MXD"                                            #02
mxd_list = arcpy.ListFiles("*.mxd")                                                     #03
for mxd in mxd_list:                                                                    #04
    mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))                              #05
    mxd.findAndReplaceWorkspacePaths(r"C:\EXAMPLE\OLD_FOLDER", r"C:\EXAMPLE\NEW_FOLDER")#06
    mxd.save()                                                                          #07
                                                                                        #08
del mxd_list                                                                            #09
                                                                     


Step 6: In line #06 of the code replace the path C:\EXAMPLE\OLD_FOLDER with the previous and/or missing path identified in step 4.


Step 7: In the same line #06 of the code replace the path C:\EXAMPLE\NEW_FOLDER with the new path where the spatial information is stored.


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 be updated, and when you open it you will notice the recovery of the link in the layers that presented this problem.


Notes:


  • You can repeat line #06 as many times as you want in case you need to modify and/or repair more than one missing route at a time. This copy must be made exactly below line #06 and would become line #07 so that the code would have ten lines, and so on with each path to be repaired.


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.


BONUS


In future occasions when a new project with geographic information systems using ArcMap try to do the following with all your MXD files to avoid that when moving the complete folder of the project you lose the sources of information in your maps and/or graphical outputs (Case D).


1) Open the MXD file

2) Go to File (a), in the drop-down menu press “Map Document Properties...” (b), in the new window check the option “Store relative pathnames to data sources” (c) and press accept.


Descripción gráfica de como establecer que un documento de ArcGIS guarde las rutas relativas (Ver paso 2)







You may like these posts

No comments