This file is indexed.

/usr/include/terralib/kernel/TeComputeAttributeStrategies.h is in libterralib-dev 4.3.0+dfsg.2-4build2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
/************************************************************************************
TerraLib - a library for developing GIS applications.
Copyright � 2001-2007 INPE and Tecgraf/PUC-Rio.

This code is part of the TerraLib library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

You should have received a copy of the GNU Lesser General Public
License along with this library.

The authors reassure the license terms regarding the warranties.
They specifically disclaim any warranties, including, but not limited to,
the implied warranties of merchantability and fitness for a particular purpose.
The library provided hereunder is on an "as is" basis, and the authors have no
obligation to provide maintenance, support, updates, enhancements, or modifications.
In no event shall INPE and Tecgraf / PUC-Rio be held liable to any party for direct,
indirect, special, incidental, or consequential damages arising out of the use
of this library and its documentation.
*************************************************************************************/

/*! \file TeComputeAttributeStrategies.h
    This file contains functions to compute attributes based on 
	alternative strategies. These strategies (Strategy Design Pattern)
	correspond to operations to derive information based on other  layers attributes and/or
	spatial components (polygonal, raster, point, raster or other cells). 
*/
#ifndef TeComputeAttrStrategies_H
#define TeComputeAttrStrategies_H

#include <TeAttribute.h>
#include <TeBox.h>
#include <TeUtils.h>
#include <TeNetwork.h>
#include <TeKdTree.h>
#include <TeLayer.h>

typedef TeSAM::TeAdaptativeKdTreeNode<TeCoord2D, vector<TePoint>, TePoint> KDNODE;
typedef TeSAM::TeAdaptativeKdTree<KDNODE> KDTREE;

class TeTheme;
class TeRaster;
class TeQuerier;

/** @defgroup NSCellFillStrategies Non-spatial strategy hierarchy to compute cells attributes
    @ingroup FillCellAlg
	Abstract class in the strategies hierarchy for computing attribute values based on other layers 
	(Strategy Design Pattern). 
	@{
 */
//!  Abstract class in the strategies hierarchy for computing attribute values based on other layers (Strategy Design Pattern). 
template<class Iterator>
class TeComputeAttrStrategy  
{
	public:
		//! Function to access the default value
		TeProperty  defaultValue () { return defaultValue_; }

		//! Virtual function to compute the attribute based on the iterator
		/* 
			\param first		the first element of the Iterator
			\param last			the last element of the Iterator
			\param columnName	the attribute name to be generated in the TeProperty's
		*/
		virtual  vector<TeProperty> compute (Iterator /*first*/, Iterator /*last*/, const string& /*columnName*/)
		{
			vector<TeProperty> result;
			result.push_back (defaultValue_);
			return result;
		}

		//! Destructor
		virtual ~TeComputeAttrStrategy(){}

		bool ComputeAttrIsDummy(double value, double dummy)
		{
			return (ABS(value - dummy) < 0.001);
		}


	protected:
		//! Empty constructor
		TeComputeAttrStrategy () {};

		//! The default returning value
		TeProperty	defaultValue_;
};


//! Class to compute the average given some elements of an iterator
template<class Iterator>
class TeAverageStrategy: public TeComputeAttrStrategy<Iterator>  
{
	public:
		//! Constructor
		/* 
			\param defaultValue		the returned value of the compute method when the iterator is empty	
		*/	
		TeAverageStrategy (double dummyV = 0.0, double defaultV = 0.0) {this->defaultValue_.value_ = Te2String (defaultV); dummy = dummyV; }

		//! Destructor
		virtual ~TeAverageStrategy() {}

