All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
SpatialStatisticsFunctions.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/sa/core/SpatialStatisticsFunctions.cpp
22 
23  \brief Functions used to calculate spatial statistics operations.
24 
25  \reference Methods adapted from TerraLib4.
26 */
27 
28 // TerraLib
29 #include "../../common/Exception.h"
30 #include "../../common/Translator.h"
31 #include "../../common/progress/TaskProgress.h"
32 #include "../../datatype/SimpleData.h"
33 #include "../../graph/core/Edge.h"
34 #include "../../graph/core/GraphMetadata.h"
35 #include "../../graph/core/Vertex.h"
36 #include "../../graph/iterator/MemoryIterator.h"
39 #include "StatisticsFunctions.h"
40 #include "Utils.h"
41 
42 // STL
43 #include <cassert>
44 
45 //BOOST
46 #include <boost/random/mersenne_twister.hpp>
47 #include <boost/random/uniform_int_distribution.hpp>
48 
49 
51 {
52  assert(gpm);
53 
54  //calculate the sum of the selected attribute
55  double totalSum = te::sa::Sum(gpm, attrIdx);
56 
57  te::graph::AbstractGraph* graph = gpm->getGraph();
58 
59  //add G and G* attributes into gpm
62  int nAttrs = graph->getMetadata()->getVertexPropertySize();
63 
64  //create graph vertex iterator
65  std::auto_ptr<te::graph::MemoryIterator> it(new te::graph::MemoryIterator(graph));
66 
67  //create task
69 
70  task.setTotalSteps(it->getVertexInteratorCount());
71  task.setMessage(TE_TR("Calculating G Statistics."));
72 
73  te::graph::Vertex* v = it->getFirstVertex();
74 
75  while(!it->isVertexIteratorAfterEnd())
76  {
77  double attrValue = te::sa::GetDataValue(v->getAttributes()[attrIdx]);
78  double excludeSum = totalSum - attrValue;
79  double G = 0.;
80  double GStar = attrValue;
81 
82  //get neighbours information
83  int id = v->getId();
84  std::set<int> neighbours = v->getSuccessors();
85  std::set<int>::iterator itNeighbours = neighbours.begin();
86  int nNeighbours = (int)neighbours.size();
87 
88  while(itNeighbours != neighbours.end())
89  {
90  te::graph::Edge* e = graph->getEdge(*itNeighbours);
91  te::graph::Vertex* vTo = 0;
92 
93  if(e)
94  {
95  if(e->getIdFrom() == id)
96  vTo = graph->getVertex(e->getIdTo());
97  else
98  vTo = graph->getVertex(e->getIdFrom());
99  }
100 
101  if(vTo)
102  {
103  double attrValueTo = te::sa::GetDataValue(vTo->getAttributes()[attrIdx]);
104 
105  G += attrValueTo;
106  GStar += attrValueTo;
107  }
108 
109  ++itNeighbours;
110  }
111 
112  G /= nNeighbours;
113  GStar /= (nNeighbours+1);
114 
115  G /= excludeSum;
116  GStar /= totalSum;
117 
118  v->setAttributeVecSize(nAttrs);
121 
122  if(!task.isActive())
123  {
124  throw te::common::Exception(TE_TR("Operation canceled by the user."));
125  }
126 
127  task.pulse();
128 
129  v = it->getNextVertex();
130  }
131 }
132 
134 {
135  assert(gpm);
136 
137  te::graph::AbstractGraph* graph = gpm->getGraph();
138 
139  //check if the graph has the weight attribute
140  int weightAttrIdx;
141  if(!te::sa::GetGraphEdgeAttrIndex(graph, TE_SA_WEIGHT_ATTR_NAME, weightAttrIdx))
142  throw;
143 
144  bool normalized = true;
145 
146  //add local mean and number of neighbours attributes into gpm
149  int nAttrs = graph->getMetadata()->getVertexPropertySize();
150 
151  //create graph vertex iterator
152  std::auto_ptr<te::graph::MemoryIterator> it(new te::graph::MemoryIterator(graph));
153 
154  //create task
156 
157  task.setTotalSteps(it->getVertexInteratorCount());
158  task.setMessage(TE_TR("Calculating Local Mean."));
159 
160  te::graph::Vertex* v = it->getFirstVertex();
161 
162  while(!it->isVertexIteratorAfterEnd())
163  {
164  double sum = 0.;
165 
166  //get neighbours information
167  int id = v->getId();
168  std::set<int> neighbours = v->getSuccessors();
169  std::set<int>::iterator itNeighbours = neighbours.begin();
170  int nNeighbours = (int)neighbours.size();
171 
172  while(itNeighbours != neighbours.end())
173  {
174  te::graph::Edge* e = graph->getEdge(*itNeighbours);
175  te::graph::Vertex* vTo = 0;
176 
177  if(e)
178  {
179  if(e->getIdFrom() == id)
180  vTo = graph->getVertex(e->getIdTo());
181  else
182  vTo = graph->getVertex(e->getIdFrom());
183  }
184 
185  if(vTo)
186  {
187  //retrieve the neighbor attribute value
188  double attrValue = te::sa::GetDataValue(vTo->getAttributes()[attrIdx]);
189 
190  //retrieve the weight associated with the neighbor
191  double attrWeight = te::sa::GetDataValue(e->getAttributes()[weightAttrIdx]);
192 
193  //verify if the weight is normalized by number of neighbours
194  if(!normalized)
195  attrWeight = attrWeight/nNeighbours;
196 
197  sum += attrWeight * attrValue;
198  }
199 
200  ++itNeighbours;
201  }
202 
203  v->setAttributeVecSize(nAttrs);
204  v->addAttribute(localMeanAttrIdx, new te::dt::SimpleData<double, te::dt::DOUBLE_TYPE>(sum));
205  v->addAttribute(nNeighboursAttrIdx, new te::dt::SimpleData<int, te::dt::INT32_TYPE>(nNeighbours));
206 
207  if(!task.isActive())
208  {
209  throw te::common::Exception(TE_TR("Operation canceled by the user."));
210  }
211 
212  task.pulse();
213 
214  v = it->getNextVertex();
215  }
216 }
217 
219 {
220  assert(gpm);
221 
222  te::graph::AbstractGraph* graph = gpm->getGraph();
223 
224  //check if the graph has the weight attribute
225  int weightAttrIdx;
226  if(!te::sa::GetGraphEdgeAttrIndex(graph, TE_SA_WEIGHT_ATTR_NAME, weightAttrIdx))
227  throw;
228 
229  // calculate the standard deviation Z
230  double mean = te::sa::FirstMoment(gpm, attrIdx);
231 
232  //add Z attribute into gpm
234  int nAttrs = graph->getMetadata()->getVertexPropertySize();
235 
236  //create graph vertex iterator
237  std::auto_ptr<te::graph::MemoryIterator> it(new te::graph::MemoryIterator(graph));
238 
239  //create task
240  {
242 
243  task.setTotalSteps(it->getVertexInteratorCount());
244  task.setMessage(TE_TR("Calculating Moran - Z Value."));
245 
246  te::graph::Vertex* v = it->getFirstVertex();
247 
248  while(!it->isVertexIteratorAfterEnd())
249  {
250  double attrValue = te::sa::GetDataValue(v->getAttributes()[attrIdx]);
251 
252  double stdDezZ = attrValue - mean;
253 
254  v->setAttributeVecSize(nAttrs);
256 
257  if(!task.isActive())
258  {
259  throw te::common::Exception(TE_TR("Operation canceled by the user."));
260  }
261 
262  task.pulse();
263 
264  v = it->getNextVertex();
265  }
266  }
267 
268  //calculate the local mean of Z(WZ)
270  nAttrs = graph->getMetadata()->getVertexPropertySize();
271 
272  bool normalized = true;
273 
274  //create task
275  {
277 
278  task.setTotalSteps(it->getVertexInteratorCount());
279  task.setMessage(TE_TR("Calculating Moran - WZ Value."));
280 
281  //vertex iterator
282  te::graph::Vertex* v = it->getFirstVertex();
283 
284  while(!it->isVertexIteratorAfterEnd())
285  {
286  double sum = 0.;
287 
288  //get neighbours information
289  int id = v->getId();
290  std::set<int> neighbours = v->getSuccessors();
291  std::set<int>::iterator itNeighbours = neighbours.begin();
292  int nNeighbours = (int)neighbours.size();
293 
294  while(itNeighbours != neighbours.end())
295  {
296  te::graph::Edge* e = graph->getEdge(*itNeighbours);
297  te::graph::Vertex* vTo = 0;
298 
299  if(e)
300  {
301  if(e->getIdFrom() == id)
302  vTo = graph->getVertex(e->getIdTo());
303  else
304  vTo = graph->getVertex(e->getIdFrom());
305  }
306 
307  if(vTo)
308  {
309  //retrieve the neighbor attribute value
310  double attrValue = te::sa::GetDataValue(vTo->getAttributes()[zAttrIdx]);
311 
312  //retrieve the weight associated with the neighbor
313  double attrWeight = te::sa::GetDataValue(e->getAttributes()[weightAttrIdx]);
314 
315  //verify if the weight is normalized by number of neighbours
316  if(!normalized)
317  attrWeight = attrWeight/nNeighbours;
318 
319  sum += attrWeight * attrValue;
320  }
321 
322  ++itNeighbours;
323  }
324 
325  v->setAttributeVecSize(nAttrs);
327 
328  if(!task.isActive())
329  {
330  throw te::common::Exception(TE_TR("Operation canceled by the user."));
331  }
332 
333  task.pulse();
334 
335  v = it->getNextVertex();
336  }
337  }
338 }
339 
341 {
342  assert(gpm);
343 
344  te::graph::AbstractGraph* graph = gpm->getGraph();
345 
346  //check if the graph has the Z attribute
347  int zAttrIdx;
349  throw;
350 
351  //check if the graph has the WZ attribute
352  int wzAttrIdx;
354  throw;
355 
356  double variance = te::sa::SecondMoment(gpm, zAttrIdx, 0); // MEAN is 0 ??
357  double sum = 0.;
358  int count = 0;
359 
360  //add moran index attribute into gpm
362  int nAttrs = graph->getMetadata()->getVertexPropertySize();
363 
364  //create graph vertex iterator
365  std::auto_ptr<te::graph::MemoryIterator> it(new te::graph::MemoryIterator(graph));
366 
368 
369  task.setTotalSteps(it->getVertexInteratorCount());
370  task.setMessage(TE_TR("Calculating Moran Index."));
371 
372  te::graph::Vertex* v = it->getFirstVertex();
373 
374  while(!it->isVertexIteratorAfterEnd())
375  {
376  double zValue = te::sa::GetDataValue(v->getAttributes()[zAttrIdx]);
377  double wzValue = te::sa::GetDataValue(v->getAttributes()[wzAttrIdx]);
378  double ZxWZ = 0.;
379 
380  if(variance != 0.)
381  ZxWZ = (zValue*wzValue)/variance;
382 
383  v->setAttributeVecSize(nAttrs);
385 
386  sum += ZxWZ;
387  ++count;
388 
389  if(!task.isActive())
390  {
391  throw te::common::Exception(TE_TR("Operation canceled by the user."));
392  }
393 
394  task.pulse();
395 
396  v = it->getNextVertex();
397  }
398 
399  return sum /= count;
400 }
401 
402 double te::sa::MoranIndex(te::sa::GeneralizedProximityMatrix* gpm, double mean, double variance, int attrIdx)
403 {
404  assert(gpm);
405 
406  double moran = 0.;
407 
408  te::graph::AbstractGraph* graph = gpm->getGraph();
409 
410  //check if the graph has the weight attribute
411  int weightAttrIdx;
412  if(!te::sa::GetGraphEdgeAttrIndex(graph, TE_SA_WEIGHT_ATTR_NAME, weightAttrIdx))
413  throw;
414 
415  bool normalized = true;
416 
417  //create graph vertex iterator
418  std::auto_ptr<te::graph::MemoryIterator> it(new te::graph::MemoryIterator(graph));
419 
420  te::graph::Vertex* v = it->getFirstVertex();
421 
422  std::size_t numberObjs = it->getVertexInteratorCount();
423 
424  while(!it->isVertexIteratorAfterEnd())
425  {
426  double normObjVal = te::sa::GetDataValue(v->getAttributes()[attrIdx]) - mean;
427  double li = 0.;
428  double weightSum = 0.;
429 
430  //get neighbours information
431  int id = v->getId();
432  std::set<int> neighbours = v->getSuccessors();
433  std::set<int>::iterator itNeighbours = neighbours.begin();
434  int nNeighbours = (int)neighbours.size();
435 
436  while(itNeighbours != neighbours.end())
437  {
438  te::graph::Edge* e = graph->getEdge(*itNeighbours);
439  te::graph::Vertex* vTo = 0;
440 
441  if(e)
442  {
443  if(e->getIdFrom() == id)
444  vTo = graph->getVertex(e->getIdTo());
445  else
446  vTo = graph->getVertex(e->getIdFrom());
447  }
448 
449  if(vTo)
450  {
451  double normNeighVal = te::sa::GetDataValue(vTo->getAttributes()[attrIdx]) - mean;
452 
453  //retrieve the weight associated with the neighbor
454  double attrWeight = te::sa::GetDataValue(e->getAttributes()[weightAttrIdx]);
455 
456  //verify if the weight is normalized by number of neighbours
457  if(!normalized)
458  attrWeight = attrWeight/nNeighbours;
459 
460  li += attrWeight*(normNeighVal)*(normObjVal);
461 
462  weightSum += attrWeight;
463  }
464 
465  ++itNeighbours;
466  }
467 
468  if (weightSum != 0.)
469  li /= weightSum;
470 
471  moran += li;
472 
473  v = it->getNextVertex();
474  }
475 
476  if(numberObjs > 1)
477  return moran/(variance*(numberObjs - 1));
478  else
479  return moran/variance;
480 }
481 
482 double te::sa::GlobalMoranSignificance(te::sa::GeneralizedProximityMatrix* gpm, int attrIdx, int permutationsNumber, double moranIndex)
483 {
484  assert(gpm);
485 
486  te::graph::AbstractGraph* graph = gpm->getGraph();
487 
488  //calculate statistics information
489  double mean = te::sa::FirstMoment(gpm, attrIdx);
490  double variance = te::sa::SecondMoment(gpm, attrIdx, mean);
491 
492  //create deviation vector
493  std::vector<double> deviations;
494 
495  std::auto_ptr<te::graph::MemoryIterator> it(new te::graph::MemoryIterator(graph));
496  te::graph::Vertex* v = it->getFirstVertex();
497 
498  while(!it->isVertexIteratorAfterEnd())
499  {
500  te::dt::AbstractData* ad = v->getAttributes()[attrIdx];
501 
502  if(ad)
503  deviations.push_back(te::sa::GetDataValue(ad));
504 
505  v = it->getNextVertex();
506  }
507 
508  //create permutation vector
509  std::vector<double> permutationsResults(permutationsNumber);
510 
511  boost::random::mt19937 gen;
512  boost::random::uniform_int_distribution<> dist(0, deviations.size() - 1);
513 
514  //create task
516 
517  task.setTotalSteps(permutationsNumber);
518  task.setMessage(TE_TR("Calculating Global Moran Significance."));
519 
520  for(int i = 0; i < permutationsNumber; ++i)
521  {
522  //shuffle the attribute value
523  te::graph::Vertex* v = it->getFirstVertex();
524 
525  while(!it->isVertexIteratorAfterEnd())
526  {
527  double value = deviations[dist(gen)];
528 
530 
531  v = it->getNextVertex();
532  }
533 
534  permutationsResults[i] = MoranIndex(gpm, mean, variance, attrIdx);
535 
536  if(!task.isActive())
537  {
538  throw te::common::Exception(TE_TR("Operation canceled by the user."));
539  }
540 
541  task.pulse();
542  }
543 
544  //fix attribute values
545  int count = 0;
546  v = it->getFirstVertex();
547 
548  while(!it->isVertexIteratorAfterEnd())
549  {
550  double value = deviations[count];
551 
553 
554  ++count;
555 
556  v = it->getNextVertex();
557  }
558 
559  // verify the significance
560  int position = 0;
561  double significance = 0.;
562 
563  for(int i = 0; i < permutationsNumber; i++)
564  {
565  if(moranIndex < permutationsResults[i])
566  position++;
567  }
568 
569  if(moranIndex >= 0)
570  significance = (double)(position+1)/(double)(permutationsNumber+1);
571  else
572  significance = (double)(permutationsNumber-position)/(double)(permutationsNumber+1);
573 
574  return significance;
575 }
576 
578 {
579  assert(gpm);
580 
581  te::graph::AbstractGraph* graph = gpm->getGraph();
582 
583  //check if the graph has the Z attribute
584  int zAttrIdx;
586  throw;
587 
588  //check if the graph has the local moran attribute
589  int lisaAttrIdx;
591  throw;
592 
593  //check if the graph has the number of neighbours attribute
594  int nNeighboursAttrIdx;
595  if(!te::sa::GetGraphVertexAttrIndex(graph, TE_SA_NUMNEIGHBORS_ATTR_NAME, nNeighboursAttrIdx))
596  throw;
597 
598  //calculate variance
599  double variance = te::sa::SecondMoment(gpm, zAttrIdx, 0); // MEAN = 0 ??
600 
601  //create deviation vector
602  std::vector<double> deviations;
603 
604  std::auto_ptr<te::graph::MemoryIterator> it(new te::graph::MemoryIterator(graph));
605  te::graph::Vertex* v = it->getFirstVertex();
606 
607  while(!it->isVertexIteratorAfterEnd())
608  {
609  te::dt::AbstractData* ad = v->getAttributes()[zAttrIdx];
610 
611  if(ad)
612  deviations.push_back(te::sa::GetDataValue(ad));
613 
614  v = it->getNextVertex();
615  }
616 
617  //add lisa significance attribute into gpm
619  int nAttrs = graph->getMetadata()->getVertexPropertySize();
620 
621  //calculate LISA Significance value
622  boost::random::mt19937 gen;
623  boost::random::uniform_int_distribution<> dist(0, deviations.size() - 1);
624 
625  int index = 0;
626 
627  //create task
629 
630  task.setTotalSteps(it->getVertexInteratorCount());
631  task.setMessage(TE_TR("Calculating LISA Significance."));
632 
633  v = it->getFirstVertex();
634 
635  while(!it->isVertexIteratorAfterEnd())
636  {
637  int nNeighbours = (int)te::sa::GetDataValue(v->getAttributes()[nNeighboursAttrIdx]);
638 
639  double lisaValue = te::sa::GetDataValue(v->getAttributes()[lisaAttrIdx]);
640 
641  //create pertumation vector
642  std::vector<double> permut(permutationsNumber);
643 
644  for(int i = 0; i < permutationsNumber; ++i)
645  {
646  double sum = 0.;
647 
648  std::set<int> neighSet;
649 
650  for(int j = 0; j < nNeighbours; ++j)
651  {
652  int pos = dist(gen);
653 
654  if(index == pos || neighSet.find(pos) != neighSet.end())
655  {
656  j--;
657  }
658  else
659  {
660  neighSet.insert(pos);
661  sum += deviations[pos];
662  }
663  }
664 
665  double WZperm = 0.;
666 
667  if(nNeighbours != 0)
668  WZperm = sum/nNeighbours;
669 
670  if(variance == 0)
671  permut[i] = 0.;
672  else
673  permut[i] = deviations[index] * WZperm / variance;
674  }
675 
676  int position = 0;
677 
678  for(int k = 0; k < permutationsNumber; k++)
679  {
680  if(lisaValue > permut[k])
681  position++;
682  }
683 
684  //calculate significance
685  double significance = 0.;
686 
687  if(lisaValue >= 0)
688  significance = (double) (permutationsNumber-position)/(permutationsNumber+1);
689  else
690  significance = (double) position/( permutationsNumber + 1 );
691 
692  v->setAttributeVecSize(nAttrs);
693  v->addAttribute(lisaSigAttrIdx, new te::dt::SimpleData<double, te::dt::DOUBLE_TYPE>(significance));
694 
695  if(!task.isActive())
696  {
697  throw te::common::Exception(TE_TR("Operation canceled by the user."));
698  }
699 
700  task.pulse();
701 
702  ++index;
703 
704  v = it->getNextVertex();
705  }
706 }
707 
709 {
710  assert(gpm);
711 
712  te::graph::AbstractGraph* graph = gpm->getGraph();
713 
714  //check if the graph has the Z attribute
715  int zAttrIdx;
717  throw;
718 
719  //check if the graph has the WZ attribute
720  int wzAttrIdx;
722  throw;
723 
724  //add boxmap attribute into gpm
726  int nAttrs = graph->getMetadata()->getVertexPropertySize();
727 
728  //create graph vertex iterator
729  std::auto_ptr<te::graph::MemoryIterator> it(new te::graph::MemoryIterator(graph));
730 
731  //create task
733 
734  task.setTotalSteps(it->getVertexInteratorCount());
735  task.setMessage(TE_TR("Calculating Box Map."));
736 
737  te::graph::Vertex* v = it->getFirstVertex();
738 
739  while(!it->isVertexIteratorAfterEnd())
740  {
741  double zValue = te::sa::GetDataValue(v->getAttributes()[zAttrIdx]);
742  double wzValue = te::sa::GetDataValue(v->getAttributes()[wzAttrIdx]);
743 
744  int result = 0;
745 
746  if(zValue >= mean && wzValue >= mean)
747  result = 1;
748  else if(zValue < mean && wzValue >= mean)
749  result = 4;
750  else if(zValue < mean && wzValue < mean)
751  result = 2;
752  else if(zValue >= mean && wzValue < mean)
753  result = 3;
754 
755  v->setAttributeVecSize(nAttrs);
756  v->addAttribute(boxMapAttrIdx, new te::dt::SimpleData<int, te::dt::INT32_TYPE>(result));
757 
758  if(!task.isActive())
759  {
760  throw te::common::Exception(TE_TR("Operation canceled by the user."));
761  }
762 
763  task.pulse();
764 
765  v = it->getNextVertex();
766  }
767 }
768 
769 void te::sa::LISAMap(te::sa::GeneralizedProximityMatrix* gpm, int permutationsNumber)
770 {
771  assert(gpm);
772 
773  te::graph::AbstractGraph* graph = gpm->getGraph();
774 
775  //check if the graph has the LISASig attribute
776  int lisaSigAttrIdx;
778  throw;
779 
780  //add boxmap attribute into gpm
782  int nAttrs = graph->getMetadata()->getVertexPropertySize();
783 
784  //create graph vertex iterator
785  std::auto_ptr<te::graph::MemoryIterator> it(new te::graph::MemoryIterator(graph));
786 
787  //create task
789 
790  task.setTotalSteps(it->getVertexInteratorCount());
791  task.setMessage(TE_TR("Calculating LISA Map."));
792 
793  te::graph::Vertex* v = it->getFirstVertex();
794 
795  while(!it->isVertexIteratorAfterEnd())
796  {
797  double lisaSigValue = te::sa::GetDataValue(v->getAttributes()[lisaSigAttrIdx]);
798 
799  int significanceClass = 0;
800 
801  if(lisaSigValue <= 0.001) //permutationNumber >= 999
802  significanceClass = 3;
803  else if(lisaSigValue <= 0.01 && lisaSigValue > 0.001) //permutationNumber >= 99
804  significanceClass = 2;
805  else if(lisaSigValue <= 0.05 && lisaSigValue > 0.01)
806  significanceClass = 1;
807 
808  v->setAttributeVecSize(nAttrs);
809  v->addAttribute(lisaMapAttrIdx, new te::dt::SimpleData<int, te::dt::INT32_TYPE>(significanceClass));
810 
811  if(!task.isActive())
812  {
813  throw te::common::Exception(TE_TR("Operation canceled by the user."));
814  }
815 
816  task.pulse();
817 
818  v = it->getNextVertex();
819  }
820 }
821 
823 {
824  assert(gpm);
825 
826  te::graph::AbstractGraph* graph = gpm->getGraph();
827 
828  //check if the graph has the LISAMap attribute
829  int lisaMapAttrIdx;
830  if(!te::sa::GetGraphVertexAttrIndex(graph, TE_SA_LISAMAP_ATTR_NAME, lisaMapAttrIdx))
831  throw;
832 
833  //check if the graph has the BoxMap attribute
834  int boxMapAttrIdx;
835  if(!te::sa::GetGraphVertexAttrIndex(graph, TE_SA_BOXMAP_ATTR_NAME, boxMapAttrIdx))
836  throw;
837 
838  //add boxmap attribute into gpm
840  int nAttrs = graph->getMetadata()->getVertexPropertySize();
841 
842  //create graph vertex iterator
843  std::auto_ptr<te::graph::MemoryIterator> it(new te::graph::MemoryIterator(graph));
844 
845  //create task
847 
848  task.setTotalSteps(it->getVertexInteratorCount());
849  task.setMessage(TE_TR("Calculating Moran Map."));
850 
851  te::graph::Vertex* v = it->getFirstVertex();
852 
853  while(!it->isVertexIteratorAfterEnd())
854  {
855  int lisaMapValue = (int) te::sa::GetDataValue(v->getAttributes()[lisaMapAttrIdx]);
856  int boxMapValue = (int) te::sa::GetDataValue(v->getAttributes()[boxMapAttrIdx]);
857 
858  int result = 0;
859 
860  if(lisaMapValue != 0)
861  result = boxMapValue;
862 
863  v->setAttributeVecSize(nAttrs);
864  v->addAttribute(moranMapAttrIdx, new te::dt::SimpleData<int, te::dt::INT32_TYPE>(result));
865 
866  if(!task.isActive())
867  {
868  throw te::common::Exception(TE_TR("Operation canceled by the user."));
869  }
870 
871  task.pulse();
872 
873  v = it->getNextVertex();
874  }
875 }
void setAttributeVecSize(int size)
This function is used to set the number of attributes associated with the vertex elements.
Definition: Vertex.cpp:79
void setMessage(const std::string &message)
Set the task message.
Functions used in statistics operations.
TESAEXPORT void MoranMap(te::sa::GeneralizedProximityMatrix *gpm)
Function used to calculate the moran map info for a gpm, classifies the objects based in the scatterp...
TESAEXPORT double GlobalMoranSignificance(te::sa::GeneralizedProximityMatrix *gpm, int attrIdx, int permutationsNumber, double moranIndex)
Function used to calculate the global moran significance.
virtual int getVertexPropertySize()
Used to verify the number of properties associated to vertex elements.
This class defines a Generalized Proximity Matrix.
TESAEXPORT double FirstMoment(te::sa::GeneralizedProximityMatrix *gpm, int attrIdx)
Function used to calculate mean (first moment) of a specific attribute from a gpm.
virtual te::graph::Edge * getEdge(int id)=0
It returns the edge element if it's exist.
This class can be used to inform the progress of a task.
Definition: TaskProgress.h:53
TESAEXPORT void LocalMean(te::sa::GeneralizedProximityMatrix *gpm, int attrIdx)
Function used to calculate the local mean of each vertex from gpm graph.
std::vector< te::dt::AbstractData * > & getAttributes()
It returns the vector of attributes associated with this element.
Definition: Vertex.cpp:74
#define TE_TR(message)
It marks a string in order to get translated.
Definition: Translator.h:347
Utilitary function for spatial analysis module.
#define TE_SA_GSTAR_ATTR_NAME
Definition: Config.h:50
TESAEXPORT int AddGraphVertexAttribute(te::graph::AbstractGraph *graph, std::string attrName, int dataType, int srid=TE_UNKNOWN_SRS, int subType=te::gm::UnknownGeometryType)
Function used to create the vertex attribute metadata in the graph of the gpm.
Definition: Utils.cpp:106
bool isActive() const
Verify if the task is active.
Functions used to calculate spatial statistics operations.
From the point of view of graph theory, vertices are treated as featureless and indivisible objects...
Definition: Vertex.h:68
void setTotalSteps(int value)
Set the task total stepes.
virtual te::graph::GraphMetadata * getMetadata()=0
Function used to access the graph metadata.
Class used to define the edge struct of a graph. Its compose with a identifier, the vertex origin and...
Definition: Edge.h:58
TESAEXPORT void LisaStatisticalSignificance(te::sa::GeneralizedProximityMatrix *gpm, int permutationsNumber)
Function used to calculate LISA Statical Significance for each gpm element.
TESAEXPORT double Sum(te::sa::GeneralizedProximityMatrix *gpm, int attrIdx)
Function used to calculate sum of a specific attribute from a gpm.
#define TE_SA_LISAMAP_ATTR_NAME
Definition: Config.h:74
#define TE_SA_WEIGHT_ATTR_NAME
Definition: Config.h:41
#define TE_SA_STDDEVZ_ATTR_NAME
Definition: Config.h:59
#define TE_SA_LOCALMEAN_ATTR_NAME
Definition: Config.h:53
Abstract class used to define the main functions of graph struct. All graph implementations must used...
Definition: AbstractGraph.h:55
#define TE_SA_BOXMAP_ATTR_NAME
Definition: Config.h:71
TESAEXPORT void LISAMap(te::sa::GeneralizedProximityMatrix *gpm, int permutationsNumber)
Function used to calculate the lisa map info for a gpm, classifies the objects based in the statistic...
#define TE_SA_LISASIGNIFICANCE_ATTR_NAME
Definition: Config.h:68
void pulse()
Calls setCurrentStep() function using getCurrentStep() + 1.
A base class for values that can be retrieved from the data access module.
Definition: AbstractData.h:57
std::set< int > & getSuccessors()
Returns the Successors vector.
Definition: Vertex.cpp:106
int getIdFrom()
It returns the vertex origin identification.
Definition: Edge.cpp:71
#define TE_SA_NUMNEIGHBORS_ATTR_NAME
Definition: Config.h:56
TESAEXPORT void BoxMap(te::sa::GeneralizedProximityMatrix *gpm, double mean)
Function used to calculate the box map info for a gpm, classifies the objects in quadrants based in t...
This class is designed to declare objects to be thrown as exceptions by TerraLib. ...
Definition: Exception.h:58
TESAEXPORT void ZAndWZ(te::sa::GeneralizedProximityMatrix *gpm, int attrIdx)
Function used to calculate the standard deviation Z and local mean of the desviation Z (WZ)...
#define TE_SA_LOCALMEANWZ_ATTR_NAME
Definition: Config.h:62
TESAEXPORT bool GetGraphVertexAttrIndex(te::graph::AbstractGraph *graph, std::string attrName, int &index)
Function used to get the vertex attribute index in the graph of the gpm.
Definition: Utils.cpp:168
TESAEXPORT double MoranIndex(te::sa::GeneralizedProximityMatrix *gpm)
Function used to calculate the moran index, also calculates the local moran value.
#define TE_SA_MORANINDEX_ATTR_NAME
Definition: Config.h:65
#define TE_SA_G_ATTR_NAME
Definition: Config.h:47
int getId()
It returns the vertex id.
Definition: Vertex.cpp:69
void addAttribute(int idx, te::dt::AbstractData *ad)
Add a new attribute to this element.
Definition: Vertex.cpp:84
A template for atomic data types (integers, floats, strings and others).
Definition: SimpleData.h:59
TESAEXPORT double SecondMoment(te::sa::GeneralizedProximityMatrix *gpm, int attrIdx, double mean)
Function used to calculate variance (second moment) of a specific attribute from a gpm...
TESAEXPORT double GetDataValue(te::dt::AbstractData *ad)
Function used to get the numeric value from a gpm property.
Definition: Utils.cpp:200
virtual te::graph::Vertex * getVertex(int id)=0
It returns the vertex element if it's exist.
#define TE_SA_MORANMAP_ATTR_NAME
Definition: Config.h:77
TESAEXPORT void GStatistics(te::sa::GeneralizedProximityMatrix *gpm, int attrIdx)
The local spatial statistic G is calculated for each zone based on the spatial weights object used...
std::vector< te::dt::AbstractData * > & getAttributes()
It returns the vector of attributes associated with this element.
Definition: Edge.cpp:81
TESAEXPORT bool GetGraphEdgeAttrIndex(te::graph::AbstractGraph *graph, std::string attrName, int &index)
Function used to get the edge attribute index in the graph of the gpm.
Definition: Utils.cpp:184
int getIdTo()
It returns the vertex destiny identification.
Definition: Edge.cpp:76
This class defines the GPM class.