-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchData.js
More file actions
62 lines (57 loc) · 2.21 KB
/
Copy pathfetchData.js
File metadata and controls
62 lines (57 loc) · 2.21 KB
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
import { CSV } from "https://js.sabae.cc/CSV.js";
import { parseGeometry } from "./parseGeometry.js";
import { DateTime } from "https://js.sabae.cc/DateTime.js";
const types = {
NORMAL: "t_travospublic_measure_5m",
CCTV: "t_travospublic_measure_5m_img",
};
export const fetchDataNormal = async (dt) => {
return await fetchData(types.NORMAL, dt);
};
export const fetchDataCCTV = async (dt) => {
return await fetchData(types.CCTV, dt);
};
export const fetchDataAll = async (dt) => {
const data1 = await fetchDataNormal(dt);
const data2 = await fetchDataCCTV(dt);
//data2.forEach(i => i.color = "blue");
const data = [...data1, ...data2];
return data;
};
const fetchData = async (type, dt) => { // dt: DateTime or YYMMDDhhmm
if (dt == null) {
dt = new DateTime();
}
if (dt instanceof DateTime) {
dt = dt.toStringMinLog();
}
//const url = `https://api.jartic-open-traffic.org/geoserver?service=WFS&version=2.0.0&request=GetFeature&typeNames=t_travospublic_measure_5m&srsName=EPSG:4326&outputFormat=text/csv&exceptions=application/json&cql_filter=道路種別=3 AND 時間コード=202505201245`;
const url = `https://api.jartic-open-traffic.org/geoserver?service=WFS&version=2.0.0&request=GetFeature&typeNames=${type}&srsName=EPSG:4326&outputFormat=text/csv&exceptions=application/json&cql_filter=時間コード=${dt}`;
//const csv = await (await fetch(url)).text();
const txt = await (await fetch(url)).json();
const csv = CSV.decode(txt);
// delete (集計値)
for (let i = 0; i < csv[0].length; i++) {
csv[0][i] = csv[0][i].replace("(集計値)", "");
csv[0][i] = csv[0][i].replace("小型大型判別不能交通量", "車種判別不能交通量");
}
const data = CSV.toJSON(csv);
//console.log(data);
data.forEach(i => {
delete i.FID;
const ll = parseGeometry(i.ジオメトリ);
delete i["上り・自動車交通量"];
delete i["下り・自動車交通量"];
delete i.ジオメトリ;
if (ll.length > 2) throw new Error("not supported geometry: " + i.ジオメトリ);
i.lat = ll[0][1];
i.lng = ll[0][0];
if (ll.length > 1) {
i.lat2 = ll[1][1];
i.lng2 = ll[1][0];
} else {
i.lat2 = i.lng2 = "";
}
});
return data;
};