1 #include "mapstorage.h"
5 MapStorage::MapStorage() : modified(false), file_name("")
7 nodemap_storage["coordinates_x"] = new Graph::NodeMap<double>(graph);
8 coords.setXMap(*nodemap_storage["coordinates_x"]);
9 nodemap_storage["coordinates_y"] = new Graph::NodeMap<double>(graph);
10 coords.setYMap(*nodemap_storage["coordinates_y"]);
12 nodemap_storage["id"] = new Graph::NodeMap<double>(graph);
13 edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
16 MapStorage::~MapStorage()
18 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
19 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
23 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
24 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
30 int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap, double default_value = 0.0)
32 if( nodemap_storage.find(name) == nodemap_storage.end() )
34 nodemap_storage[name]=nodemap;
35 // set the maps default value
36 nodemap_default[name] = default_value;
42 int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap, double default_value = 0.0)
44 if( edgemap_storage.find(name) == edgemap_storage.end() )
46 edgemap_storage[name]=edgemap;
47 // set the maps default value
48 edgemap_default[name] = default_value;
54 double MapStorage::maxOfNodeMap(const std::string & name)
57 for (NodeIt j(graph); j!=INVALID; ++j)
59 if( (*nodemap_storage[name])[j]>max )
61 max=(*nodemap_storage[name])[j];
67 double MapStorage::maxOfEdgeMap(const std::string & name)
70 for (EdgeIt j(graph); j!=INVALID; ++j)
72 if( (*edgemap_storage[name])[j]>max )
74 max=(*edgemap_storage[name])[j];
80 double MapStorage::minOfNodeMap(const std::string & name)
86 min=(*nodemap_storage[name])[j];
92 for (; j!=INVALID; ++j)
94 if( (*nodemap_storage[name])[j]<min )
96 min=(*nodemap_storage[name])[j];
102 double MapStorage::minOfEdgeMap(const std::string & name)
108 min=(*edgemap_storage[name])[j];
114 for (EdgeIt j(graph); j!=INVALID; ++j)
116 if( (*edgemap_storage[name])[j]<min )
118 min=(*edgemap_storage[name])[j];
124 int MapStorage::readFromFile(const std::string &filename)
130 LemonReader lreader(filename);
131 ContentReader content(lreader);
134 const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
135 const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
137 GraphReader<Graph> greader(filename, graph);
138 for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
139 it != nodeMapNames.end(); ++it)
141 if (*it == "coordinates_x")
144 //std::cout << "read X nodemap" << std::endl;
146 else if (*it == "coordinates_y")
149 //std::cout << "read Y nodemap" << std::endl;
151 else if (*it == "id")
153 //std::cout << "read id nodemap" << std::endl;
157 nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
158 //std::cout << "read " << *it << " nodemap" << std::endl;
160 greader.readNodeMap(*it, *nodemap_storage[*it]);
162 for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
163 it != edgeMapNames.end(); ++it)
167 //std::cout << "read id edgemap" << std::endl;
171 edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
172 //std::cout << "read " << *it << " edgemap" << std::endl;
174 greader.readEdgeMap(*it, *edgemap_storage[*it]);
177 } catch (DataFormatError& error) {
178 Gtk::MessageDialog mdialog("Read Error");
179 mdialog.set_message(error.what());
185 if (!read_x || !read_y)
188 for (NodeIt n(graph); n != INVALID; ++n)
192 const double pi = 3.142;
193 double step = 2 * pi / (double) node_num;
195 for (NodeIt n(graph); n != INVALID; ++n)
197 nodemap_storage["coordinates_x"]->set(n, 250.0 * cos(i * step));
198 nodemap_storage["coordinates_y"]->set(n, 250.0 * sin(i * step));
203 // fill in the default values for the maps
204 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
205 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
207 if ((it->first != "id") &&
208 (it->first != "coordiantes_x") &&
209 (it->first != "coordinates_y"))
211 nodemap_default[it->first] = 0.0;
214 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
215 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
217 if (it->first != "id")
219 edgemap_default[it->first] = 0.0;
226 void MapStorage::writeToFile(const std::string &filename)
228 GraphWriter<Graph> gwriter(filename, graph);
230 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
231 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
233 gwriter.writeNodeMap(it->first, *(it->second));
234 //std::cout << "wrote " << it->first << " nodemap" << std::endl;
236 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
237 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
239 gwriter.writeEdgeMap(it->first, *(it->second));
240 //std::cout << "wrote " << it->first << " edgemap" << std::endl;
245 void MapStorage::clear()
247 for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
248 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
250 if ((it->first != "coordinates_x") &&
251 (it->first != "coordinates_y") &&
255 nodemap_storage.erase(it);
258 for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
259 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
261 if (it->first != "id")
264 edgemap_storage.erase(it);
267 for (std::map<std::string, double>::iterator it =
268 nodemap_default.begin(); it != nodemap_default.end(); ++it)
270 nodemap_default.erase(it);
272 for (std::map<std::string, double>::iterator it =
273 edgemap_default.begin(); it != edgemap_default.end(); ++it)
275 edgemap_default.erase(it);