📄 tabrelation.cpp
字号:
* * Return the index key for the specified field in poFeature. * Simply maps the call to the proper method in the TABINDFile class. * * Returns a reference to a TABINDFile internal buffer that should not * be freed by the caller. **********************************************************************/UGKByte *TABRelation::BuildFieldKey(TABFeature *poFeature, int nFieldNo, TABFieldType eType, int nIndexNo){ UGKByte *pKey = NULL; switch(eType) { case TABFChar: pKey = m_poRelINDFileRef->BuildKey(nIndexNo, poFeature->GetFieldAsString(nFieldNo)); break; case TABFDecimal: case TABFFloat: pKey = m_poRelINDFileRef->BuildKey(nIndexNo, poFeature->GetFieldAsDouble(nFieldNo)); break; case TABFInteger: case TABFSmallInt: case TABFDate: case TABFLogical: default: pKey = m_poRelINDFileRef->BuildKey(nIndexNo, poFeature->GetFieldAsInteger(nFieldNo)); break; } return pKey;}/********************************************************************** * TABRelation::GetNativeFieldType() * * Returns the native MapInfo field type for the specified field. * * Returns TABFUnknown if file is not opened, or if specified field index is * invalid. * * Note that field ids are positive and start at 0. **********************************************************************/TABFieldType TABRelation::GetNativeFieldType(int nFieldId){ int i, numFields; if (m_poMainTable==NULL || m_poRelTable==NULL || m_panMainTableFieldMap==NULL || m_panRelTableFieldMap==NULL) return TABFUnknown; /*----------------------------------------------------------------- * Look for nFieldId in the field maps and call the corresponding * TAB file's GetNativeFieldType() *----------------------------------------------------------------*/ numFields = m_poMainTable->GetLayerDefn()->GetFieldCount(); for(i=0; i<numFields; i++) { if (m_panMainTableFieldMap[i] == nFieldId) { return m_poMainTable->GetNativeFieldType(i); } } numFields = m_poRelTable->GetLayerDefn()->GetFieldCount(); for(i=0; i<numFields; i++) { if (m_panRelTableFieldMap[i] == nFieldId) { return m_poRelTable->GetNativeFieldType(i); } } return TABFUnknown;}/********************************************************************** * TABRelation::AddFieldNative() * * Create a new field using a native mapinfo data type... this is an * alternative to defining fields through the UGK interface. * This function should be called after creating a new dataset, but before * writing the first feature. * * This function will build/update the UGKFeatureDefn that will have to be * used when writing features to this dataset. * * A reference to the UGKFeatureDefn can be obtained using GetLayerDefn(). * * Returns 0 on success, -1 on error. **********************************************************************/int TABRelation::AddFieldNative(const char *pszName, TABFieldType eMapInfoType, int nWidth /*=0*/, int nPrecision /*=0*/, UGKBool bIndexed /*=FALSE*/, UGKBool bUnique/*=FALSE*/){ if (m_poMainTable==NULL || m_poRelTable==NULL || m_panMainTableFieldMap==NULL || m_panRelTableFieldMap==NULL) return -1; if (!bUnique) { /*------------------------------------------------------------- * Add field to poMainTable and to m_poDefn *------------------------------------------------------------*/ if (m_poMainTable->AddFieldNative(pszName, eMapInfoType, nWidth, nPrecision, bIndexed) != 0) return -1; UGKFeatureDefn *poMainDefn = m_poMainTable->GetLayerDefn(); m_panMainTableFieldMap = (int*)UGK_Realloc(m_panMainTableFieldMap, poMainDefn->GetFieldCount()*sizeof(int)); m_poDefn->AddFieldDefn(poMainDefn->GetFieldDefn(poMainDefn-> GetFieldCount()-1)); m_panMainTableFieldMap[poMainDefn->GetFieldCount()-1] = m_poDefn->GetFieldCount()-1; } else { /*------------------------------------------------------------- * Add field to poRelTable and to m_poDefn *------------------------------------------------------------*/ if (m_poRelTable->AddFieldNative(pszName, eMapInfoType, nWidth, nPrecision, bIndexed) != 0) return -1; UGKFeatureDefn *poRelDefn = m_poRelTable->GetLayerDefn(); m_panRelTableFieldMap = (int*)UGK_Realloc(m_panRelTableFieldMap, poRelDefn->GetFieldCount()*sizeof(int)); m_poDefn->AddFieldDefn(poRelDefn->GetFieldDefn(poRelDefn-> GetFieldCount()-1)); m_panRelTableFieldMap[poRelDefn->GetFieldCount()-1] = m_poDefn->GetFieldCount()-1; // The first field in this table must be indexed. if (poRelDefn->GetFieldCount() == 1) m_poRelTable->SetFieldIndexed(0); } return 0;}/********************************************************************** * TABRelation::IsFieldIndexed() * * Returns TRUE is specified field is indexed. * * Note that field ids are positive and start at 0. **********************************************************************/UGKBool TABRelation::IsFieldIndexed(int nFieldId){ int i, numFields; if (m_poMainTable==NULL || m_poRelTable==NULL || m_panMainTableFieldMap==NULL || m_panRelTableFieldMap==NULL) return FALSE; /*----------------------------------------------------------------- * Look for nFieldId in the field maps and call the corresponding * TAB file's GetNativeFieldType() *----------------------------------------------------------------*/ numFields = m_poMainTable->GetLayerDefn()->GetFieldCount(); for(i=0; i<numFields; i++) { if (m_panMainTableFieldMap[i] == nFieldId) { return m_poMainTable->IsFieldIndexed(i); } } numFields = m_poRelTable->GetLayerDefn()->GetFieldCount(); for(i=0; i<numFields; i++) { if (m_panRelTableFieldMap[i] == nFieldId) { return m_poRelTable->IsFieldIndexed(i); } } return FALSE;}/********************************************************************** * TABRelation::SetFieldIndexed() * * Request that the specified field be indexed. This will create the .IND * file, etc. * * Note that field ids are positive and start at 0. * * Returns 0 on success, -1 on error. **********************************************************************/int TABRelation::SetFieldIndexed(int nFieldId){ int i, numFields; if (m_poMainTable==NULL || m_poRelTable==NULL || m_panMainTableFieldMap==NULL || m_panRelTableFieldMap==NULL) return -1; /*----------------------------------------------------------------- * Look for nFieldId in the field maps and call the corresponding * TAB file's GetNativeFieldType() *----------------------------------------------------------------*/ numFields = m_poMainTable->GetLayerDefn()->GetFieldCount(); for(i=0; i<numFields; i++) { if (m_panMainTableFieldMap[i] == nFieldId) { return m_poMainTable->SetFieldIndexed(i); } } numFields = m_poRelTable->GetLayerDefn()->GetFieldCount(); for(i=0; i<numFields; i++) { if (m_panRelTableFieldMap[i] == nFieldId) { return m_poRelTable->SetFieldIndexed(i); } } return -1;}/********************************************************************** * TABRelation::IsFieldUnique() * * Returns TRUE is specified field is part of the unique table (poRelTable). * * Note that field ids are positive and start at 0. **********************************************************************/UGKBool TABRelation::IsFieldUnique(int nFieldId){ int i, numFields; if (m_poMainTable==NULL || m_poRelTable==NULL || m_panMainTableFieldMap==NULL || m_panRelTableFieldMap==NULL) return FALSE; /*----------------------------------------------------------------- * Look for nFieldId in the poRelTable field map *----------------------------------------------------------------*/ numFields = m_poRelTable->GetLayerDefn()->GetFieldCount(); for(i=0; i<numFields; i++) { if (m_panRelTableFieldMap[i] == nFieldId) { return TRUE; // If it's here then it is unique! } } return FALSE;}/********************************************************************** * TABRelation::SetFeature() * * Write a feature to this dataset. * * For now only sequential writes are supported (i.e. with nFeatureId=-1) * but eventually we should be able to do random access by specifying * a value through nFeatureId. * * Returns the new featureId (> 0) on success, or -1 if an * error happened in which case, CPLError() will have been called to * report the reason of the failure. **********************************************************************/int TABRelation::SetFeature(TABFeature *poFeature, int nFeatureId /*=-1*/){ TABFeature *poMainFeature=NULL; assert(m_poMainTable && m_poRelTable); // We'll need the feature Defn later... UGKFeatureDefn *poMainDefn, *poRelDefn; poMainDefn = m_poMainTable->GetLayerDefn(); poRelDefn = m_poRelTable->GetLayerDefn(); /*----------------------------------------------------------------- * Create one feature for each table * Copy the geometry only to the feature from the main table *----------------------------------------------------------------*/ poMainFeature = poFeature->CloneTABFeature(poMainDefn); if (poFeature->GetFeatureClass() != TABFCNoGeomFeature) { UGKGeometry *poGeom; poGeom = poFeature->GetGeometryRef(); poMainFeature->SetGeometry(poGeom); } /*----------------------------------------------------------------- * Copy fields to poMainFeature *----------------------------------------------------------------*/ for(int i=0; i<poMainDefn->GetFieldCount(); i++) { if (m_panMainTableFieldMap[i] != -1) { poMainFeature->SetField(i, poFeature->GetRawFieldRef(m_panMainTableFieldMap[i])); } } /*----------------------------------------------------------------- * Look for a record id for the unique fields, and write a new * record if necessary *----------------------------------------------------------------*/ int nRecordNo = 0; int nUniqueIndexNo=-1; if (m_panMainTableFieldMap[0] != -1) nUniqueIndexNo =m_poRelTable->GetFieldIndexNumber( 0 ); if (nUniqueIndexNo > 0) { UGKByte *pKey = BuildFieldKey(poFeature, 0, m_poRelTable->GetNativeFieldType(0), nUniqueIndexNo); if ((nRecordNo=m_poRelINDFileRef->FindFirst(nUniqueIndexNo, pKey))==-1) return -1; if (nRecordNo == 0) { /*--------------------------------------------------------- * No record in poRelTable yet for this unique value... * add one now... *--------------------------------------------------------*/ TABFeature *poRelFeature = new TABFeature(poRelDefn); for(int i=0; i<poRelDefn->GetFieldCount(); i++) { if (m_panRelTableFieldMap[i] != -1) { poRelFeature->SetField(i, poFeature->GetRawFieldRef(m_panRelTableFieldMap[i])); } } nRecordNo = ++m_nUniqueRecordNo; poRelFeature->SetField(m_nRelFieldNo, nRecordNo); if (m_poRelTable->SetFeature(poRelFeature, -1) < 0) return -1; delete poRelFeature; } } /*----------------------------------------------------------------- * Write poMainFeature to the main table *----------------------------------------------------------------*/ poMainFeature->SetField(m_nMainFieldNo, nRecordNo); nFeatureId = m_poMainTable->SetFeature(poMainFeature, nFeatureId); delete poMainFeature; return nFeatureId;}/********************************************************************** * TABFile::SetFeatureDefn() * * NOT FULLY IMPLEMENTED YET... * * Returns 0 on success, -1 on error. **********************************************************************/int TABRelation::SetFeatureDefn(UGKFeatureDefn *poFeatureDefn, TABFieldType *paeMapInfoNativeFieldTypes /* =NULL */){ if (m_poDefn && m_poDefn->GetFieldCount() > 0) { assert(m_poDefn==NULL); return -1; } /*----------------------------------------------------------------- * Keep a reference to the UGKFeatureDefn... we'll have to take the * reference count into account when we are done with it. *----------------------------------------------------------------*/ if (m_poDefn && m_poDefn->Dereference() == 0) delete m_poDefn; m_poDefn = poFeatureDefn; m_poDefn->Reference(); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -