generated from magda-io/magda-function-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
132 lines (118 loc) · 3.56 KB
/
index.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
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
import "isomorphic-fetch";
import URI from "urijs";
import isUrl from "is-url";
import { Record } from "@magda/registry-client";
import extent2WGS84 from "./extent2WGS84";
export type UrlProcessorResult = {
dataset: Omit<Record, "authnReadPolicyId">;
distributions: Omit<Record, "authnReadPolicyId">[];
};
const isNumber = (v: any) => typeof v === "number";
async function getSpatialCoverageAspect(data: any) {
const extentData = data?.extent
? data.extent
: data?.fullExtent
? data.fullExtent
: data?.initialExtent;
if (
!isNumber(extentData?.xmax) ||
!isNumber(extentData?.ymax) ||
!isNumber(extentData?.xmin) ||
!isNumber(extentData?.ymin)
) {
return undefined;
}
// -- Bounding box in order minlon (west), minlat (south), maxlon (east), maxlat (north)
let bbox = [
extentData.xmin,
extentData.ymin,
extentData.xmax,
extentData.ymax
];
// --- transform the coordinate system
if (extentData?.spatialReference) {
bbox = await extent2WGS84(bbox, extentData.spatialReference);
}
return {
bbox,
spatialDataInputMethod: "bbox"
};
}
export default async function processEsriApiUrl(
input: string
): Promise<UrlProcessorResult> {
if (!isUrl(input)) {
throw new Error("Expect the input to be an valid URL.");
}
const uri = URI(input);
const query = uri.search(true);
if (query?.f != "json") {
uri.search({ ...query, f: "json" });
}
const url = uri.toString();
const res = await fetch(uri.toString());
if (res.status !== 200) {
throw new Error(
`Failed to retrieve data from url : ${url} Error: ${res.statusText}`
);
}
const data = await res.json();
if (!data?.extent && !data?.fullExtent && !data?.initialExtent) {
throw new Error(
`Failed to locate any extent data from esri API: ${url}`
);
}
const spatialCoverageAspect = await getSpatialCoverageAspect(data);
const name = data?.name ? data.name : "";
const description = data?.description
? data.description
: data?.serviceDescription
? data.serviceDescription
: "";
const result: UrlProcessorResult = {
dataset: {
id: "",
name,
sourceTag: "",
tenantId: 0,
aspects: {
"dcat-dataset-strings": {
title: name,
description: description
},
source: {
id: "esri-url-processor",
type: "external",
url: uri.toString()
}
}
},
distributions: [
{
id: "",
name,
sourceTag: "",
tenantId: 0,
aspects: {
"dcat-distribution-strings": {
title: name,
description: description,
format: "ESRI REST"
},
source: {
id: "esri-url-processor",
type: "external",
url: uri.toString()
}
}
}
]
};
if (spatialCoverageAspect) {
result.dataset.aspects["spatial-coverage"] = spatialCoverageAspect;
result.distributions[0].aspects[
"spatial-coverage"
] = spatialCoverageAspect;
}
return result;
}