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;
372 for(valuesIt = valuesToSummarize.begin(); valuesIt != valuesToSummarize.end(); ++valuesIt)
374 if((*valuesIt).second.size() > 1 && (stat != -1))
376 std::vector<double> xValues;
377 std::vector<double> yValues;
378 std::vector<te::da::ObjectId*> oids;
380 for(
size_t i = 0; i < (*valuesIt).second.size(); ++i)
382 xValues.push_back((*valuesIt).second[i].first);
383 yValues.push_back((*valuesIt).second[i].second);
384 oids = oidsToSummarize[(*valuesIt).first];
387 double summarizedXValue, summarizedYValue;
395 for(
size_t j = 0; j < oids.size(); ++j)
396 scatter->
addData(summarizedXValue, summarizedYValue, oids[j]);
402 double xValue = (*valuesIt).second[0].first;
403 double yValue = (*valuesIt).second[0].second;
405 std::vector<te::da::ObjectId*> oids;
406 oids = oidsToSummarize[(*valuesIt).first];
408 scatter->
addData(xValue, yValue, oids[0]);
426 void buildNumericFrequencies(
int slices,
int stat, std::map<std::string, std::vector<std::pair<double, te::da::ObjectId*> > > valuestoSummarize,
427 std::vector<double>& intervals, std::map<
double, std::vector<te::da::ObjectId*> >& intervalToOIds,
428 std::vector< unsigned int>& frequencies,
double& minValue,
double& interval)
431 std::map<std::string, std::vector<std::pair<double, te::da::ObjectId*> > >::iterator valuesIt;
432 std::vector<std::pair<double, std::vector<te::da::ObjectId*> > > summarizedValuesToOId;
436 for(valuesIt = valuestoSummarize.begin(); valuesIt != valuestoSummarize.end(); ++valuesIt)
438 if((*valuesIt).second.size() > 1 && stat != -1)
440 std::vector<double> values;
441 std::vector<te::da::ObjectId*> oids;
442 for(
size_t i = 0; i < (*valuesIt).second.size(); ++i)
444 values.push_back((*valuesIt).second[i].first);
445 oids.push_back((*valuesIt).second[i].second);
448 double summarizedValue;
451 summarizedValuesToOId.push_back(std::make_pair(summarizedValue, oids));
455 for(
size_t i = 0; i < (*valuesIt).second.size(); ++i)
457 std::vector<te::da::ObjectId*> oids;
458 oids.push_back((*valuesIt).second[i].second);
459 summarizedValuesToOId.push_back(std::make_pair((*valuesIt).second[i].first, oids));
464 double maxValue = -std::numeric_limits<double>::max();
466 for(
size_t i = 0; i < summarizedValuesToOId.size(); ++i)
468 double currentValue = summarizedValuesToOId[i].first;
471 if(minValue > currentValue)
472 minValue = currentValue;
473 if(maxValue < currentValue)
474 maxValue = currentValue;
478 interval = ((maxValue - minValue)/slices);
481 for (
double i = minValue; i <(maxValue+interval); i+=interval)
483 intervals.push_back(i);
484 std::vector<te::da::ObjectId*> valuesOIds;
485 intervalToOIds.insert(std::make_pair(i, valuesOIds));
488 frequencies.resize(intervals.size(), 0);
491 for(
size_t i = 0; i < summarizedValuesToOId.size(); ++i)
493 double currentValue = summarizedValuesToOId[i].first;
494 for (
size_t j = 0; j<intervals.size(); ++j)
496 if((currentValue >= intervals[j]) && (currentValue <= intervals[j+1]))
498 for(
size_t k= 0; k < summarizedValuesToOId[i].second.size(); ++k)
499 intervalToOIds.at(intervals[j]).push_back(summarizedValuesToOId[i].second[k]);
501 frequencies[j] = frequencies[j]++;
518 void buildStringFrequencies(
int stat, std::map<std::string, std::vector<std::pair<std::string, te::da::ObjectId*> > > valuestoSummarize,
519 std::map<std::string, std::vector<te::da::ObjectId*> >& intervalToOIds,
520 std::vector< unsigned int>& frequencies)
523 std::map<std::string, std::vector<std::pair<std::string, te::da::ObjectId*> > >::iterator valuesIt;
524 std::map<std::string, std::vector<te::da::ObjectId*> >::iterator intervalsIt;
525 std::vector<std::pair<std::string, std::vector<te::da::ObjectId*> > > summarizedValuesToOId;
529 for(valuesIt = valuestoSummarize.begin(); valuesIt != valuestoSummarize.end(); ++valuesIt)
531 if((*valuesIt).second.size() > 1 && (stat != -1))
533 std::vector<std::string> values;
534 std::vector<te::da::ObjectId*> oids;
535 for(
size_t i = 0; i < (*valuesIt).second.size(); ++i)
537 values.push_back((*valuesIt).second[i].first);
538 oids.push_back((*valuesIt).second[i].second);
541 std::string summarizedValue;
544 summarizedValuesToOId.push_back(std::make_pair(summarizedValue, oids));
548 for(
size_t i = 0; i < (*valuesIt).second.size(); ++i)
550 std::vector<te::da::ObjectId*> oids;
551 oids.push_back((*valuesIt).second[i].second);
552 summarizedValuesToOId.push_back(std::make_pair((*valuesIt).second[i].first, oids));
558 for(
size_t i = 0; i < summarizedValuesToOId.size(); ++i)
561 std::string currentValue = summarizedValuesToOId[i].first;
562 for ( j= 0, intervalsIt = intervalToOIds.begin(); intervalsIt != intervalToOIds.end(); ++intervalsIt,++j)
564 if(currentValue == (*intervalsIt).first)
566 for(
size_t k= 0; k < summarizedValuesToOId[i].second.size(); ++k)
567 intervalToOIds.at(currentValue).push_back(summarizedValuesToOId[i].second[k]);
569 frequencies[j] = frequencies[j]++;
584 std::vector<size_t>::iterator it;
585 std::vector<std::string> propNames;
587 for(it=pkeys.begin(); it!=pkeys.end(); ++it)
598 valuesOIDs.push_back(oid);
605 std::vector<std::size_t> objIdIdx;
608 std::map<double, std::vector<te::da::ObjectId*> > valuesIdsByinterval;
609 std::vector<te::da::ObjectId*> valuesOIds;
612 if(rpos != std::string::npos)
614 std::auto_ptr<te::rst::Raster> raster(dataset->
getRaster(rpos));
616 unsigned int nCol = raster->getNumberOfColumns();
617 unsigned int nLin = raster->getNumberOfRows();
625 unsigned int maxInputPoints = (nCol * nLin) * 0.10;
630 while (pit != pitend)
641 newScatter->
addX(val1);
642 newScatter->
addY(val2);
649 for (
unsigned int c=0; c < nCol; ++c)
655 for (
unsigned int r=0; r <nLin; ++r)
658 raster->getValue(c, r, val1, propX);
659 raster->getValue(c, r, val2, propY);
661 newScatter->
addX(val1);
662 newScatter->
addY(val2);
675 std::pair<std::string, int> dsProps;
679 std::map<std::string, std::vector<te::da::ObjectId*> > oidsToSummarize;
680 std::map<std::string, std::vector<std::pair<double, double> > > valuesToSummarize;
694 double x_doubleValue = 0.;
695 double y_doubleValue = 0.;
699 if(dataset->
isNull(propX))
702 x_doubleValue =
getDouble(dataset, propX);
706 if(dataset->
isNull(propX))
709 std::auto_ptr<te::dt::DateTime> dateTime = dataset->
getDateTime(propX);
710 x_doubleValue =
getDouble(dateTime.release());
716 if(dataset->
isNull(propY))
718 y_doubleValue =
getDouble(dataset, propY);
722 if(dataset->
isNull(propY))
725 std::auto_ptr<te::dt::DateTime> dateTime = dataset->
getDateTime(propY);
726 y_doubleValue =
getDouble(dateTime.release());
734 newScatter->
addData(x_doubleValue, y_doubleValue, currentOid);
739 valuesToSummarize[
te::da::getBasePkey(currentOid, dsProps)].push_back(std::make_pair(x_doubleValue, y_doubleValue));
757 chartStyle->
setTitle(QString::fromStdString(
"Scatter"));
764 chart->attach(chartDisplay);
768 displayWidget->show();
769 displayWidget->setWindowTitle(
"Scatter");
770 return displayWidget;
778 std::vector<std::size_t> objIdIdx;
781 if(rpos != std::string::npos)
783 std::auto_ptr<te::rst::Raster> rstptr = dataset->
getRaster(rpos);
784 std::map<double, unsigned int> values = rstptr->getBand(propId)->getHistogramR(0, 0, 0, 0, slices);
786 for(std::map<double, unsigned int>::iterator it = values.begin(); it != values.end(); ++it)
790 newHistogram->
setMinValue(rstptr->getBand(propId)->getMinValue(
true).real());
799 newHistogram->
setType(propType);
805 std::pair<std::string, int> dsProps;
809 std::map<std::string, std::vector<std::pair<double, te::da::ObjectId*> > > valuesToSummarize;
821 if(dataset->
isNull(propId))
829 double currentValue =
getDouble(dataset, propId);
831 valuesToSummarize[
te::da::getBasePkey(currentOid, dsProps)].push_back(std::make_pair(currentValue, currentOid));
837 double minValue = std::numeric_limits<double>::max();
840 double interval = std::numeric_limits<double>::max();
843 std::vector<double> intervals;
846 std::vector< unsigned int> frequencies;
849 std::map<double, std::vector<te::da::ObjectId*> > intervalToOIds;
852 buildNumericFrequencies(slices, stat, valuesToSummarize, intervals, intervalToOIds, frequencies, minValue, interval);
855 for (
unsigned int i= 0; i<intervals.size(); ++i)
858 newHistogram->
insert(std::make_pair(data, frequencies[i]), intervalToOIds.at(intervals[i]));
875 std::vector<std::size_t> objIdIdx;
881 if(rpos != std::string::npos)
883 std::auto_ptr<te::rst::Raster> rstptr = dataset->
getRaster(rpos);
884 std::map<double, unsigned int> values = rstptr->getBand(propId)->getHistogramR();
886 for(std::map<double, unsigned int>::iterator it = values.begin(); it != values.end(); ++it)
891 newHistogram->
setMinValue(rstptr->getBand(propId)->getMinValue(
true).real());
896 newHistogram->
setType(propType);
899 std::vector< unsigned int> frequencies;
903 std::set <std::string> intervals;
904 std::set <std::string>::iterator intervalsIt;
905 std::map<std::string, std::vector<te::da::ObjectId*> > valuesIdsByinterval;
906 std::vector<te::da::ObjectId*> valuesOIds;
909 std::pair<std::string, int> dsProps;
913 std::map<std::string, std::vector<std::pair<std::string, te::da::ObjectId*> > > valuesToSummarize;
920 if(dataset->
isNull(propId))
928 std::string interval = dataset->
getString(propId);
931 intervals.insert(interval);
935 for (intervalsIt = intervals.begin(); intervalsIt != intervals.end(); ++intervalsIt)
937 valuesIdsByinterval.insert(make_pair((*intervalsIt), valuesOIds));
940 frequencies.resize(intervals.size(), 0);
948 if(dataset->
isNull(propId))
956 std::string currentValue = dataset->
getString(propId);
958 valuesToSummarize[
te::da::getBasePkey(currentOid, dsProps)].push_back(std::make_pair(currentValue, currentOid));
967 for (i= 0, intervalsIt = intervals.begin(); intervalsIt != intervals.end(); ++intervalsIt,++i)
970 newHistogram->
insert(std::make_pair(data, frequencies[i]), valuesIdsByinterval.at(*intervalsIt));
996 chartStyle->
setTitle(QString::fromStdString(
"Histogram"));
998 chartStyle->
setAxisY(QString::fromStdString(
"Frequency"));
1003 chart->
attach(chartDisplay);
1007 displayWidget->show();
1008 displayWidget->setWindowTitle(
"Histogram");
1009 return displayWidget;
1019 chartStyle->
setTitle(QString::fromStdString(
"Histogram"));
1021 chartStyle->
setAxisY(QString::fromStdString(
"Frequency"));
1026 chart->
attach(chartDisplay);
1030 displayWidget->show();
1031 displayWidget->setWindowTitle(
"Histogram");
1032 return displayWidget;
1037 QwtText* result =
new QwtText(text.c_str());
1038 result->setBackgroundBrush(QBrush(QColor(0, 255, 0)));
1047 QwtText* result =
new QwtText(QString(text.c_str()));
1055 QwtSymbol* symbol =
new QwtSymbol( QwtSymbol::Ellipse, QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 8, 8 ));
1058 size_t height = 8, width = 8;
1083 pixmap = QPixmap::fromImage(*qimg);
1086 symbol->setPixmap(pixmap);
1087 symbol->setSize(width, height);
1101 QwtPlotCurve *baseCurve =
new QwtPlotCurve(
"Base Values");
1102 baseCurve->setOrientation( Qt::Horizontal );
1104 QwtPlotCurve *normalCurve =
new QwtPlotCurve(
"Normalizeed Values");
1105 normalCurve->setOrientation( Qt::Horizontal );
1112 std::vector<double> yValues;
1116 for(
int i = 0; i < nss.
m_count; ++i)
1118 double curXValue = xValues[i];
1120 xValues[i] = curXValue;
1122 std::sort(xValues.begin(), xValues.end());
1125 for(
int i = 0; i < nss.
m_count; ++i)
1127 double curYValue = xValues[i];
1129 curYValue = 0.5 + (
errFunction ((
float)(curYValue/std::sqrt(2.)))/2.);
1132 curYValue = -curYValue;
1133 curYValue = 0.5 - (
errFunction ((
float)(curYValue/std::sqrt(2.)))/2.);
1135 yValues.push_back(curYValue);
1138 QVector<QPointF> samples, normalSamples;
1139 for (
int i = 0; i < nss.
m_count; ++i)
1141 double val = ((double)i+(
double)1.0)/(double)nss.
m_count;
1142 normalSamples += QPointF( val, val);
1143 samples += QPointF( val, yValues[i]);
1146 baseCurve->setSamples(samples);
1147 normalCurve->setSamples(normalSamples);
1150 QPen normalCurvePen;
1151 normalCurvePen.setColor(QColor(Qt::green));
1153 normalCurvePen.setWidth(0);
1154 baseCurve->setPen(normalCurvePen);
1156 QPen normalProbCurvePen;
1157 normalProbCurvePen.setColor(QColor(Qt::red));
1159 normalProbCurvePen.setWidth(0);
1160 normalCurve->setPen(normalProbCurvePen);
1166 chartStyle->
setAxisY(QString::fromStdString(
"Probability"));
1172 baseCurve->attach(chartDisplay);
1173 normalCurve->attach(chartDisplay);
1177 displayWidget->setWindowTitle(
"Normal Distribution");
1178 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 MarkRendererManager & 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.
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.