📄 tabfile.cpp
字号:
UGKError(ET_Failure, UGKErr_NotSupported, "GetNextFeatureId_Spatial() requires availability of .MAP file." ); return -1; } return m_poMAPFile->GetNextFeatureId( nPrevId );}/********************************************************************** * TABFile::GetFeatureRef() * * Fill and return a TABFeature object for the specified feature id. 填充并返回指定对象号的TABFeature对象 * The retruned pointer is a reference to an object owned and maintained * by this TABFile object. It should not be altered or freed by the * caller and its contents is guaranteed to be valid only until the next * call to GetFeatureRef() or Close(). 返回的指针是一个对象的引用,由TABFile对象所拥有.在下次调用GetFeatureRef()或者 Close()之前不应该改变它或则释放它 * Returns NULL if the specified feature id does not exist of if an * error happened. In any case, UGKError() will have been called to * report the reason of the failure. * * If an unsupported object type is encountered (likely from a newer version * of MapInfo) then a valid feature will be returned with a NONE geometry, * and a warning will be produced with code TAB_WarningFeatureTypeNotSupported * UGKGetLastErrorNo() should be used to detect that case. **********************************************************************/TABFeature *TABFile::GetFeatureRef(int nFeatureId){ UGKErrorReset(); if (m_eAccessMode != TABRead) { UGKError(ET_Failure, UGKErr_NotSupported, "GetFeatureRef() can be used only with Read access."); return NULL; } /*----------------------------------------------------------------- * Make sure file is opened and Validate feature id by positioning * the read pointers for the .MAP and .DAT files to this feature id. *----------------------------------------------------------------*/ if (m_poMAPFile == NULL) { UGKError(ET_Failure, UGKErr_IllegalArg, "GetFeatureRef() failed: file is not opened!"); return NULL; } if (nFeatureId <= 0 || nFeatureId > m_nLastFeatureId || m_poMAPFile->MoveToObjId(nFeatureId) != 0 || m_poDATFile->GetRecordBlock(nFeatureId) == NULL ) { UGKError(ET_Failure, UGKErr_IllegalArg, "GetFeatureRef() failed: invalid feature id %d", nFeatureId); return NULL; } /*----------------------------------------------------------------- * Flush current feature object * __TODO__ try to reuse if it is already of the right type * 只保留一个Feature *----------------------------------------------------------------*/ if (m_poCurFeature) { delete m_poCurFeature; m_poCurFeature = NULL; } /*----------------------------------------------------------------- * Create new feature object of the right type *----------------------------------------------------------------*/ switch(m_poMAPFile->GetCurObjType()) { case TAB_GEOM_NONE: m_poCurFeature = new TABFeature(m_poDefn); break; case TAB_GEOM_SYMBOL_C: case TAB_GEOM_SYMBOL: m_poCurFeature = new TABPoint(m_poDefn); break; case TAB_GEOM_FONTSYMBOL_C: case TAB_GEOM_FONTSYMBOL: m_poCurFeature = new TABFontPoint(m_poDefn); break; case TAB_GEOM_CUSTOMSYMBOL_C: case TAB_GEOM_CUSTOMSYMBOL: m_poCurFeature = new TABCustomPoint(m_poDefn); break; case TAB_GEOM_LINE_C: case TAB_GEOM_LINE: case TAB_GEOM_PLINE_C: case TAB_GEOM_PLINE: case TAB_GEOM_MULTIPLINE_C: case TAB_GEOM_MULTIPLINE: case TAB_GEOM_V450_MULTIPLINE_C: case TAB_GEOM_V450_MULTIPLINE: m_poCurFeature = new TABPolyline(m_poDefn); break; case TAB_GEOM_ARC_C: case TAB_GEOM_ARC: m_poCurFeature = new TABArc(m_poDefn); break; case TAB_GEOM_REGION_C: case TAB_GEOM_REGION: case TAB_GEOM_V450_REGION_C: case TAB_GEOM_V450_REGION: m_poCurFeature = new TABRegion(m_poDefn); break; case TAB_GEOM_RECT_C: case TAB_GEOM_RECT: case TAB_GEOM_ROUNDRECT_C: case TAB_GEOM_ROUNDRECT: m_poCurFeature = new TABRectangle(m_poDefn); break; case TAB_GEOM_ELLIPSE_C: case TAB_GEOM_ELLIPSE: m_poCurFeature = new TABEllipse(m_poDefn); break; case TAB_GEOM_TEXT_C: case TAB_GEOM_TEXT: m_poCurFeature = new TABText(m_poDefn); break; case TAB_GEOM_MULTIPOINT_C: case TAB_GEOM_MULTIPOINT: m_poCurFeature = new TABMultiPoint(m_poDefn); break; default: /*------------------------------------------------------------- * Unsupported feature type... we still return a valid feature * with NONE geometry after producing a Warning. * Callers can trap that case by checking UGKGetLastErrorNo() * against TAB_WarningFeatureTypeNotSupported *------------------------------------------------------------*/ //m_poCurFeature = new TABDebugFeature(m_poDefn); m_poCurFeature = new TABFeature(m_poDefn); UGKError(ET_Warning, TAB_WarningFeatureTypeNotSupported, "Unsupported object type %d (0x%2.2x). Feature will be " "returned with NONE geometry.", m_poMAPFile->GetCurObjType(), m_poMAPFile->GetCurObjType() ); } /*----------------------------------------------------------------- * Read fields from the .DAT file * GetRecordBlock() has already been called above... *----------------------------------------------------------------*/ if (m_poCurFeature->ReadRecordFromDATFile(m_poDATFile) != 0) { delete m_poCurFeature; m_poCurFeature = NULL; return NULL; } /*----------------------------------------------------------------- * Read geometry from the .MAP file * MoveToObjId() has already been called above... *----------------------------------------------------------------*/ TABMAPObjHdr *poObjHdr = TABMAPObjHdr::NewObj(m_poMAPFile->GetCurObjType(), m_poMAPFile->GetCurObjId()); // Note that poObjHdr==NULL is a valid case if geometry type is NONE if ((poObjHdr && poObjHdr->ReadObj(m_poMAPFile->GetCurObjBlock()) != 0) || m_poCurFeature->ReadGeometryFromMAPFile(m_poMAPFile, poObjHdr) != 0) { delete m_poCurFeature; m_poCurFeature = NULL; if (poObjHdr) delete poObjHdr; return NULL; } if (poObjHdr) // May be NULL if feature geometry type is NONE delete poObjHdr; m_nCurFeatureId = nFeatureId; m_poCurFeature->SetFID(m_nCurFeatureId); m_poCurFeature->SetRecordDeleted(m_poDATFile->IsCurrentRecordDeleted()); return m_poCurFeature;}/********************************************************************** * TABFile::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 TABFile::SetFeature(TABFeature *poFeature, int nFeatureId /*=-1*/){ if (m_eAccessMode != TABWrite) { UGKError(ET_Failure, UGKErr_NotSupported, "SetFeature() can be used only with Write access."); return -1; } if (nFeatureId != -1) { UGKError(ET_Failure, UGKErr_NotSupported, "SetFeature(): random access not implemented yet."); return -1; } /*----------------------------------------------------------------- * Make sure file is opened and establish new feature id. *----------------------------------------------------------------*/ if (m_poMAPFile == NULL) { UGKError(ET_Failure, UGKErr_IllegalArg, "SetFeature() failed: file is not opened!"); return -1; } if (m_nLastFeatureId < 1) { /*------------------------------------------------------------- * OK, this is the first feature in the dataset... make sure the * .DAT schema has been initialized. *------------------------------------------------------------*/ if (m_poDefn == NULL) SetFeatureDefn(poFeature->GetDefnRef(), NULL); /*------------------------------------------------------------- * Special hack to write out at least one field if none are in * UGKFeatureDefn. *------------------------------------------------------------*/ if( m_poDATFile->GetNumFields() == 0 ) { UGKError(ET_Warning, UGKErr_IllegalArg, "MapInfo tables must contain at least 1 column, adding dummy FID column."); m_poDATFile->AddField("FID", TABFInteger, 10, 0 ); } nFeatureId = m_nLastFeatureId = 1; } else { nFeatureId = ++ m_nLastFeatureId; } /*----------------------------------------------------------------- * Write fields to the .DAT file and update .IND if necessary *----------------------------------------------------------------*/ if (m_poDATFile == NULL || m_poDATFile->GetRecordBlock(nFeatureId) == NULL || poFeature->WriteRecordToDATFile(m_poDATFile, m_poINDFile, m_panIndexNo) != 0 ) { UGKError(ET_Failure, UGKErr_FileIO, "Failed writing attributes for feature id %d in %s", nFeatureId, m_pszFname); return -1; } /*----------------------------------------------------------------- * Write geometry to the .MAP file * The call to PrepareNewObj() takes care of the .ID file. *----------------------------------------------------------------*/ TABMAPObjHdr *poObjHdr = TABMAPObjHdr::NewObj(poFeature->ValidateMapInfoType(m_poMAPFile), nFeatureId); TABMAPObjectBlock *poObjBlock = NULL; if ( poObjHdr == NULL || m_poMAPFile == NULL || m_poMAPFile->PrepareNewObj(nFeatureId, poObjHdr->m_nType) != 0 || poFeature->WriteGeometryToMAPFile(m_poMAPFile, poObjHdr) != 0 ) { UGKError(ET_Failure, UGKErr_FileIO, "Failed writing geometry for feature id %d in %s", nFeatureId, m_pszFname); return -1; } if (poObjHdr->m_nType == TAB_GEOM_NONE) { // NONE objects have no reference in the ObjectBlocks. Just flush it. delete poObjHdr; } else if ( (poObjBlock = m_poMAPFile->GetCurObjBlock()) == NULL || poObjBlock->AddObject(poObjHdr) != 0 ) { UGKError(ET_Failure, UGKErr_FileIO, "Failed writing object header for feature id %d in %s", nFeatureId, m_pszFname); return -1; } return nFeatureId;}/********************************************************************** * TABFile::GetLayerDefn() * * Returns a reference to the UGKFeatureDefn that will be used to create * features in this dataset. * * Returns a reference to an object that is maintained by this TABFile * object (and thus should not be modified or freed by the caller) or * NULL if the UGKFeatureDefn has not been initialized yet (i.e. no file * opened yet) **********************************************************************/UGKFeatureDefn *TABFile::GetLayerDefn(){ return m_poDefn;}/********************************************************************** * TABFile::SetFeatureDefn() * * Pass a reference to the UGKFeatureDefn that will be used to create * features in this dataset. This function should be called after * creating a new dataset, but before writing the first feature. * All features that will be written to this dataset must share this same * UGKFeatureDefn. * * A reference to the UGKFeatureDefn will be kept and will be used to * build the .DAT file, etc. * * Returns 0 on success, -1 on error. **********************************************************************/int TABFile::SetFeatureDefn(UGKFeatureDefn *poFeatureDefn, TABFieldType *paeMapInfoNativeFieldTypes /* =NULL */){ int iField, numFields; UGKFieldDefn *poFieldDefn; TABFieldType eMapInfoType = TABFUnknown; int nStatus = 0; if (m_eAccessMode != TABWrite) { UGKError(ET_Failure, UGKErr_NotSupported, "SetFeatureDefn() can be used only with Write access."); return -1;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -