sumolib.output.convert.keplerjson

This module includes functions for converting SUMO's fcd-output into kepler.gl JSON format (https://github.com/keplergl/kepler.gl/blob/master/docs/user-guides/c-types-of-layers/k.trip.md)

 1# Eclipse SUMO, Simulation of Urban MObility; see https://eclipse.dev/sumo
 2# Copyright (C) 2014-2026 German Aerospace Center (DLR) and others.
 3# This program and the accompanying materials are made available under the
 4# terms of the Eclipse Public License 2.0 which is available at
 5# https://www.eclipse.org/legal/epl-2.0/
 6# This Source Code may also be made available under the following Secondary
 7# Licenses when the conditions for such availability set forth in the Eclipse
 8# Public License 2.0 are satisfied: GNU General Public License, version 2
 9# or later which is available at
10# https://www.gnu.org/licenses/old-licenses/gpl-2.0-standalone.html
11# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-or-later
12
13# @file    keplerjson.py
14# @author  Michael Behrisch
15# @date    2019-11-01
16
17"""
18This module includes functions for converting SUMO's fcd-output into
19kepler.gl JSON format (https://github.com/keplergl/kepler.gl/blob/master/docs/user-guides/c-types-of-layers/k.trip.md)
20"""
21from __future__ import absolute_import
22from collections import defaultdict
23import json
24
25
26def makeFeature(vehId, coords):
27    return {
28        "type": "Feature",
29        "properties": {
30            "vehId": vehId
31        },
32        "geometry": {
33            "type": "LineString",
34            "coordinates": coords
35        }
36    }
37
38
39def fcd2keplerjson(inpFCD, outStream, further):
40    tracks = defaultdict(list)
41    baseDate = further['base-date'].timestamp()
42    for timestep in inpFCD:
43        for v in timestep.vehicle:
44            tracks[v.id].append([v.x, v.y, v.z, timestep.time + baseDate])
45    jsonRoot = {
46        "type": "FeatureCollection",
47        "features": [makeFeature(*veh) for veh in tracks.items()]
48    }
49    json.dump(jsonRoot, outStream)
def makeFeature(vehId, coords):
27def makeFeature(vehId, coords):
28    return {
29        "type": "Feature",
30        "properties": {
31            "vehId": vehId
32        },
33        "geometry": {
34            "type": "LineString",
35            "coordinates": coords
36        }
37    }
def fcd2keplerjson(inpFCD, outStream, further):
40def fcd2keplerjson(inpFCD, outStream, further):
41    tracks = defaultdict(list)
42    baseDate = further['base-date'].timestamp()
43    for timestep in inpFCD:
44        for v in timestep.vehicle:
45            tracks[v.id].append([v.x, v.y, v.z, timestep.time + baseDate])
46    jsonRoot = {
47        "type": "FeatureCollection",
48        "features": [makeFeature(*veh) for veh in tracks.items()]
49    }
50    json.dump(jsonRoot, outStream)