Skip to content

Instantly share code, notes, and snippets.

@bencevans
Last active Oct 12, 2015
Embed
What would you like to do?
localisedEstimate()
# 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