LCOV - code coverage report
Current view: directory - ogr/ogrsf_frmts/openair - ogropenairdatasource.cpp (source / functions) Found Hit Coverage
Test: gdal_filtered.info Lines: 97 88 90.7 %
Date: 2012-12-26 Functions: 9 5 55.6 %

       1                 : /******************************************************************************
       2                 :  * $Id: ogropenairdatasource.cpp 23042 2011-09-04 15:07:22Z rouault $
       3                 :  *
       4                 :  * Project:  OpenAir Translator
       5                 :  * Purpose:  Implements OGROpenAirDataSource class
       6                 :  * Author:   Even Rouault, even dot rouault at mines dash paris dot org
       7                 :  *
       8                 :  ******************************************************************************
       9                 :  * Copyright (c) 2010, Even Rouault <even dot rouault at mines dash paris dot org>
      10                 :  *
      11                 :  * Permission is hereby granted, free of charge, to any person obtaining a
      12                 :  * copy of this software and associated documentation files (the "Software"),
      13                 :  * to deal in the Software without restriction, including without limitation
      14                 :  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
      15                 :  * and/or sell copies of the Software, and to permit persons to whom the
      16                 :  * Software is furnished to do so, subject to the following conditions:
      17                 :  *
      18                 :  * The above copyright notice and this permission notice shall be included
      19                 :  * in all copies or substantial portions of the Software.
      20                 :  *
      21                 :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
      22                 :  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      23                 :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
      24                 :  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      25                 :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
      26                 :  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
      27                 :  * DEALINGS IN THE SOFTWARE.
      28                 :  ****************************************************************************/
      29                 : 
      30                 : #include "ogr_openair.h"
      31                 : #include "cpl_conv.h"
      32                 : #include "cpl_string.h"
      33                 : 
      34                 : CPL_CVSID("$Id: ogropenairdatasource.cpp 23042 2011-09-04 15:07:22Z rouault $");
      35                 : 
      36                 : /************************************************************************/
      37                 : /*                        OGROpenAirDataSource()                        */
      38                 : /************************************************************************/
      39                 : 
      40             130 : OGROpenAirDataSource::OGROpenAirDataSource()
      41                 : 
      42                 : {
      43             130 :     papoLayers = NULL;
      44             130 :     nLayers = 0;
      45                 : 
      46             130 :     pszName = NULL;
      47             130 : }
      48                 : 
      49                 : /************************************************************************/
      50                 : /*                       ~OGROpenAirDataSource()                        */
      51                 : /************************************************************************/
      52                 : 
      53             130 : OGROpenAirDataSource::~OGROpenAirDataSource()
      54                 : 
      55                 : {
      56             132 :     for( int i = 0; i < nLayers; i++ )
      57               2 :         delete papoLayers[i];
      58             130 :     CPLFree( papoLayers );
      59                 : 
      60             130 :     CPLFree( pszName );
      61             130 : }
      62                 : 
      63                 : /************************************************************************/
      64                 : /*                           TestCapability()                           */
      65                 : /************************************************************************/
      66                 : 
      67               0 : int OGROpenAirDataSource::TestCapability( const char * pszCap )
      68                 : 
      69                 : {
      70               0 :     return FALSE;
      71                 : }
      72                 : 
      73                 : /************************************************************************/
      74                 : /*                              GetLayer()                              */
      75                 : /************************************************************************/
      76                 : 
      77               3 : OGRLayer *OGROpenAirDataSource::GetLayer( int iLayer )
      78                 : 
      79                 : {
      80               3 :     if( iLayer < 0 || iLayer >= nLayers )
      81               0 :         return NULL;
      82                 :     else
      83               3 :         return papoLayers[iLayer];
      84                 : }
      85                 : 
      86                 : /************************************************************************/
      87                 : /*                                Open()                                */
      88                 : /************************************************************************/
      89                 : 
      90             130 : int OGROpenAirDataSource::Open( const char * pszFilename, int bUpdateIn)
      91                 : 
      92                 : {
      93             130 :     if (bUpdateIn)
      94                 :     {
      95               9 :         return FALSE;
      96                 :     }
      97                 : 
      98             121 :     pszName = CPLStrdup( pszFilename );
      99                 : 
     100                 : // -------------------------------------------------------------------- 
     101                 : //      Does this appear to be an openair file?
     102                 : // --------------------------------------------------------------------
     103                 : 
     104             121 :     VSILFILE* fp = VSIFOpenL(pszFilename, "rb");
     105             121 :     if (fp == NULL)
     106              50 :         return FALSE;
     107                 : 
     108                 :     char szBuffer[10000];
     109              71 :     int nbRead = (int)VSIFReadL(szBuffer, 1, sizeof(szBuffer) - 1, fp);
     110              71 :     szBuffer[nbRead] = '\0';
     111                 : 
     112                 :     int bIsOpenAir = (strstr(szBuffer, "\nAC ") != NULL &&
     113                 :                   strstr(szBuffer, "\nAN ") != NULL &&
     114                 :                   strstr(szBuffer, "\nAL ") != NULL &&
     115              71 :                   strstr(szBuffer, "\nAH") != NULL);
     116                 : 
     117              71 :     if (bIsOpenAir)
     118                 :     {
     119               1 :         VSIFSeekL( fp, 0, SEEK_SET );
     120                 : 
     121               1 :         VSILFILE* fp2 = VSIFOpenL(pszFilename, "rb");
     122               1 :         if (fp2)
     123                 :         {
     124               1 :             nLayers = 2;
     125               1 :             papoLayers = (OGRLayer**) CPLMalloc(2 * sizeof(OGRLayer*));
     126               1 :             papoLayers[0] = new OGROpenAirLayer(fp);
     127               2 :             papoLayers[1] = new OGROpenAirLabelLayer(fp2);
     128                 :         }
     129                 :     }
     130                 :     else
     131              70 :         VSIFCloseL(fp);
     132                 : 
     133              71 :     return bIsOpenAir;
     134                 : }
     135                 : 
     136                 : 
     137                 : /************************************************************************/
     138                 : /*                              GetLatLon()                             */
     139                 : /************************************************************************/
     140                 : 
     141                 : enum { DEGREE, MINUTE, SECOND };
     142                 : 
     143               8 : int OGROpenAirGetLatLon(const char* pszStr, double& dfLat, double& dfLon)
     144                 : {
     145               8 :     dfLat = 0;
     146               8 :     dfLon = 0;
     147                 : 
     148               8 :     int nCurInt = 0;
     149               8 :     double dfExp = 1.;
     150               8 :     int bHasExp = FALSE;
     151               8 :     int nCurPart = DEGREE;
     152               8 :     double dfDegree = 0, dfMinute = 0, dfSecond = 0;
     153                 :     char c;
     154               8 :     int bHasLat = FALSE, bHasLon = FALSE;
     155             126 :     while((c = *pszStr) != 0)
     156                 :     {
     157             178 :         if (c >= '0' && c <= '9')
     158                 :         {
     159              60 :             nCurInt = nCurInt * 10 + c - '0';
     160              60 :             if (bHasExp)
     161               8 :                 dfExp *= 10;
     162                 :         }
     163              58 :         else if (c == '.')
     164                 :         {
     165               4 :             bHasExp = TRUE;
     166                 :         }
     167              54 :         else if (c == ':')
     168                 :         {
     169              14 :             double dfVal = nCurInt / dfExp;
     170              14 :             if (nCurPart == DEGREE)
     171              12 :                 dfDegree = dfVal;
     172               2 :             else if (nCurPart == MINUTE)
     173               2 :                 dfMinute = dfVal;
     174               0 :             else if (nCurPart == SECOND)
     175               0 :                 dfSecond = dfVal;
     176              14 :             nCurPart ++;
     177              14 :             nCurInt = 0;
     178              14 :             dfExp = 1.;
     179              14 :             bHasExp = FALSE;
     180                 :         }
     181              40 :         else if (c == ' ')
     182                 :         {
     183                 : 
     184                 :         }
     185              24 :         else if (c == 'N' || c == 'S')
     186                 :         {
     187               8 :             double dfVal = nCurInt / dfExp;
     188               8 :             if (nCurPart == DEGREE)
     189               2 :                 dfDegree = dfVal;
     190               6 :             else if (nCurPart == MINUTE)
     191               4 :                 dfMinute = dfVal;
     192               2 :             else if (nCurPart == SECOND)
     193               2 :                 dfSecond = dfVal;
     194                 : 
     195               8 :             dfLat = dfDegree + dfMinute / 60 + dfSecond / 3600;
     196               8 :             if (c == 'S')
     197               0 :                 dfLat = -dfLat;
     198               8 :             nCurInt = 0;
     199               8 :             dfExp = 1.;
     200               8 :             bHasExp = FALSE;
     201               8 :             nCurPart = DEGREE;
     202               8 :             bHasLat = TRUE;
     203                 :         }
     204               8 :         else if (c == 'E' || c == 'W')
     205                 :         {
     206               8 :             double dfVal = nCurInt / dfExp;
     207               8 :             if (nCurPart == DEGREE)
     208               2 :                 dfDegree = dfVal;
     209               6 :             else if (nCurPart == MINUTE)
     210               6 :                 dfMinute = dfVal;
     211               0 :             else if (nCurPart == SECOND)
     212               0 :                 dfSecond = dfVal;
     213                 : 
     214               8 :             dfLon = dfDegree + dfMinute / 60 + dfSecond / 3600;
     215               8 :             if (c == 'W')
     216               0 :                 dfLon = -dfLon;
     217               8 :             bHasLon = TRUE;
     218               8 :             break;
     219                 :         }
     220                 : 
     221             110 :         pszStr++;
     222                 :     }
     223                 : 
     224               8 :     return bHasLat && bHasLon;
     225                 : }

Generated by: LCOV version 1.7