/usr/include/terralib/stat/TeKernelFunctions.h is in libterralib-dev 4.0.0-5ubuntu1.
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 | /************************************************************************************
Exploring and analysis of geographical data using TerraLib and TerraView
Copyright � 2003-2007 INPE and LESTE/UFMG.
Partially funded by CNPq - Project SAUDAVEL, under grant no. 552044/2002-4,
SENASP-MJ and INPE
This program 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.
This program is distributed hoping it will be useful, however, WITHOUT ANY
WARRANTIES; neither to the implicit warranty of MERCHANTABILITY OR SUITABILITY FOR
AN SPECIFIC FINALITY. Consult the GNU General Public License for more details.
You must have received a copy of the GNU General Public License with this program.
In negative case, write to the Free Software Foundation, Inc. in the following
address: 59 Temple Street, Suite 330, Boston, MA 02111-1307 USA.
***********************************************************************************/
/*! \file TeKernelFunctions.h
\brief This file contains functions to spatial kernel estimation: basic, adaptive and ratio
*/
#ifndef __TERRALIB_INTERNAL_KERNELFUNCTIONS_H
#define __TERRALIB_INTERNAL_KERNELFUNCTIONS_H
#include "TeException.h"
#include "TeKernelParams.h"
#include "TeStatDefines.h"
#include "TeGeometryAlgorithms.h"
#include "TeProgress.h"
#define INVALID_KERNEL -TeMAXFLOAT
#define IDX_ATTR 0
#define IDX_KERNEL 0
#define IDX_NUMERATOR 1
#define IDX_KERNEL1 0
#define IDX_KERNEL2 1
enum TeKernelError {
KERNEL_NO_CENTROID,
KERNEL_ALL_NULL,
KERNEL_INVALID_AREA,
KERNEL_INVALID_VALUE,
KERNEL_NO_VALUE
};
class STAT_DLL TeKernelException {
TeKernelError error_;
public:
TeKernelException(TeKernelError e) {
error_ = e;
}
TeKernelError getErrorCode() {
return error_;
}
};
/*! Kernel functions
* \param tau spatial threshold to define neighboorhood
* \param distance distance between event and region centroid
* \param intensity attribute value for event
*/
STAT_DLL double TeKernelQuartic(double tau, double distance, double intensity);
STAT_DLL double TeKernelNormal(double tau, double distance, double intensity);
STAT_DLL double TeKernelUniform(double tau, double distance, double intensity);
STAT_DLL double TeKernelTriangular(double tau, double distance, double intensity);
STAT_DLL double TeKernelNegExponential(double tau, double distance, double intensity);
/**
* Calcula a media geometrica de um conjunto de valores atraves da
* utilizacao de logaritmos
* Implementado pelo Andre
**/
template <typename ItReg>
double TeKernelGeometricMean(ItReg& beginReg, ItReg& endReg, int idx)
{
double mantissa, MediaE, MediaM;
int expoente, Cont;
int MediaETmp = 0;
double MediaMTmp = 0;
double log_two = log(2.0);
Cont = 0;
ItReg it = beginReg;
double val;
while (it != endReg) {
if ((*it).getDoubleProperty(idx, val) && (val > 0))
{
mantissa = frexp(val,&expoente);
MediaMTmp = MediaMTmp+log(mantissa);
MediaETmp = MediaETmp+expoente;
Cont++;
}
++it;
}
MediaE = MediaETmp;
MediaM = (MediaMTmp+(MediaE*log_two))/Cont;
MediaM = exp(MediaM);
return MediaM;
}
/*! \brief Evaluates normalization factor for kernel
* \param begin begin of event set iterator
* \param end end of event set iterator
* \return sum of intensities
**/
template <typename ItEvt>
double KernelNormalizationFactor(ItEvt itBegin, ItEvt itEnd) {
double normFactor = 0;
double intensity;
ItEvt evtIter = itBegin;
while (evtIter != itEnd) {
(*evtIter).getDoubleProperty(IDX_ATTR, intensity);
normFactor += intensity;
++evtIter;
}
return normFactor;
}
/*! \brief Evaluates kernel value of events with intensity (attribute)
* for one support region, for a kernel function
* \param center support region centroid
* \param begin begin of event set iterator
* \param end end of event set iterator
* \param radius spatial threshold
* \param kfunc type of kernel function
* This function assumes that intensity value of event is available on its first
* property - if it does not exist, it is set to 1.0 (identity)
**/
template <typename ItEvt>
double TeKernelValue (TeCoord2D& center, ItEvt& begin, ItEvt& end,
double radius, TeKernelFunctionType kfunc)
{
string intstr;
TeCoord2D location; //Event spatial location
double intensity; //Event intensity
//Final kernel value
double kernel = 0;
//Iterates through events, getting their location and intensity values
//and updating kernel value
ItEvt it = begin;
while (it != end)
{
(*it).centroid(location);
//If there is no properties, assume intensity of one
if (!(*it).getDoubleProperty(IDX_ATTR, intensity)) {
intensity = 1.0;
}
//Evaluates kernel value for one event
double distance = TeDistance (location,center);
double localK;
switch(kfunc) {
case TeKQuartic:
localK = TeKernelQuartic(radius,distance,intensity);
break;
case TeKNormal:
localK = TeKernelNormal(radius,distance,intensity);
break;
case TeKTriangular:
localK = TeKernelTriangular(radius,distance,intensity);
break;
case TeKNegExponential:
localK = TeKernelNegExponential(radius,distance,intensity);
break;
case TeKUniform:
localK = TeKernelUniform(radius,distance,intensity);
break;
}
kernel += localK;
++it;
}
return kernel;
}
/*! \brief Normalizes kernel values based on type of computation.
* \brief Gets value from IDX_KERNEL and stores it at the same place
* \param events set of spatial events
* \param regionsBegin begin of regions iterator
* \param reginsEnd end of region iterator
* \param kfunc type of kernel function
* \param ktype type of computation (SMA, density or probability)
* \param radius spatial threshold
**/
template<typename EventSet, typename ItRegionSet>
bool TeKernelNormalize(EventSet& events,
ItRegionSet& regionsBegin,
ItRegionSet& regionsEnd,
TeKernelComputeType kType,
double totKernel,
int idxProp)
{
double normKernel, kernel, area;
try {
//Evaluates sum of attributes for normalization
double normFactor =
KernelNormalizationFactor(events.begin(), events.end());
//Reiterates through regions defining normalized kernel value,
//given the computation type
ItRegionSet regIter = regionsBegin;
while (regIter != regionsEnd)
{
(*regIter).getDoubleProperty(idxProp, kernel);
if (kernel < 0) {
throw TeKernelException(KERNEL_INVALID_VALUE);
}
//Evaluates value based on computation type
switch(kType) {
case TeKMovingAverage:
normKernel = (kernel * normFactor)/totKernel;
break;
case TeKDensity:
//If region does not have an area, assumes one
(*regIter).area(area);
if (area <= 0) {
throw TeKernelException(KERNEL_INVALID_AREA);
}
normKernel = ((kernel * normFactor)/totKernel)/area;
break;
case TeKProbability:
normKernel = kernel/totKernel;
break;
}
//Store final value
(*regIter).setDoubleProperty(idxProp, normKernel);
++regIter;
}
} catch (TeKernelException /* e */) {
// int a = e.getErrorCode();
return false;
}
return true;
}
/*! \brief Evaluates kernel value for all support regions,
* storing values either on IDX_KERNEL or IDX_NUMERATOR.
* \param events set of spatial events
* \param regionsBegin begin of regions iterator
* \param reginsEnd end of region iterator
* \param kfunc type of kernel function
* \param ktype type of computation (SMA, density or probability)
* \param radius spatial threshold
* \param idxProp index of double property
**/
template<typename EventSet, typename ItRegionSet>
bool TeStatIntKernel(EventSet& events,
ItRegionSet& regionsBegin,
ItRegionSet& regionsEnd,
TeKernelFunctionType kfunc,
TeKernelComputeType kType,
double radius,
int idxProp)
{
//Support region attributes
TeCoord2D location;
double kernel;
//Regions iterator
ItRegionSet regIter = regionsBegin;
// Sum of kernels
double totKernel = 0;
//Treats unexpected expections returning false
try {
int dt = CLOCKS_PER_SEC/4;
int dt2 = CLOCKS_PER_SEC * 5;
clock_t t0, t1, t2;
t2 = clock();
t0 = t1 = t2;
int nsteps = 0;
//Iterates through regions
while (regIter != regionsEnd)
{
(*regIter).centroid(location);
//Define iterators for event sets -- may optimize
typename EventSet::iterator subSetBegin = events.begin(location,radius);
typename EventSet::iterator subSetEnd = events.end(location,radius);
if (kfunc == TeKNormal) {
subSetBegin = events.begin();
subSetEnd = events.end();
}
//Evaluates kernel value for region
kernel = TeKernelValue(location, subSetBegin, subSetEnd,
radius, kfunc);
totKernel += kernel;
(*regIter).setDoubleProperty(idxProp, kernel);
++regIter;
t2 = clock();
++nsteps;
if (TeProgress::instance())
{
if (int(t2-t1) > dt)
{
t1 = t2;
if (TeProgress::instance()->wasCancelled())
break;
if((int)(t2-t0) > dt2)
TeProgress::instance()->setProgress(nsteps);
}
}
}
if (TeProgress::instance())
TeProgress::instance()->reset();
if (totKernel <= 0) {
throw TeKernelException(KERNEL_ALL_NULL);
}
return TeKernelNormalize(events, regionsBegin, regionsEnd, kType, totKernel,
idxProp);
} catch (TeKernelException /* e */) {
// int a = e.getErrorCode();
return false;
}
return true;
}
/*! \brief Evaluates adaptive kernel value for all support regions,
* storing values (intermediary and final) in double property idxProp
* \param events set of spatial events
* \param regionsBegin begin of regions iterator
* \param reginsEnd end of region iterator
* \param kfunc type of kernel function
* \param ktype type of computation (SMA, density or probability)
* \param numReg number of support regions -- need to evaluate initial radius
* \param totalArea total area of support regions -- need to evaluate initial radius
* \param idxProp index of double property
**/
template<typename EventSet, typename ItRegionSet>
bool TeStatAdaptiveGeoMeanIntKernel(EventSet& events,
ItRegionSet& regionsBegin,
ItRegionSet& regionsEnd,
TeKernelFunctionType kfunc,
TeKernelComputeType ktype,
int /* numReg */,
double totalArea,
int idxProp)
{
try {
//Evaluate kernel with a fixed radius, based on formula, storing in vecValues
double radius = 0.68*pow((double)events.numObjects(),-0.2)*sqrt(totalArea);
double sqArea = sqrt(totalArea);
if (!TeStatIntKernel(events, regionsBegin, regionsEnd,
kfunc, ktype, radius, idxProp))
return false;
//Evaluate geometric mean of kernel values, to adjust radius
double meanKernel = TeKernelGeometricMean(regionsBegin, regionsEnd, idxProp);
if (meanKernel <= 0) {
throw TeKernelException(TeKernelException(KERNEL_ALL_NULL));
}
//Now, reassign radius, evaluating final value for kernel
ItRegionSet regIter = regionsBegin;
double prevKernel;
double newKernel;
double newRadius;
double totKernel = 0;
TeCoord2D location;
int dt = CLOCKS_PER_SEC/4;
int dt2 = CLOCKS_PER_SEC * 5;
clock_t t0, t1, t2;
t2 = clock();
t0 = t1 = t2;
int nsteps = 0;
//Iterates through regions
while (regIter != regionsEnd)
{
(*regIter).centroid(location);
//Define iterators for event sets -- may optimize
typename EventSet::iterator subSetBegin = events.begin(location,radius);
typename EventSet::iterator subSetEnd = events.end(location,radius);
if (kfunc == TeKNormal) {
subSetBegin = events.begin();
subSetEnd = events.end();
}
if (!((*regIter).getDoubleProperty(idxProp, prevKernel)) || (prevKernel < 0)) {
throw TeKernelException(KERNEL_INVALID_VALUE);
}
if (prevKernel > 0) {
newRadius = radius*pow((meanKernel/prevKernel),0.5);
if (newRadius > sqArea/4.0)
newRadius = sqArea/4.0; //Para funcionar quando o valor eh muito pequeno
newKernel = TeKernelValue(location, subSetBegin, subSetEnd, newRadius, kfunc);
}
else {
newKernel = 0.0;
}
totKernel += newKernel;
(*regIter).setDoubleProperty(idxProp, newKernel);
++regIter;
t2 = clock();
++nsteps;
if (TeProgress::instance())
{
if (int(t2-t1) > dt)
{
t1 = t2;
if (TeProgress::instance()->wasCancelled())
break;
if((int)(t2-t0) > dt2)
TeProgress::instance()->setProgress(nsteps);
}
}
}
if (TeProgress::instance())
TeProgress::instance()->reset();
if (totKernel <= 0) {
throw TeKernelException(KERNEL_ALL_NULL);
}
return TeKernelNormalize(events, regionsBegin, regionsEnd, ktype, totKernel,
idxProp);
} catch (TeKernelException /* e */) {
// int a = e.getErrorCode();
return false;
}
return true;
}
/*! Class to apply kernel method
*
*/
class STAT_DLL TeStatKernel {
public:
//! type of kernel function
TeKernelFunctionType kfunc_;
//! type of computation
TeKernelComputeType ktype_;
//! spatial threshold
double radius_;
//! sum of support region areas
double totalArea_;
//! number of support regions
int numReg_;
TeStatKernel() {
kfunc_ = TeKQuartic;
ktype_ = TeKDensity;
radius_ = 0;
totalArea_ = 0;
numReg_ = 0;
}
/*! \brief Apply kernel method to a specif set of support regions
* and events, storing results in resName_.
* \param events set of point events
* \param regionsBegin iterator for support regions
* \param regionsEnd iterator for support regions
* If radius_ is 0, then applies adaptive kernel.
*/
template<typename EventSet, typename ItRegionSet>
bool apply(EventSet& events,
ItRegionSet regionsBegin,
ItRegionSet regionsEnd) {
if (totalArea_ <= 0) {
return false;
}
if (numReg_ <= 0) {
return false;
}
bool result;
//Radius defined, kernel with a fixed radius
if (radius_ > 0) {
result = TeStatIntKernel(events,regionsBegin, regionsEnd,
kfunc_,ktype_, radius_, IDX_KERNEL);
}
//Radius undefined, adaptive kernel
else {
result = TeStatAdaptiveGeoMeanIntKernel(events, regionsBegin, regionsEnd,
kfunc_, ktype_, numReg_, totalArea_, IDX_KERNEL);
}
return result;
}
};
class STAT_DLL TeStatKernelRatio
{
public:
int numReg_;
public:
TeKernelCombinationType kComb_;
TeKernelComputeType ktype_;
TeKernelFunctionType kfunc1_;
double radius1_;
TeKernelFunctionType kfunc2_;
double radius2_;
double totalArea_;
TeStatKernelRatio() {
kComb_ = TeKRatio;
kfunc1_ = kfunc2_ = TeKQuartic;
ktype_ = TeKDensity;
radius1_ = radius2_ = 0;
totalArea_ = 0;
numReg_ = 0;
}
template <typename EventSet1, typename EventSet2, typename ItRegionSet>
bool apply (EventSet1& ev1,
EventSet2& ev2,
ItRegionSet regBegin,
ItRegionSet regEnd) {
if (totalArea_ <= 0)
return false;
if (numReg_ <= 0)
return false;
//Calcula kernel para primeiro conjunto
bool result;
//Radius defined, kernel with a fixed radius
if (radius1_ > 0) {
result = TeStatIntKernel(ev1,regBegin, regEnd,
kfunc1_,ktype_, radius1_, IDX_KERNEL1);
}
//Radius undefined, adaptive kernel
else {
result = TeStatAdaptiveGeoMeanIntKernel(ev1, regBegin, regEnd,
kfunc1_, ktype_, numReg_, totalArea_, IDX_KERNEL1);
}
if (!result)
return result;
//Calcula kernel para segundo conjunto
//Radius defined, kernel with a fixed radius
if (radius2_ > 0) {
result = TeStatIntKernel(ev2,regBegin, regEnd,
kfunc2_,ktype_, radius2_, IDX_KERNEL2);
}
//Radius undefined, adaptive kernel
else {
result = TeStatAdaptiveGeoMeanIntKernel(ev2, regBegin, regEnd,
kfunc2_, ktype_, numReg_, totalArea_, IDX_KERNEL2);
}
if (!result)
return result;
//Itera calculando o tipo de combinacao e armazenando no valor da regiao
//Percorre todas as regioes
// int Cont = 0;
double kernel;
double k1;
double k2;
double area;
ItRegionSet regIter = regBegin;
while (regIter != regEnd)
{
(*regIter).area(area);
(*regIter).getDoubleProperty(IDX_KERNEL1, k1);
(*regIter).getDoubleProperty(IDX_KERNEL2, k2);
switch(kComb_)
{
case TeKRatio:
if (k2 == 0.0)
kernel = 0.0;//INVALID_KERNEL;
else
kernel = k1/k2;
break;
case TeKLogRatio:
if (k2 == 0.0)
kernel = INVALID_KERNEL;
else
kernel = log(k1/k2);
break;
case TeAbsDifference:
kernel = k1 - k2;
break;
case TeRelDifference:
kernel = (k1 - k2) * area;
break;
case TeAbsSum:
kernel = k1+k2;
break;
case TeRelSum:
kernel = (k1 + k2) * area;
break;
}
(*regIter).setDoubleProperty(IDX_KERNEL, kernel);
++regIter;
}
return true;
}
};
#endif
|