📄 ogrfeaturequery.cpp
字号:
/****************************************************************************** * $Id: ogrfeaturequery.cpp,v 1.11 2006/11/28 00:00:36 tamas Exp $ * * Project: OpenGIS Simple Features Reference Implementation * Purpose: Implementation of simple SQL WHERE style attributes queries * for OGRFeatures. * Author: Frank Warmerdam, warmerdam@pobox.com * ****************************************************************************** * Copyright (c) 2001, 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. ****************************************************************************** * * $Log: ogrfeaturequery.cpp,v $ * Revision 1.11 2006/11/28 00:00:36 tamas * RFC 6: Geometry and Feature Style as OGR Special Fields * * Revision 1.10 2006/01/07 17:16:36 dron * Do not include ogrsf_frmts.h. * * Revision 1.9 2005/04/03 21:05:39 fwarmerdam * Fixed IN operator for Reals. * * Revision 1.8 2004/02/23 21:48:19 warmerda * Fixed bug with support for IS NULL and IS NOT NULL on list fields. * Added preliminary (untested) support for GetUsedFields(). * * Revision 1.7 2003/03/04 05:46:31 warmerda * added EvaluateAgainstIndices for OGRFeatureQuery * * Revision 1.6 2002/04/29 19:31:55 warmerda * added support for FID field * * Revision 1.5 2002/04/19 20:46:06 warmerda * added [NOT] IN, [NOT] LIKE and IS [NOT] NULL support * * Revision 1.4 2001/10/25 16:41:01 danmo * Fixed OGRFeatureQueryEvaluator() crash with string fields with unset value * * Revision 1.3 2001/07/19 18:25:07 warmerda * expanded tabs * * Revision 1.2 2001/07/18 05:03:05 warmerda * added CPL_CVSID * * Revision 1.1 2001/06/19 15:46:41 warmerda * New * */#include <assert.h>#include "ogr_feature.h"#include "ogr_p.h"#include "ogr_attrind.h"CPL_CVSID("$Id: ogrfeaturequery.cpp,v 1.11 2006/11/28 00:00:36 tamas Exp $");/************************************************************************//* Support for special attributes (feature query and selection) *//************************************************************************/char* SpecialFieldNames[SPECIAL_FIELD_COUNT] = {"FID", "OGR_GEOMETRY", "OGR_STYLE", "OGR_GEOM_WKT"};swq_field_type SpecialFieldTypes[SPECIAL_FIELD_COUNT] = {SWQ_INTEGER, SWQ_STRING, SWQ_STRING, SWQ_STRING};/************************************************************************//* OGRFeatureQuery() *//************************************************************************/OGRFeatureQuery::OGRFeatureQuery(){ poTargetDefn = NULL; pSWQExpr = NULL;}/************************************************************************//* ~OGRFeatureQuery() *//************************************************************************/OGRFeatureQuery::~OGRFeatureQuery(){ if( pSWQExpr != NULL ) swq_expr_free( (swq_expr *) pSWQExpr );}/************************************************************************//* Parse *//************************************************************************/OGRErr OGRFeatureQuery::Compile( OGRFeatureDefn *poDefn, const char * pszExpression ){/* -------------------------------------------------------------------- *//* Clear any existing expression. *//* -------------------------------------------------------------------- */ if( pSWQExpr != NULL ) swq_expr_free( (swq_expr *) pSWQExpr );/* -------------------------------------------------------------------- *//* Build list of fields. *//* -------------------------------------------------------------------- */ char **papszFieldNames; swq_field_type *paeFieldTypes; int iField; int nFieldCount = poDefn->GetFieldCount() + SPECIAL_FIELD_COUNT; papszFieldNames = (char **) CPLMalloc(sizeof(char *) * nFieldCount ); paeFieldTypes = (swq_field_type *) CPLMalloc(sizeof(swq_field_type) * nFieldCount ); for( iField = 0; iField < poDefn->GetFieldCount(); iField++ ) { OGRFieldDefn *poField = poDefn->GetFieldDefn( iField ); papszFieldNames[iField] = (char *) poField->GetNameRef(); switch( poField->GetType() ) { case OFTInteger: paeFieldTypes[iField] = SWQ_INTEGER; break; case OFTReal: paeFieldTypes[iField] = SWQ_FLOAT; break; case OFTString: paeFieldTypes[iField] = SWQ_STRING; break; default: paeFieldTypes[iField] = SWQ_OTHER; break; } } iField = 0; while (iField < SPECIAL_FIELD_COUNT) { papszFieldNames[poDefn->GetFieldCount() + iField] = SpecialFieldNames[iField]; paeFieldTypes[poDefn->GetFieldCount() + iField] = SpecialFieldTypes[iField]; ++iField; }/* -------------------------------------------------------------------- *//* Try to parse. *//* -------------------------------------------------------------------- */ const char *pszError; OGRErr eErr = OGRERR_NONE; poTargetDefn = poDefn; pszError = swq_expr_compile( pszExpression, nFieldCount, papszFieldNames, paeFieldTypes, (swq_expr **) &pSWQExpr ); if( pszError != NULL ) { CPLError( CE_Failure, CPLE_AppDefined, "%s", pszError ); eErr = OGRERR_CORRUPT_DATA; pSWQExpr = NULL; } CPLFree( papszFieldNames ); CPLFree( paeFieldTypes ); return eErr;}/************************************************************************//* OGRFeatureQueryEvaluator() *//************************************************************************/static int OGRFeatureQueryEvaluator( swq_field_op *op, OGRFeature *poFeature ){ OGRField sField; OGRField *psField; int iSpecialField = op->field_index - poFeature->GetDefnRef()->GetFieldCount(); if( iSpecialField >= 0 ) { if ( iSpecialField < SPECIAL_FIELD_COUNT ) { switch ( SpecialFieldTypes[iSpecialField] ) { case SWQ_INTEGER: sField.Integer = poFeature->GetFieldAsInteger( op->field_index ); case SWQ_STRING: sField.String = (char*) poFeature->GetFieldAsString( op->field_index ); } } else { CPLDebug( "OGRFeatureQuery", "Illegal special field index."); return FALSE; } psField = &sField; } else psField = poFeature->GetRawFieldRef( op->field_index ); switch( op->field_type ) { case SWQ_INTEGER: switch( op->operation ) { case SWQ_EQ: return psField->Integer == op->int_value; case SWQ_NE: return psField->Integer != op->int_value; case SWQ_LT: return psField->Integer < op->int_value; case SWQ_GT: return psField->Integer > op->int_value; case SWQ_LE: return psField->Integer <= op->int_value; case SWQ_GE: return psField->Integer >= op->int_value; case SWQ_ISNULL: return !poFeature->IsFieldSet( op->field_index ); case SWQ_IN: { const char *pszSrc; pszSrc = op->string_value; while( *pszSrc != '\0' ) { if( atoi(pszSrc) == psField->Integer ) return TRUE; pszSrc += strlen(pszSrc) + 1; } return FALSE; } default: CPLDebug( "OGRFeatureQuery", "Illegal operation (%d) on integer field.", op->operation ); return FALSE; } case SWQ_FLOAT: switch( op->operation ) { case SWQ_EQ: return psField->Real == op->float_value; case SWQ_NE: return psField->Real != op->float_value; case SWQ_LT: return psField->Real < op->float_value; case SWQ_GT: return psField->Real > op->float_value;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -