/
tiledbcommon.cpp
410 lines (364 loc) · 14.9 KB
/
tiledbcommon.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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/******************************************************************************
*
* Project: GDAL TileDB Driver
* Purpose: Implement GDAL TileDB Support based on https://www.tiledb.io
* Author: TileDB, Inc
*
******************************************************************************
* Copyright (c) 2023, TileDB, Inc
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "tiledbheaders.h"
#include "tiledbdrivercore.h"
/************************************************************************/
/* VSI_to_tiledb_uri() */
/************************************************************************/
CPLString TileDBDataset::VSI_to_tiledb_uri(const char *pszUri)
{
CPLString osUri;
if (STARTS_WITH_CI(pszUri, "/VSIS3/"))
osUri.Printf("s3://%s", pszUri + 7);
else if (STARTS_WITH_CI(pszUri, "/VSIGS/"))
osUri.Printf("gcs://%s", pszUri + 7);
else
{
osUri = pszUri;
// tiledb (at least at 2.4.2 on Conda) wrongly interprets relative
// directories on Windows as absolute ones.
if (CPLIsFilenameRelative(pszUri))
{
char *pszCurDir = CPLGetCurrentDir();
if (pszCurDir)
osUri = CPLFormFilename(pszCurDir, pszUri, nullptr);
CPLFree(pszCurDir);
}
}
return osUri;
}
/************************************************************************/
/* AddFilter() */
/************************************************************************/
CPLErr TileDBDataset::AddFilter(tiledb::Context &ctx,
tiledb::FilterList &filterList,
const char *pszFilterName, const int level)
{
try
{
if (pszFilterName == nullptr)
filterList.add_filter(
tiledb::Filter(ctx, TILEDB_FILTER_NONE)
.set_option(TILEDB_COMPRESSION_LEVEL, level));
else if EQUAL (pszFilterName, "GZIP")
filterList.add_filter(
tiledb::Filter(ctx, TILEDB_FILTER_GZIP)
.set_option(TILEDB_COMPRESSION_LEVEL, level));
else if EQUAL (pszFilterName, "ZSTD")
filterList.add_filter(
tiledb::Filter(ctx, TILEDB_FILTER_ZSTD)
.set_option(TILEDB_COMPRESSION_LEVEL, level));
else if EQUAL (pszFilterName, "LZ4")
filterList.add_filter(
tiledb::Filter(ctx, TILEDB_FILTER_LZ4)
.set_option(TILEDB_COMPRESSION_LEVEL, level));
else if EQUAL (pszFilterName, "RLE")
filterList.add_filter(
tiledb::Filter(ctx, TILEDB_FILTER_RLE)
.set_option(TILEDB_COMPRESSION_LEVEL, level));
else if EQUAL (pszFilterName, "BZIP2")
filterList.add_filter(
tiledb::Filter(ctx, TILEDB_FILTER_BZIP2)
.set_option(TILEDB_COMPRESSION_LEVEL, level));
else if EQUAL (pszFilterName, "DOUBLE-DELTA")
filterList.add_filter(
tiledb::Filter(ctx, TILEDB_FILTER_DOUBLE_DELTA));
else if EQUAL (pszFilterName, "POSITIVE-DELTA")
filterList.add_filter(
tiledb::Filter(ctx, TILEDB_FILTER_POSITIVE_DELTA));
else
return CE_Failure;
return CE_None;
}
catch (const tiledb::TileDBError &e)
{
CPLError(CE_Failure, CPLE_AppDefined, "%s", e.what());
return CE_Failure;
}
}
/************************************************************************/
/* Identify() */
/************************************************************************/
int TileDBDataset::Identify(GDALOpenInfo *poOpenInfo)
{
if (STARTS_WITH_CI(poOpenInfo->pszFilename, "TILEDB:"))
{
return TRUE;
}
try
{
const char *pszConfig =
CSLFetchNameValue(poOpenInfo->papszOpenOptions, "TILEDB_CONFIG");
if (pszConfig != nullptr)
{
return TRUE;
}
const bool bIsS3OrGS =
STARTS_WITH_CI(poOpenInfo->pszFilename, "/VSIS3/") ||
STARTS_WITH_CI(poOpenInfo->pszFilename, "/VSIGS/");
// If this is a /vsi virtual file systems, bail out, except if it is S3 or GS.
if (!bIsS3OrGS && STARTS_WITH(poOpenInfo->pszFilename, "/vsi"))
{
return false;
}
if (poOpenInfo->bIsDirectory ||
(bIsS3OrGS &&
!EQUAL(CPLGetExtension(poOpenInfo->pszFilename), "tif")))
{
tiledb::Context ctx;
CPLString osArrayPath =
TileDBDataset::VSI_to_tiledb_uri(poOpenInfo->pszFilename);
const auto eType = tiledb::Object::object(ctx, osArrayPath).type();
if ((poOpenInfo->nOpenFlags & GDAL_OF_VECTOR) != 0)
{
if (eType == tiledb::Object::Type::Array ||
eType == tiledb::Object::Type::Group)
return true;
}
if ((poOpenInfo->nOpenFlags & GDAL_OF_MULTIDIM_RASTER) != 0)
{
if (eType == tiledb::Object::Type::Array ||
eType == tiledb::Object::Type::Group)
return true;
}
if ((poOpenInfo->nOpenFlags & GDAL_OF_RASTER) != 0)
{
if (eType == tiledb::Object::Type::Group)
return GDAL_IDENTIFY_UNKNOWN;
return eType == tiledb::Object::Type::Array;
}
}
return FALSE;
}
catch (...)
{
return FALSE;
}
}
/************************************************************************/
/* Delete() */
/************************************************************************/
CPLErr TileDBDataset::Delete(const char *pszFilename)
{
try
{
tiledb::Context ctx;
tiledb::VFS vfs(ctx);
CPLString osArrayPath = TileDBDataset::VSI_to_tiledb_uri(pszFilename);
if (vfs.is_dir(osArrayPath))
{
vfs.remove_dir(osArrayPath);
return CE_None;
}
else
return CE_Failure;
}
catch (const tiledb::TileDBError &e)
{
CPLError(CE_Failure, CPLE_AppDefined, "%s", e.what());
return CE_Failure;
}
}
/************************************************************************/
/* Open() */
/************************************************************************/
GDALDataset *TileDBDataset::Open(GDALOpenInfo *poOpenInfo)
{
try
{
const auto eIdentify = TileDBDataset::Identify(poOpenInfo);
if (eIdentify == GDAL_IDENTIFY_FALSE)
return nullptr;
if (STARTS_WITH_CI(poOpenInfo->pszFilename, "TILEDB:") &&
!STARTS_WITH_CI(poOpenInfo->pszFilename, "TILEDB://"))
{
// subdataset URI so this is a raster
return TileDBRasterDataset::Open(poOpenInfo);
}
else
{
if ((poOpenInfo->nOpenFlags & GDAL_OF_MULTIDIM_RASTER) != 0)
{
return TileDBDataset::OpenMultiDimensional(poOpenInfo);
}
const char *pszConfig = CSLFetchNameValue(
poOpenInfo->papszOpenOptions, "TILEDB_CONFIG");
tiledb::Context oCtx;
if (pszConfig != nullptr)
{
tiledb::Config cfg(pszConfig);
oCtx = tiledb::Context(cfg);
}
else
{
tiledb::Config cfg;
cfg["sm.enable_signal_handlers"] = "false";
oCtx = tiledb::Context(cfg);
}
const std::string osPath =
TileDBDataset::VSI_to_tiledb_uri(poOpenInfo->pszFilename);
const auto eType = tiledb::Object::object(oCtx, osPath).type();
if ((poOpenInfo->nOpenFlags & GDAL_OF_VECTOR) != 0 &&
eType == tiledb::Object::Type::Group)
{
return OGRTileDBDataset::Open(poOpenInfo, eType);
}
else if ((poOpenInfo->nOpenFlags & GDAL_OF_RASTER) != 0 &&
eType == tiledb::Object::Type::Group)
{
// If this is a group which has only a single 2D array and
// no 3D+ arrays, then return this 2D array.
auto poDSUnique = std::unique_ptr<GDALDataset>(
TileDBDataset::OpenMultiDimensional(poOpenInfo));
if (poDSUnique)
{
auto poRootGroup = poDSUnique->GetRootGroup();
if (poRootGroup && poRootGroup->GetGroupNames().empty())
{
std::shared_ptr<GDALMDArray> poCandidateArray;
for (const auto &osName :
poRootGroup->GetMDArrayNames())
{
auto poArray = poRootGroup->OpenMDArray(osName);
if (poArray && poArray->GetDimensionCount() >= 3)
{
poCandidateArray.reset();
break;
}
else if (poArray &&
poArray->GetDimensionCount() == 2 &&
poArray->GetDimensions()[0]->GetType() ==
GDAL_DIM_TYPE_HORIZONTAL_Y &&
poArray->GetDimensions()[1]->GetType() ==
GDAL_DIM_TYPE_HORIZONTAL_X)
{
if (!poCandidateArray)
{
poCandidateArray = std::move(poArray);
}
else
{
poCandidateArray.reset();
break;
}
}
}
if (poCandidateArray)
{
return poCandidateArray->AsClassicDataset(1, 0);
}
}
}
return nullptr;
}
tiledb::ArraySchema schema(oCtx, osPath);
if (schema.array_type() == TILEDB_SPARSE)
return OGRTileDBDataset::Open(poOpenInfo, eType);
else
return TileDBRasterDataset::Open(poOpenInfo);
}
}
catch (const tiledb::TileDBError &e)
{
CPLError(CE_Failure, CPLE_AppDefined, "%s", e.what());
return nullptr;
}
}
/************************************************************************/
/* Create() */
/************************************************************************/
GDALDataset *TileDBDataset::Create(const char *pszFilename, int nXSize,
int nYSize, int nBandsIn, GDALDataType eType,
char **papszOptions)
{
try
{
if (nBandsIn > 0)
return TileDBRasterDataset::Create(pszFilename, nXSize, nYSize,
nBandsIn, eType, papszOptions);
else
return OGRTileDBDataset::Create(pszFilename, papszOptions);
}
catch (const tiledb::TileDBError &e)
{
CPLError(CE_Failure, CPLE_AppDefined, "%s", e.what());
}
return nullptr;
}
/************************************************************************/
/* CreateCopy() */
/************************************************************************/
GDALDataset *TileDBDataset::CreateCopy(const char *pszFilename,
GDALDataset *poSrcDS, int bStrict,
char **papszOptions,
GDALProgressFunc pfnProgress,
void *pProgressData)
{
if (poSrcDS->GetRootGroup())
{
auto poDrv = GDALDriver::FromHandle(GDALGetDriverByName("TileDB"));
if (poDrv)
{
return poDrv->DefaultCreateCopy(pszFilename, poSrcDS, bStrict,
papszOptions, pfnProgress,
pProgressData);
}
}
try
{
if (poSrcDS->GetRasterCount() > 0 ||
poSrcDS->GetMetadata("SUBDATASETS"))
{
return TileDBRasterDataset::CreateCopy(pszFilename, poSrcDS,
bStrict, papszOptions,
pfnProgress, pProgressData);
}
}
catch (const tiledb::TileDBError &e)
{
CPLError(CE_Failure, CPLE_AppDefined, "%s", e.what());
}
return nullptr;
}
/************************************************************************/
/* GDALRegister_TILEDB() */
/************************************************************************/
void GDALRegister_TileDB()
{
if (GDALGetDriverByName(DRIVER_NAME) != nullptr)
return;
GDALDriver *poDriver = new GDALDriver();
TileDBDriverSetCommonMetadata(poDriver);
poDriver->pfnIdentify = TileDBDataset::Identify;
poDriver->pfnOpen = TileDBDataset::Open;
poDriver->pfnCreate = TileDBDataset::Create;
poDriver->pfnCreateCopy = TileDBDataset::CreateCopy;
poDriver->pfnDelete = TileDBDataset::Delete;
poDriver->pfnCreateMultiDimensional = TileDBDataset::CreateMultiDimensional;
GetGDALDriverManager()->RegisterDriver(poDriver);
}