		//! Function to compute the average based on the iterator
		/* 
			\param first		the first element of the Iterator
			\param last			the last element of the Iterator
			\param columnName	the attribute name to be generated in the TeProperty's
		*/
		virtual  vector<TeProperty> compute (Iterator first, Iterator last, const string& columnName)
		{
			double	tot_val = 0.0;
			int		num = 0;

			Iterator it = first;
			while (it != last)
			{
				if( !ComputeAttrIsDummy((*it), dummy) )
				{
					tot_val += (*it);
					num++;
				}
				++it;
			}
	
			TeProperty  prop;
			prop.attr_.rep_.name_ = columnName;
			prop.attr_.rep_.type_ = TeREAL;
			prop.attr_.rep_.numChar_ = 48;
			if (num > 0)
			{
				double val = tot_val/num;
				prop.value_ = Te2String (val);
			}
			else prop.value_ = this->defaultValue_.value_;
	
			TePropertyVector  result;
			result.push_back (prop);
			return result;
		}
	private:
		double dummy;
};


//! Class to compute the average given some elements of an iterator
template<class Iterator>
class TeSTDevStrategy: public TeComputeAttrStrategy<Iterator>  
{
	public:
		//! Constructor
		/* 
			\param defaultValue		the returned value of the compute method when the iterator is empty	
		*/	
		TeSTDevStrategy (double dummyV = 0.0, double defaultV = 0.0) {this->defaultValue_.value_ = Te2String (defaultV); dummy = dummyV; }

		//! Destructor
		virtual ~TeSTDevStrategy() {}

		//! Function to compute the average based on the iterator
		/* 
			\param first		the first element of the Iterator
			\param last			the last element of the Iterator
			\param columnName	the attribute name to be generated in the TeProperty's
		*/
		virtual  vector<TeProperty> compute (Iterator first, Iterator last, const string& columnName)
		{
			double tot_val = 0.0;
			double stdev = 0.0;
			int	   num = 0;
			
			Iterator it = first;
			while (it != last)
			{
				if( !ComputeAttrIsDummy((*it), dummy) )
				{
					tot_val += (*it);
					num++;
				}
				++it;
			}
			if(num > 0)
			{
				double mean = tot_val / num;
				it = first;
				while (it != last)
				{
					if( !ComputeAttrIsDummy((*it), dummy) ) stdev += pow((*it) - mean, 2);

					++it;
				}
				stdev = stdev / num;
				stdev = sqrt(stdev);
			}

			TeProperty  prop;
			prop.attr_.rep_.name_ = columnName;
			prop.attr_.rep_.type_ = TeREAL;
			prop.attr_.rep_.numChar_ = 48;

			if (num > 0) prop.value_ = Te2String (stdev);
			else         prop.value_ = this->defaultValue_.value_;
	
			TePropertyVector  result;
			result.push_back (prop);
			return result;
		}	
	private:
		double dummy;
};


//! Class to compute the sum of the elements of an iterator
template<class Iterator>
class TeSumStrategy: public TeComputeAttrStrategy<Iterator>   
{
	public:
		//! Constructor
		/* 
			\param defaultValue		the returned value of the compute method when the iterator is empty	
		*/	
		TeSumStrategy (double dummyV = 0.0, double defaultV = 0.0) {this->defaultValue_.value_ = Te2String (defaultV); dummy = dummyV; }

		//! Destructor
		virtual ~TeSumStrategy() {}

		//! Function to compute the sum of the iterator
		/* 
			\param first		the first element of the Iterator
			\param last			the last element of the Iterator
			\param columnName	the attribute name to be generated in the TeProperty's
		*/
		virtual  vector<TeProperty> compute (Iterator first, Iterator last, const string& columnName)
		{  
			double sum = 0.0;
			Iterator it = first;
			bool found = false;
			while (it != last)
			{
				if( !ComputeAttrIsDummy((*it), dummy) )
				{
					sum += (*it);
					found = true;
				}
				++it;
			}

			TeProperty  prop;

			if (first == last || !found) prop = this->defaultValue_;
			else  prop.value_ = Te2String (sum);

			prop.attr_.rep_.name_ = columnName;
			prop.attr_.rep_.type_ = TeREAL;
			prop.attr_.rep_.numChar_ = 48;
			TePropertyVector  result;
			result.push_back (prop);
			return result;
		}
	private:
		double dummy;
};


//! Class to compute the minimum value of an iterator
template<class Iterator>
class TeMinimumStrategy: public TeComputeAttrStrategy<Iterator>   
{
	public:
		//! Constructor
		/* 
			\param defaultValue		the returned value of the compute method when the iterator is empty	
		*/	
		TeMinimumStrategy (double dummyV = 0.0, double defaultV = 0.0) {this->defaultValue_.value_ = Te2String (defaultV); dummy = dummyV; }

		//! Destructor
		virtual ~TeMinimumStrategy() {}

		//! Function to compute the minimum value of the iterator
		/* 
			\param first		the first element of the Iterator
			\param last			the last element of the Iterator
			\param columnName	the attribute name to be generated in the TeProperty's
		*/
		virtual  vector<TeProperty> compute (Iterator first, Iterator last, const string& columnName)
		{
			double min = TeMAXFLOAT;
			Iterator it = first;
			while (it != last)
			{
				if( !ComputeAttrIsDummy((*it), dummy) )
				{
					double val = (*it);
					if (val < min) min = val;
				}
				++it;
			}

			TeProperty  prop;
			prop.attr_.rep_.name_ = columnName;
			prop.attr_.rep_.type_ = TeREAL;
			prop.attr_.rep_.numChar_ = 48;

			if (first == last || min == TeMAXFLOAT)
				prop.value_ = this->defaultValue_.value_; 
			else  
				prop.value_ = Te2String (min);

			TePropertyVector  result;
			result.push_back (prop);
			return result;
		}
	private:
		double dummy;
};


//! Class to compute the maximum value of an iterator
template<class Iterator>
class TeMaximumStrategy: public TeComputeAttrStrategy<Iterator>   
{
	public:
		//! Constructor
		/* 
			\param defaultValue		the returned value of the compute method when the iterator is empty	
		*/	
		TeMaximumStrategy (double dummyV = 0.0, double defaultV = 0.0) {this->defaultValue_.value_ = Te2String (defaultV); dummy = dummyV; }

		//! Destructor
		virtual ~TeMaximumStrategy() {}

		//! Function to compute the maximum value of the iterator
		/* 
			\param first		the first element of the Iterator
			\param last			the last element of the Iterator
			\param columnName	the attribute name to be generated in the TeProperty's
		*/
		virtual vector<TeProperty> compute (Iterator first, Iterator last, const string& columnName)
		{
			double	max =  TeMINFLOAT;
			Iterator it = first;
			while (it != last)
			{
				if( !ComputeAttrIsDummy((*it), dummy) )
				{
					double val = (*it);
					if (val > max) max = val;
				}
				++it;
			}

			TeProperty  prop;
			prop.attr_.rep_.name_ = columnName;
			prop.attr_.rep_.type_ = TeREAL;
			prop.attr_.rep_.numChar_ = 48;

			if (first == last || max == TeMINFLOAT)
				prop.value_ = this->defaultValue_.value_; 
			else  
				prop.value_ = Te2String (max);

			TePropertyVector  result;
			result.push_back (prop);
			return result;
		}
	private:
		double dummy;
};


//! Class to compute the percentages of each class (described in a map) of an iterator
template<class Iterator>
class TeCategoryPercentageStrategy: public TeComputeAttrStrategy<Iterator>   
{
	public:
		//! Constructor
		/* 
			\param defaultValue		the returned value of the compute method when the iterator is empty	
			\param classes			classes for regrouping the values	
		*/	
		TeCategoryPercentageStrategy (map<string, string>& classes, double defaultValue = 0.0) 
		{
			this->defaultValue_.value_ = Te2String (defaultValue); 
			classesMap_ = classes;
			usingMap = true;
		}

