-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStrava.js
175 lines (151 loc) · 5.1 KB
/
Strava.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
model.js
This file is required. It must export a class with at least one public function called `getData`
Documentation: http://koopjs.github.io/docs/specs/provider/
*/
const request = require('request').defaults({ gzip: true, json: true })
const config = require('config')
const terraformer = require('terraformer')
function Strava (koop) {}
// Public function to return data from the
// Return: GeoJSON FeatureCollection
//
// Config parameters (config/default.json)
// req.
//
// URL path parameters:
// req.params.host (if index.js:hosts true)
// req.params.id (if index.js:disableIdParam false)
// req.params.layer
// req.params.method
Strava.prototype.getData = function (req, callback) {
const clientSecret = config.Strava.clientSecret
const clientId = config.Strava.clientId
const refreshToken = config.Strava.refreshToken
const initialExtent = config.Strava.initialExtent
request.post({
url: 'https://www.strava.com/oauth/token',
form: {
grant_type: 'refresh_token',
refresh_token: refreshToken,
client_id: clientId,
client_secret: clientSecret
}
}, function (err, httpResponse, body) {
if (err) {
console.log('request failed: ' + err)
return
}
var normalizedMin
var normalizedMax
var normalizedBounds
if (req.query && req.query.geometry) {
normalizedMin = terraformer.Tools.positionToGeographic([req.query.geometry.xmin, req.query.geometry.ymin])
normalizedMax = terraformer.Tools.positionToGeographic([req.query.geometry.xmax, req.query.geometry.ymax])
normalizedBounds = normalizedMin[1] + ',' +
normalizedMin[0] + ',' +
normalizedMax[1] + ',' +
normalizedMax[0]
}
var accessToken = body.access_token
var url = 'https://www.strava.com/api/v3/segments/explore'
var requestOptions = {
url: url,
form: {
activity_type: (req.query && req.query.activity_type) ? req.query.activity_type : 'riding',
min_cat: (req.query && req.query.min_cat) ? req.query.min_cat : 0,
max_cat: (req.query && req.query.max_cat) ? req.query.max_cat : 5,
bounds: (typeof normalizedBounds !== 'undefined') ? normalizedBounds : initialExtent,
access_token: accessToken
}
}
// Call the remote API with our developer key
request.get(requestOptions, (err, res, body) => {
if (err) return callback(err)
// translate the response into geojson
const geojson = translate(body.segments)
// Optional: cache data for 10 seconds at a time by setting the ttl or "Time to Live"
// geojson.ttl = 10
// Optional: Service metadata and geometry type
geojson.metadata = {
title: 'Koop Strava Provider',
name: 'Strava segments',
description: `Generated from ${url}`,
displayField: 'name',
idField: 'id',
maxRecordCount: 100,
geometryType: 'LineString' // Default is automatic detection in Koop
}
// hand off the data to Koop
callback(null, geojson)
})
})
}
function translate (input) {
return {
type: 'FeatureCollection',
features: input.map(formatFeature)
}
}
function formatFeature (inputFeature) {
// Most of what we need to do here is extract the longitude and latitude
const feature = {
type: 'Feature',
properties: inputFeature,
geometry: {
type: 'LineString',
coordinates: decodePoly(inputFeature.points)
}
}
return feature
}
function decodePoly (encodedString) {
var codeChunks = []
var codeChunk = []
var coord = []
var i; var x = 0; var y = 0
for (i = 0; i < encodedString.length; i++) {
codeChunk[x] = encodedString.charCodeAt(i) - 63
if ((codeChunk[x] & 0x20) === 0x0) {
codeChunk = codeChunk.reverse()
var j; var codeChunkString = ''
var zerosToAdd = 8 - ((codeChunk.length * 5) % 8)
for (j = 0; j < zerosToAdd; j++) {
codeChunkString = codeChunkString + '0'
}
for (j = 0; j < codeChunk.length; j++) {
codeChunk[j] = codeChunk[j].toString(2)
codeChunk[j] = '00000'.substr(codeChunk[j].length) + codeChunk[j]
codeChunkString = codeChunkString + codeChunk[j]
}
var codeChunkBin = parseInt(codeChunkString, 2)
if (codeChunkBin & 0x1) {
codeChunkBin = ~(codeChunkBin >> 1)
codeChunkBin = codeChunkBin - 0x1
codeChunkBin = ~(codeChunkBin >> 1)
codeChunkBin = codeChunkBin * -1
codeChunkBin = codeChunkBin << 1
} else {
codeChunkBin = codeChunkBin >> 1
}
codeChunkBin = codeChunkBin / 100000
coord[y % 2] = codeChunkBin
if ((y % 2) === 1) {
codeChunks[Math.floor(y / 2)] = coord.reverse()
if (y > 1) {
codeChunks[Math.floor(y / 2)][0] = codeChunks[Math.floor(y / 2)][0] + codeChunks[Math.floor(y / 2) - 1][0]
codeChunks[Math.floor(y / 2)][1] = codeChunks[Math.floor(y / 2)][1] + codeChunks[Math.floor(y / 2) - 1][1]
}
coord = []
}
y++
x = 0
codeChunk = []
} else {
codeChunk[x] = codeChunk[x] & 0x1F
x++
}
}
return codeChunks
}
module.exports = Strava