-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmapkit-snapshots.js
executable file
·333 lines (279 loc) · 10.9 KB
/
mapkit-snapshots.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/env node
/**
* Adapted from:
* https://developer.apple.com/documentation/snapshots/generating_a_url_and_signature_to_create_a_maps_web_snapshot
*/
// MARK: - Requires, Vars
const { readFileSync } = require("fs");
var fs = require('fs')
const { sign } = require("jwa")("ES256"); // https://www.npmjs.com/package/jwa
const opn = require('better-opn'); // https://www.npmjs.com/package/better-opn
var argv = require('minimist')(process.argv.slice(2)) // parse argument options
var concat = require('concat-stream')
var stdin
var usage = "Usage:\n mapkit-snapshots.js <file.geojson> -c config.json # -c pass in privateKey, teamId, keyId\n mapkit-snapshots.js <file.geojson> -c config.json -o # -o opens in default browser"
// Setup an object to write out with some reasonable defaults
var snapshot = {};
snapshot.config = {}
snapshot.url = {"signature": "", "length": "", "text": ""}
// Big vars
var annotations = [], encodeAnnotations = [];
var overlays = [], encodeOverlays = [];
var imgs = [], encodeImgs = [];
var signThis;
var openUrl;
var privateKey, teamId, keyId;
// MARK: - Process CLI
if (argv.help || argv.h) {
console.log(usage)
process.exit()
}
// process `config.json`
// -c pass in privateKey, teamId, keyId
/* Read your private key from the file system. (Never add your private key
* in code or in source control. Always keep it secure.)
*/
if (argv.c) {
var config;
configJson = fs.createReadStream(argv.c)
configJson.pipe(
concat( function (buffer) {
try {
config = JSON.parse(buffer)
privateKey = readFileSync(config.privateKey);
teamId = config.teamId;
keyId = config.keyId;
// update snapshot
snapshot.config = config
} catch (e) {
return console.error(e)
}
})
);
}
// -o opens in default browser
if (argv.o) {
openUrl = true;
}
if (argv._[0] && argv._[0] !== '-') {
stdin = fs.createReadStream(argv._[0])
} else if (!process.stdin.isTTY || argv._[0] === '-') {
stdin = process.stdin
} else {
console.log(usage)
process.exit(1)
}
// MARK: - Process GeoJSON
// Each query parameter must be URL-encoded.
stdin.pipe(
concat( function (buffer) {
try {
var geojson = JSON.parse(buffer)
} catch (e) {
return console.error(e)
}
geojson.features.forEach(iterateMapKit);
// images
snapshot.imgs = {};
if(Object.keys(imgs).length > 0) {
snapshot.imgs = JSON.stringify(imgs);
encodeImgs = encodeURIComponent(JSON.stringify(imgs));
signThis += `imgs=${encodeImgs}&`
}
// MARK: annotations
snapshot.annotationData = {};
if(Object.keys(annotations).length > 0) {
snapshot.annotationData.annotation = JSON.stringify(annotations);
encodeAnnotations = encodeURIComponent(JSON.stringify(annotations));
snapshot.annotationData.encodeURI = encodeAnnotations;
snapshot.annotationData.annotationCount = annotations.length;
signThis += `annotations=${encodeAnnotations}&`
}
// overlays
snapshot.overlays = {};
if(Object.keys(overlays).length > 0) {
snapshot.overlays.overlay = JSON.stringify(overlays);
encodeOverlays = encodeURIComponent(JSON.stringify(overlays));
snapshot.overlays.encodeURI = encodeOverlays;
signThis += `overlays=${encodeOverlays}&`
}
try {
console.log()
signIt(geojson.name, signThis)
} catch (e) {
console.error("signIt Error: \n\t*** Please verify the settings in config.json\n")
return console.error(e)
} finally {
}
})
)
// MARK: - iterateMapKit()
// https://developer.apple.com/documentation/snapshots/create_a_maps_web_snapshot#query-parameters
function iterateMapKit(element, index, array) {
// Use the first `feature` for setting up parameters that are only used once (center, language, etc)
// TODO: should be moved to FeatureCollection root
if (index === 0) {
// Determine the center point of the static map in this order
// 1. The IMDF property, properties.display_point
// 2. properties.center
// 3. If Point, then use Point
var lat, long;
if ("display_point" in element.properties) {
point = coordinate2point(element.properties.display_point.coordinates)
} else if ('center' in element.properties) {
if (typeof(element.properties.center) === "string") {
// The input property for `center` can be
// A geocoded address is a valid value for the `center` parameter
// "The string `auto` is also a valid value for the `center` parameter
point = encodeURIComponent(element.properties.center) /// TODO: review if we can set precision
} else {
// [long, lat] array of floats, to make it easy to reuse the GeoJSON formatting for points
point = coordinate2point(element.properties.center.coordinates)
}
} else if(element.geometry.type === "Point"){
// the `center` parameter was not set, fall back to the geometry
point = coordinate2point(element.geometry.coordinates)
} else {
console.error("error: ❌ No center `point` is defined. Please set `properties.display_point` (IMDF style), or `properties.center`")
}
signThis = `center=${point}&`;
snapshot.center = `${point}`;
// Handle cases where both `z` &`spn` are present.
// Apple > When both z and spn are provided, spn takes precedence over z.
if ('spn' in element.properties) {
latDegrees = element.properties.spn[0];
lonDegrees = element.properties.spn[1];
spn = `${latDegrees},${lonDegrees}`;
signThis += `spn=${spn}&`;
snapshot.spn = spn;
} else if ('z' in element.properties) {
if(element.properties.z < 3 || element.properties.z > 20) {
element.properties.z = 12
console.error("Out of bounds zoom level; setting to Default\nDefault: 12\nMinimum: 3\nMaximum: 20")
}
signThis += `z=${element.properties.z}&`;
snapshot.z = element.properties.z;
}
if ('size' in element.properties) {
width = element.properties.size[0];
height = element.properties.size[1];
size = `${width}x${height}`;
signThis += `size=${size}&`;
snapshot.size = size;
}
if ('scale' in element.properties) {
signThis += `scale=${element.properties.scale}&`;
snapshot.scale = element.properties.scale;
}
if ('t' in element.properties & element.properties.t != "") {
signThis += `t=${element.properties.t}&`;
snapshot.t = element.properties.t;
if(element.properties.t === "standard" ||
element.properties.t === "mutedStandard") {
if ('colorScheme' in element.properties & element.properties.colorScheme != "") {
signThis += `colorScheme=${element.properties.colorScheme}&`;
snapshot.colorScheme = element.properties.colorScheme;
}
}
}
if ('poi' in element.properties) {
signThis += `poi=${element.properties.poi}&`;
snapshot.poi = element.properties.poi;
}
if ('lang' in element.properties & element.properties.lang != "") {
signThis += `lang=${element.properties.lang}&`;
snapshot.lang = element.properties.lang;
}
if ('referer' in element.properties & element.properties.referer != "") {
signThis += `referer=${element.properties.referer}&`;
snapshot.referer = element.properties.referer;
}
if ('expires' in element.properties) {
signThis += `expires=${element.properties.expires}&`;
snapshot.expires = element.properties.expires;
}
}
// MARK: - annotations
// creates: [Annotation]
if (element.properties.annotation) {
// creates: [Image], An array of custom images to annotate the map, specified as an array of JSON Image objects.
if (element.properties.imgs) {
imgs = element.properties.imgs
}
if (element.geometry.type === "MultiPoint") {
for (var mp = 0; mp < element.geometry.coordinates.length; mp++) {
annotation = element.properties.annotation[mp]
lat = element.geometry.coordinates[mp][1]
long = element.geometry.coordinates[mp][0]
point = `${lat},${long}`
annotation.point = point
// remove unused parameters to make the URL smaller
if(annotation.offset === "0,0") {
delete annotation.offset
}
if(annotation.glyphText === "") {
delete annotation.glyphText
}
if(annotation.markerStyle === "dot") {
delete annotation.glyphText
}
if(annotation.markerStyle === "img") {
delete annotation.color
delete annotation.glyphText
} else {
// remove unused that are related to `img`
delete annotation.imgIdx
delete annotation.offset
}
// set the annotation
annotations.push(annotation)
snapshot.annotationSource = element.geometry.type;
annotation = {}
}
} // if MultiPoint
// TODO: if no points use `img`, then remove the `image` array
} // if annotation
// MARK: - overlays
// creates: [Overlay]
if (element.properties.hasOwnProperty("overlay")) {
points = [];
// Iterate over each Point in the LineString in the `overlay`
for (let coord = 0; coord < element.properties.overlay.geometry.coordinates.length; coord++) {
points.push(coordinate2point(element.properties.overlay.geometry.coordinates[coord]));
}
// An array of overlays to be displayed on the map, specified as an array of JSON Overlay objects.
overlays.push({
"points": points,
"strokeColor": element.properties.overlay.style.strokeColor,
"lineWidth": element.properties.overlay.style.lineWidth,
"lineDash": element.properties.overlay.style.lineDash
});
} // If `overlay` exists
} // iterateMapKit
// MARK: - signIt(name, params)
// Creates the signature string and returns the full Snapshot request URL including the signature.
function signIt(name, params) {
var mapkitServer = `https://snapshot.apple-mapkit.com`
var snapshotPath = `/api/v1/snapshot?${params}`; // snapshotPath is assumed to have the trailing '&'
var completePath = `${snapshotPath}teamId=${teamId}&keyId=${keyId}`;
const signature = sign(completePath, privateKey);
// Append the signature to the end of the request URL, and return.
url = `${mapkitServer}${completePath}&signature=${signature}`
snapshot.url.text = url;
snapshot.url.exec = "imagefrom " + name + " \"" + url + "\"";
snapshot.url.signature = signature;
snapshot.url.length = url.length;
console.log(JSON.stringify(snapshot, null, 2));
// Optionally open the result in the default browser using `opn`
if(openUrl) {
opn(snapshot.url.text);
}
return url;
}
// Return a GeoJSON Coordinate as a string in "lat,long" format
function coordinate2point(coordinate) {
lat = coordinate[1];
long = coordinate[0];
point = `${lat},${long}`;
return point
}