25 #include "../common/progress/TaskProgress.h" 26 #include "../common/StringUtils.h" 28 #include "../core/translator/Translator.h" 30 #include "../dataaccess/dataset/DataSet.h" 31 #include "../dataaccess/dataset/DataSetAdapter.h" 33 #include "../dataaccess/dataset/DataSetType.h" 34 #include "../dataaccess/dataset/DataSetTypeConverter.h" 35 #include "../dataaccess/dataset/ObjectIdSet.h" 36 #include "../dataaccess/datasource/DataSource.h" 37 #include "../dataaccess/datasource/DataSourceCapabilities.h" 38 #include "../dataaccess/datasource/DataSourceTransactor.h" 39 #include "../dataaccess/utils/Utils.h" 41 #include "../datatype/Property.h" 42 #include "../datatype/StringProperty.h" 44 #include "../geometry.h" 46 #include "../memory/DataSetItem.h" 48 #include "../raster/RasterProperty.h" 49 #include "../raster/RasterSummaryManager.h" 50 #include "../raster/Utils.h" 52 #include "../rp/RasterAttributes.h" 53 #include "../rp/Texture.h" 55 #include "../statistics.h" 66 #include <boost/algorithm/string.hpp> 67 #include <boost/lexical_cast.hpp> 72 std::string inVectorName,
84 std::vector<unsigned int>
bands,
85 std::vector<te::stat::StatisticalSummary> statSum,
131 std::size_t geomIdx = boost::lexical_cast<std::size_t>(
135 std::unique_ptr<te::da::DataSet> dataSetVector;
142 std::unique_ptr<te::da::DataSetAdapter> dsVector(
146 bool percentByArea =
false;
147 std::vector<std::vector<double> > pixelDistinct;
148 std::vector<te::stat::StatisticalSummary>::iterator it = std::find(
153 std::vector<double> pixelDistinctCurrentBand;
155 for(std::size_t
b = 0;
b <
m_bands.size(); ++
b)
158 pixelDistinct.push_back(pixelDistinctCurrentBand);
160 percentByArea =
true;
164 std::unique_ptr<te::da::DataSetType> outDsType;
171 std::unique_ptr<te::mem::DataSet> outDataset(
new te::mem::DataSet(outDsType.get()));
186 dsVector->moveBeforeFirst();
188 while(dsVector->moveNext())
192 std::vector<te::dt::Property*> vecProp =
m_inVectorDsType->getResult()->getProperties();
194 for(std::size_t i = 0; i < vecProp.size(); ++i)
196 if(!dsVector->isNull(i))
197 outDSetItem->
setValue(i, dsVector->getValue(i).release());
201 std::unique_ptr<te::gm::Geometry> geom = dsVector->getGeometry(geomIdx);
213 outDataset->add(outDSetItem);
215 if(outDataset->size() == 2000)
217 save(outDataset.get(), outDsType.get());
227 std::vector<std::map<double, int > > valuesFromRasterByBand;
228 std::vector<std::map<double, double > > percentOfEachClassByArea;
231 std::map<double, int > values1;
232 valuesFromRasterByBand.push_back(values1);
234 std::map<double, double > values2;
235 percentOfEachClassByArea.push_back(values2);
239 bool isPoint =
false;
240 bool isNoDataValue =
false;
241 std::vector< te::gm::Geometry* > singleGeomsPtrs;
243 switch(geom->getGeomTypeId())
250 singleGeomsPtrs.clear();
253 const std::size_t n_geom = singleGeomsPtrs.size();
255 for(std::size_t n = 0; n < n_geom; ++n)
262 std::map<double, int> values;
270 uint32_t minimumRow, minimumColumn, maximumRow, maximumColumn;
277 std::map<double, double> percents;
279 joinMaps(percentOfEachClassByArea[band], percents);
299 std::map<double, int> values;
307 uint32_t minimumRow, minimumColumn, maximumRow, maximumColumn;
314 std::map<double, double> percents;
316 joinMaps(percentOfEachClassByArea[band], percents);
333 for(std::size_t n = 0; n < n_geom; ++n)
337 const double coordX = point->
getX();
338 const double coordY = point->
getY();
351 double value = noDataValue;
358 if(value == noDataValue)
360 isNoDataValue =
true;
364 std::map<double, int> values;
368 joinMaps(valuesFromRasterByBand[band], values);
379 const double coordX = point->
getX();
380 const double coordY = point->
getY();
392 double value = noDataValue;
399 if(value == noDataValue)
401 isNoDataValue =
true;
405 std::map<double, int> values;
409 joinMaps(valuesFromRasterByBand[band], values);
422 outDataset->add(outDSetItem);
424 if(outDataset->size() == 2000)
426 save(outDataset.get(), outDsType.get());
433 std::size_t init_index =
439 for(std::size_t
band = 0;
band < valuesFromRasterByBand.size(); ++
band)
445 if(percentOfEachClassByArea[band].empty())
450 std::size_t current_index = init_index +
m_statSum.size();
452 for(std::size_t it = 0, i = init_index; i < current_index; ++it, ++i)
501 if(!summary.
m_mode.empty())
503 mode = boost::lexical_cast<std::string>(summary.
m_mode[0]);
504 for(std::size_t m = 1; m < summary.
m_mode.size(); ++m)
507 mode += boost::lexical_cast<std::string>(summary.
m_mode[m]);
519 std::vector<double>::iterator itPixelDistinct =
520 pixelDistinct[
band].begin();
522 std::map<double, double>::iterator itPercent =
525 while(itPixelDistinct != pixelDistinct[band].end())
530 std::vector<std::string> splitString;
531 boost::split(splitString, name, boost::is_any_of(
"_"));
533 boost::lexical_cast<std::string>(itPercent->first))
535 outDSetItem->
setDouble(i, itPercent->second);
550 current_index += pixelDistinct[
band].size() - 1;
558 throw te::attributefill::Exception(
TE_TR(
"Operation canceled!"));
564 std::vector<te::rp::Texture> metrics;
565 init_index = current_index;
571 for(std::size_t t = 0, i = init_index; i < current_index; ++t, ++i)
577 outDSetItem->
setDouble(i, metrics[band].m_contrast);
582 outDSetItem->
setDouble(i, metrics[band].m_dissimilarity);
587 outDSetItem->
setDouble(i, metrics[band].m_energy);
592 outDSetItem->
setDouble(i, metrics[band].m_entropy);
597 outDSetItem->
setDouble(i, metrics[band].m_homogeneity);
604 init_index = current_index;
609 for(std::size_t i = 0; i < valuesFromRasterByBand.size(); ++i)
611 std::map<double, int>::iterator it = valuesFromRasterByBand[i].begin();
613 while(it != valuesFromRasterByBand[i].end())
615 outDSetItem->
setDouble(init_index, it->first);
622 outDataset->add(outDSetItem);
624 if(outDataset->size() == 2000)
626 save(outDataset.get(), outDsType.get());
631 throw te::attributefill::Exception(
TE_TR(
"Operation canceled!"));
634 if(!outDataset->isEmpty())
635 save(outDataset.get(), outDsType.get());
642 std::vector<double>& values)
648 unsigned int col = 0;
649 unsigned int row = 0;
650 std::set<double> valuesSet;
651 double centerValue = 0;
657 for (row = 0; row < nRows; ++row)
661 inputBand.
getValue(col, row, centerValue);
663 if (centerValue != inputBandDataValue)
665 if (valuesSet.find(centerValue) == valuesSet.end())
666 valuesSet.insert(centerValue);
671 std::set<double>::iterator valuesIt = valuesSet.begin();
672 while (valuesIt != valuesSet.end())
674 values.push_back(*valuesIt);
680 std::vector<std::vector<double> > pixelDistinct)
688 pk->
setName(outdsType->getName() +
"_pkey");
690 for(std::size_t
b = 0;
b <
m_bands.size(); ++
b)
694 for(std::size_t i = 0; i <
m_statSum.size(); ++i)
701 "B" + boost::lexical_cast<std::string>(
m_bands[
b]) +
704 outdsType->add(prop);
708 "B" + boost::lexical_cast<std::string>(
m_bands[b]) +
711 outdsType->add(prop);
715 "B" + boost::lexical_cast<std::string>(
m_bands[b]) +
"_Mean",
717 outdsType->add(prop);
721 "B" + boost::lexical_cast<std::string>(
m_bands[b]) +
"_Sum",
723 outdsType->add(prop);
727 "B" + boost::lexical_cast<std::string>(
m_bands[b]) +
"_Count",
729 outdsType->add(prop);
733 "B" + boost::lexical_cast<std::string>(
m_bands[b]) +
736 outdsType->add(prop);
740 "B" + boost::lexical_cast<std::string>(
m_bands[b]) +
741 "_Standard_Deviation",
743 outdsType->add(prop);
747 "B" + boost::lexical_cast<std::string>(
m_bands[b]) +
750 outdsType->add(prop);
754 "B" + boost::lexical_cast<std::string>(
m_bands[b]) +
757 outdsType->add(prop);
761 "B" + boost::lexical_cast<std::string>(
m_bands[b]) +
764 outdsType->add(prop);
768 "B" + boost::lexical_cast<std::string>(
m_bands[b]) +
771 outdsType->add(prop);
775 "B" + boost::lexical_cast<std::string>(
m_bands[b]) +
"_Median",
777 outdsType->add(prop);
781 "B" + boost::lexical_cast<std::string>(
m_bands[b]) +
784 outdsType->add(prop);
788 "B" + boost::lexical_cast<std::string>(
m_bands[b]) +
"_Mode");
789 outdsType->add(prop);
793 std::vector<double>::iterator it = pixelDistinct[
b].begin();
794 while(it != pixelDistinct[b].end())
797 "B" + boost::lexical_cast<std::string>(
m_bands[b]) +
"_" +
798 boost::lexical_cast<std::string>(*it),
800 outdsType->add(prop);
812 "B" + boost::lexical_cast<std::string>(
m_bands[
b]) +
"_Contrast",
814 outdsType->add(propContrast);
817 "B" + boost::lexical_cast<std::string>(
m_bands[b]) +
820 outdsType->add(propDissimilarity);
823 "B" + boost::lexical_cast<std::string>(
m_bands[b]) +
"_Energy",
825 outdsType->add(propEnergy);
828 "B" + boost::lexical_cast<std::string>(
m_bands[b]) +
"_Entropy",
830 outdsType->add(propEntropy);
833 "B" + boost::lexical_cast<std::string>(
m_bands[b]) +
"_Homogeneity",
835 outdsType->add(propHomogeneity);
841 "B" + boost::lexical_cast<std::string>(
m_bands[
b]) +
"_Value",
843 outdsType->add(propValue);
854 std::vector<te::rp::Texture> textureVec;
869 double minPixel, maxPixel;
872 if((maxPixel - minPixel) > 255)
876 const std::complex<double>* cmin = rsMin->at(band).m_minVal;
877 const std::complex<double>* cmax = rsMax->at(band).m_maxVal;
879 minPixel = cmin->real();
880 maxPixel = cmax->real();
883 boost::numeric::ublas::matrix<double> glcm =
884 rattributes.
getGLCM(*rst, band, 1, 1, *polygon, minPixel, maxPixel);
886 textureVec.push_back(metrics);
893 std::map<double, int>::iterator it_newMap = newMap.begin();
895 while(it_newMap != newMap.end())
897 mainMap[it_newMap->first] += it_newMap->second;
905 std::map<double, double>::iterator it_newMap = newMap.begin();
907 while(it_newMap != newMap.end())
909 mainMap[it_newMap->first] += it_newMap->second;
921 std::unique_ptr<te::da::DataSourceTransactor> t =
m_outDsrc->getTransactor();
923 std::map<std::string, std::string> options;
929 std::string dsTypeName = outDsType->
getName();
930 std::vector<std::string> dataSetNames = t->getDataSetNames();
933 for (std::size_t i = 0; i < dataSetNames.size(); ++i)
935 std::vector<std::string> tok;
938 std::string temp_dsName;
941 temp_dsName = tok[1];
943 temp_dsName = dataSetNames[i];
945 if (dsTypeName == temp_dsName)
954 t->createDataSet(dsTypeResult, options);
959 t->add(dsTypeName, result, options);
968 catch(std::exception& e)
virtual void setName(const std::string &name)
It sets the constraint name.
std::size_t getNumGeometries() const
It returns the number of geometries in this GeometryCollection.
void getPixelDistinct(rst::Raster &inputRaster, unsigned int inputRasterBand, std::vector< double > &values)
te::gm::Envelope * getExtent()
Returns the geographic extension of the raster data.
void setInput(te::rst::Raster *inRaster, te::da::DataSourcePtr inVectorDsrc, std::string inVectorName, te::da::DataSetTypeConverter *inVectorDsType, const te::da::ObjectIdSet *oidSet=0)
MultiPolygon is a MultiSurface whose elements are Polygons.
TESTATEXPORT void GetPercentOfEachClassByArea(std::map< double, int > &values, double &resolutionX, double &resolutionY, double &area, te::stat::NumericStatisticalSummary &ss)
A structure to hold the set of statistics from a set of numerical values.
TEATTRIBUTEFILLEXPORT void GetValuesFromBand(const te::rst::Raster &raster, const unsigned int &band, const gm::Polygon &polygon, const uint32_t &minimumRow, const uint32_t &minimumColumn, const uint32_t &maximumRow, const uint32_t &maximumColumn, std::map< double, int > &values)
Gets the pixel values for a specific band that intersects the polygon.
void setDouble(std::size_t i, double value)
It sets the value of the i-th property.
TERASTEREXPORT void GetDataTypeRanges(const int &dataType, double &min, double &max)
Return the values range of a given data type.
bool intersects(const Envelope &rhs) const
It returns true if the envelopes "spatially intersects".
An atomic property like an integer or double.
std::string getPropertyName(std::size_t pos) const
It returns the name of the pos-th property.
boost::shared_ptr< DataSource > DataSourcePtr
te::da::DataSourcePtr m_outDsrc
A class that models the description of a dataset.
unsigned int getNumberOfColumns() const
Returns the raster number of columns.
void useTimer(bool flag)
Used to define if task use progress timer information.
GeomType getGeomTypeId() const _NOEXCEPT_OP(true)
It returns the geometry subclass type identifier.
std::vector< te::rp::Texture > getTexture(te::rst::Raster *rst, te::gm::Geometry *geom, int band)
void setOutput(te::da::DataSourcePtr outDsrc, std::string dsName)
DataSetType * getResult() const
std::vector< te::stat::StatisticalSummary > m_statSum
This class can be used to inform the progress of a task.
void setValue(std::size_t i, te::dt::AbstractData *value)
It sets the value of the i-th property.
std::vector< double > m_mode
An utility struct for representing 2D coordinates.
double m_noDataValue
Value to indicate elements where there is no data, default is std::numeric_limits<double>::max().
#define TE_TR(message)
It marks a string in order to get translated.
Percente of each class by area.
TEGEOMEXPORT void Multi2Single(const te::gm::Geometry *g, std::vector< te::gm::Geometry * > &geoms)
It will get a GeometryCollection and distribute in a vector.
bool isActive() const
Verify if the task is active.
void geoToGrid(const double &x, const double &y, double &col, double &row) const
Get the grid point associated to a spatial location.
std::map< double, double > m_percentEachClass
Extraction of attributes from Raster, Bands, and Polygons.
TEATTRIBUTEFILLEXPORT void GetMinMaxLineAndColumn(const te::rst::Raster &raster, const te::gm::Polygon &polygon, uint32_t &minimumRow, uint32_t &minimumColumn, uint32_t &maximumRow, uint32_t &maximumColumn)
Gets the minimum and maximum row and column values of the raster based on polygon.
void setTotalSteps(int value)
Set the task total stepes.
An converter for DataSetType.
unsigned int unsigned int nCols
te::rst::Raster * m_inRaster
void Tokenize(const std::string &str, std::vector< std::string > &tokens, const std::string &delimiters=" ")
It tokenizes a given string with a delimiter of your own choice.
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
MultiPoint is a GeometryCollection whose elements are restricted to points.
int getSRID() const
It returns the spatial reference system identifier associated to this property.
const double & getY() const
It returns the Point y-coordinate value.
static RasterSummaryManager & getInstance()
It returns a reference to the singleton instance.
A point with x and y coordinate values.
boost::numeric::ublas::matrix< double > getGLCM(const te::rst::Raster &rin, unsigned int band, int dx, int dy, double minPixel, double maxPixel, double gLevels=256)
Computes the Gray-Level Co-occurrence Matrix (GLCM) from a raster band.
An Envelope defines a 2D rectangular region.
This class represents a set of unique ids created in the same context. i.e. from the same data set...
An abstract class for raster data strucutures.
const te::da::ObjectIdSet * m_oidSet
unsigned int getNumberOfRows() const
Returns the raster number of rows.
BandProperty * getProperty()
Returns the band property.
Raster to Vector processing.
TERASTEREXPORT int Round(double val)
Round a double value to a integer value.
std::unique_ptr< te::da::DataSetTypeConverter > m_inVectorDsType
virtual int getBandDataType(std::size_t i) const =0
Returns the data type in a particular band (or dimension).
double getResolutionX() const
Returns the raster horizontal (x-axis) resolution.
boost::ptr_vector< BandSummary > RasterSummary
RasterSummary is just a typedef of a boost::ptr_vector.
te::da::DataSourcePtr m_inVectorDsrc
TESTATEXPORT void GetNumericStatisticalSummary(std::vector< double > &values, te::stat::NumericStatisticalSummary &ss, double nullValue)
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
void save(te::da::DataSet *result, te::da::DataSetType *outDsType)
std::vector< unsigned int > m_bands
A raster band description.
The type for string types: FIXED_STRING, VAR_STRING or STRING.
virtual const Band * getBand(std::size_t i) const =0
Returns the raster i-th band.
Grid * getGrid()
It returns the raster grid.
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
void setParams(std::vector< unsigned int > bands, std::vector< te::stat::StatisticalSummary > statSum, bool iteratorByBox, bool texture)
te::rp::Texture getGLCMMetrics(boost::numeric::ublas::matrix< double > glcm, double noDataValue=0.0)
Compute texture metrics from GLCM matrix.
virtual void getValue(unsigned int c, unsigned int r, double &value, std::size_t b=0) const
Returns the attribute value of a band of a cell.
double getArea() const
It returns the area of this surface, as measured in the spatial reference system of this surface...
Geometry * getGeometryN(std::size_t i) const
It returns the n-th geometry in this GeometryCollection.
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
A dataset is the unit of information manipulated by the data access module of TerraLib.
int getSRID() const
Returns the raster spatial reference system identifier.
std::vector< double > getValuesFromBand(const te::rst::Raster &raster, unsigned int band, const te::gm::Polygon &polygon)
Returns the pixel values for the band, inside the polygon.
Polygon is a subclass of CurvePolygon whose rings are defined by linear rings.
void joinMaps(std::map< double, int > &mainMap, std::map< double, int > &newMap)
It describes a primary key (pk) constraint.
virtual bool moveBeforeFirst()=0
It moves the internal pointer to a position before the first item in the collection.
TEATTRIBUTEFILLEXPORT void GetPercentOfEachClassByArea(const te::rst::Raster &raster, const unsigned int &band, const gm::Polygon &polygon, const uint32_t &minimumRow, const uint32_t &minimumColumn, const uint32_t &maximumRow, const uint32_t &maximumColumn, std::map< double, double > &percentOfEachClassByArea)
Gets the pixel percentage for a specific band that intersects the polygon.
std::unique_ptr< da::DataSetType > getDataSetType(std::vector< std::vector< double > > pixelDistinct=std::vector< std::vector< double > >())
double getResolutionY() const
Returns the raster vertical (y-axis) resolution.
A structure to hold the set of GLCM metrics.
std::string m_inVectorName
StatisticalSummary
Define grouping functions type.
void setString(std::size_t i, const std::string &value)
It sets the value of the i-th property.
TEDATAACCESSEXPORT te::gm::GeometryProperty * GetFirstGeomProperty(const DataSetType *dt)
TEDATAACCESSEXPORT DataSetAdapter * CreateAdapter(DataSet *ds, DataSetTypeConverter *converter, bool isOwner=false)
const double & getX() const
It returns the Point x-coordinate value.
An exception class for the Attribute Fill module.
virtual void getValue(unsigned int c, unsigned int r, double &value) const =0
Returns the cell attribute value.
const std::string & getName() const
It returns the property name.