?? ogrociloaderlayer.cpp
字號(hào):
/****************************************************************************** * $Id: ogrociloaderlayer.cpp 10646 2007-01-18 02:38:10Z warmerdam $ * * Project: Oracle Spatial Driver * Purpose: Implementation of the OGROCILoaderLayer class. This implements * an output only OGRLayer for writing an SQL*Loader file. * Author: Frank Warmerdam, warmerdam@pobox.com * ****************************************************************************** * Copyright (c) 2003, Frank Warmerdam <warmerdam@pobox.com> * * 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_oci.h"#include "cpl_conv.h"#include "cpl_string.h"CPL_CVSID("$Id: ogrociloaderlayer.cpp 10646 2007-01-18 02:38:10Z warmerdam $");/************************************************************************//* OGROCILoaderLayer() *//************************************************************************/OGROCILoaderLayer::OGROCILoaderLayer( OGROCIDataSource *poDSIn, const char * pszTableName, const char * pszGeomColIn, int nSRIDIn, const char *pszLoaderFilenameIn ){ poDS = poDSIn; iNextFIDToWrite = 1; bTruncationReported = FALSE; bHeaderWritten = FALSE; nLDRMode = LDRM_UNKNOWN; poFeatureDefn = new OGRFeatureDefn( pszTableName ); poFeatureDefn->Reference(); pszGeomName = CPLStrdup( pszGeomColIn ); pszFIDName = (char*)CPLGetConfigOption( "OCI_FID", "OGR_FID" ); nSRID = nSRIDIn; poSRS = poDSIn->FetchSRS( nSRID ); if( poSRS != NULL ) poSRS->Reference();/* -------------------------------------------------------------------- *//* Open the loader file. *//* -------------------------------------------------------------------- */ pszLoaderFilename = CPLStrdup( pszLoaderFilenameIn ); fpData = NULL; fpLoader = VSIFOpen( pszLoaderFilename, "wt" ); if( fpLoader == NULL ) { CPLError( CE_Failure, CPLE_OpenFailed, "Failed to open SQL*Loader control file:%s", pszLoaderFilename ); return; }}/************************************************************************//* ~OGROCILoaderLayer() *//************************************************************************/OGROCILoaderLayer::~OGROCILoaderLayer(){ if( fpData != NULL ) VSIFClose( fpData ); if( fpLoader != NULL ) { VSIFClose( fpLoader ); FinalizeNewLayer(); } CPLFree( pszLoaderFilename ); if( poSRS != NULL && poSRS->Dereference() == 0 ) delete poSRS;}/************************************************************************//* WriteLoaderHeader() *//************************************************************************/void OGROCILoaderLayer::WriteLoaderHeader(){ if( bHeaderWritten ) return;/* -------------------------------------------------------------------- *//* Determine name of geometry column to use. *//* -------------------------------------------------------------------- */ const char *pszGeometryName = CSLFetchNameValue( papszOptions, "GEOMETRY_NAME" ); if( pszGeometryName == NULL ) pszGeometryName = "ORA_GEOMETRY";/* -------------------------------------------------------------------- *//* Dermine our operation mode. *//* -------------------------------------------------------------------- */ const char *pszLDRMode = CSLFetchNameValue( papszOptions, "LOADER_MODE" ); if( pszLDRMode != NULL && EQUAL(pszLDRMode,"VARIABLE") ) nLDRMode = LDRM_VARIABLE; else if( pszLDRMode != NULL && EQUAL(pszLDRMode,"BINARY") ) nLDRMode = LDRM_BINARY; else nLDRMode = LDRM_STREAM;/* -------------------------------------------------------------------- *//* Write loader header info. *//* -------------------------------------------------------------------- */ VSIFPrintf( fpLoader, "LOAD DATA\n" ); if( nLDRMode == LDRM_STREAM ) { VSIFPrintf( fpLoader, "INFILE *\n" ); VSIFPrintf( fpLoader, "CONTINUEIF NEXT(1:1) = '#'\n" ); } else if( nLDRMode == LDRM_VARIABLE ) { const char *pszDataFilename = CPLResetExtension( pszLoaderFilename, "dat" ); fpData = VSIFOpen( pszDataFilename, "wb" ); if( fpData == NULL ) { CPLError( CE_Failure, CPLE_OpenFailed, "Unable to open data output file `%s'.", pszDataFilename ); return; } VSIFPrintf( fpLoader, "INFILE %s \"var 8\"\n", pszDataFilename ); } const char *pszExpectedFIDName = CPLGetConfigOption( "OCI_FID", "OGR_FID" ); VSIFPrintf( fpLoader, "INTO TABLE \"%s\" REPLACE\n", poFeatureDefn->GetName() ); VSIFPrintf( fpLoader, "FIELDS TERMINATED BY '|'\n" ); VSIFPrintf( fpLoader, "TRAILING NULLCOLS (\n" ); VSIFPrintf( fpLoader, " %s INTEGER EXTERNAL,\n", pszExpectedFIDName ); VSIFPrintf( fpLoader, " %s COLUMN OBJECT (\n", pszGeometryName ); VSIFPrintf( fpLoader, " SDO_GTYPE INTEGER EXTERNAL,\n" ); VSIFPrintf( fpLoader, " SDO_ELEM_INFO VARRAY TERMINATED BY '|/'\n" ); VSIFPrintf( fpLoader, " (elements INTEGER EXTERNAL),\n" ); VSIFPrintf( fpLoader, " SDO_ORDINATES VARRAY TERMINATED BY '|/'\n" ); VSIFPrintf( fpLoader, " (ordinates FLOAT EXTERNAL)\n" ); VSIFPrintf( fpLoader, " ),\n" );/* -------------------------------------------------------------------- *//* Write user field schema. *//* -------------------------------------------------------------------- */ int iField; for( iField = 0; iField < poFeatureDefn->GetFieldCount(); iField++ ) { OGRFieldDefn *poFldDefn = poFeatureDefn->GetFieldDefn(iField); if( poFldDefn->GetType() == OFTInteger ) { VSIFPrintf( fpLoader, " \"%s\" INTEGER EXTERNAL", poFldDefn->GetNameRef() ); } else if( poFldDefn->GetType() == OFTReal ) { VSIFPrintf( fpLoader, " \"%s\" FLOAT EXTERNAL", poFldDefn->GetNameRef() ); } else if( poFldDefn->GetType() == OFTString ) { VSIFPrintf( fpLoader, " \"%s\" VARCHARC(4)", poFldDefn->GetNameRef() ); } else { VSIFPrintf( fpLoader, " \"%s\" VARCHARC(4)", poFldDefn->GetNameRef() ); } if( iField < poFeatureDefn->GetFieldCount() - 1 ) VSIFPrintf( fpLoader, "," ); VSIFPrintf( fpLoader, "\n" ); } VSIFPrintf( fpLoader, ")\n" ); if( nLDRMode == LDRM_STREAM ) VSIFPrintf( fpLoader, "begindata\n" ); bHeaderWritten = TRUE;}/************************************************************************//* GetNextFeature() *//* *//* We override the next feature method because we know that we *//* implement the attribute query within the statement and so we *//* don't have to test here. Eventually the spatial query will *//* be fully tested within the statement as well. *//************************************************************************/OGRFeature *OGROCILoaderLayer::GetNextFeature(){ CPLError( CE_Failure, CPLE_NotSupported, "GetNextFeature() not supported for an OGROCILoaderLayer." ); return NULL;}/************************************************************************//* ResetReading() *//************************************************************************/void OGROCILoaderLayer::ResetReading(){ OGROCILayer::ResetReading();}/************************************************************************//* WriteFeatureStreamMode() *//************************************************************************/OGRErr OGROCILoaderLayer::WriteFeatureStreamMode( OGRFeature *poFeature ){/* -------------------------------------------------------------------- *//* Write the FID. *//* -------------------------------------------------------------------- */ VSIFPrintf( fpLoader, " %d|", poFeature->GetFID() );/* -------------------------------------------------------------------- *//* Set the geometry *//* -------------------------------------------------------------------- */ int nLineLen = 0; if( poFeature->GetGeometryRef() != NULL) { char szSRID[128]; int nGType; int i; if( nSRID == -1 ) strcpy( szSRID, "NULL" ); else sprintf( szSRID, "%d", nSRID ); if( TranslateToSDOGeometry( poFeature->GetGeometryRef(), &nGType ) == OGRERR_NONE ) { VSIFPrintf( fpLoader, "%d|", nGType ); for( i = 0; i < nElemInfoCount; i++ ) { VSIFPrintf( fpLoader, "%d|", panElemInfo[i] ); if( ++nLineLen > 18 && i < nElemInfoCount-1 ) { VSIFPrintf( fpLoader, "\n#" ); nLineLen = 0; } } VSIFPrintf( fpLoader, "/" ); for( i = 0; i < nOrdinalCount; i++ ) { VSIFPrintf( fpLoader, "%.16g|", padfOrdinals[i] ); if( ++nLineLen > 6 && i < nOrdinalCount-1 ) { VSIFPrintf( fpLoader, "\n#" ); nLineLen = 0; } } VSIFPrintf( fpLoader, "/" ); } else { VSIFPrintf( fpLoader, "0|/|/" ); } } else { VSIFPrintf( fpLoader, "0|/|/" ); }/* -------------------------------------------------------------------- *//* Set the other fields. *//* -------------------------------------------------------------------- */ int i; nLineLen = 0; VSIFPrintf( fpLoader, "\n#" ); for( i = 0; i < poFeatureDefn->GetFieldCount(); i++ ) { OGRFieldDefn *poFldDefn = poFeatureDefn->GetFieldDefn(i); if( !poFeature->IsFieldSet( i ) ) { if( poFldDefn->GetType() != OFTInteger && poFldDefn->GetType() != OFTReal ) VSIFPrintf( fpLoader, "%04d", 0 ); continue; } const char *pszStrValue = poFeature->GetFieldAsString(i); if( nLineLen > 70 ) { VSIFPrintf( fpLoader, "\n#" ); nLineLen = 0; } nLineLen += strlen(pszStrValue);
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -