From 4deb47638e4cdbd4a8195a97a76bbbe5a1014e0e Mon Sep 17 00:00:00 2001
From: Sergi Hernandez Juan <shernand@iri.upc.edu>
Date: Mon, 4 Sep 2023 18:40:02 +0200
Subject: [PATCH] Solved a bug when computing the (x,y) center of the imported
 map.

---
 src/osm/osm_map.cpp | 34 ++++++++++++++++++----------------
 1 file changed, 18 insertions(+), 16 deletions(-)

diff --git a/src/osm/osm_map.cpp b/src/osm/osm_map.cpp
index bc6ed6c..804e9f9 100644
--- a/src/osm/osm_map.cpp
+++ b/src/osm/osm_map.cpp
@@ -73,6 +73,7 @@ void COSMMap::process_map(void)
 {
   std::vector<COSMNode *>::iterator node_it;
   std::vector<COSMWay *>::iterator way_it;
+  double x,y;
 
   // remove all nodes that do not belong to any road
   for(node_it=this->nodes.begin();node_it!=this->nodes.end();)
@@ -102,6 +103,20 @@ void COSMMap::process_map(void)
   // remove nodes inside other ways width
   for(unsigned int i=0;i<this->nodes.size();i++)
     this->nodes[i]->remove_in_junction_nodes();
+  for(unsigned int i=0;i<this->nodes.size();i++)
+  {
+    this->nodes[i]->get_location(x,y);
+    // get the coordinates
+    if(x>this->max_x)
+      this->max_x=x;
+    if(x<this->min_x)
+      this->min_x=x;
+    if(y>this->max_y)
+      this->max_y=y;
+    if(y<this->min_y)
+      this->min_y=y;
+  }
+  this->normalize_locations();
 }
 
 void COSMMap::create_junctions(void)
@@ -239,6 +254,9 @@ void COSMMap::normalize_locations(void)
   this->min_x-=center_x;
   this->max_y-=center_y;
   this->min_y-=center_y;
+  // compute the width and heigth
+  this->width=this->max_x-this->min_x;
+  this->height=this->max_y-this->min_y;
 }
 
 void COSMMap::load(const std::string &filename)
@@ -264,11 +282,6 @@ void COSMMap::load(const std::string &filename)
   osmium::apply(relation_reader,*this);
   relation_reader.close();
 
-  // normalize locations
-  this->normalize_locations();
-  // compute the width and heigth
-  this->width=this->max_x-this->min_x;
-  this->height=this->max_y-this->min_y;
   this->process_map();
   this->create_road_segments();
   this->create_junctions();
@@ -277,20 +290,9 @@ void COSMMap::load(const std::string &filename)
 void COSMMap::node(const osmium::Node& node)
 {
   COSMNode *new_node;
-  double x,y;
 
   // create and add the new node
   new_node = new COSMNode(node,this);
-  new_node->get_location(x,y);
-  // get the coordinates
-  if(x>this->max_x)
-    this->max_x=x;
-  if(x<this->min_x)
-    this->min_x=x;
-  if(y>this->max_y)
-    this->max_y=y;
-  if(y<this->min_y)
-    this->min_y=y;
   this->add_node(new_node);
 }
 
-- 
GitLab