		//! Constructor
		/* 
			\param defaultValue		the returned value of the compute method when the iterator is empty	
			\param dummyV			the ignored value
		*/	

		TeCategoryPercentageStrategy(double defaultValue = 0.0, string dummyV = "")
		{
			usingMap = false;
			this->defaultValue_.value_ = Te2String (defaultValue); 
			dummy = dummyV;
		}

		//! Destructor
		virtual ~TeCategoryPercentageStrategy() {}

		//! Function to compute the percentages based on the iterator
		/* 
			\param first		the first element of the Iterator
			\param last			the last element of the Iterator
			\param columnName	the attribute name to be generated in the TeProperty's
		*/
		virtual  vector<TeProperty> compute (Iterator first, Iterator last, const string& columnName)
		{
			TeProperty category;
			int num = 0;

			// initialize count
			map<string, int>  count;

			if(usingMap)
			{
				map<string, string>::iterator itMap = classesMap_.begin();
				while (itMap != classesMap_.end())
				{
					count[(*itMap).second] = 0;
					++itMap;
				}

				Iterator it = first;
				while (it != last)
				{
					if (it.getProperty (category))
					{
						count[classesMap_[category.value_]]++;
						num++;
					}
					++it;
				}
			}
			else // use the data to initialize the counter
			{
				Iterator it = first;
				while (it != last)
				{
					if (it.getProperty (category) && category.value_ != dummy)
						count[category.value_] = 0;

					++it;
				}

				it = first;
				while (it != last)
				{
					if (it.getProperty (category) && category.value_ != dummy)
					{
						count[category.value_]++;
						num++;
					}
					++it;
				}
			}

			TePropertyVector  result;
			map <string, int>:: iterator count_it = count.begin();

			while(count_it != count.end())  
			{
				string value_ =  (*count_it).first;
				for (unsigned i = 0; i < value_.size(); i++)
					if(value_[i] == '.' || value_[i] == '+' || value_[i] == '-')
						// remove them from value because they cant be part of a column name
						value_[i] = '_';

				TeProperty prop;
				prop.attr_.rep_.name_ = columnName + value_;
				prop.attr_.rep_.type_ = TeREAL;
				prop.attr_.rep_.numChar_ = 48;

				double percent = 0.0;
				double value = (double)(*count_it).second;
				if (num != 0) percent = value/num;
				prop.value_    = Te2String (percent);
				result.push_back (prop);
				++count_it;
			}
			return result;
		}

	private:
		//! Map of classes
		map<string, string> classesMap_;
		bool usingMap;
		string dummy;
};


//! Class to compute if there is any value in an iterator
template<class Iterator>
class TePresenceStrategy: public TeComputeAttrStrategy<Iterator>
{
	public:
		//! Constructor
		/* 
			\param defaultValue		the returned value of the compute method when the iterator is empty	
		*/	
		TePresenceStrategy (bool defaultValue = 0) 
		{
			this->defaultValue_.attr_.rep_.type_ = TeINT;
			this->defaultValue_.value_ = Te2String (defaultValue); 
			this->defaultValue_.attr_.rep_.numChar_ = 48;
		}

		//! Destructor
		virtual ~TePresenceStrategy() {}

		//! Function to compute the presence based on the iterator
		//! It has value "1" if there is any element, otherwise it has the value of the constructor
		/* 
			\param first		the first element of the Iterator
			\param last			the last element of the Iterator
			\param columnName	the attribute name to be generated in the TeProperty's
		*/
		virtual  vector<TeProperty> compute (Iterator first, Iterator last, const string& columnName)
		{
			TeProperty prop;
			if (first != last) 
				prop.value_ = "1";
			else
				prop = this->defaultValue_;
	
			TePropertyVector  result;
			prop.attr_.rep_.name_ = columnName; 
			prop.attr_.rep_.type_ = TeINT;
			prop.attr_.rep_.numChar_ = 48;
			result.push_back (prop);
			return result;
		}
};

