/****************************************************************************** * * Project: OpenGIS Simple Features Reference Implementation * Purpose: Implements Open FileGDB OGR driver. * Author: Even Rouault, * ****************************************************************************** * Copyright (c) 2014, Even Rouault * * SPDX-License-Identifier: MIT ****************************************************************************/ #include "ogrsf_frmts.h" #include "gdalplugindriverproxy.h" #include "ogropenfilegdbdrivercore.h" #define ENDS_WITH(str, strLen, end) \ (strLen >= strlen(end) && EQUAL(str + strLen - strlen(end), end)) /************************************************************************/ /* OGROpenFileGDBDriverIdentify() */ /************************************************************************/ GDALIdentifyEnum OGROpenFileGDBDriverIdentify(GDALOpenInfo *poOpenInfo, const char *&pszFilename) { if (STARTS_WITH(pszFilename, "OpenFileGDB:")) return GDAL_IDENTIFY_TRUE; // First check if we have to do any work. size_t nLen = strlen(pszFilename); if (ENDS_WITH(pszFilename, nLen, ".gdb") || ENDS_WITH(pszFilename, nLen, ".gdb/")) { // Check that the filename is really a directory, to avoid confusion // with Garmin MapSource - gdb format which can be a problem when the // driver is loaded as a plugin, and loaded before the GPSBabel driver // (http://trac.osgeo.org/osgeo4w/ticket/245) if (STARTS_WITH(pszFilename, "/vsicurl/https://github.com/") || !poOpenInfo->bStatOK || !poOpenInfo->bIsDirectory) { // In case we do not manage to list the directory, try to stat one // file. VSIStatBufL stat; if (!(STARTS_WITH(pszFilename, "/vsicurl/") && VSIStatL( CPLFormFilenameSafe(pszFilename, "a00000001", "gdbtable") .c_str(), &stat) == 0)) { return GDAL_IDENTIFY_FALSE; } } else if (poOpenInfo->nOpenFlags == GDAL_OF_RASTER && (STARTS_WITH(pszFilename, "/vsimem/") || !STARTS_WITH(pszFilename, "/vsi"))) { // If asked to identify only a raster dataset, and this is a // local dataset, query the file list to find extensions that // are used by raster layers. constexpr int MAX_FILE_COUNT = 1000; CPLStringList aosFiles(VSIReadDirEx(pszFilename, MAX_FILE_COUNT)); if (!aosFiles.empty() && aosFiles.size() < MAX_FILE_COUNT) { bool bBandIndexFound = false; bool bBlkKeyIndexFound = false; bool bColIndexFound = false; bool bRowIndexFound = false; for (int i = 0; i < aosFiles.size(); ++i) { if (strstr(aosFiles[i], ".band_index.atx")) bBandIndexFound = true; else if (strstr(aosFiles[i], ".blk_key_index.atx")) bBlkKeyIndexFound = true; else if (strstr(aosFiles[i], ".col_index.atx")) bColIndexFound = true; else if (strstr(aosFiles[i], ".row_index.atx")) bRowIndexFound = true; } return bBandIndexFound && bBlkKeyIndexFound && bColIndexFound && bRowIndexFound ? GDAL_IDENTIFY_TRUE : GDAL_IDENTIFY_FALSE; } } return GDAL_IDENTIFY_TRUE; } /* We also accept zipped GDB */ else if (ENDS_WITH(pszFilename, nLen, ".gdb.zip") || ENDS_WITH(pszFilename, nLen, ".gdb.tar") || /* Canvec GBs */ (ENDS_WITH(pszFilename, nLen, ".zip") && (strstr(pszFilename, "_gdb") != nullptr || strstr(pszFilename, "_GDB") != nullptr))) { return GDAL_IDENTIFY_TRUE; } /* We also accept tables themselves */ else if (ENDS_WITH(pszFilename, nLen, ".gdbtable")) { return GDAL_IDENTIFY_TRUE; } #ifdef DEBUG /* For AFL, so that .cur_input is detected as the archive filename */ else if (EQUAL(CPLGetFilename(pszFilename), ".cur_input")) { // This file may be recognized or not by this driver, // but there were not enough elements to judge. return GDAL_IDENTIFY_UNKNOWN; } #endif else if (STARTS_WITH(pszFilename, "/vsizip/") && poOpenInfo->bIsDirectory) { VSIStatBufL stat; return VSIStatL( CPLFormFilenameSafe(pszFilename, "a00000001", "gdbtable") .c_str(), &stat) == 0 ? GDAL_IDENTIFY_TRUE : GDAL_IDENTIFY_FALSE; } else if (EQUAL(pszFilename, ".")) { GDALIdentifyEnum eRet = GDAL_IDENTIFY_FALSE; char *pszCurrentDir = CPLGetCurrentDir(); if (pszCurrentDir) { const char *pszTmp = pszCurrentDir; eRet = OGROpenFileGDBDriverIdentify(poOpenInfo, pszTmp); CPLFree(pszCurrentDir); } return eRet; } else { return GDAL_IDENTIFY_FALSE; } } static int OGROpenFileGDBDriverIdentify(GDALOpenInfo *poOpenInfo) { const char *pszFilename = poOpenInfo->pszFilename; return OGROpenFileGDBDriverIdentify(poOpenInfo, pszFilename); } /************************************************************************/ /* OGROpenFileGDBDriverSetCommonMetadata() */ /************************************************************************/ void OGROpenFileGDBDriverSetCommonMetadata(GDALDriver *poDriver) { poDriver->SetDescription(DRIVER_NAME); poDriver->SetMetadataItem(GDAL_DMD_LONGNAME, "ESRI FileGeodatabase (using OpenFileGDB)"); poDriver->SetMetadataItem(GDAL_DMD_EXTENSION, "gdb"); poDriver->SetMetadataItem(GDAL_DMD_HELPTOPIC, "drivers/vector/openfilegdb.html"); poDriver->SetMetadataItem(GDAL_DCAP_RASTER, "YES"); poDriver->SetMetadataItem(GDAL_DCAP_VECTOR, "YES"); poDriver->SetMetadataItem(GDAL_DCAP_CREATE_LAYER, "YES"); poDriver->SetMetadataItem(GDAL_DCAP_DELETE_LAYER, "YES"); poDriver->SetMetadataItem(GDAL_DCAP_CREATE_FIELD, "YES"); poDriver->SetMetadataItem(GDAL_DCAP_DELETE_FIELD, "YES"); poDriver->SetMetadataItem(GDAL_DCAP_VIRTUALIO, "YES"); poDriver->SetMetadataItem(GDAL_DCAP_CURVE_GEOMETRIES, "YES"); poDriver->SetMetadataItem(GDAL_DCAP_MEASURED_GEOMETRIES, "YES"); poDriver->SetMetadataItem(GDAL_DCAP_Z_GEOMETRIES, "YES"); poDriver->SetMetadataItem(GDAL_DMD_GEOMETRY_FLAGS, "EquatesMultiAndSingleLineStringDuringWrite " "EquatesMultiAndSinglePolygonDuringWrite"); poDriver->SetMetadataItem(GDAL_DMD_CREATIONFIELDDATATYPES, "Integer Real String Date DateTime Binary " "Integer64 Date Time"); poDriver->SetMetadataItem(GDAL_DMD_CREATIONFIELDDATASUBTYPES, "Int16 Float32"); poDriver->SetMetadataItem(GDAL_DMD_CREATION_FIELD_DEFN_FLAGS, "Nullable Default " "AlternativeName Domain"); poDriver->SetMetadataItem( GDAL_DMD_ALTER_FIELD_DEFN_FLAGS, "Name Type Nullable Default Domain AlternativeName"); // see https://support.esri.com/en/technical-article/000010906 poDriver->SetMetadataItem( GDAL_DMD_ILLEGAL_FIELD_NAMES, "ADD ALTER AND BETWEEN BY COLUMN CREATE DELETE DROP EXISTS FOR FROM " "GROUP IN INSERT INTO IS LIKE NOT NULL OR ORDER SELECT SET TABLE " "UPDATE VALUES WHERE"); poDriver->SetMetadataItem(GDAL_DCAP_NOTNULL_FIELDS, "YES"); poDriver->SetMetadataItem(GDAL_DCAP_DEFAULT_FIELDS, "YES"); poDriver->SetMetadataItem(GDAL_DCAP_NOTNULL_GEOMFIELDS, "YES"); poDriver->SetMetadataItem(GDAL_DCAP_MULTIPLE_VECTOR_LAYERS, "YES"); poDriver->SetMetadataItem(GDAL_DCAP_FIELD_DOMAINS, "YES"); poDriver->SetMetadataItem(GDAL_DCAP_RENAME_LAYERS, "YES"); poDriver->SetMetadataItem(GDAL_DCAP_RELATIONSHIPS, "YES"); poDriver->SetMetadataItem(GDAL_DCAP_CREATE_RELATIONSHIP, "YES"); poDriver->SetMetadataItem(GDAL_DCAP_DELETE_RELATIONSHIP, "YES"); poDriver->SetMetadataItem(GDAL_DCAP_UPDATE_RELATIONSHIP, "YES"); poDriver->SetMetadataItem(GDAL_DMD_RELATIONSHIP_FLAGS, "OneToOne OneToMany ManyToMany Composite " "Association ForwardPathLabel BackwardPathLabel"); poDriver->SetMetadataItem(GDAL_DMD_RELATIONSHIP_RELATED_TABLE_TYPES, "features media"); poDriver->SetMetadataItem(GDAL_DMD_SUPPORTED_SQL_DIALECTS, "OGRSQL SQLITE"); poDriver->SetMetadataItem(GDAL_DMD_CREATION_FIELD_DOMAIN_TYPES, "Coded Range"); poDriver->SetMetadataItem(GDAL_DMD_ALTER_GEOM_FIELD_DEFN_FLAGS, "Name SRS"); poDriver->SetMetadataItem(GDAL_DCAP_HONOR_GEOM_COORDINATE_PRECISION, "YES"); poDriver->SetMetadataItem( GDAL_DMD_OPENOPTIONLIST, "" " " " "); poDriver->SetMetadataItem( GDAL_DS_LAYER_CREATIONOPTIONLIST, "" " " " " " "); poDriver->SetMetadataItem(GDAL_DCAP_UPDATE, "YES"); poDriver->SetMetadataItem(GDAL_DMD_UPDATE_ITEMS, "Features"); // Setting to another value than the default one doesn't really work // with the SDK // Option name='AREA_FIELD_NAME' type='string' description='Name of // the column that contains the geometry area' default='Shape_Area' // Option name='length_field_name' type='string' description='Name of // the column that contains the geometry length' // default='Shape_Length' poDriver->pfnIdentify = OGROpenFileGDBDriverIdentify; poDriver->SetMetadataItem(GDAL_DCAP_OPEN, "YES"); poDriver->SetMetadataItem(GDAL_DCAP_CREATE, "YES"); poDriver->DeclareAlgorithm({"repack"}); } /************************************************************************/ /* DeclareDeferredOGROpenFileGDBPlugin() */ /************************************************************************/ #ifdef PLUGIN_FILENAME void DeclareDeferredOGROpenFileGDBPlugin() { if (GDALGetDriverByName(DRIVER_NAME) != nullptr) { return; } auto poDriver = new GDALPluginDriverProxy(PLUGIN_FILENAME); #ifdef PLUGIN_INSTALLATION_MESSAGE poDriver->SetMetadataItem(GDAL_DMD_PLUGIN_INSTALLATION_MESSAGE, PLUGIN_INSTALLATION_MESSAGE); #endif OGROpenFileGDBDriverSetCommonMetadata(poDriver); GetGDALDriverManager()->DeclareDeferredPluginDriver(poDriver); } #endif