📄 mitab_miffile.cpp
字号:
/*----------------------------------------------------------------- * 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_poMIDFile == NULL) { CPLError(CE_Failure, CPLE_IllegalArg, "GetFeatureRef() failed: file is not opened!"); return NULL; } if (GotoFeature(nFeatureId)!= 0 ) { CPLError(CE_Failure, CPLE_IllegalArg, "GetFeatureRef() failed: invalid feature id %d", nFeatureId); return NULL; } /*----------------------------------------------------------------- * Create new feature object of the right type *----------------------------------------------------------------*/ if ((pszLine = m_poMIFFile->GetLastLine()) != NULL) { // Delete previous feature... we'll start we a clean one. if (m_poCurFeature) delete m_poCurFeature; m_poCurFeature = NULL; m_nCurFeatureId = m_nPreloadedId; if (EQUALN(pszLine,"NONE",4)) { m_poCurFeature = new TABFeature(m_poDefn); } else if (EQUALN(pszLine,"POINT",5)) { // Special case, we need to know two lines to decide the type char **papszToken; papszToken = CSLTokenizeString(pszLine); if (CSLCount(papszToken) !=3) { CSLDestroy(papszToken); CPLError(CE_Failure, CPLE_NotSupported, "GetFeatureRef() failed: invalid point line: '%s'", pszLine); return NULL; } m_poMIFFile->SaveLine(pszLine); if ((pszLine = m_poMIFFile->GetLine()) != NULL) { CSLDestroy(papszToken); papszToken = CSLTokenizeStringComplex(pszLine," ,()\t", TRUE,FALSE); if (CSLCount(papszToken)> 0 &&EQUALN(papszToken[0],"SYMBOL",6)) { switch (CSLCount(papszToken)) { case 4: m_poCurFeature = new TABPoint(m_poDefn); break; case 7: m_poCurFeature = new TABFontPoint(m_poDefn); break; case 5: m_poCurFeature = new TABCustomPoint(m_poDefn); break; default: CSLDestroy(papszToken); CPLError(CE_Failure, CPLE_NotSupported, "GetFeatureRef() failed: invalid symbol " "line: '%s'", pszLine); return NULL; break; } } } CSLDestroy(papszToken); if (m_poCurFeature == NULL) { // No symbol clause... default to TABPoint m_poCurFeature = new TABPoint(m_poDefn); } } else if (EQUALN(pszLine,"LINE",4) || EQUALN(pszLine,"PLINE",5)) { m_poCurFeature = new TABPolyline(m_poDefn); } else if (EQUALN(pszLine,"REGION",6)) { m_poCurFeature = new TABRegion(m_poDefn); } else if (EQUALN(pszLine,"ARC",3)) { m_poCurFeature = new TABArc(m_poDefn); } else if (EQUALN(pszLine,"TEXT",4)) { m_poCurFeature = new TABText(m_poDefn); } else if (EQUALN(pszLine,"RECT",4) || EQUALN(pszLine,"ROUNDRECT",9)) { m_poCurFeature = new TABRectangle(m_poDefn); } else if (EQUALN(pszLine,"ELLIPSE",7)) { m_poCurFeature = new TABEllipse(m_poDefn); } else if (EQUALN(pszLine,"MULTIPOINT",10)) { m_poCurFeature = new TABMultiPoint(m_poDefn); } else { if (!EQUAL(pszLine,"")) CPLError(CE_Failure, CPLE_NotSupported, "Error during reading, unknown type %s.", pszLine); //m_poCurFeature = new TABDebugFeature(m_poDefn); return NULL; } } CPLAssert(m_poCurFeature); if (m_poCurFeature == NULL) return NULL; /*----------------------------------------------------------------- * Read fields from the .DAT file * GetRecordBlock() has already been called above... *----------------------------------------------------------------*/ if (m_poCurFeature->ReadRecordFromMIDFile(m_poMIDFile) != 0) { CPLError(CE_Failure, CPLE_NotSupported, "Error during reading Record."); delete m_poCurFeature; m_poCurFeature = NULL; return NULL; } /*----------------------------------------------------------------- * Read geometry from the .MAP file * MoveToObjId() has already been called above... *----------------------------------------------------------------*/ if (m_poCurFeature->ReadGeometryFromMIFFile(m_poMIFFile) != 0) { CPLError(CE_Failure, CPLE_NotSupported, "Error during reading Geometry."); delete m_poCurFeature; m_poCurFeature = NULL; return NULL; } /*--------------------------------------------------------------------- * The act of reading the geometry causes the first line of the * next object to be preloaded. Set the preloaded id appropriately. *--------------------------------------------------------------------- */ if( m_poMIFFile->GetLastLine() != NULL ) m_nPreloadedId++; else m_nPreloadedId = 0; /* Update the Current Feature ID */ m_poCurFeature->SetFID(m_nCurFeatureId); return m_poCurFeature;}/********************************************************************** * MIFFile::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 MIFFile::SetFeature(TABFeature *poFeature, int nFeatureId /*=-1*/){ if (m_eAccessMode != TABWrite) { 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_poMIDFile == NULL) { CPLError(CE_Failure, CPLE_IllegalArg, "SetFeature() failed: file is not opened!"); return -1; } if (m_bHeaderWrote == FALSE) { /*------------------------------------------------------------- * OK, this is the first feature in the dataset... make sure the * .MID schema has been initialized. *------------------------------------------------------------*/ if (m_poDefn == NULL) SetFeatureDefn(poFeature->GetDefnRef(), NULL); WriteMIFHeader(); nFeatureId = 1; } else { nFeatureId = ++ m_nWriteFeatureId; } /*----------------------------------------------------------------- * Write geometry to the .Mif file *----------------------------------------------------------------*/ if (m_poMIFFile == NULL || poFeature->WriteGeometryToMIFFile(m_poMIFFile) != 0) { CPLError(CE_Failure, CPLE_FileIO, "Failed writing geometry for feature id %d in %s", nFeatureId, m_pszFname); return -1; } if (m_poMIDFile == NULL || poFeature->WriteRecordToMIDFile(m_poMIDFile) != 0 ) { CPLError(CE_Failure, CPLE_FileIO, "Failed writing attributes for feature id %d in %s", nFeatureId, m_pszFname); return -1; } return nFeatureId; }/********************************************************************** * MIFFile::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 MIFFile * 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 *MIFFile::GetLayerDefn(){ return m_poDefn;}/********************************************************************** * MIFFile::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. * * This function will use poFeatureDefn to create a local copy that * will be used to build the .MID file, etc. * * Returns 0 on success, -1 on error. **********************************************************************/int MIFFile::SetFeatureDefn(OGRFeatureDefn *poFeatureDefn, TABFieldType *paeMapInfoNativeFieldTypes /* =NULL */){ int numFields; int nStatus = 0; /*----------------------------------------------------------------- * Check that call happens at the right time in dataset's life. *----------------------------------------------------------------*/ if ( m_eAccessMode == TABWrite && m_bHeaderWrote ) { CPLError(CE_Failure, CPLE_AssertionFailed, "SetFeatureDefn() must be called after opening a new " "dataset, but before writing the first feature to it."); return -1; } /*----------------------------------------------------------------- * Delete current feature defn if there is already one. * AddFieldNative() will take care of creating a new one for us. *----------------------------------------------------------------*/ if (m_poDefn && m_poDefn->Dereference() == 0) delete m_poDefn; m_poDefn = NULL; /*----------------------------------------------------------------- * Copy field information *----------------------------------------------------------------*/ numFields = poFeatureDefn->GetFieldCount(); for(int iField=0; iField<numFields; iField++) { TABFieldType eMapInfoType; OGRFieldDefn *poFieldDefn = poFeatureDefn->GetFieldDefn(iField); 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 = AddFieldNative(poFieldDefn->GetNameRef(), eMapInfoType, poFieldDefn->GetWidth(), poFieldDefn->GetPrecision(), FALSE, FALSE); } return nStatus;}/********************************************************************** * MIFFile::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(). * * Returns 0 on success, -1 on error. **********************************************************************/int MIFFile::AddFieldNative(const char *pszName, TABFieldType eMapInfoType, int nWidth /*=0*/, int nPrecision /*=0*/, GBool bIndexed /*=FALSE*/, GBool bUnique/*=FALSE*/){ OGRFieldDefn *poFieldDefn; char *pszCleanName = NULL; int nStatus = 0; /*----------------------------------------------------------------- * Check that call happens at the right time in dataset's life. *----------------------------------------------------------------*/ if ( m_eAccessMode == TABWrite && m_bHeaderWrote ) { CPLError(CE_Failure, CPLE_AssertionFailed, "AddFieldNative() must be called after opening a new " "dataset, but before writing the first feature to it."); return -1; } /*----------------------------------------------------------------- * Validate field width... must be <= 254 *----------------------------------------------------------------*/ if (nWidth > 254)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -