27 #include "../../../color/RGBAColor.h"
28 #include "../../../common/progress/TaskProgress.h"
29 #include "../../../dataaccess/dataset/DataSet.h"
30 #include "../../../dataaccess/dataset/DataSetType.h"
31 #include "../../../dataaccess/dataset/ObjectIdSet.h"
32 #include "../../../dataaccess/utils/Utils.h"
33 #include "../../../datatype.h"
34 #include "../../../raster.h"
35 #include "../../../maptools/ExternalGraphicRendererManager.h"
36 #include "../../../maptools/MarkRendererManager.h"
37 #include "../../../qt/widgets/Utils.h"
38 #include "../../../se/Utils.h"
39 #include "../../../se.h"
40 #include "../../../statistics/core/Enums.h"
41 #include "../../../statistics/core/NumericStatisticalSummary.h"
42 #include "../../../statistics/core/StringStatisticalSummary.h"
43 #include "../../../statistics/core/SummaryFunctions.h"
44 #include "../../../statistics/core/Utils.h"
55 #include <boost/lexical_cast.hpp>
56 #include <boost/ptr_container/ptr_vector.hpp>
67 static double cof[6]= {76.18009173, -86.50532033, 24.01409822,
68 -1.231739516, 0.120858003e-2, -0.536382e-5};
73 tmp -= (x+0.5)*log(tmp);
81 return (
float)(-tmp + log(2.50662827465*ser));
84 void gser(
float * gamser,
float a,
float x,
float * gln )
97 printf (
"x less than 0 in the GSER routine");
106 del= sum = (float)(1.0/a);
107 for (n=1; n <= ITMAX; n++)
112 if (fabs (del) < fabs (sum)*EPS)
114 *gamser = (float)(sum*exp(-x+a*log(x)-(*gln)));
122 void gcf(
float * gammcf,
float a,
float x,
float * gln )
128 float gold=0.0, g, fac=1.0, b1=1.0;
129 float b0=0.0, anf, ana, an, a1, a0=1.0;
135 for(n=1; n<= ITMAX; n++)
139 a0 = (a1+a0*ana)*fac;
140 b0 = (b1+b0*ana)*fac;
146 fac = (float)(1.0/a1);
148 if (fabs((g-gold)/g) < EPS)
150 *gammcf = (float)(exp (-x+a*log(x)-(*gln))*g);
160 float gamser, gammcf, gln;
164 gser (&gamser, a, x, &gln);
169 gcf (&gammcf, a, x, &gln);
170 return (
float)(1.0 - gammcf);
176 return x < 0.0 ? -
gammp(0.5, x*x) :
gammp(0.5,x*x);
179 double getDouble(
const std::string& value, std::vector<std::string>& sVector)
182 for(std::size_t i=0;i<sVector.size();++i)
184 if(value==sVector[i])
188 sVector.push_back(value);
189 return (
double)sVector.size()-1;
197 boost::gregorian::date basedate(1400, 01, 01);
198 boost::gregorian::date_duration days = ti->getDate().getDate() - basedate;
199 long long int seconds = ti->getTime().getTimeDuration().total_seconds();
200 long long int dias = days.days();
201 double v = (double) dias * 86400 + seconds;
206 std::auto_ptr<te::dt::Date> d ((
te::dt::Date*)dateTime);
207 boost::gregorian::date basedate(1400, 01, 01);
208 boost::gregorian::date_duration days = d->getDate() - basedate;
209 double v = days.days();
251 res = boost::lexical_cast<
double>(dataset->
getNumeric(propId));
363 std::map<std::string, std::vector<std::pair<double, double> > > valuesToSummarize,
367 std::map<std::string, std::vector<te::da::ObjectId*> >::iterator oidsIt;
368 std::map<std::string, std::vector<std::pair<double, double> > > ::iterator valuesIt;
371 for(valuesIt = valuesToSummarize.begin(); valuesIt != valuesToSummarize.end(); ++valuesIt)
375 if((*valuesIt).second.size() > 1 && (stat != -1))
377 std::vector<double> xValues;
378 std::vector<double> yValues;
379 std::vector<te::da::ObjectId*> oids;
381 for(
size_t i = 0; i < (*valuesIt).second.size(); ++i)
383 xValues.push_back((*valuesIt).second[i].first);
384 yValues.push_back((*valuesIt).second[i].second);
385 oids = oidsToSummarize[(*valuesIt).first];
388 double summarizedXValue, summarizedYValue;
396 for(
size_t j = 0; j < oids.size(); ++j)
397 scatter->
addData(summarizedXValue, summarizedYValue, oids[j]);
403 double xValue = (*valuesIt).second[0].first;
404 double yValue = (*valuesIt).second[0].second;
406 std::vector<te::da::ObjectId*> oids;
407 oids = oidsToSummarize[(*valuesIt).first];
409 scatter->
addData(xValue, yValue, oids[0]);
427 void buildNumericFrequencies(
int slices,
int stat, std::map<std::string, std::vector<std::pair<double, te::da::ObjectId*> > > valuestoSummarize,
428 std::vector<double>& intervals, std::map<
double, std::vector<te::da::ObjectId*> >& intervalToOIds,
429 std::vector< unsigned int>& frequencies,
double& minValue,
double& interval)
432 std::map<std::string, std::vector<std::pair<double, te::da::ObjectId*> > >::iterator valuesIt;
433 std::vector<std::pair<double, std::vector<te::da::ObjectId*> > > summarizedValuesToOId;
436 for(valuesIt = valuestoSummarize.begin(); valuesIt != valuestoSummarize.end(); ++valuesIt)
439 if((*valuesIt).second.size() > 1 && stat != -1)
441 std::vector<double> values;
442 std::vector<te::da::ObjectId*> oids;
443 for(
size_t i = 0; i < (*valuesIt).second.size(); ++i)
445 values.push_back((*valuesIt).second[i].first);
446 oids.push_back((*valuesIt).second[i].second);
449 double summarizedValue;
452 summarizedValuesToOId.push_back(std::make_pair(summarizedValue, oids));
456 for(
size_t i = 0; i < (*valuesIt).second.size(); ++i)
458 std::vector<te::da::ObjectId*> oids;
459 oids.push_back((*valuesIt).second[i].second);
460 summarizedValuesToOId.push_back(std::make_pair((*valuesIt).second[i].first, oids));
465 double maxValue = -std::numeric_limits<double>::max();
467 for(
size_t i = 0; i < summarizedValuesToOId.size(); ++i)
469 double currentValue = summarizedValuesToOId[i].first;
472 if(minValue > currentValue)
473 minValue = currentValue;
474 if(maxValue < currentValue)
475 maxValue = currentValue;
479 interval = ((maxValue * 1.000001 - minValue) / slices);
482 for (
double i = minValue; i <(maxValue+interval); i+=interval)
484 intervals.push_back(i);
485 std::vector<te::da::ObjectId*> valuesOIds;
486 intervalToOIds.insert(std::make_pair(i, valuesOIds));
489 frequencies.resize(intervals.size(), 0);
492 for(
size_t i = 0; i < summarizedValuesToOId.size(); ++i)
494 double currentValue = summarizedValuesToOId[i].first;
495 for (
size_t j = 0; j<intervals.size(); ++j)
497 if((currentValue >= intervals[j]) && (currentValue <= intervals[j+1]))
499 for(
size_t k= 0; k < summarizedValuesToOId[i].second.size(); ++k)
500 intervalToOIds.at(intervals[j]).push_back(summarizedValuesToOId[i].second[k]);
519 void buildStringFrequencies(
int stat, std::map<std::string, std::vector<std::pair<std::string, te::da::ObjectId*> > > valuestoSummarize,
520 std::map<std::string, std::vector<te::da::ObjectId*> >& intervalToOIds,
521 std::vector< unsigned int>& frequencies)
524 std::map<std::string, std::vector<std::pair<std::string, te::da::ObjectId*> > >::iterator valuesIt;
525 std::map<std::string, std::vector<te::da::ObjectId*> >::iterator intervalsIt;
526 std::vector<std::pair<std::string, std::vector<te::da::ObjectId*> > > summarizedValuesToOId;
529 for(valuesIt = valuestoSummarize.begin(); valuesIt != valuestoSummarize.end(); ++valuesIt)
532 if((*valuesIt).second.size() > 1 && (stat != -1))
534 std::vector<std::string> values;
535 std::vector<te::da::ObjectId*> oids;
536 for(
size_t i = 0; i < (*valuesIt).second.size(); ++i)
538 values.push_back((*valuesIt).second[i].first);
539 oids.push_back((*valuesIt).second[i].second);
542 std::string summarizedValue;
545 summarizedValuesToOId.push_back(std::make_pair(summarizedValue, oids));
549 for(
size_t i = 0; i < (*valuesIt).second.size(); ++i)
551 std::vector<te::da::ObjectId*> oids;
552 oids.push_back((*valuesIt).second[i].second);
553 summarizedValuesToOId.push_back(std::make_pair((*valuesIt).second[i].first, oids));
559 for(
size_t i = 0; i < summarizedValuesToOId.size(); ++i)
562 std::string currentValue = summarizedValuesToOId[i].first;
563 for ( j= 0, intervalsIt = intervalToOIds.begin(); intervalsIt != intervalToOIds.end(); ++intervalsIt,++j)
565 if(currentValue == (*intervalsIt).first)
567 for(
size_t k= 0; k < summarizedValuesToOId[i].second.size(); ++k)
568 intervalToOIds.at(currentValue).push_back(summarizedValuesToOId[i].second[k]);
570 frequencies[j] = frequencies[j]++;
585 std::vector<size_t>::iterator it;
586 std::vector<std::string> propNames;
588 for(it=pkeys.begin(); it!=pkeys.end(); ++it)
599 valuesOIDs.push_back(oid);
606 std::vector<std::size_t> objIdIdx;
609 std::map<double, std::vector<te::da::ObjectId*> > valuesIdsByinterval;
610 std::vector<te::da::ObjectId*> valuesOIds;
613 if(rpos != std::string::npos)
615 std::auto_ptr<te::rst::Raster> raster(dataset->
getRaster(rpos));
616 double xDummyValue = raster->getBand(propX)->getProperty()->m_noDataValue;
617 double yDummyValue = raster->getBand(propY)->getProperty()->m_noDataValue;
619 unsigned int nCol = raster->getNumberOfColumns();
620 unsigned int nLin = raster->getNumberOfRows();
628 unsigned int maxInputPoints = (nCol * nLin) * 0.10;
633 while (pit != pitend)
644 if ((val1 == xDummyValue) || (val2 == yDummyValue))
651 newScatter->
addX(val1);
652 newScatter->
addY(val2);
659 for (
unsigned int c=0; c < nCol; ++c)
665 for (
unsigned int r=0; r <nLin; ++r)
668 raster->getValue(c, r, val1, propX);
669 raster->getValue(c, r, val2, propY);
671 if ((val1 == xDummyValue) || (val2 == yDummyValue))
674 newScatter->
addX(val1);
675 newScatter->
addY(val2);
688 std::pair<std::string, int> dsProps;
692 std::map<std::string, std::vector<te::da::ObjectId*> > oidsToSummarize;
693 std::map<std::string, std::vector<std::pair<double, double> > > valuesToSummarize;
707 double x_doubleValue = 0.;
708 double y_doubleValue = 0.;
712 if(dataset->
isNull(propX))
715 x_doubleValue =
getDouble(dataset, propX);
719 if(dataset->
isNull(propX))
722 std::auto_ptr<te::dt::DateTime> dateTime = dataset->
getDateTime(propX);
723 x_doubleValue =
getDouble(dateTime.release());
729 if(dataset->
isNull(propY))
731 y_doubleValue =
getDouble(dataset, propY);
735 if(dataset->
isNull(propY))
738 std::auto_ptr<te::dt::DateTime> dateTime = dataset->
getDateTime(propY);
739 y_doubleValue =
getDouble(dateTime.release());
747 newScatter->
addData(x_doubleValue, y_doubleValue, currentOid);
752 valuesToSummarize[
te::da::getBasePkey(currentOid, dsProps)].push_back(std::make_pair(x_doubleValue, y_doubleValue));
770 chartStyle->
setTitle(QString::fromStdString(
"Scatter"));
777 chart->attach(chartDisplay);
781 displayWidget->show();
782 displayWidget->setWindowTitle(
"Scatter");
783 return displayWidget;
791 std::vector<std::size_t> objIdIdx;
794 if(rpos != std::string::npos)
796 std::auto_ptr<te::rst::Raster> rstptr = dataset->
getRaster(rpos);
797 std::map<double, unsigned int> values = rstptr->getBand(propId)->getHistogramR(0, 0, 0, 0, slices);
802 const std::complex<double>* cmin = rsMin->at(0).m_minVal;
803 const std::complex<double>* cmax = rsMax->at(0).m_maxVal;
805 double min = cmin->real();
806 double max = cmax->real();
808 for(std::map<double, unsigned int>::iterator it = values.begin(); it != values.end(); ++it)
813 newHistogram->
setMinValue(rstptr->getBand(propId)->getMinValue(
true).real());
815 newHistogram->
setInterval((max * 1.000001 - min) / slices);
825 newHistogram->
setType(propType);
831 std::pair<std::string, int> dsProps;
835 std::map<std::string, std::vector<std::pair<double, te::da::ObjectId*> > > valuesToSummarize;
847 if(dataset->
isNull(propId))
855 double currentValue =
getDouble(dataset, propId);
857 valuesToSummarize[
te::da::getBasePkey(currentOid, dsProps)].push_back(std::make_pair(currentValue, currentOid));
863 double minValue = std::numeric_limits<double>::max();
866 double interval = std::numeric_limits<double>::max();
869 std::vector<double> intervals;
872 std::vector< unsigned int> frequencies;
875 std::map<double, std::vector<te::da::ObjectId*> > intervalToOIds;
878 buildNumericFrequencies(slices, stat, valuesToSummarize, intervals, intervalToOIds, frequencies, minValue, interval);
881 for (
unsigned int i= 0; i<intervals.size(); ++i)
884 newHistogram->
insert(std::make_pair(data, frequencies[i]), intervalToOIds.at(intervals[i]));
901 std::vector<std::size_t> objIdIdx;
907 if(rpos != std::string::npos)
909 std::auto_ptr<te::rst::Raster> rstptr = dataset->
getRaster(rpos);
910 std::map<double, unsigned int> values = rstptr->getBand(propId)->getHistogramR();
912 for(std::map<double, unsigned int>::iterator it = values.begin(); it != values.end(); ++it)
917 newHistogram->
setMinValue(rstptr->getBand(propId)->getMinValue(
true).real());
923 newHistogram->
setType(propType);
926 std::vector< unsigned int> frequencies;
930 std::set <std::string> intervals;
931 std::set <std::string>::iterator intervalsIt;
932 std::map<std::string, std::vector<te::da::ObjectId*> > valuesIdsByinterval;
933 std::vector<te::da::ObjectId*> valuesOIds;
936 std::pair<std::string, int> dsProps;
940 std::map<std::string, std::vector<std::pair<std::string, te::da::ObjectId*> > > valuesToSummarize;
947 if(dataset->
isNull(propId))
955 std::string interval = dataset->
getString(propId);
958 intervals.insert(interval);
962 for (intervalsIt = intervals.begin(); intervalsIt != intervals.end(); ++intervalsIt)
964 valuesIdsByinterval.insert(make_pair((*intervalsIt), valuesOIds));
967 frequencies.resize(intervals.size(), 0);
975 if(dataset->
isNull(propId))
983 std::string currentValue = dataset->
getString(propId);
985 valuesToSummarize[
te::da::getBasePkey(currentOid, dsProps)].push_back(std::make_pair(currentValue, currentOid));
994 for (i= 0, intervalsIt = intervals.begin(); intervalsIt != intervals.end(); ++intervalsIt,++i)
997 newHistogram->
insert(std::make_pair(data, frequencies[i]), valuesIdsByinterval.at(*intervalsIt));
1004 return newHistogram;
1023 chartStyle->
setTitle(QString::fromStdString(
"Histogram"));
1025 chartStyle->
setAxisY(QString::fromStdString(
"Frequency"));
1030 chart->
attach(chartDisplay);
1034 displayWidget->show();
1035 displayWidget->setWindowTitle(
"Histogram");
1036 return displayWidget;
1046 chartStyle->
setTitle(QString::fromStdString(
"Histogram"));
1048 chartStyle->
setAxisY(QString::fromStdString(
"Frequency"));
1053 chart->
attach(chartDisplay);
1057 displayWidget->show();
1058 displayWidget->setWindowTitle(
"Histogram");
1059 return displayWidget;
1064 QwtText* result =
new QwtText(text.c_str());
1065 result->setBackgroundBrush(QBrush(QColor(0, 255, 0)));
1074 QwtText* result =
new QwtText(QString(text.c_str()));
1082 QwtSymbol* symbol =
new QwtSymbol( QwtSymbol::Ellipse, QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 8, 8 ));
1085 size_t height = 8, width = 8;
1110 pixmap = QPixmap::fromImage(*qimg);
1113 symbol->setPixmap(pixmap);
1114 symbol->setSize((
int)width, (
int)height);
1128 QwtPlotCurve *baseCurve =
new QwtPlotCurve(
"Base Values");
1129 baseCurve->setOrientation( Qt::Horizontal );
1131 QwtPlotCurve *normalCurve =
new QwtPlotCurve(
"Normalizeed Values");
1132 normalCurve->setOrientation( Qt::Horizontal );
1139 std::vector<double> yValues;
1143 for(
int i = 0; i < nss.
m_count; ++i)
1145 double curXValue = xValues[i];
1147 xValues[i] = curXValue;
1149 std::sort(xValues.begin(), xValues.end());
1152 for(
int i = 0; i < nss.
m_count; ++i)
1154 double curYValue = xValues[i];
1156 curYValue = 0.5 + (
errFunction ((
float)(curYValue/std::sqrt(2.)))/2.);
1159 curYValue = -curYValue;
1160 curYValue = 0.5 - (
errFunction ((
float)(curYValue/std::sqrt(2.)))/2.);
1162 yValues.push_back(curYValue);
1165 QVector<QPointF> samples, normalSamples;
1166 for (
int i = 0; i < nss.
m_count; ++i)
1168 double val = ((double)i+(
double)1.0)/(double)nss.
m_count;
1169 normalSamples += QPointF( val, val);
1170 samples += QPointF( val, yValues[i]);
1173 baseCurve->setSamples(samples);
1174 normalCurve->setSamples(normalSamples);
1177 QPen normalCurvePen;
1178 normalCurvePen.setColor(QColor(Qt::green));
1180 normalCurvePen.setWidth(0);
1181 baseCurve->setPen(normalCurvePen);
1183 QPen normalProbCurvePen;
1184 normalProbCurvePen.setColor(QColor(Qt::red));
1186 normalProbCurvePen.setWidth(0);
1187 normalCurve->setPen(normalProbCurvePen);
1193 chartStyle->
setAxisY(QString::fromStdString(
"Probability"));
1199 baseCurve->attach(chartDisplay);
1200 normalCurve->attach(chartDisplay);
1204 displayWidget->setWindowTitle(
"Normal Distribution");
1205 return displayWidget;
TESTATEXPORT void GetNumericStatisticalSummary(std::vector< double > &values, te::stat::NumericStatisticalSummary &ss, double nullVal)
virtual std::auto_ptr< te::dt::DateTime > getDateTime(std::size_t i) const =0
Method for retrieving a date and time attribute value.
SimpleData< std::string, STRING_TYPE > String
virtual boost::int16_t getInt16(std::size_t i) const =0
Method for retrieving a 16-bit integer attribute value (2 bytes long).
A structure to hold the set of statistics from a set of numerical values.
void setMessage(const std::string &message)
Set the task message.
Utility functions for the data access module.
virtual boost::int32_t getInt32(std::size_t i) const =0
Method for retrieving a 32-bit integer attribute value (4 bytes long).
TEDATAACCESSEXPORT ObjectId * GenerateOID(DataSet *dataset, const std::vector< std::string > &names)
const std::vector< ExternalGraphic * > getExternalGraphics() const
A class that models the description of a dataset.
virtual boost::int64_t getInt64(std::size_t i) const =0
Method for retrieving a 64-bit integer attribute value (8 bytes long).
unsigned int getColumn() const
Returns the current column in iterator.
This class can be used to inform the progress of a task.
virtual std::auto_ptr< te::rst::Raster > getRaster(std::size_t i) const =0
Method for retrieving a raster attribute value.
static PointSetIterator end(const te::rst::Raster *r, const std::vector< te::gm::Point * > p)
Returns an iterator referring to after the end of the iterator.
virtual std::string getNumeric(std::size_t i) const =0
Method for retrieving a numeric attribute value.
A Graphic is a graphic symbol with an inherent shape, color(s), and possibly size.
TEDATAACCESSEXPORT void GetOIDDatasetProps(const DataSetType *type, std::pair< std::string, int > &dsProps)
bool isActive() const
Verify if the task is active.
virtual double getDouble(std::size_t i) const =0
Method for retrieving a double attribute value.
This class implements the strategy to iterate with spatial restriction, the iteration occurs inside a...
A class to represent time instant.
void setTotalSteps(int value)
Set the task total stepes.
TESTATEXPORT std::vector< double > GetNumericData(te::da::DataSet *dataSet, const std::string propName)
Returns the values of a numeric type property in a vector of values.
A class to represent a histogram.
virtual bool moveNext()=0
It moves the internal pointer to the next item of the collection.
static RasterSummaryManager & getInstance()
It returns a reference to the singleton instance.
virtual float getFloat(std::size_t i) const =0
Method for retrieving a float attribute value.
A base class for date data types.
virtual std::string getPropertyName(std::size_t i) const =0
It returns the property name at position pos.
virtual bool isNull(std::size_t i) const =0
It checks if the attribute value is NULL.
This class represents an unique id for a data set element.
A Font specifies the text font to use in a text symbolizer.
boost::ptr_vector< BandSummary > RasterSummary
RasterSummary is just a typedef of a boost::ptr_vector.
A class to represent a histogram chart.
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
A Fill specifies the pattern for filling an area geometry.
virtual std::size_t getNumProperties() const =0
It returns the number of properties that composes an item of the dataset.
TESTATEXPORT void GetStringStatisticalSummary(std::vector< std::string > &values, te::stat::StringStatisticalSummary &ss)
A class to represent a scatter's chart.
A class to represent a scatter.
A dataset is the unit of information manipulated by the data access module of TerraLib.
virtual DateTimeType getDateTimeType() const =0
It returns the subtype of the date and time type.
const std::vector< Mark * > getMarks() const
TEDATAACCESSEXPORT void GetOIDPropertyPos(const DataSetType *type, std::vector< std::size_t > &ppos)
A Stroke specifies the appearance of a linear geometry.
A structure to hold the set of statistics from a set of categorical (sample) values.
virtual bool moveBeforeFirst()=0
It moves the internal pointer to a position before the first item in the collection.
SimpleData< double, DOUBLE_TYPE > Double
A helper class for 32-bit RGBA (Red-Green-Blue-Alpha channel) color.
TESEEXPORT int GetInt(const te::se::ParameterValue *param)
It gets the parameter value as an integer.
TEDATAACCESSEXPORT std::string getBasePkey(te::da::ObjectId *oid, std::pair< std::string, int > &dsProps)
TERASTEREXPORT std::vector< te::gm::Point * > GetRandomPointsInRaster(const te::rst::Raster &inputRaster, unsigned int numberOfPoints=1000)
Creates a vector of random positions (points) inside the raster.
A class used to define a chartDisplay's style.
virtual std::string getString(std::size_t i) const =0
Method for retrieving a string value attribute.
TEDATAACCESSEXPORT std::size_t GetFirstPropertyPos(const te::da::DataSet *dataset, int datatype)
A template for atomic data types (integers, floats, strings and others).
virtual int getPropertyDataType(std::size_t i) const =0
It returns the underlying data type of the property at position pos.
A class to represent a chart display.
const ParameterValue * getSize() const
unsigned int getRow() const
Returns the current row in iterator.
static PointSetIterator begin(const te::rst::Raster *r, const std::vector< te::gm::Point * > p)
Returns an iterator referring to the first value of the band.