1 /******************************************************************************
2  * $Id: ogrili2driver.cpp 29109 2015-05-02 11:45:44Z rouault $
3  *
4  * Project:  Interlis 2 Translator
5  * Purpose:  Implements OGRILI2Layer class.
6  * Author:   Markus Schnider, 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_ili2.h"
31 #include "cpl_conv.h"
32 
33 CPL_CVSID("$Id: ogrili2driver.cpp 29109 2015-05-02 11:45:44Z rouault $");
34 
35 /************************************************************************/
36 /*                                Open()                                */
37 /************************************************************************/
38 
OGRILI2DriverOpen(GDALOpenInfo * poOpenInfo)39 static GDALDataset *OGRILI2DriverOpen( GDALOpenInfo* poOpenInfo )
40 
41 {
42     OGRILI2DataSource    *poDS;
43 
44     if( poOpenInfo->eAccess == GA_Update ||
45         (!poOpenInfo->bStatOK && strchr(poOpenInfo->pszFilename, ',') == NULL) )
46         return NULL;
47 
48     if( poOpenInfo->fpL != NULL )
49     {
50         if( poOpenInfo->pabyHeader[0] != '<'
51             || strstr((const char*)poOpenInfo->pabyHeader,"interlis.ch/INTERLIS2") == NULL )
52         {
53             return NULL;
54         }
55     }
56     else if( poOpenInfo->bIsDirectory )
57         return NULL;
58 
59     poDS = new OGRILI2DataSource();
60 
61     if( !poDS->Open( poOpenInfo->pszFilename, poOpenInfo->papszOpenOptions, TRUE )
62         || poDS->GetLayerCount() == 0 )
63     {
64         delete poDS;
65         return NULL;
66     }
67     else
68         return poDS;
69 }
70 
71 /************************************************************************/
72 /*                               Create()                               */
73 /************************************************************************/
74 
OGRILI2DriverCreate(const char * pszName,CPL_UNUSED int nBands,CPL_UNUSED int nXSize,CPL_UNUSED int nYSize,CPL_UNUSED GDALDataType eDT,char ** papszOptions)75 static GDALDataset *OGRILI2DriverCreate( const char * pszName,
76                                          CPL_UNUSED int nBands,
77                                          CPL_UNUSED int nXSize,
78                                          CPL_UNUSED int nYSize,
79                                          CPL_UNUSED GDALDataType eDT,
80                                          char **papszOptions )
81 {
82     OGRILI2DataSource    *poDS = new OGRILI2DataSource();
83 
84     if( !poDS->Create( pszName, papszOptions ) )
85     {
86         delete poDS;
87         return NULL;
88     }
89     else
90         return poDS;
91 }
92 
93 /************************************************************************/
94 /*                           RegisterOGRILI2()                           */
95 /************************************************************************/
96 
RegisterOGRILI2()97 void RegisterOGRILI2() {
98     GDALDriver  *poDriver;
99 
100     if( GDALGetDriverByName( "Interlis 2" ) == NULL )
101     {
102         poDriver = new GDALDriver();
103 
104         poDriver->SetDescription( "Interlis 2" );
105         poDriver->SetMetadataItem( GDAL_DCAP_VECTOR, "YES" );
106         poDriver->SetMetadataItem( GDAL_DMD_LONGNAME,
107                                    "Interlis 2" );
108         poDriver->SetMetadataItem( GDAL_DMD_HELPTOPIC,
109                                    "drv_ili.html" );
110         poDriver->SetMetadataItem( GDAL_DMD_EXTENSIONS, "xtf xml ili" );
111 
112         poDriver->SetMetadataItem( GDAL_DMD_OPENOPTIONLIST,
113 "<OpenOptionList>"
114 "  <Option name='MODEL' type='string' description='Filename of the model in IlisMeta format (.imd)'/>"
115 "</OpenOptionList>" );
116 
117         poDriver->pfnOpen = OGRILI2DriverOpen;
118         poDriver->pfnCreate = OGRILI2DriverCreate;
119 
120         GetGDALDriverManager()->RegisterDriver( poDriver );
121     }
122 }
123