Skip to content

Instantly share code, notes, and snippets.

@bencevans
Last active October 12, 2015 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bencevans/4036162 to your computer and use it in GitHub Desktop.
Save bencevans/4036162 to your computer and use it in GitHub Desktop.
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