-
Notifications
You must be signed in to change notification settings - Fork 0
/
get-image-url.ts
101 lines (89 loc) · 2.07 KB
/
get-image-url.ts
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
import type { HipsOptions } from "./hips-options";
import { parseWcs } from "./parse-wcs";
/**
* Service URL.
* @internal
*/
const DEFAULT_ENDPOINT =
"http://alasky.u-strasbg.fr/hips-image-services/hips2fits";
/**
* Get an image URL from the hips2fits service.
*
* @example
*
* ```js
* import {
* getImageUrl,
* HipsImageFormat,
* HipsProjection,
* HipsService,
* HipsStretch,
* HipsCoordsys,
* } from "hips2fits-js";
*
* let url;
* let width = 1024;
* let height = 512;
*
* url = getImageUrl({
* hips: HipsService.CDS_P_DSS2_color,
* coordsys: HipsCoordsys.Icrs,
* projection: HipsProjection.Car,
* stretch: HipsStretch.Asinh,
* format: HipsImageFormat.Png,
* width,
* height,
* ra: 0,
* dec: 0,
* fov: 360,
* });
*
* let img = new Image();
* img.width = width;
* img.height = height;
* img.src = url;
*
* window.addEventListener("load", function () {
* window.document.body.appendChild(img);
* });
* ```
*
* @param options Options object configuring the Hips image generated.
* @param endPoint Base URL endpoint.
*/
export function getImageUrl(
options: HipsOptions,
endPoint = DEFAULT_ENDPOINT
): string {
let urlString = endPoint + "?";
let entries = Object.entries(options);
entries.forEach((el, i) => {
if (typeof el[0] === "undefined") {
throw new Error("getImageUrl: Undefined option key!");
}
if (typeof el[1] === "undefined") {
throw new Error("getImageUrl: Undefined option value!");
}
if (typeof el[1] !== "undefined") {
switch (el[0]) {
case "wcs":
let wcs;
if (typeof el[1] === "object") {
wcs = JSON.stringify(el[1]);
}
if (typeof el[1] === "string") {
wcs = JSON.stringify(parseWcs(el[1]));
}
urlString += el[0] + "=" + encodeURIComponent(wcs);
break;
default:
urlString += `${el[0]}=${encodeURIComponent(el[1])}`;
break;
}
if (i < entries.length - 1) {
urlString += "&";
}
}
});
return urlString;
}