//! Class for computing the majority in a given iterator of values without classes
template<class Iterator>
class TeMajorityStrategy: public TeComputeAttrStrategy<Iterator>
{
	public:
		//! Constructor
		/* 
			\param defaultType		the type of the default value	
		*/	
		TeMajorityStrategy (TeAttrDataType defaultType = TeINT, string defaultValue = "", string dummyV = "")
		{
			this->defaultValue_.attr_.rep_.type_ = defaultType;
			this->defaultValue_.value_ = defaultValue; 
			dummy = dummyV;
		}

		//! Destructor
		virtual ~TeMajorityStrategy() {}

		//! Function to compute the majority of the iterator
		/* 
			\param first		the first element of the Iterator
			\param last			the last element of the Iterator
			\param columnName	the attribute name to be generated in the TeProperty's
		*/
		virtual  vector<TeProperty> compute (Iterator first, Iterator last, const string& columnName)
		{
			TeProperty category;
			category.attr_.rep_.type_ = this->defaultValue_.attr_.rep_.type_;
	
			map<string, int>  count;
			Iterator it = first;
			while (it != last)
			{
				if (it.getProperty (category) && category.value_ != dummy)
					count[category.value_]++;

				++it;
			}
	
			TeProperty prop;
			prop.attr_.rep_.type_ = category.attr_.rep_.type_;
			prop.value_ = this->defaultValue_.value_;
			prop.attr_.rep_.name_ = columnName; 
			prop.attr_.rep_.numChar_ = 48;

			int max = 0;
			map <string, int>:: iterator count_it = count.begin();
			while(count_it != count.end())  
			{
				if ((*count_it).second > max)
				{
					prop.value_ = (*count_it).first;
					max = (*count_it).second;
				}
				++count_it;		
			}
			TePropertyVector  result;
			result.push_back (prop);
			return result;
		}
	private:
		string dummy;
};


//! Class for compute the category with more occurences in the elements of a given iterator, using classes
template<class Iterator>
class TeMajorityCategoryStrategy: public TeComputeAttrStrategy<Iterator>   
{
	public:
		//! Constructor
		/*
			\param classesMap		the returning values for each categoty
			\param defaultValue		the type of the attribute to be created
		*/	
		TeMajorityCategoryStrategy (const map<string, string>& classesMap, TeAttrDataType defaultType = TeINT) 
		{
			this->defaultValue_.attr_.rep_.type_ = defaultType;
			this->defaultValue_.value_ = "0"; 
			classesMap_ = classesMap;
		}

		//! Destructor
		virtual ~TeMajorityCategoryStrategy() {}

		//! Function to compute the category with more occurrences
		/* 
			\param first		the first element of the Iterator
			\param last			the last element of the Iterator
			\param columnName	the attribute name to be generated in the TeProperty's
		*/
		virtual vector<TeProperty> compute (Iterator first, Iterator last, const string& columnName)
		{
			TeProperty category;
			category.attr_.rep_.type_ = this->defaultValue_.attr_.rep_.type_;

			// initialize count
			map<string, int>  count;
			map<string, string>::iterator itMap = classesMap_.begin();
			while (itMap != classesMap_.end())
			{
				count[(*itMap).second] = 0;
				++itMap;
			}

			Iterator it = first;
			while (it != last)
			{
				if (it.getProperty (category))
				{
					count[classesMap_[category.value_]]++;
				}
				++it;
			}

			TePropertyVector  result;
			TeProperty prop;
			prop.attr_.rep_.type_ = TeSTRING;
			//	prop.value_ = defaultValue_.value_;
			prop.attr_.rep_.name_ = columnName; 
			prop.attr_.rep_.numChar_ = 48;

			int max = 0;
			map <string, int>:: iterator count_it = count.begin();
			while(count_it != count.end())  
			{
				if ((*count_it).second > max)
				{
					prop.value_ = (*count_it).first;
					max = (*count_it).second;
				}
				++count_it;		
			}

			result.push_back (prop);
			return result;
		}

