RouteSerializer

Bases: ModelSerializer

Source code in polarrouteserver/route_api/serializers.py
 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
class RouteSerializer(serializers.ModelSerializer):
    class Meta:
        model = Route
        fields = [
            "start_lat",
            "start_lon",
            "end_lat",
            "end_lon",
            "start_name",
            "end_name",
            "json",
            "json_unsmoothed",
            "polar_route_version",
            "info",
            "mesh",
        ]

    def to_representation(self, instance):
        """Returns unsmoothed routes if available when smoothed routes have failed."""
        data = super().to_representation(instance)

        smoothed = {}
        unsmoothed = {}
        data["json"] = []
        for key in ("traveltime", "fuel"):
            smoothed[key] = (
                [
                    x
                    for x in data["json"]
                    if x[0]["features"][0]["properties"]["objective_function"] == key
                ]
                if data["json"] is not None
                else []
            )
            unsmoothed[key] = (
                [
                    x
                    for x in data["json_unsmoothed"]
                    if x[0]["features"][0]["properties"]["objective_function"] == key
                ]
                if data["json_unsmoothed"] is not None
                else []
            )

            # if there is no smoothed route available, use unsmoothed in its place
            if len(smoothed[key]) == 0 and len(unsmoothed[key]) == 1:
                data["json"].extend(unsmoothed[key])
                data["info"] = {
                    "error": f"Smoothing failed for {key}-optimisation, returning unsmoothed route."
                }
            elif len(smoothed[key]) == 0 and len(unsmoothed[key]) == 0:
                data["info"] = {"error": f"No routes available for {key}-optimisation."}
            else:
                data["json"].extend(unsmoothed[key])

        return data

to_representation(instance)

Returns unsmoothed routes if available when smoothed routes have failed.

Source code in polarrouteserver/route_api/serializers.py
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
def to_representation(self, instance):
    """Returns unsmoothed routes if available when smoothed routes have failed."""
    data = super().to_representation(instance)

    smoothed = {}
    unsmoothed = {}
    data["json"] = []
    for key in ("traveltime", "fuel"):
        smoothed[key] = (
            [
                x
                for x in data["json"]
                if x[0]["features"][0]["properties"]["objective_function"] == key
            ]
            if data["json"] is not None
            else []
        )
        unsmoothed[key] = (
            [
                x
                for x in data["json_unsmoothed"]
                if x[0]["features"][0]["properties"]["objective_function"] == key
            ]
            if data["json_unsmoothed"] is not None
            else []
        )

        # if there is no smoothed route available, use unsmoothed in its place
        if len(smoothed[key]) == 0 and len(unsmoothed[key]) == 1:
            data["json"].extend(unsmoothed[key])
            data["info"] = {
                "error": f"Smoothing failed for {key}-optimisation, returning unsmoothed route."
            }
        elif len(smoothed[key]) == 0 and len(unsmoothed[key]) == 0:
            data["info"] = {"error": f"No routes available for {key}-optimisation."}
        else:
            data["json"].extend(unsmoothed[key])

    return data