ogrmysqltablelayer.cpp
来自「支持各种栅格图像和矢量图像读取的库」· C++ 代码 · 共 1,065 行 · 第 1/3 页
CPP
1,065 行
poDefn->SetGeomType( nGeomType ); } if( hResult != NULL ) mysql_free_result( hResult ); //Free our query results for finding type. hResult = NULL; } // Fetch the SRID for this table now nSRSId = FetchSRSId(); return poDefn;}/************************************************************************//* SetSpatialFilter() *//************************************************************************/void OGRMySQLTableLayer::SetSpatialFilter( OGRGeometry * poGeomIn ){ if( !InstallFilter( poGeomIn ) ) return; BuildWhere(); ResetReading();}/************************************************************************//* BuildWhere() *//* *//* Build the WHERE statement appropriate to the current set of *//* criteria (spatial and attribute queries). *//************************************************************************/void OGRMySQLTableLayer::BuildWhere(){ char szWHERE[4096]; CPLFree( pszWHERE ); pszWHERE = NULL; szWHERE[0] = '\0'; if( m_poFilterGeom != NULL && pszGeomColumn ) { char szEnvelope[4096]; OGREnvelope sEnvelope; szEnvelope[0] = '\0'; //POLYGON((MINX MINY, MAXX MINY, MAXX MAXY, MINX MAXY, MINX MINY)) m_poFilterGeom->getEnvelope( &sEnvelope ); sprintf(szEnvelope, "POLYGON((%.12f %.12f, %.12f %.12f, %.12f %.12f, %.12f %.12f, %.12f %.12f))", sEnvelope.MinX, sEnvelope.MinY, sEnvelope.MaxX, sEnvelope.MinY, sEnvelope.MaxX, sEnvelope.MaxY, sEnvelope.MinX, sEnvelope.MaxY, sEnvelope.MinX, sEnvelope.MinY); sprintf( szWHERE, "WHERE MBRIntersects(GeomFromText('%s'), %s)", szEnvelope, pszGeomColumn); } if( pszQuery != NULL ) { if( strlen(szWHERE) == 0 ) sprintf( szWHERE, "WHERE %s ", pszQuery ); else sprintf( szWHERE+strlen(szWHERE), "&& %s ", pszQuery ); } pszWHERE = CPLStrdup(szWHERE);}/************************************************************************//* BuildFullQueryStatement() *//************************************************************************/void OGRMySQLTableLayer::BuildFullQueryStatement(){ if( pszQueryStatement != NULL ) { CPLFree( pszQueryStatement ); pszQueryStatement = NULL; } char *pszFields = BuildFields(); pszQueryStatement = (char *) CPLMalloc(strlen(pszFields)+strlen(pszWHERE) +strlen(poFeatureDefn->GetName()) + 40); sprintf( pszQueryStatement, "SELECT %s FROM %s %s", pszFields, poFeatureDefn->GetName(), pszWHERE ); CPLFree( pszFields );}/************************************************************************//* ResetReading() *//************************************************************************/void OGRMySQLTableLayer::ResetReading(){ BuildFullQueryStatement(); OGRMySQLLayer::ResetReading();}/************************************************************************//* BuildFields() *//* *//* Build list of fields to fetch, performing any required *//* transformations (such as on geometry). *//************************************************************************/char *OGRMySQLTableLayer::BuildFields(){ int i, nSize; char *pszFieldList; nSize = 25; if( pszGeomColumn ) nSize += strlen(pszGeomColumn); if( bHasFid ) nSize += strlen(pszFIDColumn); for( i = 0; i < poFeatureDefn->GetFieldCount(); i++ ) nSize += strlen(poFeatureDefn->GetFieldDefn(i)->GetNameRef()) + 4; pszFieldList = (char *) CPLMalloc(nSize); pszFieldList[0] = '\0'; if( bHasFid && poFeatureDefn->GetFieldIndex( pszFIDColumn ) == -1 ) sprintf( pszFieldList, "%s", pszFIDColumn ); if( pszGeomColumn ) { if( strlen(pszFieldList) > 0 ) strcat( pszFieldList, ", " ); /* ------------------------------------------------------------ */ /* Geometry returned from MySQL is as WKB, with the */ /* first 4 bytes being an int that defines the SRID */ /* and the rest being the WKB. */ /* ------------------------------------------------------------ */ sprintf( pszFieldList+strlen(pszFieldList), "%s %s", pszGeomColumn, pszGeomColumn ); } for( i = 0; i < poFeatureDefn->GetFieldCount(); i++ ) { const char *pszName = poFeatureDefn->GetFieldDefn(i)->GetNameRef(); if( strlen(pszFieldList) > 0 ) strcat( pszFieldList, ", " ); strcat( pszFieldList, pszName ); } CPLAssert( (int) strlen(pszFieldList) < nSize ); return pszFieldList;}/************************************************************************//* SetAttributeFilter() *//************************************************************************/OGRErr OGRMySQLTableLayer::SetAttributeFilter( const char *pszQuery ){ CPLFree( this->pszQuery ); if( pszQuery == NULL || strlen(pszQuery) == 0 ) this->pszQuery = NULL; else this->pszQuery = CPLStrdup( pszQuery ); BuildWhere(); ResetReading(); return OGRERR_NONE;}/************************************************************************//* TestCapability() *//************************************************************************/int OGRMySQLTableLayer::TestCapability( const char * pszCap ){ if( EQUAL(pszCap,OLCRandomRead) ) return bHasFid; else if( EQUAL(pszCap,OLCFastFeatureCount) ) return TRUE; else return OGRMySQLLayer::TestCapability( pszCap );}/************************************************************************//* SetFeature() *//* *//* SetFeature() is implemented by dropping the old copy of the *//* feature in question (if there is one) and then creating a *//* new one with the provided feature id. *//************************************************************************/OGRErr OGRMySQLTableLayer::SetFeature( OGRFeature *poFeature ){ OGRErr eErr; if( poFeature->GetFID() == OGRNullFID ) { CPLError( CE_Failure, CPLE_AppDefined, "FID required on features given to SetFeature()." ); return OGRERR_FAILURE; } eErr = DeleteFeature( poFeature->GetFID() ); if( eErr != OGRERR_NONE ) return eErr; return CreateFeature( poFeature );}/************************************************************************//* DeleteFeature() *//************************************************************************/OGRErr OGRMySQLTableLayer::DeleteFeature( long nFID ){ MYSQL_RES *hResult=NULL; CPLString osCommand;/* -------------------------------------------------------------------- *//* We can only delete features if we have a well defined FID *//* column to target. *//* -------------------------------------------------------------------- */ if( !bHasFid ) { CPLError( CE_Failure, CPLE_AppDefined, "DeleteFeature(%d) failed. Unable to delete features " "in tables without\n a recognised FID column.", nFID ); return OGRERR_FAILURE; }/* -------------------------------------------------------------------- *//* Form the statement to drop the record. *//* -------------------------------------------------------------------- */ osCommand.Printf( "DELETE FROM %s WHERE %s = %ld", poFeatureDefn->GetName(), pszFIDColumn, nFID ); /* -------------------------------------------------------------------- *//* Execute the delete. *//* -------------------------------------------------------------------- */ if( mysql_query(poDS->GetConn(), osCommand.c_str() ) ){ poDS->ReportError( osCommand.c_str() ); return OGRERR_FAILURE; } // make sure to attempt to free results of successful queries hResult = mysql_store_result( poDS->GetConn() ); if( hResult != NULL ) mysql_free_result( hResult ); hResult = NULL; return OGRERR_NONE;}/************************************************************************//* CreateFeature() *//************************************************************************/OGRErr OGRMySQLTableLayer::CreateFeature( OGRFeature *poFeature ){ MYSQL_RES *hResult=NULL; CPLString osCommand; int i, bNeedComma = FALSE;/* -------------------------------------------------------------------- *//* Form the INSERT command. *//* -------------------------------------------------------------------- */ osCommand.Printf( "INSERT INTO %s (", poFeatureDefn->GetName() ); if( poFeature->GetGeometryRef() != NULL ) { osCommand = osCommand + pszGeomColumn + " "; bNeedComma = TRUE; } if( poFeature->GetFID() != OGRNullFID && pszFIDColumn != NULL ) { if( bNeedComma ) osCommand += ", "; osCommand = osCommand + pszFIDColumn + " "; bNeedComma = TRUE; } for( i = 0; i < poFeatureDefn->GetFieldCount(); i++ ) { if( !poFeature->IsFieldSet( i ) ) continue; if( !bNeedComma ) bNeedComma = TRUE; else osCommand += ", "; osCommand = osCommand + poFeatureDefn->GetFieldDefn(i)->GetNameRef(); } osCommand += ") VALUES ("; // Set the geometry bNeedComma = poFeature->GetGeometryRef() != NULL; if( poFeature->GetGeometryRef() != NULL) { char *pszWKT = NULL; if( poFeature->GetGeometryRef() != NULL ) { OGRGeometry *poGeom = (OGRGeometry *) poFeature->GetGeometryRef(); poGeom->closeRings(); poGeom->exportToWkt( &pszWKT ); } if( pszWKT != NULL )
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?