LCOV - code coverage report
Current view: directory - ogr/ogrsf_frmts/ili - ogrili1datasource.cpp (source / functions) Found Hit Coverage
Test: gdal_filtered.info Lines: 118 105 89.0 %
Date: 2012-12-26 Functions: 11 7 63.6 %

       1                 : /******************************************************************************
       2                 :  * $Id: ogrili1datasource.cpp 23890 2012-02-03 16:30:23Z pka $
       3                 :  *
       4                 :  * Project:  Interlis 1 Translator
       5                 :  * Purpose:  Implements OGRILI1DataSource class.
       6                 :  * Author:   Pirmin Kalberer, Sourcepole AG
       7                 :  *
       8                 :  ******************************************************************************
       9                 :  * Copyright (c) 2004, Pirmin Kalberer, Sourcepole AG
      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_ili1.h"
      31                 : #include "cpl_conv.h"
      32                 : #include "cpl_string.h"
      33                 : 
      34                 : #include "ili1reader.h"
      35                 : 
      36                 : #include "iomhelper.h"
      37                 : #include "iom/iom.h"
      38                 : 
      39                 : #include <string>
      40                 : 
      41                 : CPL_CVSID("$Id: ogrili1datasource.cpp 23890 2012-02-03 16:30:23Z pka $");
      42                 : 
      43                 : /************************************************************************/
      44                 : /*                         OGRILI1DataSource()                         */
      45                 : /************************************************************************/
      46                 : 
      47             289 : OGRILI1DataSource::OGRILI1DataSource()
      48                 : 
      49                 : {
      50             289 :     pszName = NULL;
      51             289 :     poReader = NULL;
      52             289 :     fpTransfer = NULL;
      53             289 :     pszTopic = NULL;
      54             289 :     nLayers = 0;
      55             289 :     papoLayers = NULL;
      56             289 : }
      57                 : 
      58                 : /************************************************************************/
      59                 : /*                        ~OGRILI1DataSource()                         */
      60                 : /************************************************************************/
      61                 : 
      62             289 : OGRILI1DataSource::~OGRILI1DataSource()
      63                 : 
      64                 : {
      65                 :     int i;
      66                 : 
      67             292 :     for(i=0;i<nLayers;i++)
      68                 :     {
      69               3 :         delete papoLayers[i];
      70                 :     }
      71             289 :     CPLFree( papoLayers );
      72                 : 
      73             289 :     CPLFree( pszName );
      74             289 :     CPLFree( pszTopic );
      75             289 :     DestroyILI1Reader( poReader );
      76             289 :     if( fpTransfer )
      77                 :     {
      78               3 :         VSIFPrintf( fpTransfer, "ETAB\n" );
      79               3 :         VSIFPrintf( fpTransfer, "ETOP\n" );
      80               3 :         VSIFPrintf( fpTransfer, "EMOD\n" );
      81               3 :         VSIFPrintf( fpTransfer, "ENDE\n" );
      82               3 :         fclose(fpTransfer);
      83                 :     }
      84             289 : }
      85                 : 
      86                 : /************************************************************************/
      87                 : /*                                Open()                                */
      88                 : /************************************************************************/
      89                 : 
      90             286 : int OGRILI1DataSource::Open( const char * pszNewName, int bTestOpen )
      91                 : 
      92                 : {
      93                 :     FILE        *fp;
      94                 :     char        szHeader[1000];
      95             286 :     std::string osBasename, osModelFilename;
      96                 : 
      97             286 :     if (strlen(pszNewName) == 0)
      98                 :     {
      99               1 :         return FALSE;
     100                 :     }
     101                 : 
     102             285 :     char **filenames = CSLTokenizeString2( pszNewName, ",", 0 );
     103                 : 
     104             285 :     osBasename = filenames[0];
     105                 : 
     106             285 :     if( CSLCount(filenames) > 1 )
     107              13 :         osModelFilename = filenames[1];
     108                 : 
     109             285 :     CSLDestroy( filenames );
     110                 : 
     111                 : /* -------------------------------------------------------------------- */
     112                 : /*      Open the source file.                                           */
     113                 : /* -------------------------------------------------------------------- */
     114             285 :     fp = VSIFOpen( osBasename.c_str(), "r" );
     115             285 :     if( fp == NULL )
     116                 :     {
     117              88 :         if( !bTestOpen )
     118                 :             CPLError( CE_Failure, CPLE_OpenFailed,
     119                 :                       "Failed to open ILI1 file `%s'.",
     120               0 :                       pszNewName );
     121                 : 
     122              88 :         return FALSE;
     123                 :     }
     124                 : 
     125                 : /* -------------------------------------------------------------------- */
     126                 : /*      If we aren't sure it is ILI1, load a header chunk and check      */
     127                 : /*      for signs it is ILI1                                             */
     128                 : /* -------------------------------------------------------------------- */
     129             197 :     if( bTestOpen )
     130                 :     {
     131             197 :         int nLen = (int)VSIFRead( szHeader, 1, sizeof(szHeader), fp );
     132             197 :         if (nLen == sizeof(szHeader))
     133             127 :             szHeader[sizeof(szHeader)-1] = '\0';
     134                 :         else
     135              70 :             szHeader[nLen] = '\0';
     136                 : 
     137             197 :         if( strstr(szHeader,"SCNT") == NULL )
     138                 :         {
     139             187 :             VSIFClose( fp );
     140             187 :             return FALSE;
     141                 :         }
     142                 :     }
     143                 : 
     144                 : /* -------------------------------------------------------------------- */
     145                 : /*      We assume now that it is ILI1.  Close and instantiate a          */
     146                 : /*      ILI1Reader on it.                                                */
     147                 : /* -------------------------------------------------------------------- */
     148              10 :     VSIFClose( fp );
     149                 : 
     150              10 :     poReader = CreateILI1Reader();
     151              10 :     if( poReader == NULL )
     152                 :     {
     153                 :         CPLError( CE_Failure, CPLE_AppDefined,
     154                 :                   "File %s appears to be ILI1 but the ILI1 reader can't\n"
     155                 :                   "be instantiated, likely because Xerces support wasn't\n"
     156                 :                   "configured in.",
     157               0 :                   pszNewName );
     158               0 :         return FALSE;
     159                 :      }
     160                 : 
     161              10 :     poReader->OpenFile( osBasename.c_str() );
     162                 : 
     163              10 :     pszName = CPLStrdup( osBasename.c_str() );
     164                 : 
     165              10 :     if (osModelFilename.length() > 0 )
     166              10 :         poReader->ReadModel( osModelFilename.c_str() );
     167                 : 
     168              10 :     if( getenv( "ARC_DEGREES" ) != NULL ) {
     169                 :       //No better way to pass arguments to the reader (it could even be an -lco arg)
     170               1 :       poReader->SetArcDegrees( atof( getenv("ARC_DEGREES") ) );
     171                 :     }
     172                 : 
     173                 :     //Parse model and read data - without surface joing and polygonizing
     174              10 :     poReader->ReadFeatures();
     175                 : 
     176              10 :     return TRUE;
     177                 : }
     178                 : 
     179                 : /************************************************************************/
     180                 : /*                               Create()                               */
     181                 : /************************************************************************/
     182                 : 
     183               3 : int OGRILI1DataSource::Create( const char *pszFilename,
     184                 :                               char **papszOptions )
     185                 : 
     186                 : {
     187               3 :     std::string osBasename, osModelFilename;
     188               3 :     char **filenames = CSLTokenizeString2( pszFilename, ",", 0 );
     189                 : 
     190               3 :     osBasename = filenames[0];
     191                 : 
     192               3 :     if( CSLCount(filenames) > 1 )
     193               1 :         osModelFilename = filenames[1];
     194                 : 
     195               3 :     CSLDestroy( filenames );
     196                 : 
     197               3 :     if( osModelFilename.length() == 0 )
     198                 :     {
     199                 :       //TODO: create automatic model
     200                 :     }
     201                 : 
     202                 : /* -------------------------------------------------------------------- */
     203                 : /*      Create the empty file.                                          */
     204                 : /* -------------------------------------------------------------------- */
     205               3 :     fpTransfer = VSIFOpen( osBasename.c_str(), "w+b" );
     206                 : 
     207               3 :     if( fpTransfer == NULL )
     208                 :     {
     209                 :         CPLError( CE_Failure, CPLE_OpenFailed,
     210                 :                   "Failed to create %s:\n%s",
     211               0 :                   osBasename.c_str(), VSIStrerror( errno ) );
     212                 : 
     213               0 :         return FALSE;
     214                 :     }
     215                 : 
     216                 : 
     217                 : /* -------------------------------------------------------------------- */
     218                 : /*      Parse model                                                     */
     219                 : /* -------------------------------------------------------------------- */
     220               3 :     iom_init();
     221                 : 
     222                 :     // set error listener to a iom provided one, that just
     223                 :     // dumps all errors to stderr
     224               3 :     iom_seterrlistener(iom_stderrlistener);
     225                 : 
     226               3 :     IOM_BASKET model = 0;
     227               3 :     if( osModelFilename.length() != 0 ) {
     228                 :       // compile ili model
     229               1 :       char *iliFiles[1] = {(char *)osModelFilename.c_str()};
     230               1 :       model=iom_compileIli(1,iliFiles);
     231               1 :       if(!model){
     232                 :         CPLError( CE_Warning, CPLE_OpenFailed,
     233                 :                   "iom_compileIli %s, %s.",
     234               0 :                   pszName, VSIStrerror( errno ) );
     235               0 :         iom_end();
     236               0 :         return FALSE;
     237                 :       }
     238                 :     }
     239                 : 
     240                 :     pszTopic = CPLStrdup(model ?
     241                 :                          GetAttrObjName(model, "iom04.metamodel.Topic") :
     242               3 :                          CPLGetBasename(osBasename.c_str()));
     243                 : 
     244                 : /* -------------------------------------------------------------------- */
     245                 : /*      Write headers                                                   */
     246                 : /* -------------------------------------------------------------------- */
     247               3 :     VSIFPrintf( fpTransfer, "SCNT\n" );
     248               3 :     VSIFPrintf( fpTransfer, "OGR/GDAL %s, INTERLIS Driver\n", GDAL_RELEASE_NAME );
     249               3 :     VSIFPrintf( fpTransfer, "////\n" );
     250               3 :     VSIFPrintf( fpTransfer, "MTID INTERLIS1\n" );
     251                 :     const char* modelname = model ?
     252                 :                             GetAttrObjName(model, "iom04.metamodel.DataModel") :
     253               3 :                             CPLGetBasename(osBasename.c_str());
     254               3 :     VSIFPrintf( fpTransfer, "MODL %s\n", modelname );
     255                 : 
     256               3 :     return TRUE;
     257                 : }
     258                 : 
     259               3 : static char *ExtractTopic(const char * pszLayerName)
     260                 : {
     261               3 :   const char *table = strchr(pszLayerName, '_');
     262               3 :   while (table && table[1] !=  '_') table = strchr(table+1, '_');
     263               3 :   return (table) ? CPLScanString(pszLayerName, table-pszLayerName, FALSE, FALSE) : NULL;
     264                 : }
     265                 : 
     266                 : /************************************************************************/
     267                 : /*                            CreateLayer()                             */
     268                 : /************************************************************************/
     269                 : 
     270                 : OGRLayer *
     271               3 : OGRILI1DataSource::CreateLayer( const char * pszLayerName,
     272                 :                                OGRSpatialReference *poSRS,
     273                 :                                OGRwkbGeometryType eType,
     274                 :                                char ** papszOptions )
     275                 : 
     276                 : {
     277               3 :     const char *table = pszLayerName;
     278               3 :     char * topic = ExtractTopic(pszLayerName);
     279               3 :     if (nLayers) VSIFPrintf( fpTransfer, "ETAB\n" );
     280               3 :     if (topic)
     281                 :     {
     282               2 :       table = pszLayerName+strlen(topic)+2; //after "__"
     283               4 :       if (pszTopic == NULL || !EQUAL(topic, pszTopic))
     284                 :       {
     285               2 :         if (pszTopic)
     286                 :         {
     287               2 :           VSIFPrintf( fpTransfer, "ETOP\n" );
     288               2 :           CPLFree(pszTopic);
     289                 :         }
     290               2 :         pszTopic = topic;
     291               2 :         VSIFPrintf( fpTransfer, "TOPI %s\n", pszTopic );
     292                 :       }
     293                 :       else
     294                 :       {
     295               0 :         CPLFree(topic);
     296                 :       }
     297                 :     }
     298                 :     else
     299                 :     {
     300               1 :       if (pszTopic == NULL) pszTopic = CPLStrdup("Unknown");
     301               1 :       VSIFPrintf( fpTransfer, "TOPI %s\n", pszTopic );
     302                 :     }
     303               3 :     VSIFPrintf( fpTransfer, "TABL %s\n", table );
     304                 : 
     305               3 :     OGRILI1Layer *poLayer = new OGRILI1Layer(table, poSRS, TRUE, eType, this);
     306                 : 
     307               3 :     nLayers ++;
     308               3 :     papoLayers = (OGRILI1Layer**)CPLRealloc(papoLayers, sizeof(OGRILI1Layer*) * nLayers);
     309               3 :     papoLayers[nLayers-1] = poLayer;
     310                 :     
     311               3 :     return poLayer;
     312                 : }
     313                 : 
     314                 : /************************************************************************/
     315                 : /*                           TestCapability()                           */
     316                 : /************************************************************************/
     317                 : 
     318               0 : int OGRILI1DataSource::TestCapability( const char * pszCap )
     319                 : 
     320                 : {
     321               0 :     if( EQUAL(pszCap,ODsCCreateLayer) )
     322               0 :         return TRUE;
     323                 :     else
     324               0 :         return FALSE;
     325                 : }
     326                 : 
     327                 : /************************************************************************/
     328                 : /*                              GetLayer()                              */
     329                 : /************************************************************************/
     330                 : 
     331              23 : OGRLayer *OGRILI1DataSource::GetLayer( int iLayer )
     332                 : {
     333              23 :   return poReader->GetLayer( iLayer );
     334                 : }

Generated by: LCOV version 1.7