GDALCompatibility.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30 */
31 
32 /**
33  @file GDALCompatibility.hpp
34 
35  @author Jean-Christophe FABRE <jean-christophe.fabre@inra.fr>
36 
37  This file contains macros to help ensuring compatibility
38  between GDAL version 1.xx and 2.xx of this library.
39  These macros mainly apply to vector part (OGR) of the library
40 */
41 
42 
43 #ifndef __OPENFLUID_UTILS_GDALCOMPATIBILITY_HPP__
44 #define __OPENFLUID_UTILS_GDALCOMPATIBILITY_HPP__
45 
46 
47 #include <gdal.h>
48 
49 
50 #if !defined(GDAL_VERSION_MAJOR)
51  #error "GDAL_VERSION_MAJOR not defined"
52 #endif
53 
54 
55 /**
56  Macro for compatibility of vector drivers registering
57 */
58 #if (GDAL_VERSION_MAJOR >= 2)
59  #define GDALAllRegister_COMPAT() GDALAllRegister()
60 #else
61  #define GDALAllRegister_COMPAT() OGRRegisterAll()
62 #endif
63 
64 
65 /**
66  Macro for compatibility of vector data source type
67 */
68 #if (GDAL_VERSION_MAJOR >= 2)
69  #define GDALDataset_COMPAT GDALDataset
70 #else
71  #define GDALDataset_COMPAT OGRDataSource
72 #endif
73 
74 
75 /**
76  Macro for compatibility of vector driver type
77 */
78 #if (GDAL_VERSION_MAJOR >= 2)
79  #define GDALDriver_COMPAT GDALDriver
80 #else
81  #define GDALDriver_COMPAT OGRSFDriver
82 #endif
83 
84 
85 /**
86  Macro for compatibility of vector data opening (in read-write mode)
87  @param _M_path Path to the file(s) to open
88  @return a pointer to a GDALDataset_COMPAT data source
89 */
90 #if (GDAL_VERSION_MAJOR >= 2)
91  #define GDALOpenRW_COMPAT(_M_path) \
92  static_cast<GDALDataset_COMPAT*>(GDALOpenEx(_M_path,GDAL_OF_VECTOR | GDAL_OF_UPDATE,nullptr,nullptr,nullptr));
93 #else
94  #define GDALOpenRW_COMPAT(_M_path) OGRSFDriverRegistrar::Open(_M_path,true);
95 #endif
96 
97 
98 /**
99  Macro for compatibility of vector data opening (in read only mode)
100  @param _M_path Path to the file(s) to open
101  @return a pointer to a GDALDataset_COMPAT data source
102 */
103 #if (GDAL_VERSION_MAJOR >= 2)
104  #define GDALOpenRO_COMPAT(_M_path) \
105  static_cast<GDALDataset_COMPAT*>(GDALOpenEx(_M_path,GDAL_OF_VECTOR | GDAL_OF_READONLY,nullptr,nullptr,nullptr));
106 #else
107  #define GDALOpenRO_COMPAT(_M_path) OGRSFDriverRegistrar::Open(_M_path,false);
108 #endif
109 
110 
111 /**
112  Macro for compatibility of syncing/flushing a dataset to disk
113  @param _M_dataset The dataset
114 */
115 #if (GDAL_VERSION_MAJOR >= 2)
116  #define GDALFlush_COMPAT(_M_dataset) _M_dataset->FlushCache()
117 #else
118  #define GDALFlush_COMPAT(_M_dataset) _M_dataset->SyncToDisk()
119 #endif
120 
121 
122 /**
123  Macro for compatibility of vector data copy
124  @param _M_driver Driver to use for the copy
125  @param _M_srcptr Data source to copy
126  @param _M_destpath Path to the copied file(s)
127  @return a pointer to a GDALDataset_COMPAT data source for the newly copied files
128 */
129 #if (GDAL_VERSION_MAJOR >= 2)
130  #define GDALCopy_COMPAT(_M_driver,_M_srcptr,_M_destpath) \
131  _M_driver->CreateCopy(_M_destpath,_M_srcptr,false,nullptr,nullptr,nullptr)
132 #else
133  #define GDALCopy_COMPAT(_M_driver,_M_srcptr,_M_destpath) \
134  _M_driver->CopyDataSource(_M_srcptr,_M_destpath, nullptr);
135 #endif
136 
137 
138 /**
139  Macro for compatibility of vector data creation
140  @param _M_driver Driver to use for the creation
141  @param _M_path Path to the file(s) to create
142  @return a pointer to a GDALDataset_COMPAT data source for the newly created files
143 */
144 #if (GDAL_VERSION_MAJOR >= 2)
145  #define GDALCreate_COMPAT(_M_driver,_M_path) \
146  _M_driver->Create(_M_path,0,0,0,GDT_Unknown,nullptr)
147 #else
148  #define GDALCreate_COMPAT(_M_driver,_M_path) \
149  _M_driver->CreateDataSource(_M_path, nullptr);
150 #endif
151 
152 
153 /**
154  Macro for compatibility of data source closing
155  @param _M_ptr Pointer to the data source to close
156 */
157 #if (GDAL_VERSION_MAJOR >= 2)
158  #define GDALClose_COMPAT(_M_ptr) GDALClose(_M_ptr);
159 #else
160  #define GDALClose_COMPAT(_M_ptr) OGRDataSource::DestroyDataSource(_M_ptr);
161 #endif
162 
163 
164 /**
165  Macro for compatibility of physical deletion of vector data
166  @param _M_driver Driver to use for the deletion
167  @param _M_path Path to the file(s) to delete
168 */
169 #if (GDAL_VERSION_MAJOR >= 2)
170  #define GDALDelete_COMPAT(_M_driver,_M_path) _M_driver->Delete(_M_path)
171 #else
172  #define GDALDelete_COMPAT(_M_driver,_M_path) _M_driver->DeleteDataSource(_M_path)
173 #endif
174 
175 
176 /**
177  Macro for compatibility of getting a driver by its name
178  @param _M_name Name of the driver
179  @return a pointer to a GDALDriver_COMPAT driver, NULL if not found
180 */
181 #if (GDAL_VERSION_MAJOR >= 2)
182  #define GDALGetDriverByName_COMPAT(_M_name) GetGDALDriverManager()->GetDriverByName(_M_name)
183 #else
184  #define GDALGetDriverByName_COMPAT(_M_name) OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(_M_name)
185 #endif
186 
187 
188 /**
189  Macro for compatibility of getting a the name of the driver from a driver object
190  @param _M_driver The driver object
191  @return the name of the driver
192 */
193 #if (GDAL_VERSION_MAJOR >= 2)
194  #define GDALGetDriverName_COMPAT(_M_driver) _M_driver->GetDescription()
195 #else
196  #define GDALGetDriverName_COMPAT(_M_driver) _M_driver->GetName()
197 #endif
198 
199 
200 /**
201  Macro for compatibility of Integer64 and Integer64List type
202 */
203 #if (GDAL_VERSION_MAJOR >= 2)
204  #define GDALOFTInteger64_COMPAT OFTInteger64
205  #define GDALOFTIntegerList64_COMPAT OFTIntegerList64
206 #else
207  #define GDALOFTInteger64_COMPAT OFTInteger
208  #define GDALOFTIntegerList64_COMPAT OFTIntegerList
209 #endif
210 
211 
212 /**
213  Macro for compatibility of surface object detection
214  @param _M_data the GDALDataset_COMPAT object
215  @return a boolean about 2D state of objects of the dataset
216 */
217 #if (GDAL_VERSION_MAJOR >= 2)
218  #define GDALIsSurface_COMPAT(_M_data) OGR_GT_IsSurface(_M_data->GetLayer(0)->GetGeomType())
219 #else
220  #define GDALIsSurface_COMPAT(_M_data) (_M_data->GetLayer(0)->GetGeomType() == wkbPolygon || \
221  _M_data->GetLayer(0)->GetGeomType() == wkbMultiPolygon)
222 #endif
223 
224 
225 #endif /* __OPENFLUID_UTILS_GDALCOMPATIBILITY_HPP__ */