#include "landmark.h" #include #include "db_api/CDBSingletonDefine.h" #include "log.h" //Landmark_list * Landmark_list::instance() //{ // static Landmark_list lm_list; // return &lm_list; //} void Landmark_list::init_mapDirection() { const char * sql = "select map_direction_id, map_id, north_angle from dat_map_direction;"; std::string Error; YADB::CDBResultSet DBRes; sDBConnPool.Query(sql,DBRes,Error); int nCount = DBRes.GetRecordCount( Error ); if (nCount > 0) { log_info( "init_map_direction. The record count=%d\n", nCount ); while ( DBRes.GetNextRecod(Error) ) { int map_id = 0; DBRes.GetField( "map_id",map_id, Error ); int north_angle = 0; DBRes.GetField( "north_angle",north_angle, Error ); std::shared_ptr md = std::make_shared(map_id,north_angle); m_map_direction.insert({map_id,md}); log_info("map_direction.....mapid:%d,north:%d",map_id,north_angle); } } } void Landmark_list::init_from_db() { init_landmarkInfo(); init_mapDirection(); } void Landmark_list::init_landmarkInfo(int id/*=-1*/) { std::string sql = "select landmark_id, name, map_id, area_id, x, y, z from dat_landmark"; if (-1 == id) { sql += ";"; } else { sql += " WHERE landmark_id=" + std::to_string(id) +";"; } std::string Error; YADB::CDBResultSet DBRes; sDBConnPool.Query(sql.c_str(),DBRes,Error); int nCount = DBRes.GetRecordCount( Error ); if (nCount > 0) { log_info( "init_landmark_info. The record count=%d\n", nCount ); std::unordered_map> map; while ( DBRes.GetNextRecod(Error) ) { int landmark_id = 0; DBRes.GetField( "landmark_id",landmark_id, Error ); std::string name; DBRes.GetField( "name",name, Error ); int map_id = 0; DBRes.GetField( "map_id",map_id, Error ); int area_id = 0; DBRes.GetField( "area_id",area_id, Error ); double x = 0; DBRes.GetField( "x",x, Error ); double y = 0; DBRes.GetField( "y",y, Error ); double z = 0; DBRes.GetField( "z",z, Error ); point p(x,y,z); std::shared_ptr landmark = std::make_shared(p, landmark_id, name, map_id, area_id); if (-1 == id) { map.insert(std::make_pair(landmark_id, landmark)); //m_v.push_back(landmark); log_info("landmark ......id:%d,name:%s,mapid:%d,area_id:%d,x:%.2f,y:%.2f,z:%.2f", landmark_id, name.c_str(), map_id, area_id, x, y, z); } else { auto markptr = Landmark_list::instance()->get(landmark_id); if (markptr != nullptr) { markptr->set(p); markptr->landmark_id = landmark_id; markptr->landmark_name = name; markptr->map_id = map_id; markptr->area_id = area_id; log_info("update landmark ......id:%d,name:%s,mapid:%d,area_id:%d,x:%.2f,y:%.2f,z:%.2f", landmark_id, name.c_str(), map_id, area_id, x, y, z); } else { log_info("add landmark ......id:%d,name:%s,mapid:%d,area_id:%d,x:%.2f,y:%.2f,z:%.2f", landmark_id, name.c_str(), map_id, area_id, x, y, z); Landmark_list::instance()->add(landmark_id, landmark); } break; } } if (-1== id) { Landmark_list::instance()->add(map); } } } std::tuple Landmark_list::get_landmark(int mapid ,int areaid,const point &pt) { int32_t id = -1; double dis = DBL_MAX; point p; auto m_v = Landmark_list::instance()->m_map; auto it_s = m_v.begin(); while(it_s!=m_v.end()) { auto lm = it_s->second; if(lm->area_id == areaid) { double tmp = pt.dist(*lm); if(tmp < dis) { id = lm->landmark_id; dis=tmp; p=*lm; } } it_s++; } if(id==-1) return std::make_tuple(0,0,0); int dir = GetDirectionType(pt,p,mapid); return std::make_tuple(id,dir,dis); } DIRECTION_TYPE Landmark_list::GetDirectionType(point card_point, point landmark_point, int map_id) { DIRECTION_TYPE direction = NODIRECTORY; int x_offset = 0; int y_offset = 0; int angle = 0; x_offset = card_point.x - landmark_point.x; y_offset = card_point.y - landmark_point.y; if(abs(x_offset) < 1) x_offset = 0; if(abs(y_offset) < 1) y_offset = 0; std::shared_ptr pMapDirectoryInfo = nullptr; auto it = m_map_direction.find(map_id); if(it == m_map_direction.end()) { return NODIRECTORY; } else { pMapDirectoryInfo = it->second; } if(0 == y_offset) { if(x_offset > 0) angle = pMapDirectoryInfo->north_angle + 0; if(x_offset < 0) angle = pMapDirectoryInfo->north_angle - 180; } else if(0 == x_offset) { if(y_offset < 0) angle = pMapDirectoryInfo->north_angle - 90; if(y_offset > 0) angle = pMapDirectoryInfo->north_angle + 90; } else if(abs(x_offset) >= abs(y_offset)) { if(x_offset > 0) angle = pMapDirectoryInfo->north_angle + 0; if(x_offset < 0) angle = pMapDirectoryInfo->north_angle - 180; } else { if(y_offset < 0) angle = pMapDirectoryInfo->north_angle - 90; if(y_offset > 0) angle = pMapDirectoryInfo->north_angle + 90; } direction = (DIRECTION_TYPE)((angle % 360) / 90); if(angle % 360 == 0) direction = NORTH; if((0 == y_offset) && (0 == x_offset)) direction = NODIRECTORY; return direction; }