Last active
October 12, 2015 13:48
-
-
Save bencevans/4036162 to your computer and use it in GitHub Desktop.
localisedEstimate()
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
# localisedEstimate: | |
# targetlngLat (Array|Object) = Containing the latLng you require a definition for | |
# Example: [9, 10] | |
# surroundinglngLats (Array) = Local LngLats (objects) to be used when finding the value of target. | |
# Example: [{lng:9.21, lat:10.23, value:34}, {lng:8.5, lat:9.78, value:10}] | |
# valueKey (String) = Key used for value in surroundinglngLats Objects. | |
# Example: 'NDWI' | |
localisedEstimate = (targetlngLat, surroundinglngLats, valueKey) -> | |
valueKey = valueKey || 'value' | |
if(!targetlngLat || !surroundinglngLats) | |
false | |
# Extract & Normalise Values | |
targetlngLat = normaliseLngLat targetlngLat | |
values = [] | |
for lngLat, i in surroundinglngLats | |
values[i] = surroundinglngLats[i][valueKey] | |
surroundinglngLats[i] = normaliseLngLat surroundinglngLats[i] | |
# Find Distances | |
distances = [] | |
greatestDistance = 0 | |
totalDistance = 0 | |
for lngLat, i in surroundinglngLats | |
distances[i] = distance targetlngLat, surroundinglngLats[i] | |
greatestDistance = distances[i] if distances[i] > greatestDistance | |
totalDistance = totalDistance + distances[i] | |
# Grade surroundinglngLats against closesed distance to target | |
grades = [] | |
for tempDistance, i in distances | |
grades[i] = tempDistance / totalDistance | |
output = 0 | |
for grade, i in grades | |
output = output + (grades[i] * values[i]) | |
output |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment