-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgps_to_kml.py
More file actions
89 lines (69 loc) · 3.27 KB
/
gps_to_kml.py
File metadata and controls
89 lines (69 loc) · 3.27 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
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
# MIT License
#
# Copyright (c) 2024 Space Robotics Lab at UMA
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Save GPS coordinates as KML."""
__author__ = "Levin Gerdes"
import argparse
import os
from typing import Any, List, Tuple
import simplekml # type: ignore
from rosbags.rosbag2 import Reader
from rosbags.serde import deserialize_cdr
def get_args() -> argparse.Namespace:
"""Parses CLI arguments"""
parser = argparse.ArgumentParser(description="")
# fmt: off
parser.add_argument("--input-path", "-i", type=str, default="~/data/bardenas2023/dataset", dest="input_path", help="Path of input mcaps")
parser.add_argument("--output-path", "-o", type=str, default="~/data/bardenas2023/extracted", dest="output_path", help="Output will be written here")
# fmt: on
return parser.parse_args()
def get_message_and_timestamp(connection, rawdata) -> Tuple[Any, int]:
"""Returns the deserialized message and its timestamp"""
msg = deserialize_cdr(rawdata, connection.msgtype)
timestamp = int(str(msg.header.stamp.sec) + str(msg.header.stamp.nanosec).zfill(9))
return msg, timestamp
if __name__ == "__main__":
args = get_args()
INPUT_PATH: str = os.path.expanduser(args.input_path)
OUTPUT_PATH: str = os.path.expanduser(args.output_path)
print(f"Input path: {INPUT_PATH}\nOutput path: {OUTPUT_PATH}")
# Get names of subdirectories in INPUT_PATH
# E.g. "2023-07-20_18-25-21"
SUBDIRS: List[str] = [
directory
for directory in os.listdir(INPUT_PATH)
if os.path.isdir(os.path.join(INPUT_PATH, directory))
]
for index, bag in enumerate(SUBDIRS):
kml = simplekml.Kml()
linestring = kml.newlinestring(name=bag)
coords = []
with Reader(os.path.join(INPUT_PATH, bag)) as reader:
print(bag)
for connection, timestamp, rawdata in reader.messages():
if connection.topic == "/gnss":
msg, time = get_message_and_timestamp(connection, rawdata)
coords.append((msg.longitude, msg.latitude))
if msg.status.status < 0:
print("Message without GNSS Fix")
linestring.coords = coords
print("Saving to disk.")
kml.save(os.path.join(OUTPUT_PATH, bag + ".kml"))