PYTHON GIS / EXCEL - Convert your geospatial database to Excel

0

 


In this article you will learn how to convert all the content of a spatial database to Excel files using Python in a very simple way.



Geographic information systems are by definition interdisciplinary and even transdisciplinary, so it is necessary the intervention of third parties that do not handle them directly for the filling of data from each discipline and thus create a comprehensive database.


In the most common practice there are two ways of doing this:


1. Work shoulder to shoulder with each person representing an area of knowledge.

2. Provide the forms that the data model contemplates to be filled out in a commonly used software such as Excel and once finished, integrate this information to the spatial objects.


Particularly I prefer the second option, in ArcGIS there is the tool “Table to Excel” that allows to convert a feature to Excel, that helps to provide these forms in .xlx format, and although you can use the Batch option to convert several elements at the same time, with the procedure that I will show you next you will maintain the structure of the original database (In case the database is organized by feature class).  


To convert a database to Excel and by extension the elements (Feature or Shapefile, and tables) contained within it, follow the steps below



How to convert a spatial database to Excel (File geodatabase .gdb or Personal geodatabase .mdb)


Step 0: Make a backup of the database (You never know what might happen).


Step 1: Identify the path that represents the database to examine in the operating system explorer.


Step 2: Open ArcMap (A blank mxd file).


Step 3: 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 4: Copy in a notepad the following code and replace the text C:\EJEMPLO\EJEMPLO.GDB in line #03 by the path of the database identified in step 1. On the other hand replace in line #04 C:\EJEMPLO\EJEMPLO_GDB by the path where the database in Excel format will be stored.


IMPORTANT NOTE: This script has always given me problems using very long paths so I recommend to work in a folder in the root of a storage unit (a).



Ejemplo de donde ubicar la base de datos dentro de una unidad de almacenamiento






import os                                                   #01
import arcpy                                                #02
arcpy.env.workspace = r"C:\EJEMPLO\EJEMPLO.gdb"             #03
outputDir = r"C:\EJEMPLO\EJEMPLO_GDB"                       #04
tables = arcpy.ListTables()                                 #05
for table in tables:                                        #06
    in_table2 = table                                       #07
    out_xls2 = (outputDir + os.sep + table + ".xls")        #08
    arcpy.TableToExcel_conversion(in_table2, out_xls2)      #09
datasets = arcpy.ListDatasets(feature_type='feature')       #10
datasets = [''] + datasets if datasets is not None else []  #11
for ds in datasets:                                         #12
    for fc in arcpy.ListFeatureClasses(feature_dataset=ds): #13
     in_table = fc                                          #14
     FLD = outputDir + "/" + ds + "_GDB"                    #15
     out_name = ds + "_GDB"                                 #16
     out_xls = (FLD + os.sep + ds + "_" + fc + ".xls")      #17
     arcpy.CreateFolder_management(outputDir, out_name)     #18
     arcpy.TableToExcel_conversion(in_table, out_xls)       #19


                                                                                            



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


Result:








Ok, this way you will have converted your database to Excel format, and it can be shared with all the people involved in the project, once the excel files corresponding to each feature are filled, you can link them to the spatial information through the ID field using the Join tool.


I hope you have 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