33 nav_msgs::OccupancyGrid
loadMap(
const std::string& fname) {
35 nav_msgs::GetMap::Response map_resp_;
37 std::string mapfname =
"";
38 std::ifstream fin(fname.c_str());
42 double occ_th, free_th;
43 std::string frame_id =
"map";
45 #ifdef HAVE_NEW_YAMLCPP 47 YAML::Node doc = YAML::Load(fin);
51 parser.GetNextDocument(doc);
54 doc[
"resolution"] >> res;
55 }
catch (YAML::InvalidScalar) {
57 "The map does not contain a resolution tag or it is invalid.");
61 doc[
"negate"] >> negate;
62 }
catch (YAML::InvalidScalar) {
63 ROS_ERROR(
"The map does not contain a negate tag or it is invalid.");
67 doc[
"occupied_thresh"] >> occ_th;
68 }
catch (YAML::InvalidScalar) {
70 "The map does not contain an occupied_thresh tag or it is invalid.");
74 doc[
"free_thresh"] >> free_th;
75 }
catch (YAML::InvalidScalar) {
77 "The map does not contain a free_thresh tag or it is invalid.");
81 doc[
"origin"][0] >> origin[0];
82 doc[
"origin"][1] >> origin[1];
83 doc[
"origin"][2] >> origin[2];
84 }
catch (YAML::InvalidScalar) {
85 ROS_ERROR(
"The map does not contain an origin tag or it is invalid.");
89 doc[
"image"] >> mapfname;
91 if(mapfname.size() == 0)
93 ROS_ERROR(
"The image tag cannot be an empty string.");
96 if(mapfname[0] !=
'/')
99 char* fname_copy = strdup(fname.c_str());
100 mapfname = std::string(dirname(fname_copy)) +
'/' + mapfname;
103 }
catch (YAML::InvalidScalar) {
104 ROS_ERROR(
"The map does not contain an image tag or it is invalid.");
108 ROS_INFO(
"Loading map from image \"%s\"", mapfname.c_str());
110 res,negate,occ_th,free_th, origin);
113 map_resp_.map.header.frame_id = frame_id;
115 ROS_INFO(
"Read a %d X %d map @ %.3lf m/cell",
116 map_resp_.map.info.width,
117 map_resp_.map.info.height,
118 map_resp_.map.info.resolution);
120 return map_resp_.map;
The main namespace for STDR Server.
nav_msgs::OccupancyGrid loadMap(const std::string &fname)
Loads a map from an image file.
void loadMapFromFile(nav_msgs::GetMap::Response *resp, const char *fname, double res, bool negate, double occ_th, double free_th, double *origin, MapMode mode=TRINARY)
The namespace for STDR map loader.