Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

Input field names with dynamic text box and delete unwanted fields

$
0
0
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):

Code:

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:

Code:


# 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)


Viewing all articles
Browse latest Browse all 2485

Trending Articles