?? mitab_tabfile.cpp
字號:
{ CPLError(CE_Failure, CPLE_NotSupported, "SetFeature() can be used only with Write access."); return -1; } if (nFeatureId != -1) { CPLError(CE_Failure, CPLE_NotSupported, "SetFeature(): random access not implemented yet."); return -1; } /*----------------------------------------------------------------- * Make sure file is opened and establish new feature id. *----------------------------------------------------------------*/ if (m_poMAPFile == NULL) { CPLError(CE_Failure, CPLE_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 * OGRFeatureDefn. *------------------------------------------------------------*/ if( m_poDATFile->GetNumFields() == 0 ) { CPLError(CE_Warning, CPLE_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 ) { CPLError(CE_Failure, CPLE_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; /*----------------------------------------------------------------- * ValidateMapInfoType() may have returned TAB_GEOM_NONE if feature * contained an invalid geometry for its class. Need to catch that * case and return the error. *----------------------------------------------------------------*/ if (poObjHdr->m_nType == TAB_GEOM_NONE && poFeature->GetFeatureClass() != TABFCNoGeomFeature ) { CPLError(CE_Failure, CPLE_FileIO, "Invalid geometry for feature id %d in %s", nFeatureId, m_pszFname); return -1; } if ( poObjHdr == NULL || m_poMAPFile == NULL || m_poMAPFile->PrepareNewObj(nFeatureId, poObjHdr->m_nType) != 0 || poFeature->WriteGeometryToMAPFile(m_poMAPFile, poObjHdr) != 0 ) { CPLError(CE_Failure, CPLE_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 ) { CPLError(CE_Failure, CPLE_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 OGRFeatureDefn 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 OGRFeatureDefn has not been initialized yet (i.e. no file * opened yet) **********************************************************************/OGRFeatureDefn *TABFile::GetLayerDefn(){ return m_poDefn;}/********************************************************************** * TABFile::SetFeatureDefn() * * Pass a reference to the OGRFeatureDefn 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 * OGRFeatureDefn. * * A reference to the OGRFeatureDefn will be kept and will be used to * build the .DAT file, etc. * * Returns 0 on success, -1 on error. **********************************************************************/int TABFile::SetFeatureDefn(OGRFeatureDefn *poFeatureDefn, TABFieldType *paeMapInfoNativeFieldTypes /* =NULL */){ int iField, numFields; OGRFieldDefn *poFieldDefn; TABFieldType eMapInfoType = TABFUnknown; int nStatus = 0; if (m_eAccessMode != TABWrite) { CPLError(CE_Failure, CPLE_NotSupported, "SetFeatureDefn() can be used only with Write access."); return -1; } /*----------------------------------------------------------------- * Keep a reference to the OGRFeatureDefn... 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(); /*----------------------------------------------------------------- * Pass field information to the .DAT file, after making sure that * it has been created and that it does not contain any field * definition yet. *----------------------------------------------------------------*/ if (m_poDATFile== NULL || m_poDATFile->GetNumFields() > 0 ) { CPLError(CE_Failure, CPLE_AssertionFailed, "SetFeatureDefn() can be called only once in a newly " "created dataset."); return -1; } numFields = poFeatureDefn->GetFieldCount(); for(iField=0; nStatus==0 && iField < numFields; iField++) { poFieldDefn = m_poDefn->GetFieldDefn(iField); /*------------------------------------------------------------- * Make sure field name is valid... check for special chars, etc. *------------------------------------------------------------*/ char *pszCleanName = TABCleanFieldName(poFieldDefn->GetNameRef()); if (!EQUAL(pszCleanName, poFieldDefn->GetNameRef())) poFieldDefn->SetName(pszCleanName); CPLFree(pszCleanName); pszCleanName = NULL; if (paeMapInfoNativeFieldTypes) { eMapInfoType = paeMapInfoNativeFieldTypes[iField]; } else { /*--------------------------------------------------------- * Map OGRFieldTypes to MapInfo native types *--------------------------------------------------------*/ switch(poFieldDefn->GetType()) { case OFTInteger: eMapInfoType = TABFInteger; break; case OFTReal: eMapInfoType = TABFFloat; break; case OFTString: default: eMapInfoType = TABFChar; } } nStatus = m_poDATFile->AddField(poFieldDefn->GetNameRef(), eMapInfoType, poFieldDefn->GetWidth(), poFieldDefn->GetPrecision()); } /*----------------------------------------------------------------- * Alloc the array to keep track of indexed fields (default=NOT indexed) *----------------------------------------------------------------*/ m_panIndexNo = (int *)CPLCalloc(numFields, sizeof(int)); return nStatus;}/********************************************************************** * TABFile::AddFieldNative() * * Create a new field using a native mapinfo data type... this is an * alternative to defining fields through the OGR interface. * This function should be called after creating a new dataset, but before * writing the first feature. * * This function will build/update the OGRFeatureDefn that will have to be * used when writing features to this dataset. * * A reference to the OGRFeatureDefn can be obtained using GetLayerDefn(). * * Note: The bUnique flag has no effect on TABFiles. See the TABView class. * * Returns 0 on success, -1 on error. **********************************************************************/int TABFile::AddFieldNative(const char *pszName, TABFieldType eMapInfoType, int nWidth /*=0*/, int nPrecision /*=0*/, GBool bIndexed /*=FALSE*/, GBool /*bUnique=FALSE*/){ OGRFieldDefn *poFieldDefn; int nStatus = 0; char *pszCleanName = NULL; if (m_eAccessMode != TABWrite) { CPLError(CE_Failure, CPLE_NotSupported, "AddFieldNative() can be used only with Write access."); return -1; } /*----------------------------------------------------------------- * Check that call happens at the right time in dataset's life. *----------------------------------------------------------------*/ if (m_eAccessMode != TABWrite || m_nLastFeatureId > 0 || m_poDATFile == NULL) { CPLError(CE_Failure, CPLE_AssertionFailed, "AddFieldNative() must be called after opening a new " "dataset, but before writing the first feature to it."); return -1; } /*----------------------------------------------------------------- * Create new OGRFeatureDefn if not done yet... *----------------------------------------------------------------*/ if (m_poDefn== NULL) { char *pszFeatureClassName = TABGetBasename(m_pszFname); m_poDefn = new OGRFeatureDefn(pszFeatureClassName); CPLFree(pszFeatureClassName); // Ref count defaults to 0... set it to 1 m_poDefn->Reference(); } /*----------------------------------------------------------------- * Validate field width... must be <= 254 *----------------------------------------------------------------*/ if (nWidth > 254) { CPLError(CE_Warning, CPLE_IllegalArg, "Invalid size (%d) for field '%s'. " "Size must be 254 or less.", nWidth, pszName); nWidth=254; } /*----------------------------------------------------------------- * Map fields with width=0 (variable length in OGR) to a valid default *----------------------------------------------------------------*/ if (eMapInfoType == TABFDecimal && nWidth == 0) nWidth=20; else if (nWidth == 0) nWidth=254; /* char fields */ /*----------------------------------------------------------------- * Make sure field name is valid... check for special chars, etc. * (pszCleanName will have to be freed.) *----------------------------------------------------------------*/ pszCleanName = TABCleanFieldName(pszName); /*----------------------------------------------------------------- * Map MapInfo native types to OGR types *----------------------------------------------------------------*/ poFieldDefn = NULL; switch(eMapInfoType) { case TABFChar: /*------------------------------------------------- * CHAR type *------------------------------------------------*/ poFieldDefn = new OGRFieldDefn(pszCleanName, OFTString); poFieldDefn->SetWidth(nWidth); break; case TABFInteger: /*------------------------------------------------- * INTEGER type *------------------------------------------------*/ poFieldDefn = new OGRFieldDefn(pszCleanName, OFTInteger); break; case TABFSmallInt: /*------------------------------------------------- * SMALLINT type *------------------------------------------------*/ poFieldDefn = new OGRFieldDefn(pszCleanName, OFTInteger); break; case TABFDecimal: /*------------------------------------------------- * DECIMAL type *-------------------------------------------
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -