TrafficConfig.ts 2.84 KB
Newer Older
Hanadi's avatar
Hanadi committed
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
import {ST_WITHIN_FILTER} from "./CommonConfig";
import {encodeFrostId} from "./Helpers";

const FROST_SERVER_URL = "http://192.168.178.27:8080/FROST-Server/v1.1";
// const FROST_SERVER_URL = "http://localhost:8080/FROST-Server/v1.1";
const FEATURES_OF_INTEREST_URL = `${FROST_SERVER_URL}/FeaturesOfInterest`;
const GEO_JSON_DATA_URL = `${FEATURES_OF_INTEREST_URL}?$top=1000&$resultFormat=GeoJSON`;

export function getTrafficGeoJsonUrlForFeaturesWithinBBox(): string {
    let filterQuery = `&$filter=${ST_WITHIN_FILTER}`;

    return `${GEO_JSON_DATA_URL}${filterQuery}`;
}

export function getObservationUrlForFeatureOfInterest(
    featureOfInterestId: string,
    fromDate?: Date,
    toDate?: Date,
    fromSpeed?: number,
    toSpeed?: number,
    fromJamFactor?: number,
    toJamFactor?: number,
    limitObservations?: number,
    skipObservations?: number,
): string {
    const conditions: string[] = [];

    if (fromSpeed) {
        conditions.push(`result ge ${fromSpeed}`);
    }
    if (toSpeed) {
        conditions.push(`result le ${toSpeed}`);
    }
    if (fromDate) {
        conditions.push(`phenomenonTime ge ${fromDate.toISOString()}`);
    }
    if (toDate) {
        conditions.push(`phenomenonTime le ${toDate.toISOString()}`);
    }
    if (fromJamFactor) {
        conditions.push(`parameters/jamFactor ge ${fromJamFactor}`);
    }
    if (toJamFactor) {
        conditions.push(`parameters/jamFactor le ${toJamFactor}`);
    }

    let filter = "";
    if (conditions.length) {
        filter = "$filter=" + conditions.join(" and ");
    }

    const orderBy = "$orderBy=phenomenonTime+DESC";
    const encodedId = encodeFrostId(featureOfInterestId);
    const limit = `$top=${limitObservations || 1}`;
    const skip = skipObservations ? `$skip=${skipObservations}` : "";
    const select = "$select=result,parameters,resultQuality";
    const queryParams = [select, filter, orderBy, limit, skip].filter(it => it !== "").join("&");
    return `${FEATURES_OF_INTEREST_URL}('${encodedId}')/Observations?${queryParams}`.replaceAll(" ", "+");
}

export function getTrafficHistoryUrl(
    id: string,
    fromDate: Date,
    toDate: Date,
    limitObservations?: number,
) {
    const conditions: string[] = [];
    conditions.push(`phenomenonTime ge ${fromDate.toISOString()}`);
    conditions.push(`phenomenonTime le ${toDate.toISOString()}`);

    let filter = "";
    if (conditions.length) {
        filter = "$filter=" + conditions
            .map(condition => `${condition}`)
            .join(" and ");
    }

    const orderBy = "$orderBy=phenomenonTime";
    const encodedId = encodeFrostId(id);
    const limit = `$top=${limitObservations || 100}`;
    const select = "$select=phenomenonTime,result,parameters";
    const queryParams = [select, filter, orderBy, limit].join("&");
    return `${FEATURES_OF_INTEREST_URL}('${encodedId}')/Observations?${queryParams}`;
}