Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce OGRLayer::FindFieldIndex() / OGR_L_FindFieldIndex() to lookup ... #23

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 18 additions & 7 deletions gdal/apps/ogr2ogr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ static TargetLayerInfo* SetupTargetLayer( OGRDataSource *poSrcDS,
int bExplodeCollections,
const char* pszZField,
char **papszFieldMap,
const char* pszWHERE );
const char* pszWHERE,
int bExactFieldNameMatch );

static void FreeTargetLayerInfo(TargetLayerInfo* psInfo);

Expand Down Expand Up @@ -866,6 +867,7 @@ int main( int nArgc, char ** papszArgv )
const char *pszSourceSRSDef = NULL;
OGRSpatialReference *poOutputSRS = NULL;
int bNullifyOutputSRS = FALSE;
int bExactFieldNameMatch = TRUE;
OGRSpatialReference *poSourceSRS = NULL;
char *pszNewLayerName = NULL;
const char *pszWHERE = NULL;
Expand Down Expand Up @@ -992,6 +994,10 @@ int main( int nArgc, char ** papszArgv )
{
bUpdate = TRUE;
}
else if( EQUAL(papszArgv[iArg],"-relaxedFieldNameMatch") )
{
bExactFieldNameMatch = FALSE;
}
else if( EQUAL(papszArgv[iArg],"-fid") )
{
CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
Expand Down Expand Up @@ -1409,7 +1415,7 @@ int main( int nArgc, char ** papszArgv )
}

if (pszFieldMap && bAddMissingFields)
{
{
Usage("if -addfields is specified, -fieldmap cannot be used.");
}

Expand Down Expand Up @@ -1770,7 +1776,8 @@ int main( int nArgc, char ** papszArgv )
bExplodeCollections,
pszZField,
papszFieldMap,
pszWHERE );
pszWHERE,
bExactFieldNameMatch );

poPassedLayer->ResetReading();

Expand Down Expand Up @@ -1925,7 +1932,8 @@ int main( int nArgc, char ** papszArgv )
bExplodeCollections,
pszZField,
papszFieldMap,
pszWHERE );
pszWHERE,
bExactFieldNameMatch );

if( psInfo == NULL && !bSkipFailures )
exit(1);
Expand Down Expand Up @@ -2199,7 +2207,8 @@ int main( int nArgc, char ** papszArgv )
bExplodeCollections,
pszZField,
papszFieldMap,
pszWHERE );
pszWHERE,
bExactFieldNameMatch );

poPassedLayer->ResetReading();

Expand Down Expand Up @@ -2319,6 +2328,7 @@ static void Usage(const char* pszAdditionalMsg, int bShort)
" [-wrapdateline][-datelineoffset val]\n"
" [[-simplify tolerance] | [-segmentize max_dist]]\n"
" [-addfields]\n"
" [-relaxedFieldNameMatch]\n"
" [-fieldTypeToString All|(type1[,type2]*)] [-unsetFieldWidth]\n"
" [-fieldmap identity | index1[,index2]*]\n"
" [-splitlistfields] [-maxsubfields val]\n"
Expand Down Expand Up @@ -2483,7 +2493,8 @@ static TargetLayerInfo* SetupTargetLayer( OGRDataSource *poSrcDS,
int bExplodeCollections,
const char* pszZField,
char **papszFieldMap,
const char* pszWHERE )
const char* pszWHERE,
int bExactFieldNameMatch )
{
OGRLayer *poDstLayer;
OGRFeatureDefn *poSrcFDefn;
Expand Down Expand Up @@ -3019,7 +3030,7 @@ static TargetLayerInfo* SetupTargetLayer( OGRDataSource *poSrcDS,
for( iField = 0; iField < nSrcFieldCount; iField++ )
{
OGRFieldDefn* poSrcFieldDefn = poSrcFDefn->GetFieldDefn(iField);
int iDstField = poDstFDefn->GetFieldIndex(poSrcFieldDefn->GetNameRef());
int iDstField = poDstLayer->FindFieldIndex(poSrcFieldDefn->GetNameRef(), bExactFieldNameMatch);
if (iDstField >= 0)
panMap[iField] = iDstField;
else
Expand Down
1 change: 1 addition & 0 deletions gdal/ogr/ogr_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ OGRErr CPL_DLL OGR_L_CreateFeature( OGRLayerH, OGRFeatureH );
OGRErr CPL_DLL OGR_L_DeleteFeature( OGRLayerH, long );
OGRFeatureDefnH CPL_DLL OGR_L_GetLayerDefn( OGRLayerH );
OGRSpatialReferenceH CPL_DLL OGR_L_GetSpatialRef( OGRLayerH );
int CPL_DLL OGR_L_FindFieldIndex( OGRLayerH, const char *, int bExactMatch );
int CPL_DLL OGR_L_GetFeatureCount( OGRLayerH, int );
OGRErr CPL_DLL OGR_L_GetExtent( OGRLayerH, OGREnvelope *, int );
OGRErr CPL_DLL OGR_L_GetExtentEx( OGRLayerH, int iGeomField,
Expand Down
20 changes: 20 additions & 0 deletions gdal/ogr/ogrsf_frmts/generic/ogrlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,26 @@ OGRFeatureDefnH OGR_L_GetLayerDefn( OGRLayerH hLayer )
return (OGRFeatureDefnH) ((OGRLayer *)hLayer)->GetLayerDefn();
}

/************************************************************************/
/* OGR_L_FindFieldIndex() */
/************************************************************************/

int OGR_L_FindFieldIndex( OGRLayerH hLayer, const char *pszFieldName, int bExactMatch )

{
VALIDATE_POINTER1( hLayer, "OGR_L_FindFieldIndex", NULL );

return ((OGRLayer *)hLayer)->FindFieldIndex( pszFieldName, bExactMatch );
}

/************************************************************************/
/* FindFieldIndex() */
/************************************************************************/

int OGRLayer::FindFieldIndex( const char *pszFieldName, int bExactMatch )
{
return GetLayerDefn()->GetFieldIndex( pszFieldName );
}

/************************************************************************/
/* GetSpatialRef() */
Expand Down
2 changes: 2 additions & 0 deletions gdal/ogr/ogrsf_frmts/nas/ogrnasdatasource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ int OGRNASDataSource::Open( const char * pszNewName, int bTestOpen )
strstr(szPtr,"NAS-Operationen_optional.xsd") == NULL &&
strstr(szPtr,"AAA-Fachschema.xsd") == NULL ) )
{
CPLDebug( "NAS",
"Skipping. No chevrons of NAS found [%s]\n", szPtr );
VSIFClose( fp );
return FALSE;
}
Expand Down
2 changes: 2 additions & 0 deletions gdal/ogr/ogrsf_frmts/oci/ogr_oci.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ class OGROCILayer : public OGRLayer
public:
OGROCILayer();
virtual ~OGROCILayer();
virtual int FindFieldIndex( const char *pszFieldName, int bExactMatch ) { return OGRLayer::FindFieldIndex( pszFieldName, bExactMatch ); }

virtual void ResetReading();
virtual OGRFeature *GetNextRawFeature();
Expand Down Expand Up @@ -312,6 +313,7 @@ class OGROCIWritableLayer : public OGROCILayer
virtual OGRSpatialReference *GetSpatialRef() { return poSRS; }
virtual OGRErr CreateField( OGRFieldDefn *poField,
int bApproxOK = TRUE );
virtual int FindFieldIndex( const char *pszFieldName, int bExactMatch );

// following methods are not base class overrides
void SetOptions( char ** );
Expand Down
20 changes: 20 additions & 0 deletions gdal/ogr/ogrsf_frmts/oci/ogrociwritablelayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -492,3 +492,23 @@ OGRErr OGROCIWritableLayer::TranslateToSDOGeometry( OGRGeometry * poGeometry,
return OGRERR_FAILURE;
}

int OGROCIWritableLayer::FindFieldIndex( const char *pszFieldName, int bExactMatch )
{
int iField = GetLayerDefn()->GetFieldIndex( pszFieldName );

if( !bExactMatch && iField < 0 )
{
// try laundered version
OGROCISession *poSession = poDS->GetSession();
char *pszSafeName = CPLStrdup( pszFieldName );

poSession->CleanName( pszSafeName );

iField = GetLayerDefn()->GetFieldIndex( pszSafeName );

CPLFree( pszSafeName );
}

return iField;
}

38 changes: 38 additions & 0 deletions gdal/ogr/ogrsf_frmts/ogrsf_frmts.dox
Original file line number Diff line number Diff line change
Expand Up @@ -2068,6 +2068,44 @@ by the OGRSFDriverManager.

*/

/**

\fn int OGR_L_FindFieldIndex( OGRLayerH hLayer, const char *, int bExactMatch );

\brief Find the index of field in a layer.

The returned number is the index of the field in the layers, or -1 if the
field doesn't exist.

If bExactMatch is set to FALSE and the field doesn't exists in the given form
the driver might apply some changes to make it match, like those it might do
if the layer was created (eg. like LAUNDER in the OCI driver).

This method is the same as the C++ method OGRLayer::FindFieldIndex().

@return field index, or -1 if the field doesn't exist

*/

/**

\fn int OGRLayer::FindFieldIndex( const char *, int bExactMatch );

\brief Find the index of field in the layer.

The returned number is the index of the field in the layers, or -1 if the
field doesn't exist.

If bExactMatch is set to FALSE and the field doesn't exists in the given form
the driver might apply some changes to make it match, like those it might do
if the layer was created (eg. like LAUNDER in the OCI driver).

This method is the same as the C function OGR_L_FindFieldIndex().

@return field index, or -1 if the field doesn't exist

*/

/**

\fn OGRSpatialReference *OGRLayer::GetSpatialRef();
Expand Down
1 change: 1 addition & 0 deletions gdal/ogr/ogrsf_frmts/ogrsf_frmts.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class CPL_DLL OGRLayer
virtual const char *GetName();
virtual OGRwkbGeometryType GetGeomType();
virtual OGRFeatureDefn *GetLayerDefn() = 0;
virtual int FindFieldIndex( const char *pszFieldName, int bExactMatch );

virtual OGRSpatialReference *GetSpatialRef();

Expand Down
4 changes: 4 additions & 0 deletions gdal/swig/include/ogr.i
Original file line number Diff line number Diff line change
Expand Up @@ -746,6 +746,10 @@ public:
OGRErr RollbackTransaction() {
return OGR_L_RollbackTransaction(self);
}

int FindFieldIndex( const char *pszFieldName, int bExactMatch ) {
return OGR_L_FindFieldIndex(self, pszFieldName, bExactMatch );
}

%newobject GetSpatialRef;
OSRSpatialReferenceShadow *GetSpatialRef() {
Expand Down