2015-11-14 13:17:02 +00:00
|
|
|
/*!
|
|
|
|
* \file geojson_printer.cc
|
|
|
|
* \brief Implementation of a class that prints PVT solutions in GeoJSON format
|
|
|
|
* \author Carles Fernandez-Prades, 2015. cfernandez(at)cttc.es
|
|
|
|
*
|
|
|
|
*
|
2020-07-28 14:57:15 +00:00
|
|
|
* -----------------------------------------------------------------------------
|
2015-11-14 13:17:02 +00:00
|
|
|
*
|
2020-12-30 12:35:06 +00:00
|
|
|
* GNSS-SDR is a Global Navigation Satellite System software-defined receiver.
|
2015-11-14 13:17:02 +00:00
|
|
|
* This file is part of GNSS-SDR.
|
|
|
|
*
|
2020-12-30 12:35:06 +00:00
|
|
|
* Copyright (C) 2010-2020 (see AUTHORS file for a list of contributors)
|
2020-02-08 00:20:02 +00:00
|
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
2015-11-14 13:17:02 +00:00
|
|
|
*
|
2020-07-28 14:57:15 +00:00
|
|
|
* -----------------------------------------------------------------------------
|
2015-11-14 13:17:02 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "geojson_printer.h"
|
2019-03-06 17:45:27 +00:00
|
|
|
#include "pvt_solution.h"
|
2017-08-11 03:18:38 +00:00
|
|
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
2015-11-14 13:39:10 +00:00
|
|
|
#include <glog/logging.h>
|
2019-03-06 17:45:27 +00:00
|
|
|
#include <ctime> // for tm
|
|
|
|
#include <exception> // for exception
|
|
|
|
#include <iomanip> // for operator<<
|
|
|
|
#include <iostream> // for cout, cerr
|
|
|
|
#include <sstream> // for stringstream
|
2018-02-26 02:15:53 +00:00
|
|
|
|
2020-11-22 10:05:16 +00:00
|
|
|
// clang-format off
|
2019-06-10 19:41:13 +00:00
|
|
|
#if HAS_STD_FILESYSTEM
|
2019-06-12 18:39:29 +00:00
|
|
|
#include <system_error>
|
2019-06-10 19:41:13 +00:00
|
|
|
namespace errorlib = std;
|
2019-06-12 18:39:29 +00:00
|
|
|
#if HAS_STD_FILESYSTEM_EXPERIMENTAL
|
|
|
|
#include <experimental/filesystem>
|
|
|
|
namespace fs = std::experimental::filesystem;
|
|
|
|
#else
|
|
|
|
#include <filesystem>
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
#endif
|
2019-06-10 19:41:13 +00:00
|
|
|
#else
|
2019-06-12 18:39:29 +00:00
|
|
|
#include <boost/filesystem/operations.hpp> // for create_directories, exists
|
|
|
|
#include <boost/filesystem/path.hpp> // for path, operator<<
|
|
|
|
#include <boost/filesystem/path_traits.hpp> // for filesystem
|
|
|
|
#include <boost/system/error_code.hpp> // for error_code
|
2019-06-10 19:41:13 +00:00
|
|
|
namespace fs = boost::filesystem;
|
|
|
|
namespace errorlib = boost::system;
|
|
|
|
#endif
|
2020-11-22 10:05:16 +00:00
|
|
|
// clang-format on
|
2015-11-14 13:17:02 +00:00
|
|
|
|
2018-10-27 22:42:28 +00:00
|
|
|
GeoJSON_Printer::GeoJSON_Printer(const std::string& base_path)
|
2015-12-02 22:44:07 +00:00
|
|
|
{
|
|
|
|
first_pos = true;
|
2018-10-27 22:42:28 +00:00
|
|
|
geojson_base_path = base_path;
|
2019-06-10 19:41:13 +00:00
|
|
|
fs::path full_path(fs::current_path());
|
|
|
|
const fs::path p(geojson_base_path);
|
|
|
|
if (!fs::exists(p))
|
2018-10-27 22:42:28 +00:00
|
|
|
{
|
|
|
|
std::string new_folder;
|
2020-12-30 20:49:29 +00:00
|
|
|
for (const auto& folder : fs::path(geojson_base_path))
|
2018-10-27 22:42:28 +00:00
|
|
|
{
|
|
|
|
new_folder += folder.string();
|
2019-06-10 19:41:13 +00:00
|
|
|
errorlib::error_code ec;
|
|
|
|
if (!fs::exists(new_folder))
|
2018-10-27 22:42:28 +00:00
|
|
|
{
|
2019-06-10 19:41:13 +00:00
|
|
|
if (!fs::create_directory(new_folder, ec))
|
2018-10-27 22:42:28 +00:00
|
|
|
{
|
2020-07-07 16:53:50 +00:00
|
|
|
std::cout << "Could not create the " << new_folder << " folder.\n";
|
2018-10-27 22:42:28 +00:00
|
|
|
geojson_base_path = full_path.string();
|
|
|
|
}
|
|
|
|
}
|
2019-06-10 19:41:13 +00:00
|
|
|
new_folder += fs::path::preferred_separator;
|
2018-10-27 22:42:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
geojson_base_path = p.string();
|
|
|
|
}
|
2018-12-02 14:42:38 +00:00
|
|
|
if (geojson_base_path != ".")
|
2018-10-27 22:42:28 +00:00
|
|
|
{
|
2020-07-07 16:53:50 +00:00
|
|
|
std::cout << "GeoJSON files will be stored at " << geojson_base_path << '\n';
|
2018-10-27 22:42:28 +00:00
|
|
|
}
|
|
|
|
|
2019-06-10 19:41:13 +00:00
|
|
|
geojson_base_path = geojson_base_path + fs::path::preferred_separator;
|
2015-12-02 22:44:07 +00:00
|
|
|
}
|
2015-11-14 13:17:02 +00:00
|
|
|
|
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
GeoJSON_Printer::~GeoJSON_Printer()
|
2015-11-14 13:17:02 +00:00
|
|
|
{
|
2020-06-25 00:50:07 +00:00
|
|
|
DLOG(INFO) << "GeoJSON printer destructor called.";
|
2019-02-10 20:55:51 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
GeoJSON_Printer::close_file();
|
|
|
|
}
|
|
|
|
catch (const std::exception& e)
|
|
|
|
{
|
|
|
|
std::cerr << e.what() << '\n';
|
|
|
|
}
|
2015-11-14 13:17:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-12-02 14:42:38 +00:00
|
|
|
bool GeoJSON_Printer::set_headers(const std::string& filename, bool time_tag_name)
|
2015-11-14 13:17:02 +00:00
|
|
|
{
|
2020-07-19 07:39:32 +00:00
|
|
|
const boost::posix_time::ptime pt = boost::posix_time::second_clock::local_time();
|
|
|
|
const tm timeinfo = boost::posix_time::to_tm(pt);
|
2015-11-22 13:43:52 +00:00
|
|
|
|
|
|
|
if (time_tag_name)
|
|
|
|
{
|
|
|
|
std::stringstream strm0;
|
2017-08-11 03:18:38 +00:00
|
|
|
const int year = timeinfo.tm_year - 100;
|
2015-11-22 13:43:52 +00:00
|
|
|
strm0 << year;
|
2017-08-11 03:18:38 +00:00
|
|
|
const int month = timeinfo.tm_mon + 1;
|
2018-03-03 01:03:39 +00:00
|
|
|
if (month < 10)
|
2015-11-22 13:43:52 +00:00
|
|
|
{
|
|
|
|
strm0 << "0";
|
|
|
|
}
|
|
|
|
strm0 << month;
|
2017-08-11 03:18:38 +00:00
|
|
|
const int day = timeinfo.tm_mday;
|
2018-03-03 01:03:39 +00:00
|
|
|
if (day < 10)
|
2015-11-22 13:43:52 +00:00
|
|
|
{
|
|
|
|
strm0 << "0";
|
|
|
|
}
|
|
|
|
strm0 << day << "_";
|
2017-08-11 03:18:38 +00:00
|
|
|
const int hour = timeinfo.tm_hour;
|
2018-03-03 01:03:39 +00:00
|
|
|
if (hour < 10)
|
2015-11-22 13:43:52 +00:00
|
|
|
{
|
|
|
|
strm0 << "0";
|
|
|
|
}
|
|
|
|
strm0 << hour;
|
2017-08-11 03:18:38 +00:00
|
|
|
const int min = timeinfo.tm_min;
|
2018-03-03 01:03:39 +00:00
|
|
|
if (min < 10)
|
2015-11-22 13:43:52 +00:00
|
|
|
{
|
|
|
|
strm0 << "0";
|
|
|
|
}
|
|
|
|
strm0 << min;
|
2017-08-11 03:18:38 +00:00
|
|
|
const int sec = timeinfo.tm_sec;
|
2018-03-03 01:03:39 +00:00
|
|
|
if (sec < 10)
|
2015-11-22 13:43:52 +00:00
|
|
|
{
|
|
|
|
strm0 << "0";
|
|
|
|
}
|
|
|
|
strm0 << sec;
|
|
|
|
|
2018-03-03 01:03:39 +00:00
|
|
|
filename_ = filename + "_" + strm0.str() + ".geojson";
|
2015-11-22 13:43:52 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
filename_ = filename + ".geojson";
|
|
|
|
}
|
2018-10-27 22:42:28 +00:00
|
|
|
filename_ = geojson_base_path + filename_;
|
2015-11-22 13:43:52 +00:00
|
|
|
|
|
|
|
geojson_file.open(filename_.c_str());
|
|
|
|
|
2015-11-14 19:41:28 +00:00
|
|
|
first_pos = true;
|
2015-11-14 13:17:02 +00:00
|
|
|
if (geojson_file.is_open())
|
|
|
|
{
|
|
|
|
DLOG(INFO) << "GeoJSON printer writing on " << filename.c_str();
|
2015-11-14 19:41:28 +00:00
|
|
|
|
2015-11-14 13:17:02 +00:00
|
|
|
// Set iostream numeric format and precision
|
2018-12-02 14:42:38 +00:00
|
|
|
geojson_file.setf(geojson_file.std::ofstream::fixed, geojson_file.std::ofstream::floatfield);
|
2015-11-14 13:17:02 +00:00
|
|
|
geojson_file << std::setprecision(14);
|
2015-11-14 19:41:28 +00:00
|
|
|
|
|
|
|
// Writing the header
|
2020-07-07 16:53:50 +00:00
|
|
|
geojson_file << "{\n";
|
|
|
|
geojson_file << R"( "type": "Feature",)" << '\n';
|
|
|
|
geojson_file << " \"properties\": {\n";
|
|
|
|
geojson_file << R"( "name": "Locations generated by GNSS-SDR" )" << '\n';
|
|
|
|
geojson_file << " },\n";
|
|
|
|
geojson_file << " \"geometry\": {\n";
|
|
|
|
geojson_file << R"( "type": "MultiPoint",)" << '\n';
|
|
|
|
geojson_file << " \"coordinates\": [\n";
|
2015-11-14 13:17:02 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-03 21:08:19 +00:00
|
|
|
|
2020-07-07 16:53:50 +00:00
|
|
|
std::cout << "File " << filename_ << " cannot be saved. Wrong permissions?\n";
|
2018-12-03 21:08:19 +00:00
|
|
|
return false;
|
2015-11-14 13:17:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-07-26 21:56:36 +00:00
|
|
|
bool GeoJSON_Printer::print_position(const Pvt_Solution* const position, bool print_average_values)
|
2015-11-14 13:17:02 +00:00
|
|
|
{
|
|
|
|
double latitude;
|
|
|
|
double longitude;
|
|
|
|
double height;
|
|
|
|
|
|
|
|
if (print_average_values == false)
|
|
|
|
{
|
2020-06-18 09:49:28 +00:00
|
|
|
latitude = position->get_latitude();
|
|
|
|
longitude = position->get_longitude();
|
|
|
|
height = position->get_height();
|
2015-11-14 13:17:02 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-06-18 09:49:28 +00:00
|
|
|
latitude = position->get_avg_latitude();
|
|
|
|
longitude = position->get_avg_longitude();
|
|
|
|
height = position->get_avg_height();
|
2015-11-14 13:17:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (geojson_file.is_open())
|
|
|
|
{
|
2015-11-14 19:41:28 +00:00
|
|
|
if (first_pos == true)
|
|
|
|
{
|
|
|
|
geojson_file << " [" << longitude << ", " << latitude << ", " << height << "]";
|
|
|
|
first_pos = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-07-07 16:53:50 +00:00
|
|
|
geojson_file << ",\n";
|
2015-11-14 19:41:28 +00:00
|
|
|
geojson_file << " [" << longitude << ", " << latitude << ", " << height << "]";
|
|
|
|
}
|
2015-11-14 13:17:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-12-03 21:08:19 +00:00
|
|
|
return false;
|
2015-11-14 13:17:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool GeoJSON_Printer::close_file()
|
|
|
|
{
|
|
|
|
if (geojson_file.is_open())
|
|
|
|
{
|
2020-07-07 16:53:50 +00:00
|
|
|
geojson_file << '\n';
|
|
|
|
geojson_file << " ]\n";
|
|
|
|
geojson_file << " }\n";
|
|
|
|
geojson_file << "}\n";
|
2015-11-14 13:17:02 +00:00
|
|
|
geojson_file.close();
|
2015-11-14 19:41:28 +00:00
|
|
|
|
|
|
|
// if nothing is written, erase the file
|
|
|
|
if (first_pos == true)
|
|
|
|
{
|
2019-07-13 12:28:48 +00:00
|
|
|
errorlib::error_code ec;
|
|
|
|
if (!fs::remove(fs::path(filename_), ec))
|
2019-02-11 20:13:02 +00:00
|
|
|
{
|
|
|
|
LOG(INFO) << "Error deleting temporary file";
|
|
|
|
}
|
2015-11-14 19:41:28 +00:00
|
|
|
}
|
2015-11-14 13:17:02 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-12-03 21:08:19 +00:00
|
|
|
return false;
|
2015-11-14 13:17:02 +00:00
|
|
|
}
|