42 if (m_maps_storage.size() == 0)
44 yCError(MAP2DSTORAGE) <<
"Map storage is empty";
50 file.open(mapsfile.c_str());
53 yCError(MAP2DSTORAGE) <<
"Sorry unable to open" << mapsfile;
59 for (
auto& it : m_maps_storage)
62 std::string map_filename = it.first +
".map";
68 ret &= it.second.saveToFile(map_filename);
79 file.open(mapsfile.c_str());
82 yCError(MAP2DSTORAGE) <<
"loadMaps() Unable to open:" << mapsfile;
89 std::getline(file,
buffer);
90 std::istringstream iss(
buffer);
95 if (dummy ==
"mapfile:")
97 std::string mapfilename;
101 std::string mapfilenameWithPath = m_rf_mapCollection.findFile(mapfilename);
109 auto p = m_maps_storage.find(map_name);
110 if (p == m_maps_storage.end())
112 if (option ==
"crop") {
113 map.
crop(-1, -1, -1, -1);
115 m_maps_storage[map_name] = map;
119 yCError(MAP2DSTORAGE) <<
"A map with the same name '" << map_name <<
"'was found, skipping...";
125 yCError(MAP2DSTORAGE) <<
"Problems opening map file" << mapfilenameWithPath;
131 yCError(MAP2DSTORAGE) <<
"Invalid syntax, missing mapfile tag";
144 std::string collection_file_name=
"maps_collection.ini";
145 std::string locations_file_name=
"locations.ini";
146 if (config.
check(
"mapCollectionFile"))
148 collection_file_name= config.
find(
"mapCollectionFile").
asString();
151 if (config.
check(
"mapCollectionContext"))
153 std::string collection_context_name= config.
find(
"mapCollectionContext").
asString();
154 m_rf_mapCollection.setDefaultContext(collection_context_name.c_str());
155 std::string collection_file_with_path = m_rf_mapCollection.findFile(collection_file_name);
156 std::string locations_file_with_path = m_rf_mapCollection.findFile(locations_file_name);
158 if (collection_file_with_path==
"")
160 yCInfo(MAP2DSTORAGE) <<
"No locations loaded";
164 bool ret = loadLocationsAndExtras(locations_file_with_path);
165 if (
ret) {
yCInfo(MAP2DSTORAGE) <<
"Location file" << locations_file_with_path <<
"successfully loaded."; }
166 else {
yCError(MAP2DSTORAGE) <<
"Problems opening file" << locations_file_with_path; }
169 if (collection_file_with_path==
"")
171 yCError(MAP2DSTORAGE) <<
"Unable to find file" << collection_file_name <<
"within the specified context:" << collection_context_name;
174 if (loadMapsCollection(collection_file_with_path))
176 yCInfo(MAP2DSTORAGE) <<
"Map collection file:" << collection_file_with_path <<
"successfully loaded.";
177 if (m_maps_storage.size() > 0)
179 yCInfo(MAP2DSTORAGE) <<
"Available maps are:";
180 for (
auto& it : m_maps_storage)
182 yCInfo(MAP2DSTORAGE) << it.first;
187 yCInfo(MAP2DSTORAGE) <<
"No maps available";
189 if (m_locations_storage.size() > 0)
191 yCInfo(MAP2DSTORAGE) <<
"Available Locations are:";
192 for (
auto& it : m_locations_storage)
194 yCInfo(MAP2DSTORAGE) << it.first;
199 yCInfo(MAP2DSTORAGE) <<
"No locations available";
202 if (m_areas_storage.size() > 0)
204 yCInfo(MAP2DSTORAGE) <<
"Available areas are:";
205 for (
auto& it : m_areas_storage)
207 yCInfo(MAP2DSTORAGE) << it.first;
212 yCInfo(MAP2DSTORAGE) <<
"No areas available";
217 yCError(MAP2DSTORAGE) <<
"Unable to load map collection file:" << collection_file_with_path;
222 if (!config.
check(
"name"))
224 m_rpcPortName =
"/map2DStorage/rpc";
232 if (!m_rpcPort.open(m_rpcPortName))
234 yCError(MAP2DSTORAGE,
"Failed to open port %s", m_rpcPortName.c_str());
237 m_rpcPort.setReader(*
this);
244 yCTrace(MAP2DSTORAGE,
"Close");
248 bool Map2DStorage::priv_load_locations_and_areas_v1(std::ifstream& file)
251 std::getline(file,
buffer);
252 if (
buffer !=
"Locations:")
254 yCError(MAP2DSTORAGE) <<
"Unable to parse Locations section!";
260 std::getline(file,
buffer);
266 yCError(MAP2DSTORAGE) <<
"Unexpected End Of File";
271 size_t bot_size = b.
size();
274 yCError(MAP2DSTORAGE) <<
"Unable to parse contents of Areas section!";
283 m_locations_storage[name] = location;
288 yCError(MAP2DSTORAGE) <<
"Unable to parse Areas section!";
295 std::getline(file,
buffer);
302 size_t bot_size = b.
size();
306 if (area_size <= 0 || bot_size != area_size * 2 + 3)
308 yCError(MAP2DSTORAGE) <<
"Unable to parse contents of Areas section!";
311 for (
size_t ai = 3; ai < bot_size; ai += 2)
317 m_areas_storage[name] = area;
322 bool Map2DStorage::priv_load_locations_and_areas_v2(std::ifstream& file)
325 std::getline(file,
buffer);
326 if (
buffer !=
"Locations:")
328 yCError(MAP2DSTORAGE) <<
"Unable to parse Locations section!";
334 std::getline(file,
buffer);
340 yCError(MAP2DSTORAGE) <<
"Unexpected End Of File";
345 size_t bot_size = b.
size();
348 yCError(MAP2DSTORAGE) <<
"Unable to parse contents of Areas section!";
357 m_locations_storage[name] = location;
362 yCError(MAP2DSTORAGE) <<
"Unable to parse Areas section!";
369 std::getline(file,
buffer);
379 size_t bot_size = b.
size();
383 if (area_size <= 0 || bot_size != area_size * 2 + 3)
385 yCError(MAP2DSTORAGE) <<
"Unable to parse contents of Areas section!";
388 for (
size_t ai = 3; ai < bot_size; ai += 2)
394 m_areas_storage[name] = area;
399 yCError(MAP2DSTORAGE) <<
"Unable to parse Paths section!";
406 std::getline(file,
buffer);
413 size_t bot_size = b.
size();
422 if (ll && ll->
size() == 4)
433 yCError(MAP2DSTORAGE) <<
"Unable to parse contents of Paths section!";
443 m_paths_storage[name] = path;
452 file.open (locations_file.c_str());
456 yCError(MAP2DSTORAGE) <<
"Unable to open" << locations_file <<
"locations file.";
462 std::getline(file,
buffer);
465 yCError(MAP2DSTORAGE) <<
"Unable to parse Version section!";
469 std::getline(file,
buffer);
470 int version = atoi(
buffer.c_str());
474 if (!priv_load_locations_and_areas_v1(file))
476 yCError(MAP2DSTORAGE) <<
"Call to load_locations_and_areas_v1 failed";
481 else if (version == 2)
483 if (!priv_load_locations_and_areas_v2(file))
485 yCError(MAP2DSTORAGE) <<
"Call to load_locations_and_areas_v2 failed";
492 yCError(MAP2DSTORAGE) <<
"Only versions 1,2 supported!";
499 yCDebug(MAP2DSTORAGE) <<
"Locations file" << locations_file <<
"loaded, "
500 << m_locations_storage.size() <<
"locations and "
501 << m_areas_storage.size() <<
" areas and "
502 << m_paths_storage.size() <<
" paths available";
509 file.open (locations_file.c_str());
513 yCError(MAP2DSTORAGE) <<
"Unable to open" << locations_file <<
"locations file.";
522 file <<
"Version:\n";
526 file <<
"Locations:\n";
527 std::map<std::string, Map2DLocation>::iterator it;
528 for (it = m_locations_storage.begin(); it != m_locations_storage.end(); ++it)
531 file << it->first << s << loc.
map_id << s << loc.
x << s << loc.
y << s << loc.
theta <<
"\n";
537 std::map<std::string, Map2DArea>::iterator it2;
538 for (it2 = m_areas_storage.begin(); it2 != m_areas_storage.end(); ++it2)
541 file << it2->first << s << area.
map_id << s << area.
points.size() << s;
542 for (
size_t i = 0; i < area.
points.size(); i++)
544 file << area.
points[i].x << s;
545 file << area.
points[i].y << s;
553 std::map<std::string, Map2DPath>::iterator it3;
554 for (it3 = m_paths_storage.begin(); it3 != m_paths_storage.end(); ++it3)
556 file << it3->first <<
" ";
557 for (
size_t i=0; i<it3->second.size(); i++)
559 loc = it3->second[i];
560 file <<
"( " <<loc.
map_id << s << loc.
x << s << loc.
y << s << loc.
theta <<
") ";
567 yCDebug(MAP2DSTORAGE) <<
"Locations file" << locations_file <<
"saved.";
574 m_maps_storage.clear();
581 auto it = m_maps_storage.find(map_name);
582 if (it == m_maps_storage.end())
585 m_maps_storage[map_name] = map;
590 m_maps_storage[map_name] = map;
597 auto it = m_maps_storage.find(map_name);
598 if (it != m_maps_storage.end())
609 for (
auto& it : m_maps_storage)
611 map_names.push_back(it.first);
618 size_t rem = m_maps_storage.erase(map_name);
631 m_locations_storage.insert(std::pair<std::string, Map2DLocation>(location_name, loc));
637 m_areas_storage.insert(std::pair<std::string, Map2DArea>(area_name, area));
643 m_paths_storage.insert(std::pair<std::string, Map2DPath>(path_name, path));
649 auto it = m_locations_storage.find(location_name);
650 if (it != m_locations_storage.end())
660 auto it = m_areas_storage.find(area_name);
661 if (it != m_areas_storage.end())
671 auto it = m_paths_storage.find(path_name);
672 if (it != m_paths_storage.end())
683 for (
auto& it : m_locations_storage)
685 locations.push_back(it.first);
693 for (
auto& it : m_areas_storage)
695 areas.push_back(it.first);
703 for (
auto& it : m_paths_storage)
705 paths.push_back(it.first);
712 std::map<std::string, Map2DLocation>::iterator orig_it;
713 orig_it = m_locations_storage.find(original_name);
714 std::map<std::string, Map2DLocation>::iterator new_it;
715 new_it = m_locations_storage.find(new_name);
717 if (orig_it != m_locations_storage.end() &&
718 new_it == m_locations_storage.end())
720 auto loc = orig_it->second;
721 m_locations_storage.erase(orig_it);
722 m_locations_storage.insert(std::pair<std::string, Map2DLocation>(new_name, loc));
730 std::map<std::string, Map2DLocation>::iterator it;
731 it = m_locations_storage.find(location_name);
732 if (it != m_locations_storage.end())
734 m_locations_storage.erase(it);
742 std::map<std::string, Map2DArea>::iterator it;
743 it = m_areas_storage.find(area_name);
744 if (it != m_areas_storage.end())
746 m_areas_storage.erase(it);
754 std::map<std::string, Map2DPath>::iterator it;
755 it = m_paths_storage.find(path_name);
756 if (it != m_paths_storage.end())
758 m_paths_storage.erase(it);
767 std::map<std::string, Map2DArea>::iterator orig_it;
768 orig_it = m_areas_storage.find(original_name);
769 std::map<std::string, Map2DArea>::iterator new_it;
770 new_it = m_areas_storage.find(new_name);
772 if (orig_it != m_areas_storage.end() &&
773 new_it == m_areas_storage.end())
775 auto area = orig_it->second;
776 m_areas_storage.erase(orig_it);
777 m_areas_storage.insert(std::pair<std::string, Map2DArea>(new_name, area));
786 std::map<std::string, Map2DPath>::iterator orig_it;
787 orig_it = m_paths_storage.find(original_name);
788 std::map<std::string, Map2DPath>::iterator new_it;
789 new_it = m_paths_storage.find(new_name);
791 if (orig_it != m_paths_storage.end() &&
792 new_it == m_paths_storage.end())
794 auto area = orig_it->second;
795 m_paths_storage.erase(orig_it);
796 m_paths_storage.insert(std::pair<std::string, Map2DPath>(new_name, area));
806 m_locations_storage.clear();
812 m_areas_storage.clear();
818 m_paths_storage.clear();
824 for (
auto it = m_maps_storage.begin(); it != m_maps_storage.end(); it++)
826 it->second.clearMapTemporaryFlags();
833 auto it = m_maps_storage.find(map_name);
834 if (it != m_maps_storage.end())
836 it->second.clearMapTemporaryFlags();
847 yCWarning(MAP2DSTORAGE) <<
"not yet implemented";
849 std::lock_guard<std::mutex> lock(m_mutex);
852 bool ok = in.
read(connection);
869 if (returnToSender !=
nullptr)
871 out.
write(*returnToSender);
875 yCError(MAP2DSTORAGE) <<
"Invalid return to sender";
bool getLocationsList(std::vector< std::string > &locations) override
Get a list of all stored locations.
bool renamePath(std::string original_name, std::string new_name) override
Searches for a path and renames it.
bool deleteArea(std::string area_name) override
Delete an area.
bool saveLocationsAndExtras(std::string locations_file) override
Save a collection of locations/area/paths etc.
bool getPath(std::string path_name, yarp::dev::Nav2D::Map2DPath &path) override
Retrieves a path.
bool close() override
Close the DeviceDriver.
bool clearAllLocations() override
Delete all stored locations.
bool deleteLocation(std::string location_name) override
Delete a location.
bool clearMapTemporaryFlags(std::string map_name) override
Clear all temporary flags from a specific map.
bool storeLocation(std::string location_name, yarp::dev::Nav2D::Map2DLocation loc) override
Store a location specified by the user in the world reference frame.
bool store_map(const yarp::dev::Nav2D::MapGrid2D &map) override
Stores a map into the map server.
bool getAreasList(std::vector< std::string > &areas) override
Get a list of all stored areas.
bool renameLocation(std::string original_name, std::string new_name) override
Searches for a location and renames it.
bool deletePath(std::string path_name) override
Delete a path.
bool saveMapsCollection(std::string maps_collection_file) override
Save a collection of maps.
bool get_map(std::string map_name, yarp::dev::Nav2D::MapGrid2D &map) override
Gets a map from the map server.
Map2DStorage()
Map2DStorage.
bool renameArea(std::string original_name, std::string new_name) override
Searches for an area and renames it.
bool remove_map(std::string map_name) override
Removes a map from the map server.
bool loadMapsCollection(std::string maps_collection_file) override
Load a collection of maps.
bool get_map_names(std::vector< std::string > &map_names) override
Gets a list containing the names of all registered maps.
bool getLocation(std::string location_name, yarp::dev::Nav2D::Map2DLocation &loc) override
Retrieves a location specified by the user in the world reference frame.
bool clearAllAreas() override
Delete all stored areas.
bool clearAllPaths() override
Delete all stored paths.
bool storePath(std::string path_name, yarp::dev::Nav2D::Map2DPath path) override
Store a path.
bool clearAllMaps() override
Removes all the registered maps from the server.
bool storeArea(std::string area_name, yarp::dev::Nav2D::Map2DArea area) override
Store an area.
bool getPathsList(std::vector< std::string > &paths) override
Get a list of all stored paths.
bool clearAllMapsTemporaryFlags() override
Clear all temporary flags from all stored maps.
bool getArea(std::string area_name, yarp::dev::Nav2D::Map2DArea &area) override
Retrieves an area.
bool open(yarp::os::Searchable ¶ms) override
Open the DeviceDriver.
bool loadLocationsAndExtras(std::string locations_file) override
Load a collection of locations/areas/paths etc.
std::vector< yarp::math::Vec2D< double > > points
void push_back(yarp::dev::Nav2D::Map2DLocation loc)
Inserts a new location into the path @loc the location to be inserted.
std::string getMapName() const
Retrieves the map name.
bool crop(int left, int top, int right, int bottom)
Modifies the map, cropping pixels at the boundaries.
bool loadFromFile(std::string map_filename)
Loads a yarp map file from disk.
A simple collection of objects that can be described and transmitted in a portable way.
void fromString(const std::string &text)
Initializes bottle from a string.
size_type size() const
Gets the number of elements in the bottle.
bool read(ConnectionReader &reader) override
Set the bottle's value based on input from a network connection.
Value & get(size_type index) const
Reads a Value v from a certain part of the list.
bool write(ConnectionWriter &writer) const override
Output a representation of the bottle to a network connection.
An interface for reading from a network connection.
virtual ConnectionWriter * getWriter()=0
Gets a way to reply to the message, if possible.
An interface for writing to a network connection.
A class for storing options and configuration information.
void fromString(const std::string &txt, bool wipe=true)
Interprets a string as a list of properties.
A base class for nested structures that can be searched.
virtual Value & find(const std::string &key) const =0
Gets a value corresponding to a given keyword.
virtual bool check(const std::string &key) const =0
Check if there exists a property of the given name.
virtual std::string toString() const =0
Return a standard text representation of the content of the object.
virtual yarp::conf::float64_t asFloat64() const
Get 64-bit floating point value.
virtual bool isString() const
Checks if value is a string.
virtual std::int32_t asInt32() const
Get 32-bit integer value.
virtual bool isList() const
Checks if value is a list.
virtual Bottle * asList() const
Get list value.
virtual bool isVocab32() const
Checks if value is a vocabulary identifier.
virtual std::string asString() const
Get string value.
#define yCInfo(component,...)
#define yCError(component,...)
#define yCTrace(component,...)
#define yCWarning(component,...)
#define yCDebug(component,...)
#define YARP_LOG_COMPONENT(name,...)
An interface for the device drivers.
An interface to the operating system, including Port based communication.