forked from Innovate-Inc/CRS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcrsGeocodeDonations.py
188 lines (160 loc) · 7.77 KB
/
crsGeocodeDonations.py
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
## Script created by Jenny Holder, Innovate! Inc.
## Created: September 2016
## Updated: September 2017
## Purpose: Associates donations with institutions addresses (locations) and writes all to DonationsFC table
## For donations with Fund Name CRS Rice Bowls, record is also written to RiceBowlsFC
## Had to install pypyodbc, requests, unidecode and copied here:
## May have to install pypyodbc
import arcpy, pypyodbc, sys, ftfy
from unidecode import unidecode
## Create connection to SQL Server database and open a cursor
connection = pypyodbc.connect('Driver={SQL Server Native Client 11.0};' 'Server=10.15.230.244\dev;' 'Database=Salesforce_Data;' 'uid=jenny.holder;pwd=crs4fun')
#connection = pypyodbc.connect('Driver={SQL Server Native Client 11.0};' 'Server=10.15.30.186;' 'Database=Salesforce_Data;' 'uid=sf_intregrationadmin;pwd=JetterbitCRS')
pyCursor = connection.cursor()
print "Made connection."
## Point to the sde connection
arcpy.env.workspace = "C:\Users\jenny.holder\AppData\Roaming\Esri\Desktop10.4\ArcCatalog\Salesforce_Data (dev).sde"
#arcpy.env.workspace = "C:\Users\jenny.holder\AppData\Roaming\Esri\Desktop10.4\ArcCatalog\Connection to 10.15.30.186.sde"
#arcpy.env.workspace = "D:\Salesforce_Data\Salesforce_Data.sde"
#### Create set of current donations to match against if rerunning manually
##lookupSet = set()
##lookupTable = 'Salesforce_Data.dbo.DonationsFC'
##lookupFields = ["ID"]
##with arcpy.da.SearchCursor(lookupTable, lookupFields) as lCursor:
## for row in lCursor:
## sfid = row[0].encode("utf8")
## lookupSet.add(sfid)
##print "Created Lookup Set."
fd = 'Salesforce_Data.dbo.Donations'
fieldNames = [f.name for f in arcpy.ListFields(fd)]
print fieldNames
with arcpy.da.SearchCursor(fd, fieldNames) as sCursor:
for row in sCursor:
## ## Use when rerunning manually via lookup set
## if row[0] in lookupSet:
## pass
## print "Passed " + row[0]
## else:
## Find point location of Institution
locationString = "SELECT Shape.STAsText() FROM InstitutionsFC where ID = '" + str(row[6]) + "'"
#print locationString
pyCursor.execute(locationString)
try:
#print "trying location"
for loc in pyCursor.fetchone():
location = "geometry::STPointFromText('" + loc + "', 3857)"
#print location
## Find diocese the point is in
intersectString = "Select Name_other from USDIOCESESAGOL where Shape.STContains(" + location + ") = 1"
#print intersectString
pyCursor.execute(intersectString)
try:
for d in pyCursor.fetchone():
diocese = d
#print diocese
except:
diocese = ''
#print diocese
## Find region the point is in
intersectString = "Select RegionName from USREGIONSAGOL where Shape.STContains(" + location + ") = 1"
#print intersectString
pyCursor.execute(intersectString)
try:
for r in pyCursor.fetchone():
region = r
#print region
except:
region = ''
#print region
## Find congressional district the point is in
intersectString = "Select UniqueID from USCONGRESSIONALDISTRICTSAGOL where Shape.STContains(" + location + ") = 1"
#print intersectString
pyCursor.execute(intersectString)
try:
for c in pyCursor.fetchone():
congDist = str(c)
#print congDist
except:
congDist = ''
#print congDist
## Find US state the point is in
intersectString = "Select STATE_NAME from USSTATESAGOL where Shape.STContains(" + location + ") = 1"
#print intersectString
pyCursor.execute(intersectString)
try:
for s in pyCursor.fetchone():
usState = str(s)
except:
usState = ''
except:
location = 'Null'
region = ''
diocese = ''
congDist = ''
usState = ''
if row[1] is None:
amount = 0
else:
amount = row[1]
#print amount
if row[2] is None:
closeDate = ''
else:
closeDate = row[2]
#print closeDate
if row[3] is None:
cleanName = ''
else:
cleanName = ftfy.fix_text(row[3])
cleanName = unidecode(cleanName)
cleanName = cleanName.replace("'", "''").rstrip()
if row[4] is None:
stageName = '-'
print stageName
else:
stageName = ftfy.fix_text(row[4])
stageName = unidecode(stageName)
#print "Unidecode: " + stageName
stageName = stageName.replace("'", "''").rstrip()
print stageName
if row[5] is None:
recordType = '-'
else:
recordType = ftfy.fix_text(row[5])
recordType = unidecode(recordType)
#print "Unicode: " + recordType
recordType = recordType.replace("'", "''").rstrip()
print recordType
## Find next Object ID to continue incrementing in full Donations table
pyCursor.execute("DECLARE @myval int EXEC dbo.next_rowid 'dbo', 'DonationsFC', @myval OUTPUT SELECT @myval")
for thisrow in pyCursor.fetchall():
nextID = thisrow[0]
#print nextID
insertFields = str(nextID) + ", '" + str(row[0]) + "', '" + str(amount) + "', '" + str(closeDate) + "', '" + str(cleanName) + "', '" + str(stageName) + "', '" + str(recordType) + "', '" + str(row[6]) + "', '" + region + "', '" + diocese + "', '" + congDist + "', '" + usState + "', " + location
outFields = 'ObjectID, ID, Amount, CloseDate, Name, StageName, Type_, AccountID, Region, Diocese, CongressionalDistrict, USState, Shape'
sqlString = "Use Salesforce_Data Insert into DonationsFC(" + outFields + ") values (" + insertFields + ")"
#print sqlString
print "Added to Donations:" + str(row[0])
pyCursor.execute(sqlString)
connection.commit()
if row[7] is None:
pass
print "----"
elif 'CRS Rice Bowl' in str(row[7]):
print str(row[7])
## Find next Object ID to continue incrementing in stand alone Rice Bowls table
pyCursor.execute("DECLARE @myval int EXEC dbo.next_rowid 'dbo', 'RiceBowlsFC', @myval OUTPUT SELECT @myval")
for thisrow in pyCursor.fetchall():
nextID = thisrow[0]
#print nextID
insertFields = str(nextID) + ", '" + str(row[0]) + "', '" + str(amount) + "', '" + str(closeDate) + "', '" + str(cleanName) + "', '" + str(stageName) + "', '" + str(recordType) + "', '" + str(row[6]) + "', '" + region + "', '" + diocese + "', '" + congDist + "', '" + usState + "', " + location
outFields = 'ObjectID, ID, Amount, CloseDate, Name, StageName, Type_, AccountID, Region, Diocese, CongressionalDistrict, USState, Shape'
sqlString = "Use Salesforce_Data Insert into RiceBowlsFC(" + outFields + ") values (" + insertFields + ")"
#print sqlString
print "*** Added to Rice Bowls:" + str(row[0])
pyCursor.execute(sqlString)
connection.commit()
## Close/delete the cursor and the connection
pyCursor.close()
del pyCursor
connection.close()