1 /******************************************************************************
2  *
3  * Project:  CouchDB Translator
4  * Purpose:  Implements OGRCouchDBDriver.
5  * Author:   Even Rouault, even dot rouault at spatialys.com
6  *
7  ******************************************************************************
8  * Copyright (c) 2011, Even Rouault <even dot rouault at spatialys.com>
9  *
10  * Permission is hereby granted, free of charge, to any person obtaining a
11  * copy of this software and associated documentation files (the "Software"),
12  * to deal in the Software without restriction, including without limitation
13  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14  * and/or sell copies of the Software, and to permit persons to whom the
15  * Software is furnished to do so, subject to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be included
18  * in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26  * DEALINGS IN THE SOFTWARE.
27  ****************************************************************************/
28 
29 #include "ogr_couchdb.h"
30 
31 // g++ -g -Wall -fPIC -shared -o ogr_CouchDB.so -Iport -Igcore -Iogr -Iogr/ogrsf_frmts -Iogr/ogrsf_frmts/couchdb ogr/ogrsf_frmts/couchdb/*.c* -L. -lgdal -Iogr/ogrsf_frmts/geojson/jsonc
32 
33 CPL_CVSID("$Id: ogrcouchdbdriver.cpp bc1ffc48e822e6bf6d2ea18d0db68c1e77465952 2021-10-06 20:27:37 +0200 Even Rouault $")
34 
35 extern "C" void RegisterOGRCouchDB();
36 
37 /************************************************************************/
38 /*                   OGRCouchDBDriverIdentify()                         */
39 /************************************************************************/
40 
OGRCouchDBDriverIdentify(GDALOpenInfo * poOpenInfo)41 static int OGRCouchDBDriverIdentify( GDALOpenInfo* poOpenInfo )
42 
43 {
44     if (STARTS_WITH(poOpenInfo->pszFilename, "http://") ||
45         STARTS_WITH(poOpenInfo->pszFilename, "https://"))
46     {
47         return -1;
48     }
49     else if (STARTS_WITH_CI(poOpenInfo->pszFilename, "CouchDB:"))
50         return 1;
51     else
52         return 0;
53 
54 }
55 
56 /************************************************************************/
57 /*                  OGRCouchDBDriverOpen()                              */
58 /************************************************************************/
59 
OGRCouchDBDriverOpen(GDALOpenInfo * poOpenInfo)60 static GDALDataset* OGRCouchDBDriverOpen( GDALOpenInfo* poOpenInfo )
61 
62 {
63     if( OGRCouchDBDriverIdentify(poOpenInfo) == 0 )
64         return nullptr;
65 
66     OGRCouchDBDataSource   *poDS = new OGRCouchDBDataSource();
67 
68     if( !poDS->Open( poOpenInfo->pszFilename, poOpenInfo->eAccess == GA_Update ) )
69     {
70         delete poDS;
71         poDS = nullptr;
72     }
73 
74     if( poDS != nullptr && !GDALIsDriverDeprecatedForGDAL35StillEnabled("COUCHDB") )
75     {
76         delete poDS;
77         return nullptr;
78     }
79 
80     return poDS;
81 }
82 
83 /************************************************************************/
84 /*                          CreateDataSource()                          */
85 /************************************************************************/
86 
OGRCouchDBDriverCreate(const char * pszName,int,int,int,GDALDataType,char **)87 static GDALDataset* OGRCouchDBDriverCreate( const char * pszName,
88                                             int /* nXSize */,
89                                             int /* nYSize */,
90                                             int /* nBands */,
91                                             GDALDataType /* eDT */,
92                                             char ** /* papszOptions */ )
93 {
94     if( !GDALIsDriverDeprecatedForGDAL35StillEnabled("COUCHDB") )
95         return nullptr;
96 
97     OGRCouchDBDataSource   *poDS = new OGRCouchDBDataSource();
98 
99     if( !poDS->Open( pszName, TRUE ) )
100     {
101         delete poDS;
102         poDS = nullptr;
103     }
104 
105     return poDS;
106 }
107 
108 /************************************************************************/
109 /*                         RegisterOGRCouchDB()                         */
110 /************************************************************************/
111 
RegisterOGRCouchDB()112 void RegisterOGRCouchDB()
113 
114 {
115     if( GDALGetDriverByName( "CouchDB" ) != nullptr )
116       return;
117 
118     GDALDriver  *poDriver = new GDALDriver();
119 
120     poDriver->SetDescription( "CouchDB" );
121     poDriver->SetMetadataItem( GDAL_DCAP_VECTOR, "YES" );
122     poDriver->SetMetadataItem( GDAL_DMD_LONGNAME, "CouchDB / GeoCouch" );
123     poDriver->SetMetadataItem( GDAL_DMD_HELPTOPIC, "drivers/vector/couchdb.html" );
124     poDriver->SetMetadataItem( GDAL_DMD_CONNECTION_PREFIX, "CouchDB:" );
125     poDriver->SetMetadataItem( GDAL_DMD_CREATIONOPTIONLIST,
126                                "<CreationOptionList/>");
127 
128     poDriver->SetMetadataItem( GDAL_DS_LAYER_CREATIONOPTIONLIST,
129     "<LayerCreationOptionList>"
130     "  <Option name='UPDATE_PERMISSIONS' type='string' description='Update permissions for the new layer.'/>"
131     "  <Option name='GEOJSON' type='boolean' description='Whether to write documents as GeoJSON documents.' default='YES'/>"
132     "  <Option name='COORDINATE_PRECISION' type='int' description='Maximum number of figures after decimal separator to write in coordinates.' default='15'/>"
133     "</LayerCreationOptionList>");
134 
135     poDriver->SetMetadataItem( GDAL_DMD_CREATIONFIELDDATATYPES,
136                                "Integer Integer64 Real String Date DateTime "
137                                "Time IntegerList Integer64List RealList "
138                                "StringList Binary" );
139 
140     poDriver->pfnIdentify = OGRCouchDBDriverIdentify;
141     poDriver->pfnOpen = OGRCouchDBDriverOpen;
142     poDriver->pfnCreate = OGRCouchDBDriverCreate;
143 
144     GetGDALDriverManager()->RegisterDriver( poDriver );
145 }
146