OGRwkbGeometryType OGRSQLiteViewLayer::GetGeomType() { if (poFeatureDefn) return poFeatureDefn->GetGeomType(); OGRSQLiteLayer* poUnderlyingLayer = GetUnderlyingLayer(); if (poUnderlyingLayer) return poUnderlyingLayer->GetGeomType(); return wkbUnknown; }
CPLErr OGRSQLiteViewLayer::EstablishFeatureDefn() { int rc; sqlite3 *hDB = poDS->GetDB(); sqlite3_stmt *hColStmt = NULL; const char *pszSQL; OGRSQLiteLayer* poUnderlyingLayer = GetUnderlyingLayer(); if (poUnderlyingLayer == NULL) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot find underlying layer %s for view %s", osUnderlyingTableName.c_str(), pszViewName); return CE_Failure; } if ( !poUnderlyingLayer->IsTableLayer() ) { CPLError(CE_Failure, CPLE_AppDefined, "Underlying layer %s for view %s is not a regular table", osUnderlyingTableName.c_str(), pszViewName); return CE_Failure; } const char* pszRealUnderlyingGeometryColumn = poUnderlyingLayer->GetGeometryColumn(); if ( pszRealUnderlyingGeometryColumn == NULL || !EQUAL(pszRealUnderlyingGeometryColumn, osUnderlyingGeometryColumn.c_str()) ) { CPLError(CE_Failure, CPLE_AppDefined, "Underlying layer %s for view %s has not expected geometry column name (%s instead of %s)", osUnderlyingTableName.c_str(), pszViewName, pszRealUnderlyingGeometryColumn ? pszRealUnderlyingGeometryColumn : "(null)", osUnderlyingGeometryColumn.c_str()); return CE_Failure; } poSRS = poUnderlyingLayer->GetSpatialRef(); if (poSRS) poSRS->Reference(); this->bHasSpatialIndex = poUnderlyingLayer->HasSpatialIndex(); /* -------------------------------------------------------------------- */ /* Get the column definitions for this table. */ /* -------------------------------------------------------------------- */ hColStmt = NULL; pszSQL = CPLSPrintf( "SELECT \"%s\", * FROM '%s' LIMIT 1", OGRSQLiteEscapeName(pszFIDColumn).c_str(), pszEscapedTableName ); rc = sqlite3_prepare( hDB, pszSQL, strlen(pszSQL), &hColStmt, NULL ); if( rc != SQLITE_OK ) { CPLError( CE_Failure, CPLE_AppDefined, "Unable to query table %s for column definitions : %s.", pszViewName, sqlite3_errmsg(hDB) ); return CE_Failure; } rc = sqlite3_step( hColStmt ); if ( rc != SQLITE_DONE && rc != SQLITE_ROW ) { CPLError( CE_Failure, CPLE_AppDefined, "In Initialize(): sqlite3_step(%s):\n %s", pszSQL, sqlite3_errmsg(hDB) ); sqlite3_finalize( hColStmt ); return CE_Failure; } /* -------------------------------------------------------------------- */ /* Collect the rest of the fields. */ /* -------------------------------------------------------------------- */ std::set<CPLString> aosEmpty; BuildFeatureDefn( pszViewName, hColStmt, aosEmpty ); sqlite3_finalize( hColStmt ); /* -------------------------------------------------------------------- */ /* Set the geometry type if we know it. */ /* -------------------------------------------------------------------- */ poFeatureDefn->SetGeomType( poUnderlyingLayer->GetGeomType() ); return CE_None; }
CPLErr OGRSQLiteViewLayer::EstablishFeatureDefn() { int rc; sqlite3 *hDB = poDS->GetDB(); sqlite3_stmt *hColStmt = NULL; const char *pszSQL; OGRSQLiteLayer* poUnderlyingLayer = GetUnderlyingLayer(); if (poUnderlyingLayer == NULL) { CPLError(CE_Failure, CPLE_AppDefined, "Cannot find underlying layer %s for view %s", osUnderlyingTableName.c_str(), pszViewName); return CE_Failure; } if ( !poUnderlyingLayer->IsTableLayer() ) { CPLError(CE_Failure, CPLE_AppDefined, "Underlying layer %s for view %s is not a regular table", osUnderlyingTableName.c_str(), pszViewName); return CE_Failure; } int nUnderlyingLayerGeomFieldIndex = poUnderlyingLayer->GetLayerDefn()->GetGeomFieldIndex(osUnderlyingGeometryColumn); if ( nUnderlyingLayerGeomFieldIndex < 0 ) { CPLError(CE_Failure, CPLE_AppDefined, "Underlying layer %s for view %s has not expected geometry column name %s", osUnderlyingTableName.c_str(), pszViewName, osUnderlyingGeometryColumn.c_str()); return CE_Failure; } this->bHasSpatialIndex = poUnderlyingLayer->HasSpatialIndex(nUnderlyingLayerGeomFieldIndex); /* -------------------------------------------------------------------- */ /* Get the column definitions for this table. */ /* -------------------------------------------------------------------- */ hColStmt = NULL; pszSQL = CPLSPrintf( "SELECT \"%s\", * FROM '%s' LIMIT 1", OGRSQLiteEscapeName(pszFIDColumn).c_str(), pszEscapedTableName ); rc = sqlite3_prepare( hDB, pszSQL, strlen(pszSQL), &hColStmt, NULL ); if( rc != SQLITE_OK ) { CPLError( CE_Failure, CPLE_AppDefined, "Unable to query table %s for column definitions : %s.", pszViewName, sqlite3_errmsg(hDB) ); return CE_Failure; } rc = sqlite3_step( hColStmt ); if ( rc != SQLITE_DONE && rc != SQLITE_ROW ) { CPLError( CE_Failure, CPLE_AppDefined, "In Initialize(): sqlite3_step(%s):\n %s", pszSQL, sqlite3_errmsg(hDB) ); sqlite3_finalize( hColStmt ); return CE_Failure; } /* -------------------------------------------------------------------- */ /* Collect the rest of the fields. */ /* -------------------------------------------------------------------- */ std::set<CPLString> aosGeomCols; std::set<CPLString> aosIgnoredCols; aosGeomCols.insert(osGeomColumn); BuildFeatureDefn( pszViewName, hColStmt, aosGeomCols, aosIgnoredCols ); sqlite3_finalize( hColStmt ); /* -------------------------------------------------------------------- */ /* Set the properties of the geometry column. */ /* -------------------------------------------------------------------- */ if( poFeatureDefn->GetGeomFieldCount() != 0 ) { OGRSQLiteGeomFieldDefn* poSrcGeomFieldDefn = poUnderlyingLayer->myGetLayerDefn()->myGetGeomFieldDefn(nUnderlyingLayerGeomFieldIndex); OGRSQLiteGeomFieldDefn* poGeomFieldDefn = poFeatureDefn->myGetGeomFieldDefn(0); poGeomFieldDefn->SetType(poSrcGeomFieldDefn->GetType()); poGeomFieldDefn->SetSpatialRef(poSrcGeomFieldDefn->GetSpatialRef()); poGeomFieldDefn->nSRSId = poSrcGeomFieldDefn->nSRSId; if( eGeomFormat != OSGF_None ) poGeomFieldDefn->eGeomFormat = eGeomFormat; } return CE_None; }