-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecalculate_city_scores.py
46 lines (37 loc) · 1.77 KB
/
recalculate_city_scores.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
import arcpy, sys, string, os
import numpy as np
from scipy.stats import norm
try:
arcpy.env.workspace = "in_memory"
city_features = arcpy.GetParameterAsText(0)#the raw data table
score_tree = arcpy.GetParameterAsText(1) #metadata table
importance_val = arcpy.GetParameterAsText(2) #not currently functional
metric_val = arcpy.GetParameterAsText(3) #this is the field to be updated
target_val = arcpy.GetParameterAsText(4) #high or low, basic calculation
scores_table = arcpy.GetParameterAsText(6) #the master data table to be updated
function_val = arcpy.GetParameterAsText(7) #function selected
arr = arcpy.da.FeatureClassToNumPyArray(city_features, [metric_val])
max_value = str(np.max(arr[metric_val]))
min_value = str(np.min(arr[metric_val]))
fieldList = [metric_val]
arcpy.JoinField_management(scores_table, "city", city_features, "city", fieldList)
# currently only functioning on high vs low
calc = "int(((!" + metric_val + "_1! -" + min_value+ ")/(" + max_value + "-" + min_value + ")) * 100)"
if target_val == 'High':
pass
else: # is Low 0+ %
calc = "100 - " + calc
arcpy.CalculateField_management(scores_table, metric_val, calc, "Python")
arcpy.DeleteField_management(scores_table, metric_val + "_1")
# update the importance value in score_tree
with arcpy.da.UpdateCursor(score_tree,['name','importance','target', 'function']) as cursor:
for row in cursor:
if row[0] == metric_val:
row[1] = importance_val
row[2] = target_val
row[3] = function_val
cursor.updateRow(row)
arcpy.SetParameter(5, scores_table)
except Exception, ErrorDesc:
sErr = "ERROR:\n" + str(ErrorDesc)
arcpy.AddError(sErr)