void TsUtility::testTimeUtilsAddYear() { std::string dateTime = "2016-Apr-12 12:59:00.373302BRT+00"; boost::posix_time::ptime boostDate(boost::posix_time::time_from_string(dateTime)); boost::local_time::time_zone_ptr zone(new boost::local_time::posix_time_zone("+00")); boost::local_time::local_date_time date(boostDate.date(), boostDate.time_of_day(), zone, true); std::shared_ptr< te::dt::TimeInstantTZ > dt(new te::dt::TimeInstantTZ(date)); terrama2::core::TimeUtils::addYear(dt, 3); terrama2::core::TimeUtils::addYear(dt, -1); std::string dateTime2 = "2018-Apr-12 12:59:00.373302BRT+00"; boost::posix_time::ptime boostDate2(boost::posix_time::time_from_string(dateTime2)); boost::local_time::time_zone_ptr zone2(new boost::local_time::posix_time_zone("+00")); boost::local_time::local_date_time date2(boostDate2.date(), boostDate2.time_of_day(), zone2, true); std::shared_ptr< te::dt::TimeInstantTZ > dt2(new te::dt::TimeInstantTZ(date2)); if(*dt.get() != *dt2.get()) QFAIL("Should not be here!"); }
te::dt::AbstractData* terrama2::core::DataAccessorOccurrenceLightning::stringToDate(te::da::DataSet* dataset, const std::vector<std::size_t>& indexes, int /*dstType*/, const std::string& timezone) const { assert(indexes.size() == 1); try { std::string dateTime = dataset->getString(indexes[0]); if(dateTime.empty()) return nullptr; boost::posix_time::ptime boostDate(boost::posix_time::time_from_string(dateTime)); return new te::dt::Date(boostDate.date()); } catch(std::exception& e) { TERRAMA2_LOG_ERROR() << e.what(); } catch(boost::exception& e) { TERRAMA2_LOG_ERROR() << boost::get_error_info<terrama2::ErrorDescription>(e); } catch(...) { TERRAMA2_LOG_ERROR() << "Unknown error"; } return nullptr; }
te::dt::AbstractData* terrama2::core::DataAccessorDcpToa5::stringToTimestamp(te::da::DataSet* dataset, const std::vector<std::size_t>& indexes, int /*dstType*/, const std::string& timezone) const { assert(indexes.size() == 1); try { std::string dateTime = dataset->getAsString(indexes[0]); boost::posix_time::ptime boostDate(boost::posix_time::time_from_string(dateTime)); boost::local_time::time_zone_ptr zone(new boost::local_time::posix_time_zone(timezone)); boost::local_time::local_date_time date(boostDate.date(), boostDate.time_of_day(), zone, true); te::dt::TimeInstantTZ* dt = new te::dt::TimeInstantTZ(date); return dt; } catch(std::exception& e) { TERRAMA2_LOG_ERROR() << e.what(); } catch(boost::exception& e) { TERRAMA2_LOG_ERROR() << boost::get_error_info<terrama2::ErrorDescription>(e); } catch(...) { TERRAMA2_LOG_ERROR() << "Unknown error"; } return nullptr; }
bool terrama2::core::isValidDataSetName(const std::string& mask, const Filter& filter, const std::string& timezone, const std::string& name, std::shared_ptr< te::dt::TimeInstantTZ >& fileTimestamp) { if(!isValidDatedMask(mask)) { QString errMsg = QObject::tr("The mask don't have the minimal needed parameters of a date!"); TERRAMA2_LOG_ERROR() << errMsg; throw terrama2::core::UtilityException() << ErrorDescription(errMsg); } auto regexString = terramaMask2Regex(mask); boost::regex expression(regexString); boost::match_results< std::string::const_iterator > match; if(!boost::regex_match(name, match, expression, boost::match_default)) return false; if((match["YEAR"] != "" || match["YEAR2DIGITS"] != "") && match["MONTH"] != "" && match["DAY"] != "") { std::string ts; if(match["YEAR"] != "") { ts = match["YEAR"].str(); } else { int year = std::stoi(match["YEAR2DIGITS"].str()); if(year < 80) ts = "20" + match["YEAR2DIGITS"].str(); else ts = "19" + match["YEAR2DIGITS"].str(); } ts += match["MONTH"].str(); ts += match["DAY"].str(); // if the name has only date part, it presumes that time is 23:59:59 ts += "T"; ts += (match["HOUR"] == "" ? "23" : match["HOUR"].str()); ts += (match["MINUTES"] == "" ? "59" : match["MINUTES"].str()); ts += (match["SECONDS"] == "" ? "59" : match["SECONDS"].str()); boost::posix_time::ptime boostDate(boost::posix_time::from_iso_string(ts)); boost::local_time::time_zone_ptr zone(new boost::local_time::posix_time_zone(timezone)); boost::local_time::local_date_time date(boostDate.date(), boostDate.time_of_day(), zone, true); fileTimestamp.reset(new te::dt::TimeInstantTZ(date)); if(!isValidTimestamp(filter, fileTimestamp)) return false; } else { fileTimestamp.reset(); } return true; }
bool terrama2::core::isValidDataSetName(const std::string& mask, const Filter& filter, std::string& timezone, const std::string& name, std::shared_ptr< te::dt::TimeInstantTZ >& fileTimestamp) { if(!isValidDatedMask(mask)) { QString errMsg = QObject::tr("The mask don't have the minimal needed parameters of a date!"); TERRAMA2_LOG_ERROR() << errMsg; throw terrama2::core::UtilityException() << ErrorDescription(errMsg); } /* yyyy year with 4 digits [0-9]{4} yy year with 2 digits [0-9]{2} MM month with 2 digits 0[1-9]|1[012] dd day with 2 digits 0[1-9]|[12][0-9]|3[01] hh hout with 2 digits [0-1][0-9]|2[0-4] mm minutes with 2 digits [0-5][0-9] ss seconds with 2 digits [0-5][0-9] */ QString m(mask.c_str()); m.replace("yyyy", "(?<YEAR>[0-9]{4})"); m.replace("yy", "(?<YEAR2DIGITS>[0-9]{2})"); m.replace("MM", "(?<MONTH>0[1-9]|1[012])"); m.replace("dd", "(?<DAY>0[1-9]|[12][0-9]|3[01])"); m.replace("hh", "(?<HOUR>[0-1][0-9]|2[0-4])"); m.replace("mm", "(?<MINUTES>[0-5][0-9])"); m.replace("ss", "(?<SECONDS>[0-5][0-9])"); // add a extension validation in case of the name has it m += "(?<EXTENSIONS>\\..+)?"; boost::regex expression(m.toStdString()); boost::match_results< std::string::const_iterator > match; if(!boost::regex_match(name, match, expression, boost::match_default)) return false; if((match["YEAR"] != "" || match["YEAR2DIGITS"] != "") && match["MONTH"] != "" && match["DAY"] != "") { std::string ts; if(match["YEAR"] != "") { ts = match["YEAR"].str(); } else { int year = std::stoi(match["YEAR2DIGITS"].str()); if(year < 80) ts = "20" + match["YEAR2DIGITS"].str(); else ts = "19" + match["YEAR2DIGITS"].str(); } ts += match["MONTH"].str(); ts += match["DAY"].str(); // if the name has only date part, it presumes that time is 00:00:00 ts += "T"; ts += (match["HOUR"] == "" ? "00" : match["HOUR"].str()); ts += (match["MINUTES"] == "" ? "00" : match["MINUTES"].str()); ts += (match["SECONDS"] == "" ? "00" : match["SECONDS"].str()); boost::posix_time::ptime boostDate(boost::posix_time::from_iso_string(ts)); boost::local_time::time_zone_ptr zone(new boost::local_time::posix_time_zone(timezone)); boost::local_time::local_date_time date(boostDate.date(), boostDate.time_of_day(), zone, true); fileTimestamp.reset(new te::dt::TimeInstantTZ(date)); if(!isValidTimestamp(filter, fileTimestamp)) return false; } else { fileTimestamp.reset(); } return true; }
int main(int argc, char* argv[]) { terrama2::core::TerraMA2Init terramaRaii("example", 0); { QUrl uri; uri.setScheme("postgis"); uri.setHost(QString::fromStdString(TERRAMA2_DATABASE_HOST)); uri.setPort(std::stoi(TERRAMA2_DATABASE_PORT)); uri.setUserName(QString::fromStdString(TERRAMA2_DATABASE_USERNAME)); uri.setPassword(QString::fromStdString(TERRAMA2_DATABASE_PASSWORD)); uri.setPath(QString::fromStdString("/"+TERRAMA2_DATABASE_DBNAME)); //DataProvider information terrama2::core::DataProvider* dataProvider = new terrama2::core::DataProvider(); terrama2::core::DataProviderPtr dataProviderPtr(dataProvider); dataProvider->uri = uri.url().toStdString(); dataProvider->intent = terrama2::core::DataProviderIntent::COLLECTOR_INTENT; dataProvider->dataProviderType = "POSTGIS"; dataProvider->active = true; auto& semanticsManager = terrama2::core::SemanticsManager::getInstance(); //DataSeries information terrama2::core::DataSeries* dataSeries = new terrama2::core::DataSeries(); terrama2::core::DataSeriesPtr dataSeriesPtr(dataSeries); dataSeries->semantics = semanticsManager.getSemantics("OCCURRENCE-postgis"); //DataSet information terrama2::core::DataSetOccurrence* dataSet = new terrama2::core::DataSetOccurrence(); dataSet->active = true; dataSet->format.emplace("table_name", "fires"); dataSet->format.emplace("timestamp_property", "data_pas"); dataSet->format.emplace("geometry_property", "geom"); dataSeries->datasetList.emplace_back(dataSet); //accessing data terrama2::core::DataAccessorOccurrencePostGIS accessor(dataProviderPtr, dataSeriesPtr); //empty filter terrama2::core::Filter filter; boost::local_time::time_zone_ptr zone(new boost::local_time::posix_time_zone("+00")); std::string dateTime = "2015-08-26 16:38:50"; boost::posix_time::ptime boostDate(boost::posix_time::time_from_string(dateTime)); boost::local_time::local_date_time date(boostDate.date(), boostDate.time_of_day(), zone, true); filter.discardBefore = std::make_shared<te::dt::TimeInstantTZ>(date); std::string dateTimeAfter = "2015-08-26 16:38:55"; boost::posix_time::ptime boostDateAfter(boost::posix_time::time_from_string(dateTimeAfter)); boost::local_time::local_date_time dateAfter(boostDateAfter.date(), boostDateAfter.time_of_day(), zone, true); filter.discardAfter = std::make_shared<te::dt::TimeInstantTZ>(dateAfter); std::string boundingBoxWkt = "POLYGON((-51.11 -17.74, -41.11 -17.74, -41.11 -20.83, -51.11 -20.83, -51.11 -17.74))"; te::gm::Geometry* geometry = te::gm::WKTReader::read(boundingBoxWkt.c_str()); geometry->setSRID(4326); filter.region = std::shared_ptr<te::gm::Geometry>(geometry); auto remover = std::make_shared<terrama2::core::FileRemover>(); terrama2::core::OccurrenceSeriesPtr occurrenceSeries = accessor.getOccurrenceSeries(filter, remover); assert(occurrenceSeries->occurrencesMap().size() == 1); auto teDataSet = (*occurrenceSeries->occurrencesMap().begin()).second.syncDataSet->dataset(); //Print column names and types (DateTime/Double) int dateColumn = -1; int geomColumn = -1; std::string names, types; for(int i = 0; i < teDataSet->getNumProperties(); ++i) { std::string name = teDataSet->getPropertyName(i); names+= name + "\t"; if(name == "data_pas") { types+= "DataTime\t"; dateColumn = i; } else if(name == "geom") { types+= "Geometry\t"; geomColumn = i; } else types+= "Int\t"; } std::cout << names << std::endl; std::cout << types << std::endl; //Print values teDataSet->moveBeforeFirst(); while(teDataSet->moveNext()) { for(int i = 0; i < teDataSet->getNumProperties(); ++i) { if(teDataSet->isNull(i)) { std::cout << "NULL"; continue; } if(i == dateColumn) { std::shared_ptr<te::dt::DateTime> dateTime = teDataSet->getDateTime(i); std::cout << dateTime->toString(); } else if(i == geomColumn) { std::cout << "<<Geometry>>"; } else { std::cout << teDataSet->getInt32(i); } std::cout << "\t"; } std::cout << std::endl; } std::cout << "\ndataset size: " << teDataSet->size() << std::endl; } return 0; }