@@ -220,9 +220,7 @@ namespace dsm {
220220 if (fileExt == " dsm" ) {
221221 // first input number is the number of nodes
222222 std::ifstream file{fileName};
223- if (!file.is_open ()) {
224- throw std::invalid_argument (buildLog (" Cannot find file: " + fileName));
225- }
223+ assert ((void (" Coordinates file not found." ), file.is_open ()));
226224 Size n;
227225 file >> n;
228226 if (n < m_nodes.size ()) {
@@ -236,6 +234,33 @@ namespace dsm {
236234 m_nodes[i]->setCoords (std::make_pair (lat, lon));
237235 }
238236 }
237+ } else if (fileExt == " csv" ) {
238+ std::ifstream ifs{fileName};
239+ assert ((void (" Coordinates file not found." ), ifs.is_open ()));
240+ // Check if the first line is nodeId;lat;lon
241+ std::string line;
242+ std::getline (ifs, line);
243+ assert ((void (" Invalid file format." ), line == " nodeId;lat;lon" ));
244+ double dLat, dLon;
245+ while (!ifs.eof ()) {
246+ std::getline (ifs, line);
247+ if (line.empty ()) {
248+ continue ;
249+ }
250+ std::istringstream iss{line};
251+ std::string nodeId, lat, lon;
252+ std::getline (iss, nodeId, ' ;' );
253+ std::getline (iss, lat, ' ;' );
254+ std::getline (iss, lon, ' \n ' );
255+ dLat = lat == " Nan" ? 0 . : std::stod (lat);
256+ dLon = lon == " Nan" ? 0 . : std::stod (lon);
257+ if (m_nodes.contains (std::stoul (nodeId))) {
258+ m_nodes[std::stoul (nodeId)]->setCoords (std::make_pair (dLat, dLon));
259+ } else {
260+ std::cerr << std::format (" WARNING: Node with id {} not found." , nodeId)
261+ << std::endl;
262+ }
263+ }
239264 } else {
240265 throw std::invalid_argument (buildLog (" File extension not supported." ));
241266 }
@@ -361,6 +386,25 @@ namespace dsm {
361386 }
362387 }
363388
389+ void Graph::exportCoordinates (std::string const & path) {
390+ // assert that path ends with ".csv"
391+ assert ((void (" Only csv export is supported." ),
392+ path.substr (path.find_last_of (" ." )) == " .csv" ));
393+ std::ofstream file{path};
394+ // Column names
395+ file << " nodeId;lat;lon\n " ;
396+ for (const auto & [id, node] : m_nodes) {
397+ file << id << ' ;' ;
398+ if (node->coords ().has_value ()) {
399+ file << node->coords ().value ().first << ' ;' << node->coords ().value ().second ;
400+ } else {
401+ file << " Nan;Nan" ;
402+ }
403+ file << ' \n ' ;
404+ }
405+ file.close ();
406+ }
407+
364408 void Graph::addNode (std::unique_ptr<Node> node) {
365409 m_nodes.emplace (std::make_pair (node->id (), std::move (node)));
366410 }
0 commit comments