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
|
/******************************************************************************
* $Id: FGdbDriver.cpp 26310 2013-08-13 19:54:26Z rouault $
*
* Project: OpenGIS Simple Features Reference Implementation
* Purpose: Implements FileGDB OGR driver.
* Author: Ragi Yaser Burhum, ragi@burhum.com
* Paul Ramsey, pramsey at cleverelephant.ca
*
******************************************************************************
* Copyright (c) 2010, Ragi Yaser Burhum
* Copyright (c) 2011, Paul Ramsey <pramsey at cleverelephant.ca>
*
* 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 "ogr_fgdb.h"
#include "cpl_conv.h"
#include "FGdbUtils.h"
CPL_CVSID("$Id: FGdbDriver.cpp 26310 2013-08-13 19:54:26Z rouault $");
extern "C" void RegisterOGRFileGDB();
/************************************************************************/
/* FGdbDriver() */
/************************************************************************/
FGdbDriver::FGdbDriver():
OGRSFDriver()
{
}
/************************************************************************/
/* ~FGdbDriver() */
/************************************************************************/
FGdbDriver::~FGdbDriver()
{
}
/************************************************************************/
/* GetName() */
/************************************************************************/
const char *FGdbDriver::GetName()
{
return "FileGDB";
}
/************************************************************************/
/* Open() */
/************************************************************************/
OGRDataSource *FGdbDriver::Open( const char* pszFilename, int bUpdate )
{
// First check if we have to do any work.
int nLen = strlen(pszFilename);
if(! ((nLen >= 4 && EQUAL(pszFilename + nLen - 4, ".gdb")) ||
(nLen >= 5 && EQUAL(pszFilename + nLen - 5, ".gdb/"))) )
return NULL;
long hr;
/* Check that the filename is really a directory, to avoid confusion with */
/* Garmin MapSource - gdb format which can be a problem when the FileGDB */
/* driver is loaded as a plugin, and loaded before the GPSBabel driver */
/* (http://trac.osgeo.org/osgeo4w/ticket/245) */
VSIStatBuf stat;
if( CPLStat( pszFilename, &stat ) != 0 || !VSI_ISDIR(stat.st_mode) )
{
return NULL;
}
Geodatabase* pGeoDatabase = NULL;
FGdbDatabaseConnection* pConnection = oMapConnections[pszFilename];
if( pConnection != NULL )
{
pGeoDatabase = pConnection->m_pGeodatabase;
pConnection->m_nRefCount ++;
CPLDebug("FileGDB", "ref_count of %s = %d now", pszFilename,
pConnection->m_nRefCount);
}
else
{
pGeoDatabase = new Geodatabase;
hr = ::OpenGeodatabase(StringToWString(pszFilename), *pGeoDatabase);
if (FAILED(hr) || pGeoDatabase == NULL)
{
delete pGeoDatabase;
GDBErr(hr, "Failed to open Geodatabase");
return NULL;
}
CPLDebug("FileGDB", "Really opening %s", pszFilename);
oMapConnections[pszFilename] = new FGdbDatabaseConnection(pGeoDatabase);
}
FGdbDataSource* pDS;
pDS = new FGdbDataSource(this);
if(!pDS->Open( pGeoDatabase, pszFilename, bUpdate ) )
{
delete pDS;
return NULL;
}
else
return pDS;
}
/***********************************************************************/
/* CreateDataSource() */
/***********************************************************************/
OGRDataSource* FGdbDriver::CreateDataSource( const char * conn,
char **papszOptions)
{
long hr;
Geodatabase *pGeodatabase;
std::wstring wconn = StringToWString(conn);
int bUpdate = TRUE; // If we're creating, we must be writing.
VSIStatBuf stat;
/* We don't support options yet, so warn if they send us some */
if ( papszOptions )
{
/* TODO: warning, ignoring options */
}
/* Only accept names of form "filename.gdb" and */
/* also .gdb.zip to be able to return FGDB with MapServer OGR output (#4199) */
const char* pszExt = CPLGetExtension(conn);
if ( !(EQUAL(pszExt,"gdb") || EQUAL(pszExt, "zip")) )
{
CPLError( CE_Failure, CPLE_AppDefined,
"FGDB data source name must use 'gdb' extension.\n" );
return NULL;
}
/* Don't try to create on top of something already there */
if( CPLStat( conn, &stat ) == 0 )
{
CPLError( CE_Failure, CPLE_AppDefined,
"%s already exists.\n", conn );
return NULL;
}
/* Try to create the geodatabase */
pGeodatabase = new Geodatabase; // Create on heap so we can store it in the Datasource
hr = CreateGeodatabase(wconn, *pGeodatabase);
/* Handle creation errors */
if ( S_OK != hr )
{
const char *errstr = "Error creating geodatabase (%s).\n";
if ( hr == -2147220653 )
errstr = "File already exists (%s).\n";
delete pGeodatabase;
CPLError( CE_Failure, CPLE_AppDefined, errstr, conn );
return NULL;
}
oMapConnections[conn] = new FGdbDatabaseConnection(pGeodatabase);
/* Ready to embed the Geodatabase in an OGR Datasource */
FGdbDataSource* pDS = new FGdbDataSource(this);
if ( ! pDS->Open(pGeodatabase, conn, bUpdate) )
{
delete pDS;
return NULL;
}
else
return pDS;
}
/***********************************************************************/
/* Release() */
/***********************************************************************/
void FGdbDriver::Release(const char* pszName)
{
FGdbDatabaseConnection* pConnection = oMapConnections[pszName];
if( pConnection != NULL )
{
pConnection->m_nRefCount --;
CPLDebug("FileGDB", "ref_count of %s = %d now", pszName,
pConnection->m_nRefCount);
if( pConnection->m_nRefCount == 0 )
{
CPLDebug("FileGDB", "Really closing %s now", pszName);
::CloseGeodatabase(*(pConnection->m_pGeodatabase));
delete pConnection->m_pGeodatabase;
pConnection->m_pGeodatabase = NULL;
delete pConnection;
oMapConnections.erase(pszName);
}
}
}
/***********************************************************************/
/* TestCapability() */
/***********************************************************************/
int FGdbDriver::TestCapability( const char * pszCap )
{
if (EQUAL(pszCap, ODrCCreateDataSource) )
return TRUE;
else if (EQUAL(pszCap, ODrCDeleteDataSource) )
return TRUE;
return FALSE;
}
/************************************************************************/
/* DeleteDataSource() */
/************************************************************************/
OGRErr FGdbDriver::DeleteDataSource( const char *pszDataSource )
{
std::wstring wstr = StringToWString(pszDataSource);
long hr;
if (S_OK != (hr = ::DeleteGeodatabase(wstr)))
{
GDBErr(hr, "Failed to delete Geodatabase");
return OGRERR_FAILURE;
}
return OGRERR_NONE;
}
/***********************************************************************/
/* RegisterOGRFileGDB() */
/***********************************************************************/
void RegisterOGRFileGDB()
{
if (! GDAL_CHECK_VERSION("OGR FGDB"))
return;
OGRSFDriverRegistrar::GetRegistrar()->RegisterDriver( new FGdbDriver );
}
|