Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add pcd map hash generator (#745) #130

Merged
merged 3 commits into from
Dec 8, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 41 additions & 2 deletions map/map_loader/script/map_hash_generator
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,15 @@ class MapHashGenerator(Node):
super().__init__("map_hash_generator")
self.lanelet_path = self.declare_parameter("lanelet2_map_path", "").value
self.lanelet_text = self.load_lanelet_text(self.lanelet_path)
self.lanelet_hash = self.generate_file_hash(self.lanelet_text)
self.lanelet_hash = self.generate_lanelet_file_hash(self.lanelet_text)

self.pcd_map_path = self.declare_parameter("pointcloud_map_path", "").value
self.pcd_map_hash = self.generate_pcd_file_hash(self.pcd_map_path)

qos_profile = QoSProfile(depth=1, durability=QoSDurabilityPolicy.TRANSIENT_LOCAL)
msg = MapHash()
msg.lanelet = self.lanelet_hash
msg.pcd = self.pcd_map_hash
self.pub = self.create_publisher(MapHash, "/api/autoware/get/map/info/hash", qos_profile)
self.pub.publish(msg)

Expand All @@ -54,9 +58,44 @@ class MapHashGenerator(Node):
return path.read_text() if path.is_file() else ""

@staticmethod
def generate_file_hash(data):
def generate_lanelet_file_hash(data):
return hashlib.sha256(data.encode()).hexdigest() if data else ""

def update_hash(self, m, path):
try:
with open(path, "rb") as pcd_file:
m.update(pcd_file.read())
except FileNotFoundError as e:
self.get_logger().error(e)
return False
return True

def generate_pcd_file_hash(self, path):
path = pathlib.Path(path)
if path.is_file():
if not path.suffix == ".pcd":
self.get_logger().error(f"[{path}] is not pcd file")
return ""
m = hashlib.sha256()
if not self.update_hash(m, path):
return ""
return m.hexdigest()

if path.is_dir():
m = hashlib.sha256()
for pcd_file_path in sorted(path.iterdir()):
if not pcd_file_path.suffix == ".pcd":
continue
if not self.update_hash(m, pcd_file_path):
return ""
if m.hexdigest() == hashlib.sha256().hexdigest():
self.get_logger().error(f"there are no pcd files in [{path}]")
return ""
return m.hexdigest()

self.get_logger().error(f"[{path}] is neither file nor directory")
return ""


def main(args=None):
rclpy.init(args=args)
Expand Down