-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCSV_toolbox.pyt
143 lines (113 loc) · 5.22 KB
/
CSV_toolbox.pyt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import arcpy
class Toolbox(object):
def __init__(self):
"""Define the toolbox (the name of the toolbox is the name of the
.pyt file)."""
self.label = "CSV Toolbox"
self.alias = "CSV Toolbox"
# List of tool classes associated with this toolbox
self.tools = [AppendCSVtoFC, Append_AnyCSVtoFC]
class AppendCSVtoFC(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Append CSV to FeatureClass"
self.description = "Appends the Data in the CSV File to Selected featureClass"
self.canRunInBackground = False
def getParameterInfo(self):
"""Define parameter definitions"""
param0 = arcpy.Parameter("in_csv","Input CSV","Input","DEFile","Required")
param0.filter.list = ['csv']
param1 = arcpy.Parameter("Featureclass","Featureclass","Input", "DEFeatureClass","Required")
params = [param0, param1]
return params
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
return
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return
def execute(self, parameters, messages):
"""The source code of the tool."""
import csv
input_csv_path=parameters[0].valueAsText
fc_path=parameters[1].valueAsText
#open a Search Cursor on this featureClass
cursor=arcpy.da.InsertCursor(fc_path, ['Name', 'SHAPE@Y', 'SHAPE@X'])
with open(input_csv_path, 'rb') as csv_file:
reader=csv.reader(csv_file)
for row in reader:
messages.addMessage(row[2])
input=(row[2], row[0],row[1])
#Insert into Cursor
cursor.insertRow(input)
del cursor
messages.addMessage("Finished")
return
class Append_AnyCSVtoFC(object):
def __init__(self):
"""Define the tool (tool name is the name of the class)."""
self.label = "Append Any CSV"
self.description = "Appends Data from any Arbitrary CSV File to Selected featureClass"
self.canRunInBackground = False
def getParameterInfo(self):
"""Define parameter definitions"""
param0 = arcpy.Parameter("in_csv","Input CSV","Input","DEFile","Required")
param0.filter.list = ['csv']
param1 = arcpy.Parameter("XField","X Field","Input","GPString","Required")
param2 = arcpy.Parameter("YField","Y Field","Input","GPString","Required")
param3 = arcpy.Parameter("Name_Field","Name Field","Input","GPString","Required")
param4 = arcpy.Parameter("Featureclass","Featureclass","Input", "DEFeatureClass","Required")
params = [param0, param1, param2, param3, param4]
return params
def isLicensed(self):
"""Set whether tool is licensed to execute."""
return True
def updateParameters(self, parameters):
"""Modify the values and properties of parameters before internal
validation is performed. This method is called whenever a parameter
has been changed."""
#read the First Row in the CSV file
with open(parameters[0].valueAsText, 'rb') as f:
first_line = f.readline()
#split the line
split=first_line.split(',')
#Add in the dropDown for XField
parameters[1].filter.list =split
#Add in the dropDown for Y Field
parameters[2].filter.list =split
#Add in the dropDown for Name Field
parameters[3].filter.list =split
return
def updateMessages(self, parameters):
"""Modify the messages created by internal validation for each tool
parameter. This method is called after internal validation."""
return
def execute(self, parameters, messages):
"""The source code of the tool."""
import csv
input_csv_path=parameters[0].valueAsText
fc_path=parameters[4].valueAsText
#open a Search Cursor on this featureClass
cursor=arcpy.da.InsertCursor(fc_path, ['Name', 'SHAPE@X', 'SHAPE@Y'])
with open(input_csv_path, 'rb') as csv_file:
reader=csv.reader(csv_file)
#set the indices from the headers
split=reader.next()
#set the Indices
xIndex=split.index(parameters[1].valueAsText)
yIndex=split.index(parameters[2].valueAsText)
nameIndex=split.index(parameters[3].valueAsText)
for row in reader:
messages.addMessage(row[nameIndex])
input=(row[nameIndex], row[xIndex],row[yIndex])
#Insert into Cursor
cursor.insertRow(input)
del cursor
messages.addMessage("Finished")
return