Select to view content in your preferred language

Input field names with dynamic text box and delete unwanted fields

3937
1
Jump to solution
04-27-2013 11:33 AM
SamCoggins1
Occasional Contributor
Hi all

I'm trying to find a way to add field names to each feature class in a geodatabase with Python.

My pseudo-code so far is:

1) Buffer points
2) Add field to each featureclass with name that represents the featureclass.
3) Calculate field, entering the value Y in each field.
4) Perform Spatial join on, the Y value will appear in each record where the point intersects featureclasses.
5) Delete all unwanted fields so that I am left with the point identifier, and each column containing Y's
6) Output to .csv

I can do the spatial join and have that running. At the moment I have no way of determining the unique field name for each featureclass.

So I would like the field names to be meaningful, (not just Field1). I am using about 40 features which represent a variety of environmental considerations. For example, for a file representing wetlands I would like a field called Wetland. I have looked at TKInter which I've read can be used to enter text dynamically and then used as a variable, which I think would work well. It would ensure that I know what each field name means. I just don't seem to be able to use the input text as a variable.

The TKInter code is (I am open to other suggestions of course):

import arcpy from arcpy import env from Tkinter import *  # Environment Workspace env.workspace = r'G:\TEST\Python_Test\Test.gdb'  def receive():     text = E1.get()     print (text) top = Tk() L1 = Label(top, text="User Name") L1.pack( side = LEFT) E1 = Entry(top, bd =5) E1.pack(side = RIGHT) b = Button(top, text="OK", width=10, command=receive) b.pack() top.mainloop()


My code so far is:

 # Import arcpy module import arcpy from arcpy import env  env.workspace = r'G:\Test\Python_Test\Test.gdb\env'  # Local variables: Buffer1 = r'G:\Test\Python_Test\Test.gdb\Buffer1' Buffer2 = r'G:\Test\Python_Test\Test.gdb\Buffer2'  # Add and calculate fields (WOULD BE NICE TO REPLACE THIS SECTION WITH TKinter CODE) inFile1 = 'Env_consideration1' arcpy.AddField_management(inFile1, "RVMA", "TEXT", "", "", "50", "", "NULLABLE", "REQUIRED", "") print inFile1 + " Added field" arcpy.CalculateField_management(inFile1, "RVMA", "\"Y\"", "PYTHON", "") print inFile1 + " Calculating field"  inFile2 = 'Env_consideration2' arcpy.AddField_management(inFile2, "GWR", "TEXT", "", "", "50", "", "NULLABLE", "REQUIRED", "") print inFile2 + " Added field" arcpy.CalculateField_management(inFile2, "GWR", "\"Y\"", "PYTHON", "") print inFile2 + " Calculating field"  fcList = arcpy.ListFeatureClasses()  for f in fcList:          # Process: Spatial Join     arcpy.SpatialJoin_analysis(Buffer1, f, Buffer2, "JOIN_ONE_TO_ONE", "KEEP_ALL")     print "Completed spatial join " + f      # Process: Delete     arcpy.Delete_management(Buffer1, "FeatureClass")     print "deleted Buffer1 " + f      # Process: Copy     arcpy.Copy_management(Buffer2, Buffer1, "")     print "Copied Buffer2 to Buffer1 "  + f      # Process: Delete     arcpy.Delete_management(Buffer2, "FeatureClass")     print "Deleted Buffer2 "  + f  keep = ['OBJECTID', 'Shape', 'Tower', 'RVMA','GWR', 'Shape_Area', 'Shape_Length']  discard = [] for field in [f.name for f in arcpy.ListFields(Buffer1)if f.type <> 'OBJECTID']:     if field not in keep:         discard.append(field) arcpy.DeleteField_management(Buffer1, discard) 
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
SamCoggins1
Occasional Contributor
EDITED:

I managed to figure this out... My code now iterates through feature classes, requests a field name, calculates fields, deletes unwanted fields. And then joins the left over fields to each other with a spatial join. I was stuck on adding the interactive tkinter dialog box in the first loop, and the delete field text at the end of the first loop. I still have some to do on this, but thought I'd post the answers to my original question. Thought I'd put my code up for others:

import Tkinter import tkSimpleDialog import arcpy from arcpy import env  env.workspace = r'G:\TEST\Python_Test\Test.gdb\env'  fcList = arcpy.ListFeatureClasses()  for fc in fcList:     print fc     root = Tkinter.Tk()     var = tkSimpleDialog.askstring("Field Name", "Enter Field Name")     print var      arcpy.AddField_management(fc, var, "TEXT", "", "", "50", "", "NULLABLE", "REQUIRED", "")     print "Adding field name " + var + " to " + fc     arcpy.CalculateField_management(fc, var, "\"Y\"", "PYTHON", "")     print "Calculating field " + var + " to " + fc      keep = ['OBJECTID', 'Shape', 'Tower', var, 'Shape_Area', 'Shape_Length']      discard = []     for field in [f.name for f in arcpy.ListFields(fc)if f.type <> 'OBJECTID']:         if field not in keep:             discard.append(field)     arcpy.DeleteField_management(fc, discard)     print "Deleted all fields except those specified"  # Input variables for spatial join Buffer1 = r'G:\Test\Python_Test\Test.gdb\Buffer1' Buffer2 = r'G:\Test\Python_Test\Test.gdb\Buffer2'  sjList = arcpy.ListFeatureClasses()  # For loop for spatial join process  for sj in sjList:      # Spatial join between buffered points and feature     arcpy.SpatialJoin_analysis(Buffer1, sj, Buffer2, "JOIN_ONE_TO_ONE", "KEEP_ALL")     print "Completed spatial join " + sj      # Delete Buffer1 containing no joined features     arcpy.Delete_management(Buffer1, "FeatureClass")     print "deleted Buffer1 " + sj      # Copies Buffer2 containing joined features and outputs as Buffer1     arcpy.Copy_management(Buffer2, Buffer1, "")     print "Copied Buffer2 to Buffer1 "  + sj      # Deletes Buffer2 to allow script to create Buffer2 in next iteration     arcpy.Delete_management(Buffer2, "FeatureClass")     print "Deleted Buffer2 "  + sj

View solution in original post

0 Kudos
1 Reply
SamCoggins1
Occasional Contributor
EDITED:

I managed to figure this out... My code now iterates through feature classes, requests a field name, calculates fields, deletes unwanted fields. And then joins the left over fields to each other with a spatial join. I was stuck on adding the interactive tkinter dialog box in the first loop, and the delete field text at the end of the first loop. I still have some to do on this, but thought I'd post the answers to my original question. Thought I'd put my code up for others:

import Tkinter import tkSimpleDialog import arcpy from arcpy import env  env.workspace = r'G:\TEST\Python_Test\Test.gdb\env'  fcList = arcpy.ListFeatureClasses()  for fc in fcList:     print fc     root = Tkinter.Tk()     var = tkSimpleDialog.askstring("Field Name", "Enter Field Name")     print var      arcpy.AddField_management(fc, var, "TEXT", "", "", "50", "", "NULLABLE", "REQUIRED", "")     print "Adding field name " + var + " to " + fc     arcpy.CalculateField_management(fc, var, "\"Y\"", "PYTHON", "")     print "Calculating field " + var + " to " + fc      keep = ['OBJECTID', 'Shape', 'Tower', var, 'Shape_Area', 'Shape_Length']      discard = []     for field in [f.name for f in arcpy.ListFields(fc)if f.type <> 'OBJECTID']:         if field not in keep:             discard.append(field)     arcpy.DeleteField_management(fc, discard)     print "Deleted all fields except those specified"  # Input variables for spatial join Buffer1 = r'G:\Test\Python_Test\Test.gdb\Buffer1' Buffer2 = r'G:\Test\Python_Test\Test.gdb\Buffer2'  sjList = arcpy.ListFeatureClasses()  # For loop for spatial join process  for sj in sjList:      # Spatial join between buffered points and feature     arcpy.SpatialJoin_analysis(Buffer1, sj, Buffer2, "JOIN_ONE_TO_ONE", "KEEP_ALL")     print "Completed spatial join " + sj      # Delete Buffer1 containing no joined features     arcpy.Delete_management(Buffer1, "FeatureClass")     print "deleted Buffer1 " + sj      # Copies Buffer2 containing joined features and outputs as Buffer1     arcpy.Copy_management(Buffer2, Buffer1, "")     print "Copied Buffer2 to Buffer1 "  + sj      # Deletes Buffer2 to allow script to create Buffer2 in next iteration     arcpy.Delete_management(Buffer2, "FeatureClass")     print "Deleted Buffer2 "  + sj
0 Kudos