⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 miffile.cpp

📁 linux下一款GIS程序源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
    m_pszVersion = NULL;    UGK_Free(m_pszCharset);    m_pszCharset = NULL;    UGK_Free(m_pabFieldIndexed);    m_pabFieldIndexed = NULL;    UGK_Free(m_pabFieldUnique);    m_pabFieldUnique = NULL;    UGK_Free( m_pszIndex );    m_pszIndex = NULL;    UGK_Free(m_paeFieldType);    m_paeFieldType = NULL;    m_nCurFeatureId = 0;    m_nPreloadedId = 0;    m_nFeatureCount =0;       m_bBoundsSet = FALSE;    return 0;}/********************************************************************** *                   MIFFile::GetNextFeatureId() * * Returns feature id that follows nPrevId, or -1 if it is the * last feature id.  Pass nPrevId=-1 to fetch the first valid feature id. **********************************************************************/int MIFFile::GetNextFeatureId(int nPrevId){    if (m_eAccessMode != TABRead)    {        UGKError(ET_Failure, UGKErr_NotSupported,                 "GetNextFeatureId() can be used only with Read access.");        return -1;    }    if (nPrevId <= 0 && m_poMIFFile->GetLastLine() != NULL)        return 1;       // Feature Ids start at 1    else if (nPrevId > 0 && m_poMIFFile->GetLastLine() != NULL)        return nPrevId + 1;    else        return -1;    return 0;}/********************************************************************** *                   MIFFile::GotoFeature() * * Private method to move MIF and MID pointers ready to read specified  * feature.  Note that Feature Ids start at 1. * * Returns 0 on success, -1 on error (likely request for invalid feature id) **********************************************************************/int MIFFile::GotoFeature(int nFeatureId){    if (nFeatureId < 1)      return -1;    if (nFeatureId == m_nPreloadedId) // CorrectPosition    {        return 0;    }    else    {        if (nFeatureId < m_nCurFeatureId || m_nCurFeatureId == 0)            ResetReading();        while(m_nPreloadedId < nFeatureId)        {            if (NextFeature() == FALSE)              return -1;        }        assert(m_nPreloadedId == nFeatureId);        return 0;    }}/********************************************************************** *                   MIFFile::NextFeature() **********************************************************************/UGKBool MIFFile::NextFeature(){    const char *pszLine;    while ((pszLine = m_poMIFFile->GetLine()) != NULL)    {        if (m_poMIFFile->IsValidFeature(pszLine))        {            m_poMIDFile->GetLine();            m_nPreloadedId++;            return TRUE;        }    }    return FALSE;}/********************************************************************** *                   MIFFile::GetFeatureRef() * * Fill and return a TABFeature object for the specified feature id. * * The retruned pointer is a reference to an object owned and maintained * by this MIFFile 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(). * * Returns NULL if the specified feature id does not exist of if an * error happened.  In any case, CPLError() will have been called to * report the reason of the failure. **********************************************************************/TABFeature *MIFFile::GetFeatureRef(int nFeatureId){    const char *pszLine;    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_poMIDFile == NULL)    {        UGKError(ET_Failure, UGKErr_IllegalArg,                 "GetFeatureRef() failed: file is not opened!");        return NULL;    }    if (GotoFeature(nFeatureId)!= 0 )    {        UGKError(ET_Failure, UGKErr_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 = TokenizeString(pszLine);                        if (CountOfList(papszToken) !=3)            {                FreeStrList(papszToken);                UGKError(ET_Failure, UGKErr_NotSupported,                         "GetFeatureRef() failed: invalid point line: '%s'",                         pszLine);                return NULL;            }                        m_poMIFFile->SaveLine(pszLine);            if ((pszLine = m_poMIFFile->GetLine()) != NULL)            {                FreeStrList(papszToken);                papszToken = TokenizeStringComplex(pszLine," ,()\t",                                                      TRUE,FALSE);                if (CountOfList(papszToken)> 0 &&EQUALN(papszToken[0],"SYMBOL",6))                {                    switch (CountOfList(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:                        FreeStrList(papszToken);                        UGKError(ET_Failure, UGKErr_NotSupported,                                 "GetFeatureRef() failed: invalid symbol "                                 "line: '%s'", pszLine);                        return NULL;                        break;                    }                }            }            FreeStrList(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,""))               UGKError(ET_Failure, UGKErr_NotSupported,                   "Error during reading, unknown type %s.",                     pszLine);                    //m_poCurFeature = new TABDebugFeature(m_poDefn);            return NULL;        }    }    assert(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)    {        UGKError(ET_Failure, UGKErr_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)    {        UGKError(ET_Failure, UGKErr_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)    {        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_poMIDFile == NULL)    {        UGKError(ET_Failure, UGKErr_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)    {        UGKError(ET_Failure, UGKErr_FileIO,                 "Failed writing geometry for feature id %d in %s",                 nFeatureId, m_pszFname);        return -1;    }    if (m_poMIDFile == NULL ||        poFeature->WriteRecordToMIDFile(m_poMIDFile) != 0 )    {        UGKError(ET_Failure, UGKErr_FileIO,                 "Failed writing attributes for feature id %d in %s",                 nFeatureId, m_pszFname);        return -1;    }       return nFeatureId; }/********************************************************************** *                   MIFFile::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 MIFFile * 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 *MIFFile::GetLayerDefn(){    return m_poDefn;}/********************************************************************** *                   MIFFile::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. * * 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(UGKFeatureDefn *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 )    {        UGKError(ET_Failure, UGKErr_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;        UGKFieldDefn *poFieldDefn = poFeatureDefn->GetFieldDefn(iField);        if (paeMapInfoNativeFieldTypes)        {            eMapInfoType = paeMapInfoNativeFieldTypes[iField];        }        else        {            /*---------------------------------------------------------             * Map UGKFieldTypes to MapInfo native types             *--------------------------------------------------------*/            switch(poFieldDefn->GetType())            {              case OFTInteger:                eMapInfoType = TABFInteger;                break;              case OFTReal:                eMapInfoType = TABFFloat;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -