Last active
July 1, 2019 20:30
-
-
Save andybell/f4371b69103787d8699a8e097ea2fed5 to your computer and use it in GitHub Desktop.
Earth Genome GRAT Normalize Index Field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import arcpy | |
def normalizeField(in_table, in_field, out_field, index_min, index_max): | |
""" | |
Normalizes a field to a new range | |
:param in_table: input table | |
:param in_field: field with the original values | |
:param out_field: output field | |
:param index_min: the minimum value for the new range | |
:param index_max: the mazimum value for the new range | |
:return: | |
""" | |
# https://community.esri.com/thread/113780 | |
with arcpy.da.SearchCursor(in_table, in_field) as cur: | |
x, = next(iter(cur)) | |
minimum = maximum = float(x) | |
for x, in cur: | |
if x < minimum: | |
minimum = x | |
if x > maximum: | |
maximum = x | |
print("Min {}".format(minimum)) | |
print("Max {}".format(maximum)) | |
with arcpy.da.UpdateCursor(in_table, [in_field, out_field]) as cur: | |
for in_field, out_field in cur: | |
# https://math.stackexchange.com/questions/159269/take-set-of-values-and-change-scale | |
out_field = ((float(index_max) - float(index_min)) * (float(in_field)-float(minimum)))/(float(maximum)-float(minimum)) + float(index_min) | |
cur.updateRow([in_field, out_field]) | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment