| 1 |
/****************************************************************************** |
| 2 |
* |
| 3 |
* Project: OpenGIS Simple Features Reference Implementation |
| 4 |
* Purpose: Implements FileGDB OGR Datasource. |
| 5 |
* Author: Ragi Yaser Burhum, [email protected] |
| 6 |
* Paul Ramsey, pramsey at cleverelephant.ca |
| 7 |
* |
| 8 |
****************************************************************************** |
| 9 |
* Copyright (c) 2010, Ragi Yaser Burhum |
| 10 |
* Copyright (c) 2011, Paul Ramsey <pramsey at cleverelephant.ca> |
| 11 |
* Copyright (c) 2011-2013, Even Rouault <even dot rouault at spatialys.com> |
| 12 |
* |
| 13 |
* SPDX-License-Identifier: MIT |
| 14 |
****************************************************************************/ |
| 15 |
|
| 16 |
#include "ogr_fgdb.h" |
| 17 |
#include "cpl_conv.h" |
| 18 |
#include "cpl_string.h" |
| 19 |
#include "gdal.h" |
| 20 |
#include "FGdbUtils.h" |
| 21 |
#include "cpl_multiproc.h" |
| 22 |
|
| 23 |
#include "filegdb_fielddomain.h" |
| 24 |
#include "filegdb_relationship.h" |
| 25 |
|
| 26 |
using std::vector; |
| 27 |
using std::wstring; |
| 28 |
|
| 29 |
/************************************************************************/ |
| 30 |
/* OGRFileGDBGroup */ |
| 31 |
/************************************************************************/ |
| 32 |
|
| 33 |
class OGRFileGDBGroup final : public GDALGroup |
| 34 |
{ |
| 35 |
protected: |
| 36 |
friend class FGdbDataSource; |
| 37 |
std::vector<std::shared_ptr<GDALGroup>> m_apoSubGroups{}; |
| 38 |
std::vector<OGRLayer *> m_apoLayers{}; |
| 39 |
|
| 40 |
public: |
| 41 |
OGRFileGDBGroup(const std::string &osParentName, const char *pszName) |
| 42 |
: GDALGroup(osParentName, pszName) |
| 43 |
{ |
| 44 |
} |
| 45 |
|
| 46 |
std::vector<std::string> |
| 47 |
GetGroupNames(CSLConstList papszOptions) const override; |
| 48 |
std::shared_ptr<GDALGroup> |
| 49 |
OpenGroup(const std::string &osName, |
| 50 |
CSLConstList papszOptions) const override; |
| 51 |
|
| 52 |
std::vector<std::string> |
| 53 |
GetVectorLayerNames(CSLConstList papszOptions) const override; |
| 54 |
OGRLayer *OpenVectorLayer(const std::string &osName, |
| 55 |
CSLConstList papszOptions) const override; |
| 56 |
}; |
| 57 |
|
| 58 |
std::vector<std::string> OGRFileGDBGroup::GetGroupNames(CSLConstList) const |
| 59 |
{ |
| 60 |
std::vector<std::string> ret; |
| 61 |
for (const auto &poSubGroup : m_apoSubGroups) |
| 62 |
ret.emplace_back(poSubGroup->GetName()); |
| 63 |
return ret; |
| 64 |
} |
| 65 |
|
| 66 |
std::shared_ptr<GDALGroup> OGRFileGDBGroup::OpenGroup(const std::string &osName, |
| 67 |
CSLConstList) const |
| 68 |
{ |
| 69 |
for (const auto &poSubGroup : m_apoSubGroups) |
| 70 |
{ |
| 71 |
if (poSubGroup->GetName() == osName) |
| 72 |
return poSubGroup; |
| 73 |
} |
| 74 |
return nullptr; |
| 75 |
} |
| 76 |
|
| 77 |
std::vector<std::string> |
| 78 |
OGRFileGDBGroup::GetVectorLayerNames(CSLConstList) const |
| 79 |
{ |
| 80 |
std::vector<std::string> ret; |
| 81 |
for (const auto &poLayer : m_apoLayers) |
| 82 |
ret.emplace_back(poLayer->GetName()); |
| 83 |
return ret; |
| 84 |
} |
| 85 |
|
| 86 |
OGRLayer *OGRFileGDBGroup::OpenVectorLayer(const std::string &osName, |
| 87 |
CSLConstList) const |
| 88 |
{ |
| 89 |
for (const auto &poLayer : m_apoLayers) |
| 90 |
{ |
| 91 |
if (poLayer->GetName() == osName) |
| 92 |
return poLayer; |
| 93 |
} |
| 94 |
return nullptr; |
| 95 |
} |
| 96 |
|
| 97 |
/************************************************************************/ |
| 98 |
/* FGdbDataSource() */ |
| 99 |
/************************************************************************/ |
| 100 |
|
| 101 |
FGdbDataSource::FGdbDataSource(bool bUseDriverMutex, |
| 102 |
FGdbDatabaseConnection *pConnection, |
| 103 |
bool bUseOpenFileGDB) |
| 104 |
: m_bUseDriverMutex(bUseDriverMutex), m_pConnection(pConnection), |
| 105 |
m_pGeodatabase(nullptr), m_poOpenFileGDBDrv(nullptr), |
| 106 |
m_bUseOpenFileGDB(bUseOpenFileGDB) |
| 107 |
{ |
| 108 |
SetDescription(pConnection->m_osName.c_str()); |
| 109 |
poDriver = GDALDriver::FromHandle(GDALGetDriverByName("FileGDB")); |
| 110 |
} |
| 111 |
|
| 112 |
/************************************************************************/ |
| 113 |
/* ~FGdbDataSource() */ |
| 114 |
/************************************************************************/ |
| 115 |
|
| 116 |
FGdbDataSource::~FGdbDataSource() |
| 117 |
{ |
| 118 |
CPLMutexHolderOptionalLockD(m_bUseDriverMutex ? FGdbDriver::hMutex |
| 119 |
: nullptr); |
| 120 |
|
| 121 |
// CloseInternal(); |
| 122 |
size_t count = m_layers.size(); |
| 123 |
for (size_t i = 0; i < count; ++i) |
| 124 |
{ |
| 125 |
if (FGdbLayer *poFgdbLayer = dynamic_cast<FGdbLayer *>(m_layers[i])) |
| 126 |
{ |
| 127 |
poFgdbLayer->CloseGDBObjects(); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
if (m_bUseDriverMutex) |
| 132 |
FGdbDriver::Release(m_osPublicName); |
| 133 |
|
| 134 |
// size_t count = m_layers.size(); |
| 135 |
for (size_t i = 0; i < count; ++i) |
| 136 |
{ |
| 137 |
// only delete layers coming from this driver -- the tables opened by |
| 138 |
// the OpenFileGDB driver will be deleted when we close the OpenFileGDB |
| 139 |
// datasource |
| 140 |
if (FGdbLayer *poFgdbLayer = dynamic_cast<FGdbLayer *>(m_layers[i])) |
| 141 |
{ |
| 142 |
delete poFgdbLayer; |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/************************************************************************/ |
| 148 |
/* Open() */ |
| 149 |
/************************************************************************/ |
| 150 |
|
| 151 |
int FGdbDataSource::Open(const char *pszNewName, int /* bUpdate */, |
| 152 |
const char *pszPublicName) |
| 153 |
{ |
| 154 |
m_osFSName = pszNewName; |
| 155 |
m_osPublicName = (pszPublicName) ? pszPublicName : pszNewName; |
| 156 |
m_pGeodatabase = m_pConnection->GetGDB(); |
| 157 |
m_poOpenFileGDBDrv = (GDALDriver *)GDALGetDriverByName("OpenFileGDB"); |
| 158 |
|
| 159 |
return LoadLayers(L"\\"); |
| 160 |
} |
| 161 |
|
| 162 |
/************************************************************************/ |
| 163 |
/* CloseInternal() */ |
| 164 |
/************************************************************************/ |
| 165 |
|
| 166 |
int FGdbDataSource::CloseInternal(int bCloseGeodatabase) |
| 167 |
{ |
| 168 |
size_t count = m_layers.size(); |
| 169 |
for (size_t i = 0; i < count; ++i) |
| 170 |
{ |
| 171 |
if (FGdbLayer *poFgdbLayer = dynamic_cast<FGdbLayer *>(m_layers[i])) |
| 172 |
{ |
| 173 |
poFgdbLayer->CloseGDBObjects(); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
if (m_pConnection && bCloseGeodatabase) |
| 178 |
m_pConnection->CloseGeodatabase(); |
| 179 |
m_pGeodatabase = nullptr; |
| 180 |
return true; |
| 181 |
} |
| 182 |
|
| 183 |
/************************************************************************/ |
| 184 |
/* OpenFGDBTables() */ |
| 185 |
/************************************************************************/ |
| 186 |
|
| 187 |
bool FGdbDataSource::OpenFGDBTables(OGRFileGDBGroup *group, |
| 188 |
const std::wstring &type, |
| 189 |
const std::vector<std::wstring> &layers) |
| 190 |
{ |
| 191 |
#ifdef DISPLAY_RELATED_DATASETS |
| 192 |
std::vector<std::wstring> relationshipTypes; |
| 193 |
m_pGeodatabase->GetDatasetRelationshipTypes(relationshipTypes); |
| 194 |
std::vector<std::wstring> datasetTypes; |
| 195 |
m_pGeodatabase->GetDatasetTypes(datasetTypes); |
| 196 |
#endif |
| 197 |
|
| 198 |
fgdbError hr; |
| 199 |
for (unsigned int i = 0; i < layers.size(); i++) |
| 200 |
{ |
| 201 |
Table *pTable = new Table; |
| 202 |
// CPLDebug("FGDB", "Opening %s", WStringToString(layers[i]).c_str()); |
| 203 |
if (FAILED(hr = m_pGeodatabase->OpenTable(layers[i], *pTable))) |
| 204 |
{ |
| 205 |
delete pTable; |
| 206 |
|
| 207 |
std::wstring fgdb_error_desc_w; |
| 208 |
fgdbError er; |
| 209 |
er = FileGDBAPI::ErrorInfo::GetErrorDescription(hr, |
| 210 |
fgdb_error_desc_w); |
| 211 |
const char *pszLikelyReason = |
| 212 |
"Might be due to unsupported spatial reference system. Using " |
| 213 |
"OpenFileGDB driver or FileGDB SDK >= 1.4 should solve it"; |
| 214 |
if (er == S_OK) |
| 215 |
{ |
| 216 |
std::string fgdb_error_desc = |
| 217 |
WStringToString(fgdb_error_desc_w); |
| 218 |
if (fgdb_error_desc == "FileGDB compression is not installed.") |
| 219 |
{ |
| 220 |
pszLikelyReason = "Using FileGDB SDK 1.4 or later should " |
| 221 |
"solve this issue."; |
| 222 |
} |
| 223 |
} |
| 224 |
|
| 225 |
GDBErr(hr, "Error opening " + WStringToString(layers[i]), |
| 226 |
CE_Warning, |
| 227 |
(". Skipping it. " + CPLString(pszLikelyReason)).c_str()); |
| 228 |
continue; |
| 229 |
} |
| 230 |
|
| 231 |
#ifdef DISPLAY_RELATED_DATASETS |
| 232 |
CPLDebug("FGDB", "Finding datasets related to %s", |
| 233 |
WStringToString(layers[i]).c_str()); |
| 234 |
// Slow ! |
| 235 |
for (const auto &relType : relationshipTypes) |
| 236 |
{ |
| 237 |
for (const auto &itemType : datasetTypes) |
| 238 |
{ |
| 239 |
std::vector<std::wstring> relatedDatasets; |
| 240 |
m_pGeodatabase->GetRelatedDatasets(layers[i], relType, itemType, |
| 241 |
relatedDatasets); |
| 242 |
|
| 243 |
std::vector<std::string> relatedDatasetDefs; |
| 244 |
m_pGeodatabase->GetRelatedDatasetDefinitions( |
| 245 |
layers[i], relType, itemType, relatedDatasetDefs); |
| 246 |
|
| 247 |
if (!relatedDatasets.empty() || !relatedDatasetDefs.empty()) |
| 248 |
{ |
| 249 |
CPLDebug("FGDB", "relationshipType: %s", |
| 250 |
WStringToString(relType).c_str()); |
| 251 |
CPLDebug("FGDB", "itemType: %s", |
| 252 |
WStringToString(itemType).c_str()); |
| 253 |
} |
| 254 |
for (const auto &name : relatedDatasets) |
| 255 |
{ |
| 256 |
CPLDebug("FGDB", "Related dataset: %s", |
| 257 |
WStringToString(name).c_str()); |
| 258 |
} |
| 259 |
for (const auto &xml : relatedDatasetDefs) |
| 260 |
{ |
| 261 |
CPLDebug("FGDB", "Related dataset def: %s", xml.c_str()); |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
#endif |
| 266 |
|
| 267 |
FGdbLayer *pLayer = new FGdbLayer(); |
| 268 |
if (!pLayer->Initialize(this, pTable, layers[i], type)) |
| 269 |
{ |
| 270 |
delete pLayer; |
| 271 |
return GDBErr(hr, "Error initializing OGRLayer for " + |
| 272 |
WStringToString(layers[i])); |
| 273 |
} |
| 274 |
|
| 275 |
m_layers.push_back(pLayer); |
| 276 |
group->m_apoLayers.emplace_back(pLayer); |
| 277 |
} |
| 278 |
return true; |
| 279 |
} |
| 280 |
|
| 281 |
/************************************************************************/ |
| 282 |
/* LoadLayers() */ |
| 283 |
/************************************************************************/ |
| 284 |
|
| 285 |
bool FGdbDataSource::LoadLayers(const std::wstring &root) |
| 286 |
{ |
| 287 |
std::vector<wstring> tables; |
| 288 |
std::vector<wstring> featureclasses; |
| 289 |
std::vector<wstring> featuredatasets; |
| 290 |
fgdbError hr; |
| 291 |
|
| 292 |
auto poRootGroup = std::make_shared<OGRFileGDBGroup>(std::string(), ""); |
| 293 |
m_poRootGroup = poRootGroup; |
| 294 |
|
| 295 |
/* Find all the Tables in the root */ |
| 296 |
if (FAILED(hr = m_pGeodatabase->GetChildDatasets(root, L"Table", tables))) |
| 297 |
{ |
| 298 |
return GDBErr(hr, "Error reading Tables in " + WStringToString(root)); |
| 299 |
} |
| 300 |
/* Open the tables we found */ |
| 301 |
if (!tables.empty() && !OpenFGDBTables(poRootGroup.get(), L"Table", tables)) |
| 302 |
return false; |
| 303 |
|
| 304 |
/* Find all the Feature Classes in the root */ |
| 305 |
if (FAILED(hr = m_pGeodatabase->GetChildDatasets(root, L"Feature Class", |
| 306 |
featureclasses))) |
| 307 |
{ |
| 308 |
return GDBErr(hr, "Error reading Feature Classes in " + |
| 309 |
WStringToString(root)); |
| 310 |
} |
| 311 |
/* Open the tables we found */ |
| 312 |
if (!featureclasses.empty() && |
| 313 |
!OpenFGDBTables(poRootGroup.get(), L"Feature Class", featureclasses)) |
| 314 |
return false; |
| 315 |
|
| 316 |
/* Find all the Feature Datasets in the root */ |
| 317 |
if (FAILED(hr = m_pGeodatabase->GetChildDatasets(root, L"Feature Dataset", |
| 318 |
featuredatasets))) |
| 319 |
{ |
| 320 |
return GDBErr(hr, "Error reading Feature Datasets in " + |
| 321 |
WStringToString(root)); |
| 322 |
} |
| 323 |
/* Look for Feature Classes inside the Feature Dataset */ |
| 324 |
for (unsigned int i = 0; i < featuredatasets.size(); i++) |
| 325 |
{ |
| 326 |
const std::string featureDatasetPath( |
| 327 |
WStringToString(featuredatasets[i])); |
| 328 |
if (FAILED(hr = m_pGeodatabase->GetChildDatasets( |
| 329 |
featuredatasets[i], L"Feature Class", featureclasses))) |
| 330 |
{ |
| 331 |
return GDBErr(hr, "Error reading Feature Classes in " + |
| 332 |
featureDatasetPath); |
| 333 |
} |
| 334 |
std::string featureDatasetName(featureDatasetPath); |
| 335 |
if (!featureDatasetName.empty() && featureDatasetPath[0] == '\\') |
| 336 |
featureDatasetName = featureDatasetName.substr(1); |
| 337 |
auto poFeatureDatasetGroup = std::make_shared<OGRFileGDBGroup>( |
| 338 |
poRootGroup->GetName(), featureDatasetName.c_str()); |
| 339 |
poRootGroup->m_apoSubGroups.emplace_back(poFeatureDatasetGroup); |
| 340 |
if (!featureclasses.empty() && |
| 341 |
!OpenFGDBTables(poFeatureDatasetGroup.get(), L"Feature Class", |
| 342 |
featureclasses)) |
| 343 |
return false; |
| 344 |
} |
| 345 |
|
| 346 |
// Look for items which aren't present in the GDB_Items table (see |
| 347 |
// https://github.com/OSGeo/gdal/issues/4463) We do this by browsing the |
| 348 |
// catalog table (using the OpenFileGDB driver) and looking for items we |
| 349 |
// haven't yet found. If we find any, we have no choice but to load these |
| 350 |
// using the OpenFileGDB driver, as the ESRI SDK refuses to acknowledge that |
| 351 |
// they exist (despite ArcGIS itself showing them!) |
| 352 |
int iGDBItems = -1; |
| 353 |
const char *const apszDrivers[2] = {"OpenFileGDB", nullptr}; |
| 354 |
const std::string osSystemCatalog = |
| 355 |
CPLFormFilenameSafe(m_osFSName, "a00000001", "gdbtable"); |
| 356 |
if (m_bUseOpenFileGDB) |
| 357 |
{ |
| 358 |
m_poOpenFileGDBDS.reset(GDALDataset::Open(osSystemCatalog.c_str(), |
| 359 |
GDAL_OF_VECTOR, apszDrivers, |
| 360 |
nullptr, nullptr)); |
| 361 |
} |
| 362 |
if (m_poOpenFileGDBDS != nullptr && |
| 363 |
m_poOpenFileGDBDS->GetLayer(0) != nullptr) |
| 364 |
{ |
| 365 |
OGRLayer *poCatalogLayer = m_poOpenFileGDBDS->GetLayer(0); |
| 366 |
const int iNameIndex = |
| 367 |
poCatalogLayer->GetLayerDefn()->GetFieldIndex("Name"); |
| 368 |
int i = -1; |
| 369 |
for (auto &poFeat : poCatalogLayer) |
| 370 |
{ |
| 371 |
i++; |
| 372 |
const std::string osTableName = |
| 373 |
poFeat->GetFieldAsString(iNameIndex); |
| 374 |
|
| 375 |
if (osTableName.compare("GDB_Items") == 0) |
| 376 |
{ |
| 377 |
iGDBItems = i; |
| 378 |
} |
| 379 |
|
| 380 |
// test if layer is already added |
| 381 |
if (GDALDataset::GetLayerByName(osTableName.c_str())) |
| 382 |
continue; |
| 383 |
|
| 384 |
const CPLString osLCTableName(CPLString(osTableName).tolower()); |
| 385 |
const bool bIsPrivateLayer = osLCTableName.size() >= 4 && |
| 386 |
osLCTableName.substr(0, 4) == "gdb_"; |
| 387 |
if (!bIsPrivateLayer) |
| 388 |
{ |
| 389 |
if (OGRLayer *poLayer = |
| 390 |
m_poOpenFileGDBDS->GetLayerByName(osTableName.c_str())) |
| 391 |
{ |
| 392 |
m_layers.push_back(poLayer); |
| 393 |
poRootGroup->m_apoLayers.emplace_back(poLayer); |
| 394 |
} |
| 395 |
} |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
// Read relationships. Note that we don't use the SDK method for this, as |
| 400 |
// it's too slow! |
| 401 |
if (iGDBItems >= 0) |
| 402 |
{ |
| 403 |
const std::string osGDBItems = CPLFormFilenameSafe( |
| 404 |
m_osFSName, CPLSPrintf("a%08x", iGDBItems + 1), "gdbtable"); |
| 405 |
std::unique_ptr<GDALDataset> poGDBItems(GDALDataset::Open( |
| 406 |
osGDBItems.c_str(), GDAL_OF_VECTOR, apszDrivers, nullptr, nullptr)); |
| 407 |
if (poGDBItems != nullptr) |
| 408 |
{ |
| 409 |
if (OGRLayer *poItemsLayer = poGDBItems->GetLayer(0)) |
| 410 |
{ |
| 411 |
const int iType = poItemsLayer->FindFieldIndex("Type", true); |
| 412 |
const int iDefinition = |
| 413 |
poItemsLayer->FindFieldIndex("Definition", true); |
| 414 |
if (iType < 0 || iDefinition < 0) |
| 415 |
{ |
| 416 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 417 |
"Wrong structure for GDB_Items table"); |
| 418 |
return FALSE; |
| 419 |
} |
| 420 |
|
| 421 |
// Hunt for relationships in GDB_Items table |
| 422 |
for (const auto &poFeature : poItemsLayer) |
| 423 |
{ |
| 424 |
CPLString osType; |
| 425 |
const char *pszType = poFeature->GetFieldAsString(iType); |
| 426 |
if (pszType != nullptr && |
| 427 |
EQUAL(pszType, pszRelationshipTypeUUID)) |
| 428 |
{ |
| 429 |
// relationship item |
| 430 |
auto poRelationship = ParseXMLRelationshipDef( |
| 431 |
poFeature->GetFieldAsString(iDefinition)); |
| 432 |
if (poRelationship) |
| 433 |
{ |
| 434 |
const std::string relationshipName( |
| 435 |
poRelationship->GetName()); |
| 436 |
m_osMapRelationships[relationshipName] = |
| 437 |
std::move(poRelationship); |
| 438 |
} |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
return true; |
| 446 |
} |
| 447 |
|
| 448 |
/************************************************************************/ |
| 449 |
/* TestCapability() */ |
| 450 |
/************************************************************************/ |
| 451 |
|
| 452 |
int FGdbDataSource::TestCapability(const char *pszCap) const |
| 453 |
{ |
| 454 |
if (EQUAL(pszCap, ODsCMeasuredGeometries)) |
| 455 |
return TRUE; |
| 456 |
else if (EQUAL(pszCap, ODsCZGeometries)) |
| 457 |
return TRUE; |
| 458 |
|
| 459 |
return FALSE; |
| 460 |
} |
| 461 |
|
| 462 |
/************************************************************************/ |
| 463 |
/* GetLayer() */ |
| 464 |
/************************************************************************/ |
| 465 |
|
| 466 |
const OGRLayer *FGdbDataSource::GetLayer(int iLayer) const |
| 467 |
{ |
| 468 |
int count = static_cast<int>(m_layers.size()); |
| 469 |
|
| 470 |
if (iLayer < 0 || iLayer >= count) |
| 471 |
return nullptr; |
| 472 |
else |
| 473 |
return m_layers[iLayer]; |
| 474 |
} |
| 475 |
|
| 476 |
/************************************************************************/ |
| 477 |
/* OGRFGdbSingleFeatureLayer */ |
| 478 |
/************************************************************************/ |
| 479 |
|
| 480 |
class OGRFGdbSingleFeatureLayer final : public OGRLayer |
| 481 |
{ |
| 482 |
private: |
| 483 |
char *pszVal; |
| 484 |
OGRFeatureDefn *poFeatureDefn; |
| 485 |
int iNextShapeId; |
| 486 |
|
| 487 |
public: |
| 488 |
OGRFGdbSingleFeatureLayer(const char *pszLayerName, const char *pszVal); |
| 489 |
~OGRFGdbSingleFeatureLayer() override; |
| 490 |
|
| 491 |
void ResetReading() override |
| 492 |
{ |
| 493 |
iNextShapeId = 0; |
| 494 |
} |
| 495 |
|
| 496 |
OGRFeature *GetNextFeature() override; |
| 497 |
|
| 498 |
const OGRFeatureDefn *GetLayerDefn() const override |
| 499 |
{ |
| 500 |
return poFeatureDefn; |
| 501 |
} |
| 502 |
|
| 503 |
int TestCapability(const char *) const override |
| 504 |
{ |
| 505 |
return FALSE; |
| 506 |
} |
| 507 |
}; |
| 508 |
|
| 509 |
/************************************************************************/ |
| 510 |
/* OGRFGdbSingleFeatureLayer() */ |
| 511 |
/************************************************************************/ |
| 512 |
|
| 513 |
OGRFGdbSingleFeatureLayer::OGRFGdbSingleFeatureLayer(const char *pszLayerName, |
| 514 |
const char *pszValIn) |
| 515 |
{ |
| 516 |
poFeatureDefn = new OGRFeatureDefn(pszLayerName); |
| 517 |
SetDescription(poFeatureDefn->GetName()); |
| 518 |
poFeatureDefn->Reference(); |
| 519 |
OGRFieldDefn oField("FIELD_1", OFTString); |
| 520 |
poFeatureDefn->AddFieldDefn(&oField); |
| 521 |
|
| 522 |
iNextShapeId = 0; |
| 523 |
pszVal = pszValIn ? CPLStrdup(pszValIn) : nullptr; |
| 524 |
} |
| 525 |
|
| 526 |
/************************************************************************/ |
| 527 |
/* ~OGRFGdbSingleFeatureLayer() */ |
| 528 |
/************************************************************************/ |
| 529 |
|
| 530 |
OGRFGdbSingleFeatureLayer::~OGRFGdbSingleFeatureLayer() |
| 531 |
{ |
| 532 |
if (poFeatureDefn != nullptr) |
| 533 |
poFeatureDefn->Release(); |
| 534 |
CPLFree(pszVal); |
| 535 |
} |
| 536 |
|
| 537 |
/************************************************************************/ |
| 538 |
/* GetNextFeature() */ |
| 539 |
/************************************************************************/ |
| 540 |
|
| 541 |
OGRFeature *OGRFGdbSingleFeatureLayer::GetNextFeature() |
| 542 |
{ |
| 543 |
if (iNextShapeId != 0) |
| 544 |
return nullptr; |
| 545 |
|
| 546 |
OGRFeature *poFeature = new OGRFeature(poFeatureDefn); |
| 547 |
if (pszVal) |
| 548 |
poFeature->SetField(0, pszVal); |
| 549 |
poFeature->SetFID(iNextShapeId++); |
| 550 |
return poFeature; |
| 551 |
} |
| 552 |
|
| 553 |
/************************************************************************/ |
| 554 |
/* ExecuteSQL() */ |
| 555 |
/************************************************************************/ |
| 556 |
|
| 557 |
OGRLayer *FGdbDataSource::ExecuteSQL(const char *pszSQLCommand, |
| 558 |
OGRGeometry *poSpatialFilter, |
| 559 |
const char *pszDialect) |
| 560 |
|
| 561 |
{ |
| 562 |
if (m_pGeodatabase == nullptr) |
| 563 |
return nullptr; |
| 564 |
|
| 565 |
/* -------------------------------------------------------------------- */ |
| 566 |
/* Use generic implementation for recognized dialects */ |
| 567 |
/* -------------------------------------------------------------------- */ |
| 568 |
if (IsGenericSQLDialect(pszDialect)) |
| 569 |
return GDALDataset::ExecuteSQL(pszSQLCommand, poSpatialFilter, |
| 570 |
pszDialect); |
| 571 |
|
| 572 |
/* -------------------------------------------------------------------- */ |
| 573 |
/* Special case GetLayerDefinition */ |
| 574 |
/* -------------------------------------------------------------------- */ |
| 575 |
if (STARTS_WITH_CI(pszSQLCommand, "GetLayerDefinition ")) |
| 576 |
{ |
| 577 |
FGdbLayer *poLayer = cpl::down_cast<FGdbLayer *>( |
| 578 |
GetLayerByName(pszSQLCommand + strlen("GetLayerDefinition "))); |
| 579 |
if (poLayer) |
| 580 |
{ |
| 581 |
char *pszVal = nullptr; |
| 582 |
poLayer->GetLayerXML(&pszVal); |
| 583 |
OGRLayer *poRet = |
| 584 |
new OGRFGdbSingleFeatureLayer("LayerDefinition", pszVal); |
| 585 |
CPLFree(pszVal); |
| 586 |
return poRet; |
| 587 |
} |
| 588 |
else |
| 589 |
return nullptr; |
| 590 |
} |
| 591 |
|
| 592 |
/* -------------------------------------------------------------------- */ |
| 593 |
/* Special case GetLayerMetadata */ |
| 594 |
/* -------------------------------------------------------------------- */ |
| 595 |
if (STARTS_WITH_CI(pszSQLCommand, "GetLayerMetadata ")) |
| 596 |
{ |
| 597 |
FGdbLayer *poLayer = cpl::down_cast<FGdbLayer *>( |
| 598 |
GetLayerByName(pszSQLCommand + strlen("GetLayerMetadata "))); |
| 599 |
if (poLayer) |
| 600 |
{ |
| 601 |
char *pszVal = nullptr; |
| 602 |
poLayer->GetLayerMetadataXML(&pszVal); |
| 603 |
OGRLayer *poRet = |
| 604 |
new OGRFGdbSingleFeatureLayer("LayerMetadata", pszVal); |
| 605 |
CPLFree(pszVal); |
| 606 |
return poRet; |
| 607 |
} |
| 608 |
else |
| 609 |
return nullptr; |
| 610 |
} |
| 611 |
|
| 612 |
/* -------------------------------------------------------------------- */ |
| 613 |
/* Special case REPACK */ |
| 614 |
/* -------------------------------------------------------------------- */ |
| 615 |
if (EQUAL(pszSQLCommand, "REPACK")) |
| 616 |
{ |
| 617 |
auto hr = m_pGeodatabase->CompactDatabase(); |
| 618 |
if (FAILED(hr)) |
| 619 |
{ |
| 620 |
GDBErr(hr, "CompactDatabase failed"); |
| 621 |
return new OGRFGdbSingleFeatureLayer("result", "false"); |
| 622 |
} |
| 623 |
return new OGRFGdbSingleFeatureLayer("result", "true"); |
| 624 |
} |
| 625 |
|
| 626 |
/* TODO: remove that workaround when the SDK has finally a decent */ |
| 627 |
/* SQL support ! */ |
| 628 |
if (STARTS_WITH_CI(pszSQLCommand, "SELECT ") && pszDialect == nullptr) |
| 629 |
{ |
| 630 |
CPLDebug("FGDB", "Support for SELECT is known to be partially " |
| 631 |
"non-compliant with FileGDB SDK API v1.2.\n" |
| 632 |
"So for now, we use default OGR SQL engine. " |
| 633 |
"Explicitly specify -dialect FileGDB\n" |
| 634 |
"to use the SQL engine from the FileGDB SDK API"); |
| 635 |
OGRLayer *poLayer = |
| 636 |
GDALDataset::ExecuteSQL(pszSQLCommand, poSpatialFilter, pszDialect); |
| 637 |
if (poLayer) |
| 638 |
m_oSetSelectLayers.insert(poLayer); |
| 639 |
return poLayer; |
| 640 |
} |
| 641 |
|
| 642 |
/* -------------------------------------------------------------------- */ |
| 643 |
/* Run the SQL */ |
| 644 |
/* -------------------------------------------------------------------- */ |
| 645 |
EnumRows *pEnumRows = new EnumRows; |
| 646 |
long hr; |
| 647 |
try |
| 648 |
{ |
| 649 |
hr = m_pGeodatabase->ExecuteSQL(StringToWString(pszSQLCommand), true, |
| 650 |
*pEnumRows); |
| 651 |
} |
| 652 |
catch (...) |
| 653 |
{ |
| 654 |
CPLError(CE_Failure, CPLE_AppDefined, |
| 655 |
"Exception occurred at executing '%s'. Application may " |
| 656 |
"become unstable", |
| 657 |
pszSQLCommand); |
| 658 |
delete pEnumRows; |
| 659 |
return nullptr; |
| 660 |
} |
| 661 |
|
| 662 |
if (FAILED(hr)) |
| 663 |
{ |
| 664 |
GDBErr(hr, CPLSPrintf("Failed at executing '%s'", pszSQLCommand)); |
| 665 |
delete pEnumRows; |
| 666 |
return nullptr; |
| 667 |
} |
| 668 |
|
| 669 |
if (STARTS_WITH_CI(pszSQLCommand, "SELECT ")) |
| 670 |
{ |
| 671 |
OGRLayer *poLayer = new FGdbResultLayer(this, pszSQLCommand, pEnumRows); |
| 672 |
m_oSetSelectLayers.insert(poLayer); |
| 673 |
return poLayer; |
| 674 |
} |
| 675 |
else |
| 676 |
{ |
| 677 |
delete pEnumRows; |
| 678 |
return nullptr; |
| 679 |
} |
| 680 |
} |
| 681 |
|
| 682 |
/************************************************************************/ |
| 683 |
/* ReleaseResultSet() */ |
| 684 |
/************************************************************************/ |
| 685 |
|
| 686 |
void FGdbDataSource::ReleaseResultSet(OGRLayer *poResultsSet) |
| 687 |
{ |
| 688 |
if (poResultsSet) |
| 689 |
m_oSetSelectLayers.erase(poResultsSet); |
| 690 |
delete poResultsSet; |
| 691 |
} |
| 692 |
|
| 693 |
/************************************************************************/ |
| 694 |
/* GetFieldDomain() */ |
| 695 |
/************************************************************************/ |
| 696 |
|
| 697 |
const OGRFieldDomain * |
| 698 |
FGdbDataSource::GetFieldDomain(const std::string &name) const |
| 699 |
{ |
| 700 |
const auto baseRet = GDALDataset::GetFieldDomain(name); |
| 701 |
if (baseRet) |
| 702 |
return baseRet; |
| 703 |
|
| 704 |
std::string domainDef; |
| 705 |
const auto hr = |
| 706 |
m_pGeodatabase->GetDomainDefinition(StringToWString(name), domainDef); |
| 707 |
if (FAILED(hr)) |
| 708 |
{ |
| 709 |
// GDBErr(hr, "Failed in GetDomainDefinition()"); |
| 710 |
return nullptr; |
| 711 |
} |
| 712 |
|
| 713 |
auto poDomain = ParseXMLFieldDomainDef(domainDef); |
| 714 |
if (!poDomain) |
| 715 |
return nullptr; |
| 716 |
const std::string domainName(poDomain->GetName()); |
| 717 |
m_oMapFieldDomains[domainName] = std::move(poDomain); |
| 718 |
return GDALDataset::GetFieldDomain(name); |
| 719 |
} |
| 720 |
|
| 721 |
/************************************************************************/ |
| 722 |
/* GetFieldDomainNames() */ |
| 723 |
/************************************************************************/ |
| 724 |
|
| 725 |
std::vector<std::string> FGdbDataSource::GetFieldDomainNames(CSLConstList) const |
| 726 |
{ |
| 727 |
std::vector<std::wstring> oDomainNamesWList; |
| 728 |
const auto hr = m_pGeodatabase->GetDomains(oDomainNamesWList); |
| 729 |
if (FAILED(hr)) |
| 730 |
{ |
| 731 |
GDBErr(hr, "Failed in GetDomains()"); |
| 732 |
return std::vector<std::string>(); |
| 733 |
} |
| 734 |
|
| 735 |
std::vector<std::string> oDomainNamesList; |
| 736 |
oDomainNamesList.reserve(oDomainNamesWList.size()); |
| 737 |
for (const auto &osNameW : oDomainNamesWList) |
| 738 |
{ |
| 739 |
oDomainNamesList.emplace_back(WStringToString(osNameW)); |
| 740 |
} |
| 741 |
return oDomainNamesList; |
| 742 |
} |
| 743 |
|
| 744 |
/************************************************************************/ |
| 745 |
/* GetRelationshipNames() */ |
| 746 |
/************************************************************************/ |
| 747 |
|
| 748 |
std::vector<std::string> |
| 749 |
FGdbDataSource::GetRelationshipNames(CPL_UNUSED CSLConstList papszOptions) const |
| 750 |
|
| 751 |
{ |
| 752 |
std::vector<std::string> oasNames; |
| 753 |
oasNames.reserve(m_osMapRelationships.size()); |
| 754 |
for (auto it = m_osMapRelationships.begin(); |
| 755 |
it != m_osMapRelationships.end(); ++it) |
| 756 |
{ |
| 757 |
oasNames.emplace_back(it->first); |
| 758 |
} |
| 759 |
return oasNames; |
| 760 |
} |
| 761 |
|
| 762 |
/************************************************************************/ |
| 763 |
/* GetRelationship() */ |
| 764 |
/************************************************************************/ |
| 765 |
|
| 766 |
const GDALRelationship * |
| 767 |
FGdbDataSource::GetRelationship(const std::string &name) const |
| 768 |
|
| 769 |
{ |
| 770 |
auto it = m_osMapRelationships.find(name); |
| 771 |
if (it == m_osMapRelationships.end()) |
| 772 |
return nullptr; |
| 773 |
|
| 774 |
return it->second.get(); |
| 775 |
} |