fzi-forschungszentrum-informatik / Lanelet2

Map handling framework for automated driving
BSD 3-Clause "New" or "Revised" License
800 stars 327 forks source link

How to do the same job as BinParser in lanelet2_io in Python #313

Closed OliverLeeyiyang closed 1 year ago

OliverLeeyiyang commented 1 year ago

Hi,

I am now doing a project that rewrites part of map_based_prediction in Autoware, which uses lanelet2 to deal with the map data. The problem that I am facing is that one of the methods in map_based_prediction is like this:

void fromBinMsg(const autoware_auto_mapping_msgs::msg::HADMapBin & msg, lanelet::LaneletMapPtr map)
{
  if (!map) {
    std::cerr << __FUNCTION__ << ": map is null pointer!";
    return;
  }

  std::string data_str;
  data_str.assign(msg.data.begin(), msg.data.end());

  std::stringstream ss;
  ss << data_str;
  boost::archive::binary_iarchive oa(ss);
  oa >> *map;
  lanelet::Id id_counter = 0;
  oa >> id_counter;
  lanelet::utils::registerId(id_counter);
}

which has some similar part with BinParser in lanelet2_io:

std::unique_ptr<LaneletMap> BinParser::parse(const std::string& filename, ErrorMessages& /*errors*/) const {
  std::ifstream fs(filename, std::ifstream::binary);
  if (!fs.good()) {
    throw ParseError("Failed open archive " + filename);
  }
  std::unique_ptr<LaneletMap> laneletMap = std::make_unique<LaneletMap>();
  boost::archive::binary_iarchive ia(fs);
  ia >> *laneletMap;
  Id idCounter = 0;
  ia >> idCounter;
  utils::registerId(idCounter);
  return laneletMap;
}

Especially these two lines:

boost::archive::binary_iarchive ia(fs);
oa >> *map;

I want to know if you have some ideas about how to do the same thing of these two lines in Python.

And I have tried to use the code in python examples like this:

data_list = msg.data.tolist()
for d in data_list:
     a_lanelet = self.get_a_lanelet(d)
     self.lanelet_map.add(a_lanelet)

It can work, but seems that lost the attributes of lanelet when I deal LaneletMap later.:( What's more, the data_list here is like a list that contains int numbers:

[22, 0, 0, 0, 0, 0, 0, 0, 115, 101, 114, 105, 97, 108, 105, 122, 97,...]

Thank you in advance!

immel-f commented 1 year ago

Sorry for the late reply. I am not sure if there is a way to directly replicate the way used in the Autoware C++ version, as it uses some C++ and Boost binary handling features. Maybe using Pickle instead could work? However we have not tested this.

kennethweitzel commented 10 months ago

Hey @OliverLeeyiyang, I have the same problem and I just wanted to know if you were able to resolve this issue and if yes, how?

OliverLeeyiyang commented 10 months ago

Hey @OliverLeeyiyang, I have the same problem and I just wanted to know if you were able to resolve this issue and if yes, how?

Hi @kennethweitzel, if the problem that you face is the deserialization of the message in Python, unfortunately, I didn't find a solution to it.

However, if your question is about the use of the map information from the serialized topic in Python, you could try to find how the topic is serialized and try to publish( or load map) it on your own. Here is my solution: https://github.com/OliverLeeyiyang/Integration-Autoware_map_based_prediction/blob/main/src/tum_prediction/tum_prediction/map_loader.py.

kennethweitzel commented 10 months ago

Thanks a lot! Unfortunately I need the map to be loaded in the MGRS coordinate system, which this lanelet package alone is not capable of. That's why my approach was to subscribe to the HADMapBin topic, as it was loaded under Autoware in MGRS format and then published (which is possible with the autoware lanelet2 extension). Since then I'm looking for a way to load the map in MGRS in Python.

OliverLeeyiyang commented 10 months ago

Thanks a lot! Unfortunately I need the map to be loaded in the MGRS coordinate system, which this lanelet package alone is not capable of. That's why my approach was to subscribe to the HADMapBin topic, as it was loaded under Autoware in MGRS format and then published (which is possible with the autoware lanelet2 extension). Since then I'm looking for a way to load the map in MGRS in Python.

You are welcome! To do so, you could try to see this #242 and create a MGRS projector in Python on your own. I didn't try this since I didn't have enough time when I did this project.

Another way to solve it could be using Boost-Python to create a MGRS LaneletMap generator based on autoware source code and run in Python. And you need to define operator>> in it to generate the LaneletMap from the message. But since some part of the MGRS projector in autoware had been edited (such as adding new lanelet tags), you may need to be careful of the map you want to load.

If you finally find a good solution, please also let me know :)