|
| 1 | +#!/bin/env python3 |
| 2 | + |
| 3 | +# Copyright 2024 TIER IV, Inc. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +import argparse |
| 18 | +from decimal import Decimal |
| 19 | +import os |
| 20 | +import subprocess |
| 21 | +import xml.etree.ElementTree as ET |
| 22 | + |
| 23 | + |
| 24 | +def sort_attributes(root): |
| 25 | + for shallow_element in root: |
| 26 | + # sort nodes |
| 27 | + attrib = shallow_element.attrib |
| 28 | + if len(attrib) > 1: |
| 29 | + attributes = sorted(attrib.items()) |
| 30 | + attrib.clear() |
| 31 | + attrib.update(attributes) |
| 32 | + if shallow_element.tag == "relation": |
| 33 | + pass |
| 34 | + |
| 35 | + # sort the element in the tag |
| 36 | + for deep_element in shallow_element: |
| 37 | + attrib = deep_element.attrib |
| 38 | + if len(attrib) > 1: |
| 39 | + # adjust attribute order, e.g. by sorting |
| 40 | + attributes = sorted(attrib.items()) |
| 41 | + attrib.clear() |
| 42 | + attrib.update(attributes) |
| 43 | + |
| 44 | + # sort tags |
| 45 | + sorted_shallow_element = sorted(shallow_element, key=lambda x: x.items()) |
| 46 | + if len(shallow_element) != 0: |
| 47 | + # NOTE: This "tail" is related to the indent of the next tag |
| 48 | + first_tail = shallow_element[0].tail |
| 49 | + last_tail = shallow_element[-1].tail |
| 50 | + for idx, val_shallow_element in enumerate(sorted_shallow_element): |
| 51 | + shallow_element[idx] = val_shallow_element |
| 52 | + if idx == len(shallow_element) - 1: |
| 53 | + shallow_element[idx].tail = last_tail |
| 54 | + else: |
| 55 | + shallow_element[idx].tail = first_tail |
| 56 | + |
| 57 | + |
| 58 | +def remove_diff_to_ignore(osm_root): |
| 59 | + decimal_precision = 11 # for lat/lon values |
| 60 | + |
| 61 | + # remove attributes of the osm tag |
| 62 | + osm_root.attrib.clear() |
| 63 | + |
| 64 | + # remove the MetaInfo tag generated by Vector Map Builder |
| 65 | + if osm_root[0].tag == "MetaInfo": |
| 66 | + osm_root.remove(osm_root[0]) |
| 67 | + |
| 68 | + # remove unnecessary attributes for diff |
| 69 | + for osm_child in osm_root: |
| 70 | + if osm_child.tag == "osm": |
| 71 | + osm_child.attrib.pop("osm") |
| 72 | + if "visible" in osm_child.attrib and osm_child.attrib["visible"]: |
| 73 | + osm_child.attrib.pop("visible") |
| 74 | + if "version" in osm_child.attrib and osm_child.attrib["version"]: |
| 75 | + osm_child.attrib.pop("version") |
| 76 | + if "action" in osm_child.attrib and osm_child.attrib["action"] == "modify": |
| 77 | + osm_child.attrib.pop("action") |
| 78 | + if "lat" in osm_child.attrib: |
| 79 | + osm_child.attrib["lat"] = str( |
| 80 | + Decimal(float(osm_child.attrib["lat"])).quantize(Decimal(f"1e-{decimal_precision}")) |
| 81 | + ) |
| 82 | + if "lon" in osm_child.attrib: |
| 83 | + osm_child.attrib["lon"] = str( |
| 84 | + Decimal(float(osm_child.attrib["lon"])).quantize(Decimal(f"1e-{decimal_precision}")) |
| 85 | + ) |
| 86 | + |
| 87 | + |
| 88 | +if __name__ == "__main__": |
| 89 | + parser = argparse.ArgumentParser() |
| 90 | + parser.add_argument( |
| 91 | + "-s", "--sort-attributes", action="store_true", help="Sort attributes of LL2 maps" |
| 92 | + ) |
| 93 | + parser.add_argument( |
| 94 | + "-i", |
| 95 | + "--ignore-minor-attributes", |
| 96 | + action="store_true", |
| 97 | + help="Ignore minor attributes of LL2 maps which does not change the map's behavior", |
| 98 | + ) |
| 99 | + args = parser.parse_args() |
| 100 | + |
| 101 | + original_osm_file_name = "/tmp/static_centerline_generator/input/lanelet2_map.osm" |
| 102 | + modified_osm_file_name = "/tmp/static_centerline_generator/output/lanelet2_map.osm" |
| 103 | + |
| 104 | + # load LL2 maps |
| 105 | + original_osm_tree = ET.parse(original_osm_file_name) |
| 106 | + original_osm_root = original_osm_tree.getroot() |
| 107 | + modified_osm_tree = ET.parse(modified_osm_file_name) |
| 108 | + modified_osm_root = modified_osm_tree.getroot() |
| 109 | + |
| 110 | + # sort attributes |
| 111 | + if args.sort_attributes: |
| 112 | + sort_attributes(modified_osm_root) |
| 113 | + sort_attributes(original_osm_root) |
| 114 | + |
| 115 | + # ignore minor attributes |
| 116 | + if args.ignore_minor_attributes: |
| 117 | + remove_diff_to_ignore(original_osm_root) |
| 118 | + remove_diff_to_ignore(modified_osm_root) |
| 119 | + |
| 120 | + # write LL2 maps |
| 121 | + output_dir_path = "/tmp/static_centerline_generator/show_lanelet2_map_diff/" |
| 122 | + os.makedirs(output_dir_path + "original/", exist_ok=True) |
| 123 | + os.makedirs(output_dir_path + "modified/", exist_ok=True) |
| 124 | + |
| 125 | + original_osm_tree.write(output_dir_path + "original/lanelet2_map.osm") |
| 126 | + modified_osm_tree.write(output_dir_path + "modified/lanelet2_map.osm") |
| 127 | + |
| 128 | + # show diff |
| 129 | + print("[INFO] Show diff of following LL2 maps") |
| 130 | + print(f" {output_dir_path + 'original/lanelet2_map.osm'}") |
| 131 | + print(f" {output_dir_path + 'modified/lanelet2_map.osm'}") |
| 132 | + subprocess.run( |
| 133 | + [ |
| 134 | + "diff", |
| 135 | + output_dir_path + "original/lanelet2_map.osm", |
| 136 | + output_dir_path + "modified/lanelet2_map.osm", |
| 137 | + "--color", |
| 138 | + ] |
| 139 | + ) |
0 commit comments