Creating a Custom arcgis tool

1771
2
05-02-2013 11:19 AM
by Anonymous User
Not applicable
Original User: luke.kaim

Hi Everyone,

I have a script that works in IDLE,  but now I want to create a custom tool to be run inside arcgis. I am a little confused about how to modify the code to get it to work though. I have a point shapefile and 1 or 2 polygon layers. If there are two polygon layer then I use union. I know I will need gp.GetParameterAsText, but I am a little confused how to get that to work within my update cursor as it is right now. I want this tool to work on any input point shapefile and any input polygon shapefile. The two input files have to have an address field at the very least. How do I let the user select which fields they want to be used in the cursor? Also order of these fields matters in the way I currently have the script written. The goal of this script is for Quality analysis. I want to make sure a points attributes match the polygon it is in.

def SpatialJoin(infile,join,outfile):
    #this is to test a different workflow compared to the one above and see which one is faster. 
    arcpy.SpatialJoin_analysis(infile,join,outfile,"JOIN_ONE_TO_ONE","KEEP_ALL","","INTERSECT")
    #add field to the spatial join that will be updated later if the addresses match.
    arcpy.AddField_management(outfile,"Missmatch2","LONG","","","","","NULLABLE","REQUIRED")
  

def UpdateCursor(infile,fieldname):
    # update the spatial join shapefile if addresses match.
    with arcpy.da.UpdateCursor(infile,fieldname) as cursor:
        #update cursor.
        for row in cursor:
            #for loop to read over cursor.
            if row[0]==row[2]:
                if row[3]!= " ":
                    row[4]=1
                elif row[1]!=row[3]:
                    row[4]=5
                else:
                    row[4]=4

            elif row[0]!=row[2]:
                #if address column does not eqaul polygon address column then row missmatch eqaul 1
                True
                row[4]=2
                if row[2] == " ":
                    row[4]=3
            
            #print row[2]
            cursor.updateRow(row)
            #update row values into the cursor


print "Start"          
# Import  module
import arcpy, datetime
# Overwrite environment parameters
arcpy.env.overwriteOutput = True
# Set work space
from arcpy import env
workspace=arcpy.env.workspace =r"C:\Users\Luke Kaim\Documents\University of Maine\Spring_2013\GIS_Application\finalproject"
#declare variable names
stateSHP="USA_adm1.shp"
Country="Country.shp"
Union="Union.shp"
globeSHP("globeAtNight.shp")
PointField=["SHAPE@","PointID","PointCNTRY","PointState"]

createShape('GaN2012.txt',"globeAtNight.shp",PointField)
Join=["PointCNTRY","PointState","CountryPly", "StatePly","Missmatch2"]
if Country != " ":
    arcpy.Union_analysis([Country,stateSHP],Union,"ALL")  
start1=datetime.datetime.now()
#start new timer.
##arcpy.Union_analysis([Country,stateSHP],Union,"ALL")  
SpatialJoin("globeAtNight.shp",Union,"Join.shp")
###function call to join all points to country polygon.
UpdateCursor("Join.shp",Join)
end1=datetime.datetime.now()
#end new timer. 
print "DONE updateJoin", end1-start1
print "END"




Any help will be greatly appreciated.
0 Kudos
2 Replies
by Anonymous User
Not applicable
Original User: scoggins

Your script is slightly confusing. You start with a spatial join and then import modules half way through? Maybe the first part is an example? And you'll want to make sure to use arcpy.GetParameterAsText, not gp.GetParameterAsText... just saying!
0 Kudos
TimDine
Occasional Contributor II
I see a school and 'Final Project' in your workspace, so a few ideas to point you in the right direction:
-import statements at go at the top, then the function definitions (Clean style thing)
-check on arcpy.AddMessage in addition to print if you want to see messages in your script console
-arcpy.GetParameterAsText
-Read these sections of the ArcGIS Help "Accessing parameters in a script tool", "Understanding script tool parameters", "Setting script tool parameters" (Pay attention to the 'Obtained from' property of the parameter).
0 Kudos