	protected:
			map<string, string> classesMap_;
};
/** @} */   



/** @defgroup CellFillStrategies Spatial strategy hierarchy to compute cells attributes
    @ingroup FillCellAlg
	Abstract class in the strategies hierarchy for computing attribute values based on other layers 
	(Strategy Design Pattern). 
	@{
 */
//! Virtual class for computing attributes based on the spatial characteristics of the objects (Strategy Design Pattern)
class TL_DLL TeComputeSpatialStrategy  
{
	public:
		//! Destructor
		virtual ~TeComputeSpatialStrategy() {}

		//! Virtual function to compute the attribute
		/* 
			\param box		compute the attribute of a cell having this box
		*/
		virtual string compute(TeBox box) = 0;

		//! The type of attriute generated by the class. Default is TeREAL
		virtual TeAttrDataType Type() { return TeREAL; }

		//! Return all the geometries within a given box
		/* 
			\param box		the box to be used
			\patam att		the attribute returned together with the objects. If none, this function returns only the geometries
		*/
		TeQuerier* getWithinGeometry(TeBox box, string att = "");

	protected:
		//! Constructor
		/* 
			\param theme_		the theme to be used as basis for computing the attribute
			\param rep_			the representation to be used	
		*/	
		TeComputeSpatialStrategy (TeTheme* theme_, TeGeomRep rep_) { theme = theme_; rep = rep_; }

		//! Theme used to compute the attributes
		TeTheme* theme;

		//! The geometry of the theme used
		TeGeomRep rep;
};


//! Class to compute the average of elements weighted by their areas
//! It is useful when working with values representing averages
class TL_DLL TeAverageWeighByAreaStrategy: public TeComputeSpatialStrategy 
{
	public:
		//! Constructor
		/* 
			\param theme_		the theme to be used as basis for computing the attribute
			\param att_			the attribute used to compute the sum. It must be numerical
			\param rep_			the representation to be used	
		*/	
		TeAverageWeighByAreaStrategy  (TeTheme* theme_, string att_, TeGeomRep rep_) : TeComputeSpatialStrategy(theme_, rep_) { attribute = att_; }

		//! Destructor
		virtual ~TeAverageWeighByAreaStrategy() {}

		//! Function to compute the attribute
		/* 
			\param box		compute the attribute of a cell having this box
		*/
		virtual string compute (TeBox box);

	private:
		//! The attribute used to calculate the average
		string attribute;
};


//! Class to compute the sum of elements weighted by their areas
//! This function distribute the values keeping the overall sum of the elements on the layer
class TL_DLL TeSumWeighByAreaStrategy: public TeComputeSpatialStrategy  
{
	public:
		//! Constructor
		/* 
			\param theme_		the theme to be used as basis for computing the attribute
			\param att_			the attribute used to compute the sum. It must be numerical
			\param rep_			the representation to be used	
		*/	
		TeSumWeighByAreaStrategy (TeTheme* theme_, string att_, TeGeomRep rep_) : TeComputeSpatialStrategy(theme_, rep_) { attribute = att_; }

		//! Destructor
		virtual ~TeSumWeighByAreaStrategy() {}

		//! Function to compute the attribute
		/* 
			\param box		compute the attribute of a cell having this box
		*/
		virtual  string compute (TeBox box);

	private:
		//! The attribute used to calculate the average
		string attribute;
};


//! Class to compute the value that occurs more frequently in a given box 
class TL_DLL TeCategoryMajorityStrategy: public TeComputeSpatialStrategy  
{
	public:
		//! Constructor
		/* 
			\param theme_		the theme to be used as basis for computing the attribute
			\param att_			the attribute used to compute the majority
			\param classesMap	a map to reclassify the values
		*/	
		TeCategoryMajorityStrategy (TeTheme* theme_, string att_, map<string, string>& classesMap) : TeComputeSpatialStrategy(theme_, TePOLYGONS) 
        	{classesMap = classesMap; attribute = att_;}

