LCOV - code coverage report
Current view: directory - ogr/ogrsf_frmts/segukooa - ogrsegukooalayer.cpp (source / functions) Found Hit Coverage
Test: gdal_filtered.info Lines: 343 287 83.7 %
Date: 2012-04-28 Functions: 26 15 57.7 %

       1                 : /******************************************************************************
       2                 :  * $Id: ogrsegukooalayer.cpp 23222 2011-10-11 22:29:08Z rouault $
       3                 :  *
       4                 :  * Project:  SEG-P1 / UKOOA P1-90 Translator
       5                 :  * Purpose:  Implements OGRUKOOAP190Layer class.
       6                 :  * Author:   Even Rouault, <even dot rouault at mines dash paris dot org>
       7                 :  *
       8                 :  ******************************************************************************
       9                 :  * Copyright (c) 2011, 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, DAMSEGUKOOAS 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_segukooa.h"
      31                 : #include "cpl_conv.h"
      32                 : #include "cpl_string.h"
      33                 : #include "ogr_p.h"
      34                 : #include "ogr_srs_api.h"
      35                 : 
      36                 : CPL_CVSID("$Id: ogrsegukooalayer.cpp 23222 2011-10-11 22:29:08Z rouault $");
      37                 : 
      38                 : /************************************************************************/
      39                 : /*                            ExtractField()                            */
      40                 : /************************************************************************/
      41                 : 
      42             270 : static void ExtractField(char* szField, const char* pszLine, int nOffset, int nLen)
      43                 : {
      44             270 :     memcpy(szField, pszLine + nOffset, nLen);
      45             270 :     szField[nLen] = '\0';
      46             270 : }
      47                 : 
      48                 : /************************************************************************/
      49                 : /*                           GetNextFeature()                           */
      50                 : /************************************************************************/
      51                 : 
      52              32 : OGRFeature *OGRSEGUKOOABaseLayer::GetNextFeature()
      53                 : {
      54                 :     OGRFeature  *poFeature;
      55                 : 
      56               0 :     while(TRUE)
      57                 :     {
      58              32 :         poFeature = GetNextRawFeature();
      59              32 :         if (poFeature == NULL)
      60               4 :             return NULL;
      61                 : 
      62              28 :         if((m_poFilterGeom == NULL
      63                 :             || FilterGeometry( poFeature->GetGeometryRef() ) )
      64                 :         && (m_poAttrQuery == NULL
      65                 :             || m_poAttrQuery->Evaluate( poFeature )) )
      66                 :         {
      67              28 :             return poFeature;
      68                 :         }
      69                 :         else
      70               0 :             delete poFeature;
      71                 :     }
      72                 : }
      73                 : 
      74                 : /************************************************************************/
      75                 : /*                         OGRUKOOAP190Layer()                          */
      76                 : /************************************************************************/
      77                 : 
      78                 : typedef struct
      79                 : {
      80                 :     const char*     pszName;
      81                 :     OGRFieldType    eType;
      82                 : } FieldDesc;
      83                 : 
      84                 : static const FieldDesc UKOOAP190Fields[] =
      85                 : {
      86                 :     { "LINENAME", OFTString },
      87                 :     { "VESSEL_ID", OFTString },
      88                 :     { "SOURCE_ID", OFTString },
      89                 :     { "OTHER_ID", OFTString },
      90                 :     { "POINTNUMBER", OFTInteger },
      91                 :     { "LONGITUDE", OFTReal },
      92                 :     { "LATITUDE", OFTReal },
      93                 :     { "EASTING", OFTReal },
      94                 :     { "NORTHING", OFTReal },
      95                 :     { "DEPTH", OFTReal },
      96                 :     { "DAYOFYEAR", OFTInteger },
      97                 :     { "TIME", OFTTime },
      98                 :     { "DATETIME", OFTDateTime }
      99                 : };
     100                 : 
     101                 : #define FIELD_LINENAME      0
     102                 : #define FIELD_VESSEL_ID     1
     103                 : #define FIELD_SOURCE_ID     2
     104                 : #define FIELD_OTHER_ID      3
     105                 : #define FIELD_POINTNUMBER   4
     106                 : #define FIELD_LONGITUDE     5
     107                 : #define FIELD_LATITUDE      6
     108                 : #define FIELD_EASTING       7
     109                 : #define FIELD_NORTHING      8
     110                 : #define FIELD_DEPTH         9
     111                 : #define FIELD_DAYOFYEAR     10
     112                 : #define FIELD_TIME          11
     113                 : #define FIELD_DATETIME      12
     114                 : 
     115               8 : OGRUKOOAP190Layer::OGRUKOOAP190Layer( const char* pszFilename,
     116               8 :                                       VSILFILE* fp )
     117                 : 
     118                 : {
     119               8 :     this->fp = fp;
     120               8 :     nNextFID = 0;
     121               8 :     bEOF = FALSE;
     122               8 :     poSRS = NULL;
     123               8 :     nYear = 0;
     124                 : 
     125               8 :     poFeatureDefn = new OGRFeatureDefn( CPLGetBasename(pszFilename) );
     126               8 :     poFeatureDefn->Reference();
     127               8 :     poFeatureDefn->SetGeomType( wkbPoint );
     128                 : 
     129             112 :     for(int i=0;i<(int)(sizeof(UKOOAP190Fields)/sizeof(UKOOAP190Fields[0]));i++)
     130                 :     {
     131                 :         OGRFieldDefn    oField( UKOOAP190Fields[i].pszName,
     132             104 :                                 UKOOAP190Fields[i].eType );
     133             104 :         poFeatureDefn->AddFieldDefn( &oField );
     134                 :     }
     135                 : 
     136                 :     bUseEastingNorthingAsGeometry =
     137               8 :         CSLTestBoolean(CPLGetConfigOption("UKOOAP190_USE_EASTING_NORTHING", "NO"));
     138                 : 
     139               8 :     ParseHeaders();
     140               8 : }
     141                 : 
     142                 : /************************************************************************/
     143                 : /*                         ~OGRUKOOAP190Layer()                         */
     144                 : /************************************************************************/
     145                 : 
     146               8 : OGRUKOOAP190Layer::~OGRUKOOAP190Layer()
     147                 : 
     148                 : {
     149               8 :     poFeatureDefn->Release();
     150                 : 
     151               8 :     VSIFCloseL( fp );
     152                 : 
     153               8 :     if (poSRS)
     154               8 :         poSRS->Release();
     155               8 : }
     156                 : 
     157                 : /************************************************************************/
     158                 : /*                          ParseHeaders()                              */
     159                 : /************************************************************************/
     160                 : 
     161             104 : void OGRUKOOAP190Layer::ParseHeaders()
     162                 : {
     163              96 :     while(TRUE)
     164                 :     {
     165             104 :         const char* pszLine = CPLReadLine2L(fp,81,NULL);
     166             104 :         if (pszLine == NULL || EQUALN(pszLine, "EOF", 3))
     167                 :         {
     168               0 :             break;
     169                 :         }
     170                 : 
     171             104 :         int nLineLen = strlen(pszLine);
     172             208 :         while(nLineLen > 0 && pszLine[nLineLen-1] == ' ')
     173                 :         {
     174               0 :             ((char*)pszLine)[nLineLen-1] = '\0';
     175               0 :             nLineLen --;
     176                 :         }
     177                 : 
     178             104 :         if (pszLine[0] != 'H')
     179               8 :             break;
     180                 : 
     181              96 :         if (nLineLen < 33)
     182               0 :             continue;
     183                 : 
     184             104 :         if (!bUseEastingNorthingAsGeometry &&
     185                 :             strncmp(pszLine, "H1500", 5) == 0 && poSRS == NULL)
     186                 :         {
     187              16 :             if (strncmp(pszLine + 33 - 1, "WGS84", 5) == 0 ||
     188                 :                 strncmp(pszLine + 33 - 1, "WGS-84", 6) == 0)
     189                 :             {
     190               8 :                 poSRS = new OGRSpatialReference(SRS_WKT_WGS84);
     191                 :             }
     192               0 :             else if (strncmp(pszLine + 33 - 1, "WGS72", 5) == 0)
     193                 :             {
     194               0 :                 poSRS = new OGRSpatialReference();
     195               0 :                 poSRS->SetFromUserInput("WGS72");
     196                 :             }
     197                 :         }
     198              88 :         else if (!bUseEastingNorthingAsGeometry &&
     199                 :                  strncmp(pszLine, "H1501", 5) == 0 && poSRS != NULL &&
     200                 :                  nLineLen >= 32 + 6 * 6 + 10)
     201                 :         {
     202                 :             char aszParams[6][6+1];
     203                 :             char szZ[10+1];
     204                 :             int i;
     205               0 :             for(i=0;i<6;i++)
     206                 :             {
     207               0 :                 ExtractField(aszParams[i], pszLine, 33 - 1 + i * 6, 6);
     208                 :             }
     209               0 :             ExtractField(szZ, pszLine, 33 - 1 + 6 * 6, 10);
     210                 :             poSRS->SetTOWGS84(atof(aszParams[0]),
     211                 :                               atof(aszParams[1]),
     212                 :                               atof(aszParams[2]),
     213                 :                               atof(aszParams[3]),
     214                 :                               atof(aszParams[4]),
     215                 :                               atof(aszParams[5]),
     216               0 :                               atof(szZ));
     217                 :         }
     218              88 :         else if (strncmp(pszLine, "H0200", 5) == 0)
     219                 :         {
     220               8 :             char** papszTokens = CSLTokenizeString(pszLine + 33 - 1);
     221              32 :             for(int i = 0; papszTokens[i] != NULL; i++)
     222                 :             {
     223              24 :                 if (strlen(papszTokens[i]) == 4)
     224                 :                 {
     225               8 :                     int nVal = atoi(papszTokens[i]);
     226               8 :                     if (nVal >= 1900)
     227                 :                     {
     228               8 :                         if (nYear != 0 && nYear != nVal)
     229                 :                         {
     230                 :                             CPLDebug("SEGUKOOA",
     231               0 :                                      "Several years found in H0200. Ignoring them!");
     232               0 :                             nYear = 0;
     233               0 :                             break;
     234                 :                         }
     235               8 :                         nYear = nVal;
     236                 :                     }
     237                 :                 }
     238                 :             }
     239               8 :             CSLDestroy(papszTokens);
     240                 :         }
     241                 :     }
     242               8 :     VSIFSeekL( fp, 0, SEEK_SET );
     243               8 : }
     244                 : 
     245                 : /************************************************************************/
     246                 : /*                            ResetReading()                            */
     247                 : /************************************************************************/
     248                 : 
     249               0 : void OGRUKOOAP190Layer::ResetReading()
     250                 : 
     251                 : {
     252               0 :     nNextFID = 0;
     253               0 :     bEOF = FALSE;
     254               0 :     VSIFSeekL( fp, 0, SEEK_SET );
     255               0 : }
     256                 : 
     257                 : /************************************************************************/
     258                 : /*                         GetNextRawFeature()                          */
     259                 : /************************************************************************/
     260                 : 
     261              12 : OGRFeature *OGRUKOOAP190Layer::GetNextRawFeature()
     262                 : {
     263              12 :     if (bEOF)
     264               0 :         return NULL;
     265                 : 
     266                 :     const char* pszLine;
     267                 : 
     268              48 :     while(TRUE)
     269                 :     {
     270              60 :         pszLine = CPLReadLine2L(fp,81,NULL);
     271              60 :         if (pszLine == NULL || EQUALN(pszLine, "EOF", 3))
     272                 :         {
     273               2 :             bEOF = TRUE;
     274               2 :             return NULL;
     275                 :         }
     276                 : 
     277              58 :         int nLineLen = strlen(pszLine);
     278             116 :         while(nLineLen > 0 && pszLine[nLineLen-1] == ' ')
     279                 :         {
     280               0 :             ((char*)pszLine)[nLineLen-1] = '\0';
     281               0 :             nLineLen --;
     282                 :         }
     283                 : 
     284              58 :         if (pszLine[0] == 'H' || nLineLen < 46)
     285              48 :             continue;
     286                 : 
     287              10 :         OGRFeature* poFeature = new OGRFeature(poFeatureDefn);
     288              10 :         poFeature->SetFID(nNextFID ++);
     289                 : 
     290                 :         char szLineName[12 + 1];
     291              10 :         ExtractField(szLineName, pszLine, 2-1, 12);
     292              10 :         int i = 11;
     293              46 :         while (i >= 0)
     294                 :         {
     295              36 :             if (szLineName[i] == ' ')
     296              26 :                 szLineName[i] = '\0';
     297                 :             else
     298              10 :                 break;
     299              26 :             i --;
     300                 :         }
     301              10 :         poFeature->SetField(FIELD_LINENAME, szLineName);
     302                 : 
     303                 :         char szVesselId[1+1];
     304              10 :         szVesselId[0] = pszLine[17-1];
     305              10 :         if (szVesselId[0] != ' ')
     306                 :         {
     307               0 :             szVesselId[1] = '\0';
     308               0 :             poFeature->SetField(FIELD_VESSEL_ID, szVesselId);
     309                 :         }
     310                 : 
     311                 :         char szSourceId[1+1];
     312              10 :         szSourceId[0] = pszLine[18-1];
     313              10 :         if (szSourceId[0] != ' ')
     314                 :         {
     315               0 :             szSourceId[1] = '\0';
     316               0 :             poFeature->SetField(FIELD_SOURCE_ID, szSourceId);
     317                 :         }
     318                 : 
     319                 :         char szOtherId[1+1];
     320              10 :         szOtherId[0] = pszLine[19-1];
     321              10 :         if (szOtherId[0] != ' ')
     322                 :         {
     323               0 :             szOtherId[1] = '\0';
     324               0 :             poFeature->SetField(FIELD_OTHER_ID, szOtherId);
     325                 :         }
     326                 : 
     327                 :         char szPointNumber[6+1];
     328              10 :         ExtractField(szPointNumber, pszLine, 20-1, 6);
     329              10 :         poFeature->SetField(4, atoi(szPointNumber));
     330                 : 
     331                 :         char szDeg[3+1];
     332                 :         char szMin[2+1];
     333                 :         char szSec[5+1];
     334                 : 
     335              10 :         ExtractField(szDeg, pszLine, 26-1, 2);
     336              10 :         ExtractField(szMin, pszLine, 26+2-1, 2);
     337              10 :         ExtractField(szSec, pszLine, 26+2+2-1, 5);
     338              10 :         double dfLat = atoi(szDeg) + atoi(szMin) / 60.0 + atof(szSec) / 3600.0;
     339              10 :         if (pszLine[26+2+2+5-1] == 'S')
     340               4 :             dfLat = -dfLat;
     341              10 :         poFeature->SetField(FIELD_LATITUDE, dfLat);
     342                 : 
     343              10 :         ExtractField(szDeg, pszLine, 36-1, 3);
     344              10 :         ExtractField(szMin, pszLine, 36+3-1, 2);
     345              10 :         ExtractField(szSec, pszLine, 36+3+2-1, 5);
     346              10 :         double dfLon = atoi(szDeg) + atoi(szMin) / 60.0 + atof(szSec) / 3600.0;
     347              10 :         if (pszLine[36+3+2+5-1] == 'W')
     348               4 :             dfLon = -dfLon;
     349              10 :         poFeature->SetField(FIELD_LONGITUDE, dfLon);
     350                 : 
     351              10 :         OGRGeometry* poGeom = NULL;
     352              10 :         if (!bUseEastingNorthingAsGeometry)
     353              10 :             poGeom = new OGRPoint(dfLon, dfLat);
     354                 : 
     355              10 :         if (nLineLen >= 64)
     356                 :         {
     357                 :             char szEasting[9+1];
     358              10 :             ExtractField(szEasting, pszLine, 47-1, 9);
     359              10 :             double dfEasting = atof(szEasting);
     360              10 :             poFeature->SetField(FIELD_EASTING, dfEasting);
     361                 : 
     362                 :             char szNorthing[9+1];
     363              10 :             ExtractField(szNorthing, pszLine, 56-1, 9);
     364              10 :             double dfNorthing = atof(szNorthing);
     365              10 :             poFeature->SetField(FIELD_NORTHING, dfNorthing);
     366                 : 
     367              10 :             if (bUseEastingNorthingAsGeometry)
     368               0 :                 poGeom = new OGRPoint(dfEasting, dfNorthing);
     369                 :         }
     370                 : 
     371              10 :         if (poGeom)
     372                 :         {
     373              10 :             if (poSRS)
     374              10 :                 poGeom->assignSpatialReference(poSRS);
     375              10 :             poFeature->SetGeometryDirectly(poGeom);
     376                 :         }
     377                 : 
     378              10 :         if (nLineLen >= 70)
     379                 :         {
     380                 :             char szDepth[6+1];
     381              10 :             ExtractField(szDepth, pszLine, 65-1, 6);
     382              10 :             double dfDepth = atof(szDepth);
     383              10 :             poFeature->SetField(FIELD_DEPTH, dfDepth);
     384                 :         }
     385                 : 
     386              10 :         int nDayOfYear = 0;
     387              10 :         if (nLineLen >= 73)
     388                 :         {
     389                 :             char szDayOfYear[3+1];
     390              10 :             ExtractField(szDayOfYear, pszLine, 71-1, 3);
     391              10 :             nDayOfYear = atoi(szDayOfYear);
     392              10 :             poFeature->SetField(FIELD_DAYOFYEAR, nDayOfYear);
     393                 :         }
     394                 : 
     395              10 :         if (nLineLen >= 79)
     396                 :         {
     397                 :             char szH[2+1], szM[2+1], szS[2+1];
     398              10 :             ExtractField(szH, pszLine, 74-1, 2);
     399              10 :             ExtractField(szM, pszLine, 74-1+2, 2);
     400              10 :             ExtractField(szS, pszLine, 74-1+2+2, 2);
     401              10 :             poFeature->SetField(FIELD_TIME, 0, 0, 0, atoi(szH), atoi(szM), atoi(szS) );
     402                 : 
     403              10 :             if (nYear != 0)
     404                 :             {
     405                 :                 #define isleap(y) ((((y) % 4) == 0 && ((y) % 100) != 0) || ((y) % 400) == 0)
     406                 :                 static const int mon_lengths[2][12] = {
     407                 :                     {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
     408                 :                     {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
     409                 :                 } ;
     410              10 :                 int bIsLeap = isleap(nYear);
     411              10 :                 int nMonth = 0;
     412              10 :                 int nDays = 0;
     413              10 :                 if ((bIsLeap && nDayOfYear >= 1 && nDayOfYear <= 366) ||
     414                 :                     (!bIsLeap && nDayOfYear >= 1 && nDayOfYear <= 365))
     415                 :                 {
     416              20 :                     while(nDayOfYear > nDays + mon_lengths[bIsLeap][nMonth])
     417                 :                     {
     418               0 :                         nDays += mon_lengths[bIsLeap][nMonth];
     419               0 :                         nMonth ++;
     420                 :                     }
     421              10 :                     int nDayOfMonth = nDayOfYear - nDays;
     422              10 :                     nMonth ++;
     423                 : 
     424                 :                     poFeature->SetField(FIELD_DATETIME, nYear, nMonth, nDayOfMonth,
     425              10 :                                         atoi(szH), atoi(szM), atoi(szS) );
     426                 :                 }
     427                 : 
     428                 :             }
     429                 :         }
     430                 : 
     431              10 :         return poFeature;
     432                 :     }
     433                 : }
     434                 : 
     435                 : /************************************************************************/
     436                 : /*                           OGRSEGP1Layer()                            */
     437                 : /************************************************************************/
     438                 : 
     439                 : static const FieldDesc SEGP1Fields[] =
     440                 : {
     441                 :     { "LINENAME", OFTString },
     442                 :     { "POINTNUMBER", OFTInteger },
     443                 :     { "RESHOOTCODE", OFTString },
     444                 :     { "LONGITUDE", OFTReal },
     445                 :     { "LATITUDE", OFTReal },
     446                 :     { "EASTING", OFTReal },
     447                 :     { "NORTHING", OFTReal },
     448                 :     { "DEPTH", OFTReal },
     449                 : #if 0
     450                 :     { "YEAR", OFTInteger },
     451                 :     { "DAYOFYEAR", OFTInteger },
     452                 :     { "TIME", OFTTime },
     453                 :     { "DATETIME", OFTDateTime }
     454                 : #endif
     455                 : };
     456                 : 
     457                 : #define SEGP1_FIELD_LINENAME      0
     458                 : #define SEGP1_FIELD_POINTNUMBER   1
     459                 : #define SEGP1_FIELD_RESHOOTCODE   2
     460                 : #define SEGP1_FIELD_LONGITUDE     3
     461                 : #define SEGP1_FIELD_LATITUDE      4
     462                 : #define SEGP1_FIELD_EASTING       5
     463                 : #define SEGP1_FIELD_NORTHING      6
     464                 : #define SEGP1_FIELD_DEPTH         7
     465                 : #define SEGP1_FIELD_DAYOFYEAR     8
     466                 : #define SEGP1_FIELD_TIME          9
     467                 : #define SEGP1_FIELD_DATETIME      10
     468                 : 
     469               8 : OGRSEGP1Layer::OGRSEGP1Layer( const char* pszFilename,
     470                 :                               VSILFILE* fp,
     471               8 :                               int nLatitudeCol )
     472                 : 
     473                 : {
     474               8 :     this->fp = fp;
     475               8 :     this->nLatitudeCol = nLatitudeCol;
     476               8 :     nNextFID = 0;
     477               8 :     bEOF = FALSE;
     478               8 :     poSRS = NULL;
     479                 : 
     480               8 :     poFeatureDefn = new OGRFeatureDefn( CPLGetBasename(pszFilename) );
     481               8 :     poFeatureDefn->Reference();
     482               8 :     poFeatureDefn->SetGeomType( wkbPoint );
     483                 : 
     484              72 :     for(int i=0;i<(int)(sizeof(SEGP1Fields)/sizeof(SEGP1Fields[0]));i++)
     485                 :     {
     486                 :         OGRFieldDefn    oField( SEGP1Fields[i].pszName,
     487              64 :                                 SEGP1Fields[i].eType );
     488              64 :         poFeatureDefn->AddFieldDefn( &oField );
     489                 :     }
     490                 : 
     491                 :     bUseEastingNorthingAsGeometry =
     492               8 :         CSLTestBoolean(CPLGetConfigOption("SEGP1_USE_EASTING_NORTHING", "NO"));
     493                 : 
     494               8 :     ResetReading();
     495               8 : }
     496                 : 
     497                 : /************************************************************************/
     498                 : /*                            ~OGRSEGP1Layer()                          */
     499                 : /************************************************************************/
     500                 : 
     501               8 : OGRSEGP1Layer::~OGRSEGP1Layer()
     502                 : 
     503                 : {
     504               8 :     poFeatureDefn->Release();
     505                 : 
     506               8 :     VSIFCloseL( fp );
     507                 : 
     508               8 :     if (poSRS)
     509               0 :         poSRS->Release();
     510               8 : }
     511                 : 
     512                 : /************************************************************************/
     513                 : /*                            ResetReading()                            */
     514                 : /************************************************************************/
     515                 : 
     516               8 : void OGRSEGP1Layer::ResetReading()
     517                 : 
     518                 : {
     519               8 :     nNextFID = 0;
     520               8 :     bEOF = FALSE;
     521               8 :     VSIFSeekL( fp, 0, SEEK_SET );
     522                 : 
     523                 :     /* Skip first 20 header lines */
     524               8 :     const char* pszLine = NULL;
     525             168 :     for(int i=0; i<20;i++)
     526                 :     {
     527             160 :         pszLine = CPLReadLine2L(fp,81,NULL);
     528             160 :         if (pszLine == NULL)
     529                 :         {
     530               0 :             bEOF = TRUE;
     531               0 :             break;
     532                 :         }
     533                 :     }
     534               8 : }
     535                 : 
     536                 : /************************************************************************/
     537                 : /*                         GetNextRawFeature()                          */
     538                 : /************************************************************************/
     539                 : 
     540              12 : OGRFeature *OGRSEGP1Layer::GetNextRawFeature()
     541                 : {
     542              12 :     if (bEOF)
     543               0 :         return NULL;
     544                 : 
     545              12 :     const char* pszLine = NULL;
     546                 :     while(TRUE)
     547                 :     {
     548              12 :         pszLine = CPLReadLine2L(fp,81,NULL);
     549              12 :         if (pszLine == NULL || EQUALN(pszLine, "EOF", 3))
     550                 :         {
     551               2 :             bEOF = TRUE;
     552               2 :             return NULL;
     553                 :         }
     554                 : 
     555              10 :         int nLineLen = strlen(pszLine);
     556              20 :         while(nLineLen > 0 && pszLine[nLineLen-1] == ' ')
     557                 :         {
     558               0 :             ((char*)pszLine)[nLineLen-1] = '\0';
     559               0 :             nLineLen --;
     560                 :         }
     561                 : 
     562              10 :         char* pszExpandedLine = ExpandTabs(pszLine);
     563              10 :         pszLine = pszExpandedLine;
     564              10 :         nLineLen = strlen(pszLine);
     565                 : 
     566              10 :         OGRFeature* poFeature = new OGRFeature(poFeatureDefn);
     567              10 :         poFeature->SetFID(nNextFID ++);
     568                 : 
     569              10 :         OGRGeometry* poGeom = NULL;
     570                 : 
     571              10 :         if (nLatitudeCol-1 + 19 <= nLineLen)
     572                 :         {
     573                 :             char szDeg[3+1];
     574                 :             char szMin[2+1];
     575                 :             char szSec[4+1];
     576                 : 
     577              10 :             ExtractField(szDeg, pszLine, nLatitudeCol-1, 2);
     578              10 :             ExtractField(szMin, pszLine, nLatitudeCol+2-1, 2);
     579              10 :             ExtractField(szSec, pszLine, nLatitudeCol+2+2-1, 4);
     580              10 :             double dfLat = atoi(szDeg) + atoi(szMin) / 60.0 + atoi(szSec) / 100.0 / 3600.0;
     581              10 :             if (pszLine[nLatitudeCol+2+2+4-1] == 'S')
     582               4 :                 dfLat = -dfLat;
     583              10 :             poFeature->SetField(SEGP1_FIELD_LATITUDE, dfLat);
     584                 : 
     585              10 :             ExtractField(szDeg, pszLine, nLatitudeCol+9-1, 3);
     586              10 :             ExtractField(szMin, pszLine, nLatitudeCol+9+3-1, 2);
     587              10 :             ExtractField(szSec, pszLine, nLatitudeCol+9+3+2-1, 4);
     588              10 :             double dfLon = atoi(szDeg) + atoi(szMin) / 60.0 + atoi(szSec) / 100.0 / 3600.0;
     589              10 :             if (pszLine[nLatitudeCol+9+3+2+4-1] == 'W')
     590               4 :                 dfLon = -dfLon;
     591              10 :             poFeature->SetField(SEGP1_FIELD_LONGITUDE, dfLon);
     592                 : 
     593              10 :             if (!bUseEastingNorthingAsGeometry)
     594              10 :                 poGeom = new OGRPoint(dfLon, dfLat);
     595                 :         }
     596                 : 
     597                 :         /* Normal layout -> extract other fields */
     598              10 :         if (nLatitudeCol == 27)
     599                 :         {
     600                 :             char szLineName[16 + 1];
     601              10 :             ExtractField(szLineName, pszLine, 2-1, 16);
     602              10 :             int i = 15;
     603              86 :             while (i >= 0)
     604                 :             {
     605              76 :                 if (szLineName[i] == ' ')
     606              66 :                     szLineName[i] = '\0';
     607                 :                 else
     608              10 :                     break;
     609              66 :                 i --;
     610                 :             }
     611              10 :             poFeature->SetField(SEGP1_FIELD_LINENAME, szLineName);
     612                 : 
     613                 :             char szPointNumber[8+1];
     614              10 :             ExtractField(szPointNumber, pszLine, 18-1, 8);
     615              10 :             poFeature->SetField(SEGP1_FIELD_POINTNUMBER, atoi(szPointNumber));
     616                 : 
     617                 :             char szReshootCode[1+1];
     618              10 :             ExtractField(szReshootCode, pszLine, 26-1, 1);
     619              10 :             poFeature->SetField(SEGP1_FIELD_RESHOOTCODE, szReshootCode);
     620                 : 
     621              10 :             if (nLineLen >= 61)
     622                 :             {
     623                 :                 char szEasting[8+1];
     624              10 :                 ExtractField(szEasting, pszLine, 46-1, 8);
     625              10 :                 double dfEasting = atof(szEasting);
     626              10 :                 poFeature->SetField(SEGP1_FIELD_EASTING, dfEasting);
     627                 : 
     628                 :                 char szNorthing[8+1];
     629              10 :                 ExtractField(szNorthing, pszLine, 54-1, 8);
     630              10 :                 double dfNorthing = atof(szNorthing);
     631              10 :                 poFeature->SetField(SEGP1_FIELD_NORTHING, dfNorthing);
     632                 : 
     633              10 :                 if (bUseEastingNorthingAsGeometry)
     634               0 :                     poGeom = new OGRPoint(dfEasting, dfNorthing);
     635                 :             }
     636                 : 
     637              10 :             if (nLineLen >= 66)
     638                 :             {
     639                 :                 char szDepth[5+1];
     640              10 :                 ExtractField(szDepth, pszLine, 62-1, 5);
     641              10 :                 double dfDepth = atof(szDepth);
     642              10 :                 poFeature->SetField(SEGP1_FIELD_DEPTH, dfDepth);
     643                 :             }
     644                 :         }
     645                 : 
     646              10 :         if (poGeom)
     647                 :         {
     648              10 :             if (poSRS)
     649               0 :                 poGeom->assignSpatialReference(poSRS);
     650              10 :             poFeature->SetGeometryDirectly(poGeom);
     651                 :         }
     652                 : 
     653              10 :         CPLFree(pszExpandedLine);
     654                 : 
     655              10 :         return poFeature;
     656                 :     }
     657                 : }
     658                 : 
     659                 : /************************************************************************/
     660                 : /*                           ExpandTabs()                               */
     661                 : /************************************************************************/
     662                 : 
     663              14 : char* OGRSEGP1Layer::ExpandTabs(const char* pszLine)
     664                 : {
     665              14 :     char* pszExpandedLine = (char*)CPLMalloc(strlen(pszLine) * 8 + 1);
     666              14 :     int j = 0;
     667             938 :     for(int i=0; pszLine[i] != '\0'; i++)
     668                 :     {
     669                 :         /* A tab must be space-expanded to reach the next column number */
     670                 :         /* which is a multiple of 8 */
     671             924 :         if (pszLine[i] == 9)
     672                 :         {
     673               0 :             do
     674                 :             {
     675               0 :                 pszExpandedLine[j ++] = ' ';
     676                 :             } while ((j % 8) != 0);
     677                 :         }
     678                 :         else
     679             924 :             pszExpandedLine[j ++] = pszLine[i];
     680                 :     }
     681              14 :     pszExpandedLine[j] = '\0';
     682                 : 
     683              14 :     return pszExpandedLine;
     684                 : }
     685                 : 
     686                 : /************************************************************************/
     687                 : /*                        DetectLatitudeColumn()                        */
     688                 : /************************************************************************/
     689                 : 
     690                 : /* Some SEG-P1 files have unusual offsets for latitude/longitude, so */
     691                 : /* we try our best to identify it even in case of non-standard layout */
     692                 : /* Return non-0 if detection is successfull (column number starts at 1) */
     693                 : 
     694               4 : int OGRSEGP1Layer::DetectLatitudeColumn(const char* pszLine)
     695                 : {
     696               4 :     int nLen = strlen(pszLine);
     697              12 :     if (nLen >= 45 && pszLine[0] == ' ' &&
     698               4 :         (pszLine[35-1] == 'N' || pszLine[35-1] == 'S') &&
     699               4 :         (pszLine[45-1] == 'E' || pszLine[45-1] == 'W'))
     700               4 :         return 27;
     701                 : 
     702               0 :     for(int i=8;i<nLen-10;i++)
     703                 :     {
     704               0 :         if ((pszLine[i] == 'N' || pszLine[i] == 'S') &&
     705               0 :             (pszLine[i+10] == 'E' || pszLine[i+10] == 'W'))
     706               0 :             return i - 8 + 1;
     707                 :     }
     708                 : 
     709               0 :     return 0;
     710                 : }
     711                 : 
     712                 : 
     713                 : /************************************************************************/
     714                 : /*                        OGRSEGUKOOALineLayer()                        */
     715                 : /************************************************************************/
     716                 : 
     717               8 : OGRSEGUKOOALineLayer::OGRSEGUKOOALineLayer(const char* pszFilename,
     718               8 :                                            OGRLayer *poBaseLayer)
     719                 : {
     720               8 :     nNextFID = 0;
     721               8 :     bEOF = FALSE;
     722                 : 
     723                 :     poFeatureDefn = new OGRFeatureDefn( CPLSPrintf("%s_lines",
     724               8 :                                                    CPLGetBasename(pszFilename)) );
     725               8 :     poFeatureDefn->Reference();
     726               8 :     poFeatureDefn->SetGeomType( wkbLineString );
     727                 : 
     728               8 :     OGRFieldDefn    oField( "LINENAME", OFTString );
     729               8 :     poFeatureDefn->AddFieldDefn( &oField );
     730                 : 
     731               8 :     this->poBaseLayer = poBaseLayer;
     732               8 :     poNextBaseFeature = NULL;
     733               8 : }
     734                 : 
     735                 : /************************************************************************/
     736                 : /*                       ~OGRSEGUKOOALineLayer()                        */
     737                 : /************************************************************************/
     738                 : 
     739               8 : OGRSEGUKOOALineLayer::~OGRSEGUKOOALineLayer()
     740                 : {
     741               8 :     delete poNextBaseFeature;
     742               8 :     delete poBaseLayer;
     743                 : 
     744               8 :     poFeatureDefn->Release();
     745               8 : }
     746                 : 
     747                 : /************************************************************************/
     748                 : /*                            ResetReading()                            */
     749                 : /************************************************************************/
     750                 : 
     751               0 : void OGRSEGUKOOALineLayer::ResetReading()
     752                 : 
     753                 : {
     754               0 :     nNextFID = 0;
     755               0 :     bEOF = FALSE;
     756               0 :     delete poNextBaseFeature;
     757               0 :     poNextBaseFeature = NULL;
     758               0 :     poBaseLayer->ResetReading();
     759               0 : }
     760                 : 
     761                 : /************************************************************************/
     762                 : /*                         GetNextRawFeature()                          */
     763                 : /************************************************************************/
     764                 : 
     765               8 : OGRFeature *OGRSEGUKOOALineLayer::GetNextRawFeature()
     766                 : {
     767               8 :     if (bEOF)
     768               0 :         return NULL;
     769                 : 
     770                 :     /* Merge points of base layer that have same value for attribute(0) */
     771                 :     /* into a single linestring */
     772                 : 
     773               8 :     OGRFeature* poFeature = NULL;
     774               8 :     OGRLineString* poLS = NULL;
     775                 : 
     776               8 :     if (poNextBaseFeature == NULL)
     777               4 :         poNextBaseFeature = poBaseLayer->GetNextFeature();
     778                 : 
     779              32 :     while(poNextBaseFeature != NULL)
     780                 :     {
     781              40 :         if (poNextBaseFeature->IsFieldSet(0) &&
     782              20 :             poNextBaseFeature->GetFieldAsString(0)[0] != '\0')
     783                 :         {
     784              20 :             if (poFeature != NULL &&
     785                 :                 strcmp(poFeature->GetFieldAsString(0),
     786                 :                     poNextBaseFeature->GetFieldAsString(0)) != 0)
     787                 :             {
     788               4 :                 return poFeature;
     789                 :             }
     790                 : 
     791                 :             OGRPoint* poPoint =
     792              16 :                 (OGRPoint*) poNextBaseFeature->GetGeometryRef();
     793              16 :             if (poPoint != NULL)
     794                 :             {
     795              16 :                 if (poFeature == NULL)
     796                 :                 {
     797               8 :                     poFeature = new OGRFeature(poFeatureDefn);
     798               8 :                     poFeature->SetFID(nNextFID ++);
     799                 :                     poFeature->SetField(0,
     800               8 :                         poNextBaseFeature->GetFieldAsString(0));
     801              16 :                     poLS = new OGRLineString();
     802               8 :                     if (poBaseLayer->GetSpatialRef())
     803                 :                         poLS->assignSpatialReference(
     804               4 :                                     poBaseLayer->GetSpatialRef());
     805               8 :                     poFeature->SetGeometryDirectly(poLS);
     806                 :                 }
     807                 : 
     808              16 :                 poLS->addPoint(poPoint);
     809                 :             }
     810                 :         }
     811                 : 
     812              16 :         delete poNextBaseFeature;
     813              16 :         poNextBaseFeature = poBaseLayer->GetNextFeature();
     814                 :     }
     815                 : 
     816               4 :     bEOF = TRUE;
     817               4 :     return poFeature;
     818                 : }

Generated by: LCOV version 1.7