New version of XML reader/writer.
Now, there are only a single XmlIo class both for reading and writing.
1 #include "mapstorage.h"
7 MapStorage::MapStorage() : modified(false), file_name("")
9 nodemap_storage["coordinates_x"] = new Graph::NodeMap<double>(graph);
10 coords.setXMap(*nodemap_storage["coordinates_x"]);
11 nodemap_storage["coordinates_y"] = new Graph::NodeMap<double>(graph);
12 coords.setYMap(*nodemap_storage["coordinates_y"]);
14 nodemap_storage["id"] = new Graph::NodeMap<double>(graph);
15 edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
17 nodemap_default["id"] = 1.0;
18 edgemap_default["id"] = 1.0;
21 MapStorage::~MapStorage()
23 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
24 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
28 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
29 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
35 int MapStorage::addNodeMap(const std::string & name, Graph::NodeMap<double> *nodemap, double default_value = 0.0)
37 if( nodemap_storage.find(name) == nodemap_storage.end() )
39 nodemap_storage[name]=nodemap;
40 // set the maps default value
41 nodemap_default[name] = default_value;
47 int MapStorage::addEdgeMap(const std::string & name, Graph::EdgeMap<double> *edgemap, double default_value = 0.0)
49 if( edgemap_storage.find(name) == edgemap_storage.end() )
51 edgemap_storage[name]=edgemap;
52 // set the maps default value
53 edgemap_default[name] = default_value;
59 double MapStorage::maxOfNodeMap(const std::string & name)
62 for (NodeIt j(graph); j!=INVALID; ++j)
64 if( (*nodemap_storage[name])[j]>max )
66 max=(*nodemap_storage[name])[j];
72 double MapStorage::maxOfEdgeMap(const std::string & name)
75 for (EdgeIt j(graph); j!=INVALID; ++j)
77 if( (*edgemap_storage[name])[j]>max )
79 max=(*edgemap_storage[name])[j];
85 double MapStorage::minOfNodeMap(const std::string & name)
91 min=(*nodemap_storage[name])[j];
97 for (; j!=INVALID; ++j)
99 if( (*nodemap_storage[name])[j]<min )
101 min=(*nodemap_storage[name])[j];
107 double MapStorage::minOfEdgeMap(const std::string & name)
113 min=(*edgemap_storage[name])[j];
119 for (EdgeIt j(graph); j!=INVALID; ++j)
121 if( (*edgemap_storage[name])[j]<min )
123 min=(*edgemap_storage[name])[j];
129 int MapStorage::readFromFile(const std::string &filename)
133 bool read_edge_id = false;
136 LemonReader lreader(filename);
137 ContentReader content(lreader);
140 const std::vector<std::string>& nodeMapNames = content.nodeSetMaps(0);
141 const std::vector<std::string>& edgeMapNames = content.edgeSetMaps(0);
143 GraphReader<Graph> greader(filename, graph);
144 for (std::vector<std::string>::const_iterator it = nodeMapNames.begin();
145 it != nodeMapNames.end(); ++it)
147 if (*it == "coordinates_x")
150 //std::cout << "read X nodemap" << std::endl;
152 else if (*it == "coordinates_y")
155 //std::cout << "read Y nodemap" << std::endl;
157 else if (*it == "id")
159 //std::cout << "read id nodemap" << std::endl;
163 nodemap_storage[*it] = new Graph::NodeMap<double>(graph);
164 //std::cout << "read " << *it << " nodemap" << std::endl;
166 greader.readNodeMap(*it, *nodemap_storage[*it]);
168 for (std::vector<std::string>::const_iterator it = edgeMapNames.begin();
169 it != edgeMapNames.end(); ++it)
173 //std::cout << "read id edgemap" << std::endl;
177 edgemap_storage[*it] = new Graph::EdgeMap<double>(graph);
178 //std::cout << "read " << *it << " edgemap" << std::endl;
180 greader.readEdgeMap(*it, *edgemap_storage[*it]);
183 } catch (DataFormatError& error) {
184 Gtk::MessageDialog mdialog(error.what());
192 edgemap_storage["id"] = new Graph::EdgeMap<double>(graph);
194 for (EdgeIt e(graph); e != INVALID; ++e)
196 (*edgemap_storage["id"])[e] = i++;
200 if (!read_x || !read_y)
203 for (NodeIt n(graph); n != INVALID; ++n)
207 const double pi = 3.142;
208 double step = 2 * pi / (double) node_num;
210 for (NodeIt n(graph); n != INVALID; ++n)
212 nodemap_storage["coordinates_x"]->set(n, 250.0 * std::cos(i * step));
213 nodemap_storage["coordinates_y"]->set(n, 250.0 * std::sin(i * step));
218 // fill in the default values for the maps
219 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
220 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
222 if ((it->first != "id") &&
223 (it->first != "coordiantes_x") &&
224 (it->first != "coordinates_y"))
226 nodemap_default[it->first] = 0.0;
228 else if (it->first == "id")
231 double max = (*nodemap_storage["id"])[n];
232 for (; n != INVALID; ++n)
234 if ((*nodemap_storage["id"])[n] > max)
235 max = (*nodemap_storage["id"])[n];
237 nodemap_default["id"] = max + 1.0;
240 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
241 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
243 if (it->first != "id")
245 edgemap_default[it->first] = 0.0;
249 double max = std::numeric_limits<double>::min();
250 for (EdgeIt e(graph); e != INVALID; ++e)
252 if ((*edgemap_storage["id"])[e] > max)
253 max = (*edgemap_storage["id"])[e];
255 if (max > std::numeric_limits<double>::min())
256 edgemap_default["id"] = max + 1.0;
258 edgemap_default["id"] = 1.0;
265 void MapStorage::writeToFile(const std::string &filename)
267 GraphWriter<Graph> gwriter(filename, graph);
269 for (std::map<std::string, Graph::NodeMap<double>*>::const_iterator it =
270 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
272 gwriter.writeNodeMap(it->first, *(it->second));
273 //std::cout << "wrote " << it->first << " nodemap" << std::endl;
275 for (std::map<std::string, Graph::EdgeMap<double>*>::const_iterator it =
276 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
278 gwriter.writeEdgeMap(it->first, *(it->second));
279 //std::cout << "wrote " << it->first << " edgemap" << std::endl;
284 void MapStorage::clear()
286 for (std::map<std::string, Graph::NodeMap<double>*>::iterator it =
287 nodemap_storage.begin(); it != nodemap_storage.end(); ++it)
289 if ((it->first != "coordinates_x") &&
290 (it->first != "coordinates_y") &&
294 nodemap_storage.erase(it);
297 for (std::map<std::string, Graph::EdgeMap<double>*>::iterator it =
298 edgemap_storage.begin(); it != edgemap_storage.end(); ++it)
300 if (it->first != "id")
303 edgemap_storage.erase(it);
306 for (std::map<std::string, double>::iterator it =
307 nodemap_default.begin(); it != nodemap_default.end(); ++it)
309 if (it->first != "id")
310 nodemap_default.erase(it);
312 for (std::map<std::string, double>::iterator it =
313 edgemap_default.begin(); it != edgemap_default.end(); ++it)
315 if (it->first != "id")
316 edgemap_default.erase(it);