searchengine/util/tsrc/cpixtoolsunittest/src/geotests.cpp
changeset 0 671dee74050a
child 8 6547bf8ca13a
equal deleted inserted replaced
-1:000000000000 0:671dee74050a
       
     1 /*
       
     2 * Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 #include <iostream>
       
    18 #include <set>
       
    19 
       
    20 
       
    21 #include "itk.h"
       
    22 
       
    23 #include "cpixgeotools.h"
       
    24 #include "cpixstrtools.h"
       
    25 
       
    26 
       
    27 
       
    28 struct GeoData {
       
    29     char              gpsLongitudeRef_;
       
    30     Cpt::ExifGpsCoord gpsLongitude_;
       
    31     char              gpsLatitudeRef_;
       
    32     Cpt::ExifGpsCoord gpsLatitude_;
       
    33 };
       
    34 
       
    35 GeoData TestData[] = {
       
    36 
       
    37     // Quads around origo. On level 15, they should be about of
       
    38     // 0.611495 / 0.305238 km size
       
    39     {
       
    40         Cpt::QNr::EAST,
       
    41         Cpt::ExifGpsCoord(Cpt::ExifRational(0,1),
       
    42                           Cpt::ExifRational(0,1),
       
    43                           Cpt::ExifRational(1,100)),
       
    44         Cpt::QNr::NORTH,
       
    45         Cpt::ExifGpsCoord(Cpt::ExifRational(0,1),
       
    46                           Cpt::ExifRational(0,1),
       
    47                           Cpt::ExifRational(1,100))
       
    48     },
       
    49     {
       
    50         Cpt::QNr::EAST,
       
    51         Cpt::ExifGpsCoord(Cpt::ExifRational(0,1),
       
    52                           Cpt::ExifRational(0,1),
       
    53                           Cpt::ExifRational(1,100)),
       
    54         Cpt::QNr::SOUTH,
       
    55         Cpt::ExifGpsCoord(Cpt::ExifRational(0,1),
       
    56                           Cpt::ExifRational(0,1),
       
    57                           Cpt::ExifRational(1,100))
       
    58     },
       
    59     {
       
    60         Cpt::QNr::WEST,
       
    61         Cpt::ExifGpsCoord(Cpt::ExifRational(0,1),
       
    62                           Cpt::ExifRational(0,1),
       
    63                           Cpt::ExifRational(1,100)),
       
    64         Cpt::QNr::NORTH,
       
    65         Cpt::ExifGpsCoord(Cpt::ExifRational(0,1),
       
    66                           Cpt::ExifRational(0,1),
       
    67                           Cpt::ExifRational(1,100))
       
    68     },
       
    69     {
       
    70         Cpt::QNr::WEST,
       
    71         Cpt::ExifGpsCoord(Cpt::ExifRational(0,1),
       
    72                           Cpt::ExifRational(0,1),
       
    73                           Cpt::ExifRational(1,100)),
       
    74         Cpt::QNr::SOUTH,
       
    75         Cpt::ExifGpsCoord(Cpt::ExifRational(0,1),
       
    76                           Cpt::ExifRational(0,1),
       
    77                           Cpt::ExifRational(1,100))
       
    78     },
       
    79 
       
    80     // Quads at mid-points of 4 great quads. On level 15, they should
       
    81     // be about of 0.4324.. / 0.305238 km size
       
    82     // 
       
    83     {
       
    84         Cpt::QNr::EAST,
       
    85         Cpt::ExifGpsCoord(Cpt::ExifRational(90,1),
       
    86                           Cpt::ExifRational(0,1),
       
    87                           Cpt::ExifRational(0,1)),
       
    88         Cpt::QNr::NORTH,
       
    89         Cpt::ExifGpsCoord(Cpt::ExifRational(45,1),
       
    90                           Cpt::ExifRational(0,1),
       
    91                           Cpt::ExifRational(0,1))
       
    92     },
       
    93     {
       
    94         Cpt::QNr::EAST,
       
    95         Cpt::ExifGpsCoord(Cpt::ExifRational(90,1),
       
    96                           Cpt::ExifRational(0,1),
       
    97                           Cpt::ExifRational(0,1)),
       
    98         Cpt::QNr::SOUTH,
       
    99         Cpt::ExifGpsCoord(Cpt::ExifRational(45,1),
       
   100                           Cpt::ExifRational(0,1),
       
   101                           Cpt::ExifRational(0,1))
       
   102     },
       
   103     {
       
   104         Cpt::QNr::WEST,
       
   105         Cpt::ExifGpsCoord(Cpt::ExifRational(90,1),
       
   106                           Cpt::ExifRational(0,1),
       
   107                           Cpt::ExifRational(0,1)),
       
   108         Cpt::QNr::NORTH,
       
   109         Cpt::ExifGpsCoord(Cpt::ExifRational(45,1),
       
   110                           Cpt::ExifRational(0,1),
       
   111                           Cpt::ExifRational(0,1))
       
   112     },
       
   113     {
       
   114         Cpt::QNr::WEST,
       
   115         Cpt::ExifGpsCoord(Cpt::ExifRational(90,1),
       
   116                           Cpt::ExifRational(0,1),
       
   117                           Cpt::ExifRational(0,1)),
       
   118         Cpt::QNr::SOUTH,
       
   119         Cpt::ExifGpsCoord(Cpt::ExifRational(45,1),
       
   120                           Cpt::ExifRational(0,1),
       
   121                           Cpt::ExifRational(0,1))
       
   122     },
       
   123 
       
   124 
       
   125     // Quads at mid-points / 60 degrees of 4 great quads. On level 15,
       
   126     // they should be about of 0.3057 / 0.305238 km size
       
   127     // 
       
   128     {
       
   129         Cpt::QNr::EAST,
       
   130         Cpt::ExifGpsCoord(Cpt::ExifRational(90,1),
       
   131                           Cpt::ExifRational(0,1),
       
   132                           Cpt::ExifRational(0,1)),
       
   133         Cpt::QNr::NORTH,
       
   134         Cpt::ExifGpsCoord(Cpt::ExifRational(60,1),
       
   135                           Cpt::ExifRational(0,1),
       
   136                           Cpt::ExifRational(0,1))
       
   137     },
       
   138     {
       
   139         Cpt::QNr::EAST,
       
   140         Cpt::ExifGpsCoord(Cpt::ExifRational(90,1),
       
   141                           Cpt::ExifRational(0,1),
       
   142                           Cpt::ExifRational(0,1)),
       
   143         Cpt::QNr::SOUTH,
       
   144         Cpt::ExifGpsCoord(Cpt::ExifRational(60,1),
       
   145                           Cpt::ExifRational(0,1),
       
   146                           Cpt::ExifRational(0,1))
       
   147     },
       
   148     {
       
   149         Cpt::QNr::WEST,
       
   150         Cpt::ExifGpsCoord(Cpt::ExifRational(90,1),
       
   151                           Cpt::ExifRational(0,1),
       
   152                           Cpt::ExifRational(0,1)),
       
   153         Cpt::QNr::NORTH,
       
   154         Cpt::ExifGpsCoord(Cpt::ExifRational(60,1),
       
   155                           Cpt::ExifRational(0,1),
       
   156                           Cpt::ExifRational(0,1))
       
   157     },
       
   158     {
       
   159         Cpt::QNr::WEST,
       
   160         Cpt::ExifGpsCoord(Cpt::ExifRational(90,1),
       
   161                           Cpt::ExifRational(0,1),
       
   162                           Cpt::ExifRational(0,1)),
       
   163         Cpt::QNr::SOUTH,
       
   164         Cpt::ExifGpsCoord(Cpt::ExifRational(60,1),
       
   165                           Cpt::ExifRational(0,1),
       
   166                           Cpt::ExifRational(0,1))
       
   167     },
       
   168 
       
   169     // Quads around the poles. They should have a very short width
       
   170     // indeed (centimeters).
       
   171     {
       
   172         Cpt::QNr::EAST,
       
   173         Cpt::ExifGpsCoord(Cpt::ExifRational(179,1),
       
   174                           Cpt::ExifRational(59,1),
       
   175                           Cpt::ExifRational(5999,100)),
       
   176         Cpt::QNr::NORTH,
       
   177         Cpt::ExifGpsCoord(Cpt::ExifRational(89,1),
       
   178                           Cpt::ExifRational(59,1),
       
   179                           Cpt::ExifRational(5999,100))
       
   180     },
       
   181     {
       
   182         Cpt::QNr::EAST,
       
   183         Cpt::ExifGpsCoord(Cpt::ExifRational(179,1),
       
   184                           Cpt::ExifRational(59,1),
       
   185                           Cpt::ExifRational(5999,100)),
       
   186         Cpt::QNr::SOUTH,
       
   187         Cpt::ExifGpsCoord(Cpt::ExifRational(89,1),
       
   188                           Cpt::ExifRational(59,1),
       
   189                           Cpt::ExifRational(5999,100))
       
   190     },
       
   191     {
       
   192         Cpt::QNr::WEST,
       
   193         Cpt::ExifGpsCoord(Cpt::ExifRational(179,1),
       
   194                           Cpt::ExifRational(59,1),
       
   195                           Cpt::ExifRational(5999,100)),
       
   196         Cpt::QNr::NORTH,
       
   197         Cpt::ExifGpsCoord(Cpt::ExifRational(89,1),
       
   198                           Cpt::ExifRational(59,1),
       
   199                           Cpt::ExifRational(5999,100))
       
   200     },
       
   201     {
       
   202         Cpt::QNr::WEST,
       
   203         Cpt::ExifGpsCoord(Cpt::ExifRational(179,1),
       
   204                           Cpt::ExifRational(59,1),
       
   205                           Cpt::ExifRational(5999,100)),
       
   206         Cpt::QNr::SOUTH,
       
   207         Cpt::ExifGpsCoord(Cpt::ExifRational(89,1),
       
   208                           Cpt::ExifRational(59,1),
       
   209                           Cpt::ExifRational(5999,100))
       
   210     },
       
   211 };
       
   212 
       
   213 
       
   214 std::ostream & operator<<(std::ostream   & os,
       
   215                           const GeoData  & geoData)
       
   216 {
       
   217     os
       
   218         << "GPS "
       
   219         << geoData.gpsLongitudeRef_ << ' '
       
   220         << geoData.gpsLongitude_ << ' '
       
   221         << geoData.gpsLatitudeRef_ << ' '
       
   222         << geoData.gpsLatitude_;
       
   223 
       
   224     return os;
       
   225 }
       
   226 
       
   227 
       
   228 void testPlainGeo(Itk::TestMgr * testMgr)
       
   229 {
       
   230     using namespace std;
       
   231     using namespace Itk;
       
   232     using namespace Cpt;
       
   233 
       
   234     for (int idx = 0; idx < sizeof(TestData) / sizeof(GeoData); ++idx)
       
   235         {
       
   236             GeoData
       
   237                 & geoData(TestData[idx]);
       
   238 
       
   239             QNr
       
   240                 qnr(geoData.gpsLatitudeRef_,
       
   241                     geoData.gpsLatitude_,
       
   242                     geoData.gpsLongitudeRef_,
       
   243                     geoData.gpsLongitude_);
       
   244 
       
   245             cout << "QNr from " << geoData << endl;
       
   246 
       
   247             for (int i = QNr::MAX_LEVEL-1; i >= 0; --i)
       
   248                 {
       
   249                     cout << "level " << i << endl;
       
   250 
       
   251                     std::wstring
       
   252                         wstr = qnr.toWString();
       
   253 
       
   254                     auto_array<char>
       
   255                         str(new char[wstr.length() + 1]);
       
   256                     wcstombs(str.get(),
       
   257                              wstr.c_str(),
       
   258                              wstr.length() + 1);
       
   259 
       
   260                     cout << "qnr str: " << str.get() << endl;
       
   261 
       
   262                     int16_t
       
   263                         level = qnr.getLevel();
       
   264 
       
   265                     ITK_EXPECT(testMgr,
       
   266                                level == i,
       
   267                                "Qnr level is %d instead of %d",
       
   268                                level,
       
   269                                i);
       
   270 
       
   271                     double
       
   272                         kmWidth = qnr.getKmWidth(),
       
   273                         kmHeight = qnr.getKmHeight();
       
   274 
       
   275                     printf("qnr km width: %16.8f\n",
       
   276                            kmWidth);
       
   277                     printf("qnr km height: %16.8f\n\n",
       
   278                            kmHeight);
       
   279 
       
   280                     qnr = qnr.getParentQNr();
       
   281                 }
       
   282         }
       
   283 }
       
   284 
       
   285 
       
   286 void printQNrArea(Itk::TestMgr   * ,
       
   287                   const Cpt::QNr & qnr)
       
   288 {
       
   289     using namespace std;
       
   290     using namespace Cpt;
       
   291 
       
   292     cout << "On level " << qnr.getLevel() << endl;
       
   293     cout << "C: " << qnr << endl;
       
   294 
       
   295     set<QNr>
       
   296         adjacents;
       
   297     qnr.getAdjacents(adjacents);
       
   298 
       
   299     cout << "Adjacents: " << endl;
       
   300 
       
   301     set<QNr>::const_iterator
       
   302         it = adjacents.begin(),
       
   303         end = adjacents.end();
       
   304 
       
   305     for (; it != end; ++it)
       
   306         {
       
   307             cout << " o " << *it << endl;
       
   308         }
       
   309 }
       
   310 
       
   311 
       
   312 void testAdjacentQNrs(Itk::TestMgr * testMgr)
       
   313 {
       
   314     using namespace std;
       
   315     using namespace Itk;
       
   316     using namespace Cpt;
       
   317 
       
   318     for (size_t i = 0; i < sizeof(TestData) / sizeof(GeoData); ++i)
       
   319         {
       
   320             GeoData
       
   321                 & geoData = TestData[i];
       
   322 
       
   323             QNr
       
   324                 qnr(geoData.gpsLatitudeRef_,
       
   325                     geoData.gpsLatitude_,
       
   326                     geoData.gpsLongitudeRef_,
       
   327                     geoData.gpsLongitude_);
       
   328 
       
   329             cout << "=============="
       
   330                  << " QNr from " << geoData << endl;
       
   331 
       
   332             // level 15
       
   333             printQNrArea(testMgr,
       
   334                          qnr);
       
   335 
       
   336             // level 14
       
   337             qnr = qnr.getParentQNr();
       
   338             printQNrArea(testMgr,
       
   339                          qnr);
       
   340 
       
   341             // level 2
       
   342             while (qnr.getLevel() != 2)
       
   343                 {
       
   344                     qnr = qnr.getParentQNr();
       
   345                 }
       
   346             printQNrArea(testMgr,
       
   347                          qnr);
       
   348 
       
   349             // level 1
       
   350             qnr = qnr.getParentQNr();
       
   351             printQNrArea(testMgr,
       
   352                          qnr);
       
   353 
       
   354             // level 0
       
   355             qnr = qnr.getParentQNr();
       
   356             printQNrArea(testMgr,
       
   357                          qnr);
       
   358 
       
   359             cout << endl;
       
   360         }
       
   361 }
       
   362 
       
   363  
       
   364 
       
   365 Itk::TesterBase * CreateGeoTests()
       
   366 {
       
   367     using namespace Itk;
       
   368 
       
   369     SuiteTester
       
   370         * geoTests = new SuiteTester("geo");
       
   371 
       
   372 #define TEST "plain"
       
   373     geoTests->add(TEST,
       
   374                   testPlainGeo,
       
   375                   TEST);
       
   376 #undef TEST
       
   377 
       
   378 #define TEST "adjacentQNrs"
       
   379     geoTests->add(TEST,
       
   380                   testAdjacentQNrs,
       
   381                   TEST);
       
   382 #undef TEST
       
   383 
       
   384         
       
   385     // ... add more tests to suite
       
   386     
       
   387     return geoTests;
       
   388 }
       
   389 
       
   390