Skip to content

Instantly share code, notes, and snippets.

@petersen-poul
Last active September 18, 2019 01:46
Show Gist options
  • Save petersen-poul/e11ec6ae74912dd219ce554eff998db4 to your computer and use it in GitHub Desktop.
Save petersen-poul/e11ec6ae74912dd219ce554eff998db4 to your computer and use it in GitHub Desktop.
{
"name": "Lat/Long Distance between a pair of points",
"description": "Extends a dataset with the distance in meters between pairs of lat/long fields.",
"inputs": [
{
"name": "dataset-in",
"type": "dataset-id",
"description": "Dataset for extending with distance calculation."
},
{
"name": "latA-field",
"type": "string",
"description": "The name/id of the latitude A field."
},
{
"name": "longA-field",
"type": "string",
"description": "The name/id of the longitude A field."
},
{
"name": "latB-field",
"type": "string",
"description": "The name/id of the latitude B field."
},
{
"name": "longB-field",
"type": "string",
"description": "The name/id of the longitude B field."
}
],
"outputs": [
{
"name": "dataset-out",
"type": "dataset-id",
"description": "The ID of the extended dataset"
}
]
}
; Computes the distance between pairs of lat/long coordinates.
; All lat/long coordinates must be in signed degrees (no N/W/S/E etc)
(define (latlong-pair-dist dataset-in latA-field longA-field latB-field longB-field)
(create-and-wait-dataset {
"origin_dataset" dataset-in
"new_fields" [
{
"field" ( flatline "( let ("
"R 6371000 "
"latA (to-radians ( field {{latA-field}}) )"
"latB (to-radians ( field {{latB-field}}) )"
"latD ( - latB latA ) "
"longD ( to-radians ( - ( field {{longA-field}} ) ( field {{longB-field}} ) ) )"
"a ( + ( square ( sin ( / latD 2 ) ) ) ( * (cos latA) (cos latB) (square ( sin ( / longD 2))) ) ) "
"c ( * 2 ( asin ( min (list 1 (sqrt a)) ) ) ) ) "
"( * R c ) )" )
"name" "Distance (m)"
}
]
}
)
)
(define dataset-out (latlong-pair-dist dataset-in latA-field longA-field latB-field longB-field))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment