/*! * \file kml_printer.cc * \brief Implementation of a class that prints PVT information to a kml file * \author Javier Arribas, 2011. jarribas(at)cttc.es * Álvaro Cebrián Juan, 2018. acebrianjuan(at)gmail.com * * * ------------------------------------------------------------------------- * * Copyright (C) 2010-2018 (see AUTHORS file for a list of contributors) * * GNSS-SDR is a software defined Global Navigation * Satellite Systems receiver * * This file is part of GNSS-SDR. * * GNSS-SDR is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * GNSS-SDR is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with GNSS-SDR. If not, see . * * ------------------------------------------------------------------------- */ #include "kml_printer.h" #include #include // for create_directories, exists #include // for path, operator<< #include // for filesystem #include #include #include using google::LogMessage; Kml_Printer::Kml_Printer(const std::string& base_path) { positions_printed = false; indent = " "; kml_base_path = base_path; boost::filesystem::path full_path(boost::filesystem::current_path()); const boost::filesystem::path p(kml_base_path); if (!boost::filesystem::exists(p)) { std::string new_folder; for (auto& folder : boost::filesystem::path(kml_base_path)) { new_folder += folder.string(); boost::system::error_code ec; if (!boost::filesystem::exists(new_folder)) { if (!boost::filesystem::create_directory(new_folder, ec)) { std::cout << "Could not create the " << new_folder << " folder." << std::endl; kml_base_path = full_path.string(); } } new_folder += boost::filesystem::path::preferred_separator; } } else { kml_base_path = p.string(); } if (kml_base_path != ".") { std::cout << "KML files will be stored at " << kml_base_path << std::endl; } kml_base_path = kml_base_path + boost::filesystem::path::preferred_separator; boost::filesystem::path tmp_base_path = boost::filesystem::temp_directory_path(); boost::filesystem::path tmp_filename = boost::filesystem::unique_path(); boost::filesystem::path tmp_file = tmp_base_path / tmp_filename; tmp_file_str = tmp_file.string(); point_id = 0; } bool Kml_Printer::set_headers(const std::string& filename, bool time_tag_name) { boost::posix_time::ptime pt = boost::posix_time::second_clock::local_time(); tm timeinfo = boost::posix_time::to_tm(pt); if (time_tag_name) { std::stringstream strm0; const int year = timeinfo.tm_year - 100; strm0 << year; const int month = timeinfo.tm_mon + 1; if (month < 10) { strm0 << "0"; } strm0 << month; const int day = timeinfo.tm_mday; if (day < 10) { strm0 << "0"; } strm0 << day << "_"; const int hour = timeinfo.tm_hour; if (hour < 10) { strm0 << "0"; } strm0 << hour; const int min = timeinfo.tm_min; if (min < 10) { strm0 << "0"; } strm0 << min; const int sec = timeinfo.tm_sec; if (sec < 10) { strm0 << "0"; } strm0 << sec; kml_filename = filename + "_" + strm0.str() + ".kml"; } else { kml_filename = filename + ".kml"; } kml_filename = kml_base_path + kml_filename; kml_file.open(kml_filename.c_str()); tmp_file.open(tmp_file_str.c_str()); if (kml_file.is_open() && tmp_file.is_open()) { DLOG(INFO) << "KML printer writing on " << filename.c_str(); // Set iostream numeric format and precision kml_file.setf(kml_file.std::ofstream::fixed, kml_file.std::ofstream::floatfield); kml_file << std::setprecision(14); tmp_file.setf(tmp_file.std::ofstream::fixed, tmp_file.std::ofstream::floatfield); tmp_file << std::setprecision(14); kml_file << R"()" << std::endl << R"()" << std::endl << indent << "" << std::endl << indent << indent << "GNSS Track" << std::endl << indent << indent << "" << std::endl << indent << indent << indent << indent << "GNSS-SDR Receiver position log file created at " << pt << "" << std::endl << indent << indent << indent << indent << "https://gnss-sdr.org/" << std::endl << indent << indent << indent << "" << std::endl << indent << indent << "]]>" << std::endl << indent << indent << "" << std::endl << indent << indent << "" << std::endl << indent << indent << "" << std::endl << indent << indent << "" << std::endl << indent << indent << "" << std::endl << indent << indent << indent << "" << std::endl << indent << indent << indent << indent << "normal" << std::endl << indent << indent << indent << indent << "#track_n" << std::endl << indent << indent << indent << "" << std::endl << indent << indent << indent << "" << std::endl << indent << indent << indent << indent << "highlight" << std::endl << indent << indent << indent << indent << "#track_h" << std::endl << indent << indent << indent << "" << std::endl << indent << indent << "" << std::endl << indent << indent << "" << std::endl << indent << indent << "" << std::endl << indent << indent << indent << "Points" << std::endl; return true; } std::cout << "File " << kml_filename << " cannot be saved. Wrong permissions?" << std::endl; return false; } bool Kml_Printer::print_position(const std::shared_ptr& position, bool print_average_values) { double latitude; double longitude; double height; positions_printed = true; const std::shared_ptr& position_ = position; double speed_over_ground = position_->get_speed_over_ground(); // expressed in m/s double course_over_ground = position_->get_course_over_ground(); // expressed in deg double hdop = position_->get_hdop(); double vdop = position_->get_vdop(); double pdop = position_->get_pdop(); std::string utc_time = to_iso_extended_string(position_->get_position_UTC_time()); if (utc_time.length() < 23) { utc_time += "."; } utc_time.resize(23, '0'); // time up to ms utc_time.append("Z"); // UTC time zone if (print_average_values == false) { latitude = position_->get_latitude(); longitude = position_->get_longitude(); height = position_->get_height(); } else { latitude = position_->get_avg_latitude(); longitude = position_->get_avg_longitude(); height = position_->get_avg_height(); } if (kml_file.is_open() && tmp_file.is_open()) { point_id++; kml_file << indent << indent << indent << "" << std::endl << indent << indent << indent << indent << "" << point_id << "" << std::endl << indent << indent << indent << indent << "" << std::endl << indent << indent << indent << indent << "" << std::endl << indent << indent << indent << indent << indent << indent << "Time:" << utc_time << "" << std::endl << indent << indent << indent << indent << indent << indent << "Longitude:" << longitude << "deg" << std::endl << indent << indent << indent << indent << indent << indent << "Latitude:" << latitude << "deg" << std::endl << indent << indent << indent << indent << indent << indent << "Altitude:" << height << "m" << std::endl << indent << indent << indent << indent << indent << indent << "Speed:" << speed_over_ground << "m/s" << std::endl << indent << indent << indent << indent << indent << indent << "Course:" << course_over_ground << "deg" << std::endl << indent << indent << indent << indent << indent << indent << "HDOP:" << hdop << "" << std::endl << indent << indent << indent << indent << indent << indent << "VDOP:" << vdop << "" << std::endl << indent << indent << indent << indent << indent << indent << "PDOP:" << pdop << "" << std::endl << indent << indent << indent << indent << indent << "" << std::endl << indent << indent << indent << indent << "]]>" << std::endl << indent << indent << indent << indent << "" << std::endl << indent << indent << indent << indent << indent << "" << utc_time << "" << std::endl << indent << indent << indent << indent << "" << std::endl << indent << indent << indent << indent << "#track" << std::endl << indent << indent << indent << indent << "" << std::endl << indent << indent << indent << indent << indent << "absolute" << std::endl << indent << indent << indent << indent << indent << "" << longitude << "," << latitude << "," << height << "" << std::endl << indent << indent << indent << indent << "" << std::endl << indent << indent << indent << "" << std::endl; tmp_file << indent << indent << indent << indent << indent << longitude << "," << latitude << "," << height << std::endl; return true; } return false; } bool Kml_Printer::close_file() { if (kml_file.is_open() && tmp_file.is_open()) { tmp_file.close(); kml_file << indent << indent << "" << indent << indent << "" << std::endl << indent << indent << indent << "Path" << std::endl << indent << indent << indent << "#yellowLineGreenPoly" << std::endl << indent << indent << indent << "" << std::endl << indent << indent << indent << indent << "0" << std::endl << indent << indent << indent << indent << "1" << std::endl << indent << indent << indent << indent << "absolute" << std::endl << indent << indent << indent << indent << "" << std::endl; // Copy the contents of tmp_file into kml_file std::ifstream src(tmp_file_str, std::ios::binary); kml_file << src.rdbuf(); kml_file << indent << indent << indent << indent << "" << std::endl << indent << indent << indent << "" << std::endl << indent << indent << "" << std::endl << indent << "" << std::endl << ""; kml_file.close(); return true; } return false; } Kml_Printer::~Kml_Printer() { try { close_file(); } catch (const std::exception& e) { std::cerr << e.what() << '\n'; } if (!positions_printed) { if (remove(kml_filename.c_str()) != 0) { LOG(INFO) << "Error deleting temporary KML file"; } } }