PYTHON GIS / EXCEL - How to list empty items in the database

0

 


In this article you will learn how to make a list of the empty features inside a spatial database using Python in a simple way.


If you need to make a control of the elements contained in the database that you have been developing during the execution of a project, knowing which elements are empty can be very helpful, either to fill or delete them, and as far as I know, there is no way to do this directly in ArcGIS.


How to make a list of empty features in a spatial database (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 #07 by the path of the database identified in step 1. On the other hand replace in line #05 C:\EJEMPLO by the path where the list will be saved in .txt format. If you want to modify the name of the output file, you can change the word “List” in line #06 by the name of your choice (without removing the quotes).


IMPORTANT NOTE: This script has always given me problems using very long paths so I recommend working 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 arcpy, os                                                                                  #01
import sys                                                                                        #02
reload(sys)                                                                                       #03
sys.setdefaultencoding("utf-8")                                                                   #04
outputDir = r"C:\EJEMPLO"                                                                         #05
outputName = "List"                                                                               #06
A = r"C:\EJEMPLO\EJEMPLO.gdb"                                                                     #07
textFile = (outputDir + os.sep + outputName + ".txt")                                             #08
outFile = open(textFile, "w")                                                                     #09
def listFcsInGDB(gdb):                                                                            #10
    arcpy.env.workspace = gdb                                                                     #11
    tables = arcpy.ListTables()                                                                   #12
    for table in tables:                                                                          #13
        outFile.write("{0}, {1}".format(table, arcpy.GetCount_management(table)) + "\n" )         #14
    for fds in arcpy.ListDatasets('','feature') + ['']:                                           #15
        for fc in arcpy.ListFeatureClasses('','',fds):                                            #16
            outFile.write("{0}, {1}, {2}".format(fds, fc, arcpy.GetCount_management(fc)) + "\n" ) #17
gdb = A                                                                                           #18
fcs = listFcsInGDB(gdb)                                                                           #19
outFile.close()                                                                                   #20
import fileinput                                                                                  #21
headers = 'FEATURE_DATASET, FEATURE_CLASS, NUMBER_OF_ELEMENTS'.split()                            #22
for line in fileinput.input([outputDir + os.sep + outputName + ".txt"], inplace=True):            #23
    if fileinput.isfirstline():                                                                   #24
        print '\t'.join(headers)                                                                  #25
    print line,                                                                                   #26
                                                                                            



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


Step 6: Once the .txt file is generated (a), open it, select all with CTR+E (b) and copy the text with CTR+C (c).


Muestra grafica del resultado del archivo de texto y ejemplo de como copiar el contenido


Step 7: Open an Excel file, in the text import section select as separator the comma character (,) as shown in the following image.


Ejemplo de como importar el contenido del archivo de texto a excel


Step 8: Apply a filter in the resulting columns in Excel, the elements that remain in the last column with a zero (0) are the elements that are empty in the database.


Ejemplo de como obtener el listado de los elementos vacíos en Excel



NOTES:


  • If inside the database there are feature datasets, you must move to the right column the listed elements that are outside the dataset, for example the tables and features that do not belong to a dataset (See next image).


Ejemplo de como organizar los datos obtenidos del archivo de texto



This way you will have a list of the empty elements stored in the GDB, once you have done all your verifications you may need to massively delete several empty elements, for this reason I invite you to go through my article, Delete empty features in the database.


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 on the contact page or leave your comment, I will be attentive to respond.

You may like these posts

No comments