		//! Destructor
		virtual ~TeCategoryMajorityStrategy() {}

		//! Function to compute the attribute
		/* 
			\param box		compute the attribute of a cell having this box
		*/
		virtual  string compute (TeBox box);	

	protected:
			//! The map of values
			map<string, string> classesMap;

			//! The attribute used in the majority
			string attribute;
};


//! Class to count the elements in a given box 
//! This class returns the number of elements whose BOX intercepts the box in the argument of compute
//! For safety, use the count classes that inherit this one
class TL_DLL TeCountObjectsStrategy: public TeComputeSpatialStrategy  
{
	public:
		//! Constructor
		/* 
			\param theme_		the theme to be used as basis for computing the attribute
			\param geom_		the representation to be used	
		*/	
		TeCountObjectsStrategy (TeTheme* theme_, TeGeomRep geom_) : TeComputeSpatialStrategy(theme_, geom_) { }

		//! Destructor
		virtual ~TeCountObjectsStrategy() { }

		//! Function to compute the attribute
		/* 
			\param box		compute the attribute of a cell having this box
		*/
		virtual string compute (TeBox box);

		//! This class generates TeINT attributes
		virtual TeAttrDataType Type() { return TeINT; }
};


//! Class to compute the number of polygons in a given box
class TL_DLL TeCountPolygonalObjectsStrategy: public TeCountObjectsStrategy
{
	public:
		//! Constructor
		/* 
			\param theme_		the theme to be used as basis for computing the attribute
		*/	
		TeCountPolygonalObjectsStrategy (TeTheme* theme_) : TeCountObjectsStrategy(theme_, TePOLYGONS) {}

		//! Function to compute the attribute
		/* 
			\param box		compute the attribute of a cell having this box
		*/
		virtual string compute (TeBox box);
};


//! Class to compute the number of lines in a given box
class TL_DLL TeCountLineObjectsStrategy: public TeCountObjectsStrategy
{
	public:
		//! Constructor
		/* 
			\param theme_		the theme to be used as basis for computing the attribute
		*/	
		TeCountLineObjectsStrategy (TeTheme* theme_) : TeCountObjectsStrategy(theme_, TeLINES) {}

		//! Function to compute the attribute
		/* 
			\param box		compute the attribute of a cell having this box
		*/
		virtual string compute (TeBox box);
};


//! Class to count the number of points in a given box
class TL_DLL TeCountPointObjectsStrategy: public TeCountObjectsStrategy 
{
	public:
		//! Constructor
		/* 
			\param theme_		the theme to be used as basis for computing the attribute
		*/	
		TeCountPointObjectsStrategy (TeTheme* theme_) : TeCountObjectsStrategy(theme_, TePOINTS) {}
};


//! Class to compute the length of lines within a given box 
class TL_DLL TeLineLengthStrategy: public TeComputeSpatialStrategy 
{
	public:
		//! Constructor
		/* 
			\param theme_		the theme to be used as basis for computing the attribute
		*/	
		TeLineLengthStrategy (TeTheme* theme_) : TeComputeSpatialStrategy(theme_, TeLINES){}

		//! Destructor
		virtual ~TeLineLengthStrategy() {}

		//! Function to compute the attribute
		/* 
			\param box		compute the attribute of a cell having this box
		*/
		virtual  string compute (TeBox box);
};


//! Class to compute the area of polygons covering a given box
class TL_DLL TeTotalAreaPercentageStrategy: public TeComputeSpatialStrategy  
{
	public:
		//! Constructor
		/* 
			\param theme_		the theme to be used as basis for computing the attribute
		*/	
		TeTotalAreaPercentageStrategy (TeTheme* theme_) : TeComputeSpatialStrategy(theme_, TePOLYGONS){}

		//! Destructor
		virtual ~TeTotalAreaPercentageStrategy() {}

