-
Notifications
You must be signed in to change notification settings - Fork 50
/
mdal_driver_manager.cpp
284 lines (234 loc) · 7.3 KB
/
mdal_driver_manager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/*
MDAL - Mesh Data Abstraction Library (MIT License)
Copyright (C) 2018 Peter Petrik (zilolv at gmail dot com)
*/
#include "mdal_config.hpp"
#include "mdal_driver_manager.hpp"
#include "frmts/mdal_2dm.hpp"
#include "frmts/mdal_xms_tin.hpp"
#include "frmts/mdal_ascii_dat.hpp"
#include "frmts/mdal_binary_dat.hpp"
#include "frmts/mdal_selafin.hpp"
#include "frmts/mdal_esri_tin.hpp"
#include "frmts/mdal_dynamic_driver.hpp"
#include "mdal_utils.hpp"
#ifdef BUILD_PLY
#include "frmts/mdal_ply.hpp"
#endif
#ifdef HAVE_HDF5
#include "frmts/mdal_xmdf.hpp"
#include "frmts/mdal_flo2d.hpp"
#include "frmts/mdal_hec2d.hpp"
#endif
#ifdef HAVE_GDAL
#include "frmts/mdal_gdal_grib.hpp"
#include "frmts/mdal_h2i.hpp"
#endif
#ifdef HAVE_NETCDF
#include "frmts/mdal_ugrid.hpp"
#include "frmts/mdal_sww.hpp"
#include "frmts/mdal_tuflowfv.hpp"
#endif
#if defined HAVE_GDAL && defined HAVE_NETCDF
#include "frmts/mdal_gdal_netcdf.hpp"
#endif
#if defined HAVE_HDF5 && defined HAVE_XML
#include "frmts/mdal_xdmf.hpp"
#endif
#if defined HAVE_SQLITE3 && defined HAVE_NETCDF
#include "frmts/mdal_3di.hpp"
#endif
std::string MDAL::DriverManager::getUris( const std::string &file, const std::string &driverName ) const
{
if ( !MDAL::fileExists( file ) )
{
MDAL::Log::error( MDAL_Status::Err_FileNotFound, "File " + file + " could not be found" );
return std::string();
}
if ( !driverName.empty() )
{
std::shared_ptr<MDAL::Driver> requestedDriver = driver( driverName );
if ( !requestedDriver )
{
MDAL::Log::error( MDAL_Status::Err_MissingDriver, "No such driver with name " + driverName );
return std::string();
}
std::unique_ptr<MDAL::Driver> drv( requestedDriver->create() );
return drv->buildUri( file );
}
else
{
for ( const auto &driver : mDrivers )
{
if ( ( driver->hasCapability( Capability::ReadMesh ) ) &&
driver->canReadMesh( file ) )
{
std::unique_ptr<MDAL::Driver> drv( driver->create() );
return drv->buildUri( file );
}
}
}
return std::string();
}
std::unique_ptr<MDAL::Mesh> MDAL::DriverManager::load( const std::string &meshFile, const std::string &meshName ) const
{
std::unique_ptr<MDAL::Mesh> mesh;
if ( !MDAL::fileExists( meshFile ) )
{
MDAL::Log::error( MDAL_Status::Err_FileNotFound, "File " + meshFile + " could not be found" );
return std::unique_ptr<MDAL::Mesh>();
}
for ( const auto &driver : mDrivers )
{
if ( ( driver->hasCapability( Capability::ReadMesh ) ) &&
driver->canReadMesh( meshFile ) )
{
std::unique_ptr<MDAL::Driver> drv( driver->create() );
mesh = drv->load( meshFile, meshName );
if ( mesh ) // stop if he have the mesh
break;
}
}
if ( !mesh )
MDAL::Log::error( MDAL_Status::Err_UnknownFormat, "Unable to load mesh (null)" );
return mesh;
}
std::unique_ptr<MDAL::Mesh> MDAL::DriverManager::load(
const std::string &driverName,
const std::string &meshFile,
const std::string &meshName ) const
{
std::unique_ptr<MDAL::Mesh> mesh;
if ( !MDAL::fileExists( meshFile ) )
{
MDAL::Log::error( MDAL_Status::Err_FileNotFound, "File " + meshFile + " could not be found" );
return mesh;
}
std::shared_ptr<MDAL::Driver> requestedDriver;
requestedDriver = driver( driverName );
if ( !requestedDriver )
{
MDAL::Log::error( MDAL_Status::Err_MissingDriver, "Could not find driver with name: " + driverName );
return mesh;
}
std::unique_ptr<Driver> drv( requestedDriver->create() );
mesh = drv->load( meshFile, meshName );
return mesh;
}
void MDAL::DriverManager::loadDatasets( Mesh *mesh, const std::string &datasetFile ) const
{
if ( !MDAL::fileExists( datasetFile ) )
{
MDAL::Log::error( MDAL_Status::Err_FileNotFound, "File " + datasetFile + " could not be found" );
return;
}
if ( !mesh )
{
MDAL::Log::error( MDAL_Status::Err_IncompatibleMesh, "Mesh is not valid (null)" );
return;
}
for ( const auto &driver : mDrivers )
{
if ( driver->hasCapability( Capability::ReadDatasets ) &&
driver->canReadDatasets( datasetFile ) )
{
std::unique_ptr<Driver> drv( driver->create() );
drv->load( datasetFile, mesh );
return;
}
}
MDAL::Log::error( MDAL_Status::Err_UnknownFormat, "No driver was able to load requested file: " + datasetFile );
}
void MDAL::DriverManager::save( Mesh *mesh, const std::string &uri ) const
{
std::string driverName;
std::string meshName;
std::string fileName;
MDAL::parseDriverAndMeshFromUri( uri, driverName, fileName, meshName );
std::shared_ptr<MDAL::Driver> selectedDriver = driver( driverName );
if ( !selectedDriver )
{
MDAL::Log::error( MDAL_Status::Err_MissingDriver, "Could not find driver with name: " + driverName );
return;
}
std::unique_ptr<Driver> drv( selectedDriver->create() );
drv->save( fileName, meshName, mesh );
}
size_t MDAL::DriverManager::driversCount() const
{
return mDrivers.size();
}
std::shared_ptr<MDAL::Driver> MDAL::DriverManager::driver( size_t index ) const
{
if ( mDrivers.size() <= index )
{
return std::shared_ptr<MDAL::Driver>();
}
else
{
return mDrivers[index];
}
}
std::shared_ptr<MDAL::Driver> MDAL::DriverManager::driver( const std::string &driverName ) const
{
for ( const auto &dr : mDrivers )
{
if ( dr->name() == driverName )
return dr;
}
return std::shared_ptr<MDAL::Driver>();
}
MDAL::DriverManager::DriverManager()
{
// MESH DRIVERS
mDrivers.push_back( std::make_shared<MDAL::Driver2dm>() );
mDrivers.push_back( std::make_shared<MDAL::DriverXmsTin>() );
mDrivers.push_back( std::make_shared<MDAL::DriverSelafin>() );
mDrivers.push_back( std::make_shared<MDAL::DriverEsriTin>() );
#ifdef BUILD_PLY
mDrivers.push_back( std::make_shared<MDAL::DriverPly>() );
#endif
#ifdef HAVE_HDF5
mDrivers.push_back( std::make_shared<MDAL::DriverFlo2D>() );
mDrivers.push_back( std::make_shared<MDAL::DriverHec2D>() );
#endif
#ifdef HAVE_NETCDF
mDrivers.push_back( std::make_shared<MDAL::DriverTuflowFV>() );
mDrivers.push_back( std::make_shared<MDAL::DriverSWW>() );
mDrivers.push_back( std::make_shared<MDAL::DriverUgrid>() );
#endif
#if defined HAVE_SQLITE3 && defined HAVE_NETCDF
mDrivers.push_back( std::make_shared<MDAL::Driver3Di>() );
#endif
#if defined HAVE_GDAL && defined HAVE_NETCDF
mDrivers.push_back( std::make_shared<MDAL::DriverGdalNetCDF>() );
#endif
#ifdef HAVE_GDAL
mDrivers.push_back( std::make_shared<MDAL::DriverGdalGrib>() );
mDrivers.push_back( std::make_shared<MDAL::DriverH2i>() );
#endif
// DATASET DRIVERS
mDrivers.push_back( std::make_shared<MDAL::DriverAsciiDat>() );
mDrivers.push_back( std::make_shared<MDAL::DriverBinaryDat>() );
#ifdef HAVE_HDF5
mDrivers.push_back( std::make_shared<MDAL::DriverXmdf>() );
#endif
#if defined HAVE_HDF5 && defined HAVE_XML
mDrivers.push_back( std::make_shared<MDAL::DriverXdmf>() );
#endif
loadDynamicDrivers();
}
void MDAL::DriverManager::loadDynamicDrivers()
{
std::string dirPath = MDAL::getEnvVar( "MDAL_DRIVER_PATH" );
if ( dirPath.empty() )
return;
dirPath += '/';
std::vector<std::string> libList = MDAL::Library::libraryFilesInDir( dirPath );
for ( const std::string &libFile : libList )
{
std::shared_ptr<MDAL::Driver> driver( MDAL::DriverDynamic::create( dirPath + libFile ) );
if ( driver )
mDrivers.push_back( driver );
}
}