1 /******************************************************************************
2  *
3  * Project:  OpenGIS Simple Features Reference Implementation
4  * Purpose:  The generic portions of the GDALDataset class.
5  * Author:   Frank Warmerdam, warmerdam@pobox.com
6  *
7  ******************************************************************************
8  * Copyright (c) 1999,  Les Technologies SoftMap Inc.
9  * Copyright (c) 2008-2014, Even Rouault <even dot rouault at spatialys.com>
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 "ogrsf_frmts.h"
31 #include "ogr_api.h"
32 #include "ograpispy.h"
33 
34 CPL_CVSID("$Id: ogrdatasource.cpp 428c6a6a0a81364b703d448b3baa74bb17ae2cbd 2020-10-21 15:43:49 +0200 Even Rouault $")
35 
36 /************************************************************************/
37 /*                           ~OGRDataSource()                           */
38 /************************************************************************/
39 
OGRDataSource()40 OGRDataSource::OGRDataSource() {}
41 
42 /************************************************************************/
43 /*                         DestroyDataSource()                          */
44 /************************************************************************/
45 
46 //! @cond Doxygen_Suppress
DestroyDataSource(OGRDataSource * poDS)47 void OGRDataSource::DestroyDataSource( OGRDataSource *poDS )
48 
49 {
50     delete poDS;
51 }
52 //! @endcond
53 
54 /************************************************************************/
55 /*                           OGR_DS_Destroy()                           */
56 /************************************************************************/
57 
OGR_DS_Destroy(OGRDataSourceH hDS)58 void OGR_DS_Destroy( OGRDataSourceH hDS )
59 
60 {
61     if( hDS == nullptr )
62         return;
63     GDALClose( reinterpret_cast<GDALDatasetH>(hDS) );
64     //VALIDATE_POINTER0( hDS, "OGR_DS_Destroy" );
65 }
66 
67 /************************************************************************/
68 /*                          OGR_DS_Reference()                          */
69 /************************************************************************/
70 
OGR_DS_Reference(OGRDataSourceH hDataSource)71 int OGR_DS_Reference( OGRDataSourceH hDataSource )
72 
73 {
74     VALIDATE_POINTER1( hDataSource, "OGR_DS_Reference", 0 );
75 
76     return reinterpret_cast<GDALDataset *>(hDataSource)->Reference();
77 }
78 
79 /************************************************************************/
80 /*                         OGR_DS_Dereference()                         */
81 /************************************************************************/
82 
OGR_DS_Dereference(OGRDataSourceH hDataSource)83 int OGR_DS_Dereference( OGRDataSourceH hDataSource )
84 
85 {
86     VALIDATE_POINTER1( hDataSource, "OGR_DS_Dereference", 0 );
87 
88     return reinterpret_cast<GDALDataset *>(hDataSource)->Dereference();
89 }
90 
91 /************************************************************************/
92 /*                         OGR_DS_GetRefCount()                         */
93 /************************************************************************/
94 
OGR_DS_GetRefCount(OGRDataSourceH hDataSource)95 int OGR_DS_GetRefCount( OGRDataSourceH hDataSource )
96 
97 {
98     VALIDATE_POINTER1( hDataSource, "OGR_DS_GetRefCount", 0 );
99 
100     return reinterpret_cast<GDALDataset *>(hDataSource)->GetRefCount();
101 }
102 
103 /************************************************************************/
104 /*                     OGR_DS_GetSummaryRefCount()                      */
105 /************************************************************************/
106 
OGR_DS_GetSummaryRefCount(OGRDataSourceH hDataSource)107 int OGR_DS_GetSummaryRefCount( OGRDataSourceH hDataSource )
108 
109 {
110     VALIDATE_POINTER1( hDataSource, "OGR_DS_GetSummaryRefCount", 0 );
111 
112     return reinterpret_cast<GDALDataset *>(hDataSource)->GetSummaryRefCount();
113 }
114 
115 /************************************************************************/
116 /*                         OGR_DS_CreateLayer()                         */
117 /************************************************************************/
118 
OGR_DS_CreateLayer(OGRDataSourceH hDS,const char * pszName,OGRSpatialReferenceH hSpatialRef,OGRwkbGeometryType eType,char ** papszOptions)119 OGRLayerH OGR_DS_CreateLayer( OGRDataSourceH hDS,
120                               const char * pszName,
121                               OGRSpatialReferenceH hSpatialRef,
122                               OGRwkbGeometryType eType,
123                               char ** papszOptions )
124 
125 {
126     VALIDATE_POINTER1( hDS, "OGR_DS_CreateLayer", nullptr );
127 
128     if (pszName == nullptr)
129     {
130         CPLError ( CE_Failure, CPLE_ObjectNull, "Name was NULL in OGR_DS_CreateLayer");
131         return nullptr;
132     }
133     OGRLayerH hLayer = OGRLayer::ToHandle(
134         reinterpret_cast<GDALDataset *>(hDS)->CreateLayer(
135             pszName, OGRSpatialReference::FromHandle(hSpatialRef), eType, papszOptions ));
136 
137 #ifdef OGRAPISPY_ENABLED
138     if( bOGRAPISpyEnabled )
139         OGRAPISpy_DS_CreateLayer(hDS, pszName, hSpatialRef, eType, papszOptions, hLayer);
140 #endif
141 
142     return hLayer;
143 }
144 
145 /************************************************************************/
146 /*                          OGR_DS_CopyLayer()                          */
147 /************************************************************************/
148 
OGR_DS_CopyLayer(OGRDataSourceH hDS,OGRLayerH hSrcLayer,const char * pszNewName,char ** papszOptions)149 OGRLayerH OGR_DS_CopyLayer( OGRDataSourceH hDS,
150                             OGRLayerH hSrcLayer, const char *pszNewName,
151                             char **papszOptions )
152 
153 {
154     VALIDATE_POINTER1( hDS, "OGR_DS_CopyLayer", nullptr );
155     VALIDATE_POINTER1( hSrcLayer, "OGR_DS_CopyLayer", nullptr );
156     VALIDATE_POINTER1( pszNewName, "OGR_DS_CopyLayer", nullptr );
157 
158     return OGRLayer::ToHandle(
159         reinterpret_cast<GDALDataset *>(hDS)->CopyLayer(
160             OGRLayer::FromHandle(hSrcLayer), pszNewName, papszOptions ));
161 }
162 
163 /************************************************************************/
164 /*                         OGR_DS_DeleteLayer()                         */
165 /************************************************************************/
166 
OGR_DS_DeleteLayer(OGRDataSourceH hDS,int iLayer)167 OGRErr OGR_DS_DeleteLayer( OGRDataSourceH hDS, int iLayer )
168 
169 {
170     VALIDATE_POINTER1( hDS, "OGR_DS_DeleteLayer", OGRERR_INVALID_HANDLE );
171 
172 #ifdef OGRAPISPY_ENABLED
173     if( bOGRAPISpyEnabled )
174         OGRAPISpy_DS_DeleteLayer(reinterpret_cast<GDALDatasetH>(hDS), iLayer);
175 #endif
176 
177     OGRErr eErr = reinterpret_cast<GDALDataset *>(hDS)->DeleteLayer( iLayer );
178 
179     return eErr;
180 }
181 
182 /************************************************************************/
183 /*                       OGR_DS_GetLayerByName()                        */
184 /************************************************************************/
185 
OGR_DS_GetLayerByName(OGRDataSourceH hDS,const char * pszLayerName)186 OGRLayerH OGR_DS_GetLayerByName( OGRDataSourceH hDS, const char *pszLayerName )
187 
188 {
189     VALIDATE_POINTER1( hDS, "OGR_DS_GetLayerByName", nullptr );
190 
191     OGRLayerH hLayer = OGRLayer::ToHandle(
192         reinterpret_cast<GDALDataset *>(hDS)->GetLayerByName( pszLayerName ));
193 
194 #ifdef OGRAPISPY_ENABLED
195     if( bOGRAPISpyEnabled )
196         OGRAPISpy_DS_GetLayerByName(reinterpret_cast<GDALDatasetH>(hDS), pszLayerName, hLayer);
197 #endif
198 
199     return hLayer;
200 }
201 
202 /************************************************************************/
203 /*                         OGR_DS_ExecuteSQL()                          */
204 /************************************************************************/
205 
OGR_DS_ExecuteSQL(OGRDataSourceH hDS,const char * pszStatement,OGRGeometryH hSpatialFilter,const char * pszDialect)206 OGRLayerH OGR_DS_ExecuteSQL( OGRDataSourceH hDS,
207                              const char *pszStatement,
208                              OGRGeometryH hSpatialFilter,
209                              const char *pszDialect )
210 
211 {
212     VALIDATE_POINTER1( hDS, "OGR_DS_ExecuteSQL", nullptr );
213 
214     OGRLayerH hLayer = OGRLayer::ToHandle(
215         reinterpret_cast<GDALDataset *>(hDS)->ExecuteSQL( pszStatement,
216                 OGRGeometry::FromHandle(hSpatialFilter), pszDialect ));
217 
218 #ifdef OGRAPISPY_ENABLED
219     if( bOGRAPISpyEnabled )
220         OGRAPISpy_DS_ExecuteSQL(reinterpret_cast<GDALDatasetH>(hDS), pszStatement, hSpatialFilter, pszDialect, hLayer);
221 #endif
222 
223     return hLayer;
224 }
225 
226 
227 /************************************************************************/
228 /*                      OGR_DS_ReleaseResultSet()                       */
229 /************************************************************************/
230 
OGR_DS_ReleaseResultSet(OGRDataSourceH hDS,OGRLayerH hLayer)231 void OGR_DS_ReleaseResultSet( OGRDataSourceH hDS, OGRLayerH hLayer )
232 
233 {
234     VALIDATE_POINTER0( hDS, "OGR_DS_ReleaseResultSet" );
235 
236 #ifdef OGRAPISPY_ENABLED
237     if( bOGRAPISpyEnabled )
238         OGRAPISpy_DS_ReleaseResultSet(reinterpret_cast<GDALDatasetH>(hDS), hLayer);
239 #endif
240 
241     reinterpret_cast<GDALDataset *>(hDS)->ReleaseResultSet( OGRLayer::FromHandle(hLayer) );
242 }
243 
244 /************************************************************************/
245 /*                       OGR_DS_TestCapability()                        */
246 /************************************************************************/
247 
OGR_DS_TestCapability(OGRDataSourceH hDS,const char * pszCapability)248 int OGR_DS_TestCapability( OGRDataSourceH hDS, const char *pszCapability )
249 
250 {
251     VALIDATE_POINTER1( hDS, "OGR_DS_TestCapability", 0 );
252     VALIDATE_POINTER1( pszCapability, "OGR_DS_TestCapability", 0 );
253 
254     return reinterpret_cast<GDALDataset *>(hDS)->TestCapability( pszCapability );
255 }
256 
257 /************************************************************************/
258 /*                        OGR_DS_GetLayerCount()                        */
259 /************************************************************************/
260 
OGR_DS_GetLayerCount(OGRDataSourceH hDS)261 int OGR_DS_GetLayerCount( OGRDataSourceH hDS )
262 
263 {
264     VALIDATE_POINTER1( hDS, "OGR_DS_GetLayerCount", 0 );
265 
266 #ifdef OGRAPISPY_ENABLED
267     if( bOGRAPISpyEnabled )
268         OGRAPISpy_DS_GetLayerCount(reinterpret_cast<GDALDatasetH>(hDS));
269 #endif
270 
271     return reinterpret_cast<GDALDataset *>(hDS)->GetLayerCount();
272 }
273 
274 /************************************************************************/
275 /*                          OGR_DS_GetLayer()                           */
276 /************************************************************************/
277 
OGR_DS_GetLayer(OGRDataSourceH hDS,int iLayer)278 OGRLayerH OGR_DS_GetLayer( OGRDataSourceH hDS, int iLayer )
279 
280 {
281     VALIDATE_POINTER1( hDS, "OGR_DS_GetLayer", nullptr );
282 
283     OGRLayerH hLayer = OGRLayer::ToHandle(
284         reinterpret_cast<GDALDataset *>(hDS)->GetLayer( iLayer ));
285 
286 #ifdef OGRAPISPY_ENABLED
287     if( bOGRAPISpyEnabled )
288         OGRAPISpy_DS_GetLayer(reinterpret_cast<GDALDatasetH>(hDS), iLayer, hLayer);
289 #endif
290 
291     return hLayer;
292 }
293 
294 /************************************************************************/
295 /*                           OGR_DS_GetName()                           */
296 /************************************************************************/
297 
OGR_DS_GetName(OGRDataSourceH hDS)298 const char *OGR_DS_GetName( OGRDataSourceH hDS )
299 
300 {
301     VALIDATE_POINTER1( hDS, "OGR_DS_GetName", nullptr );
302 
303     return reinterpret_cast<GDALDataset *>(hDS)->GetDescription();
304 }
305 
306 /************************************************************************/
307 /*                         OGR_DS_SyncToDisk()                          */
308 /************************************************************************/
309 
OGR_DS_SyncToDisk(OGRDataSourceH hDS)310 OGRErr OGR_DS_SyncToDisk( OGRDataSourceH hDS )
311 
312 {
313     VALIDATE_POINTER1( hDS, "OGR_DS_SyncToDisk", OGRERR_INVALID_HANDLE );
314 
315     reinterpret_cast<GDALDataset *>(hDS)->FlushCache();
316     if( CPLGetLastErrorType() != 0 )
317         return OGRERR_FAILURE;
318     else
319         return OGRERR_NONE;
320 }
321 
322 /************************************************************************/
323 /*                          OGR_DS_GetDriver()                          */
324 /************************************************************************/
325 
OGR_DS_GetDriver(OGRDataSourceH hDS)326 OGRSFDriverH OGR_DS_GetDriver( OGRDataSourceH hDS )
327 
328 {
329     VALIDATE_POINTER1( hDS, "OGR_DS_GetDriver", nullptr );
330 
331     return reinterpret_cast<OGRSFDriverH>(
332         reinterpret_cast<OGRDataSource *>(hDS)->GetDriver());
333 }
334 
335 /************************************************************************/
336 /*                         OGR_DS_GetStyleTable()                       */
337 /************************************************************************/
338 
OGR_DS_GetStyleTable(OGRDataSourceH hDS)339 OGRStyleTableH OGR_DS_GetStyleTable( OGRDataSourceH hDS )
340 
341 {
342     VALIDATE_POINTER1( hDS, "OGR_DS_GetStyleTable", nullptr );
343 
344     return reinterpret_cast<OGRStyleTableH>(
345         reinterpret_cast<GDALDataset *>(hDS)->GetStyleTable( ));
346 }
347 
348 /************************************************************************/
349 /*                         OGR_DS_SetStyleTableDirectly()               */
350 /************************************************************************/
351 
OGR_DS_SetStyleTableDirectly(OGRDataSourceH hDS,OGRStyleTableH hStyleTable)352 void OGR_DS_SetStyleTableDirectly( OGRDataSourceH hDS,
353                                    OGRStyleTableH hStyleTable )
354 
355 {
356     VALIDATE_POINTER0( hDS, "OGR_DS_SetStyleTableDirectly" );
357 
358     reinterpret_cast<GDALDataset *>(hDS)->SetStyleTableDirectly(
359         reinterpret_cast<OGRStyleTable *>(hStyleTable) );
360 }
361 
362 /************************************************************************/
363 /*                         OGR_DS_SetStyleTable()                       */
364 /************************************************************************/
365 
OGR_DS_SetStyleTable(OGRDataSourceH hDS,OGRStyleTableH hStyleTable)366 void OGR_DS_SetStyleTable( OGRDataSourceH hDS, OGRStyleTableH hStyleTable )
367 
368 {
369     VALIDATE_POINTER0( hDS, "OGR_DS_SetStyleTable" );
370     VALIDATE_POINTER0( hStyleTable, "OGR_DS_SetStyleTable" );
371 
372     reinterpret_cast<GDALDataset *>(hDS)->SetStyleTable(
373         reinterpret_cast<OGRStyleTable *>(hStyleTable) );
374 }
375