		//! Function to compute the attribute
		/* 
			\param box		compute the attribute of a cell having this box
		*/
		virtual string compute (TeBox box);
};

/*
// pedro@20061214
//! Class to compute the distance based on a net 
class TeMinimumDistanceNetStrategy:  public TeComputeSpatialStrategy
{
	public:
		//! Constructor
		TeMinimumDistanceNetStrategy(TeTheme* theme_, TeGeomRep rep_, TeGraphNetwork* net, double dist_ = 50) : TeComputeSpatialStrategy(theme_, rep_) { maxDist = dist_; tree = NULL; }
		
		//! Destructor
		virtual ~TeMinimumDistanceNetStrategy() { if(tree) delete tree; }

        void CreatePoints(TeCoord2D p1, TeCoord2D p2); // create all intermediate points between them, using the maximum distance

		virtual string compute (TeBox box);

		virtual TeAttrDataType Type() { return TeREAL; }

	private:	

		double maxDist;

		vector<pair<TeCoord2D, TePoint> > dataSet;

		KDTREE* tree;
};
*/


//! Class to compute distances using KD-trees
class TL_DLL TeMinimumDistanceStrategy:  public TeComputeSpatialStrategy
{
	public:
		//! Constructor
		/* 
			\param theme_		the theme to be used as basis for computing the attribute
			\param gep_			the geometry to be used
			\param distError_	the maximum error tolerated by this operator
		*/	
		TeMinimumDistanceStrategy(TeTheme* theme_, TeGeomRep rep_, double distError_ = 50); 

		//! Destructor
		virtual ~TeMinimumDistanceStrategy() { if(tree) delete tree; }

		//! Function to compute the attribute
		/* 
			\param box		compute the attribute of a cell having this box
		*/
		virtual string compute (TeBox box);

	protected:
		//! Create intermediate points between two given points, using the maximum distance
        void CreatePoints(TeCoord2D p1, TeCoord2D p2);

		//! The maximum error in the computation
		double maxDist;

		//!
		vector<pair<TeCoord2D, TePoint> > dataSet;

		//! The tree to store the points
		KDTREE* tree;
};


//! Class to compute the minimum distance to a theme of polygons. The calculatd distance is approximated, and has an error of at most
//! the parameter dist_ in the constructor
class TL_DLL TeMinimumDistancePolygonsStrategy: public TeMinimumDistanceStrategy
{
	public:
		//! Constructor
		/* 
			\param theme_		the theme to be used as basis for computing the attribute
			\param distError_	the maximum error tolerated by this operator
		*/	
		TeMinimumDistancePolygonsStrategy(TeTheme* theme_, double distError_ = 150);

		//! Destructor
		virtual ~TeMinimumDistancePolygonsStrategy() {}

		//! Function to compute the attribute
		/* 
			\param box		compute the attribute of a cell having this box
		*/
		virtual string compute (TeBox box);
};


//! Class to compute 
class TL_DLL TeMinimumDistanceLinesStrategy : public TeMinimumDistanceStrategy
{
	public:
		//! Constructor
		/* 
			\param theme_		the theme to be used as basis for computing the attribute
			\param distError_	the maximum error tolerated by this operator
		*/	
		TeMinimumDistanceLinesStrategy(TeTheme* theme_, double distError_ = 150);

		//! Destructor
		virtual ~TeMinimumDistanceLinesStrategy() {}
};


//! Class to calculate the minimum distance of a box to points.
//! It calculates the exact value once the KD-tree calculates the exact distance to points
class TL_DLL TeMinimumDistancePointsStrategy: public TeMinimumDistanceStrategy
{
	public:
		//! Constructor
		/* 
			\param theme_		the theme to be used as basis for computing the attribute
		*/	
		TeMinimumDistancePointsStrategy(TeTheme* theme_);

		//! Destructor
		virtual ~TeMinimumDistancePointsStrategy() {}
};

/** @} */
#endif