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

📄 miffile.cpp

📁 linux下一款GIS程序源码
💻 CPP
📖 第 1 页 / 共 4 页
字号:
                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 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 MIFFile::AddFieldNative(const char *pszName, TABFieldType eMapInfoType,                            int nWidth /*=0*/, int nPrecision /*=0*/,                            UGKBool bIndexed /*=FALSE*/, UGKBool bUnique/*=FALSE*/){    UGKFieldDefn *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 )    {        UGKError(ET_Failure, UGKErr_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)    {        UGKError(ET_Warning, UGKErr_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 UGK) to a valid default     *----------------------------------------------------------------*/    if (eMapInfoType == TABFDecimal && nWidth == 0)        nWidth=20;    else if (nWidth == 0)        nWidth=254; /* char fields */    /*-----------------------------------------------------------------     * Create new UGKFeatureDefn if not done yet...     *----------------------------------------------------------------*/    if (m_poDefn == NULL)    {        char *pszFeatureClassName = TABGetBasename(m_pszFname);        m_poDefn = new UGKFeatureDefn(pszFeatureClassName);        UGK_Free(pszFeatureClassName);        // Ref count defaults to 0... set it to 1        m_poDefn->Reference();    }    /*-----------------------------------------------------------------     * 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 UGK types     *----------------------------------------------------------------*/    poFieldDefn = NULL;    switch(eMapInfoType)    {      case TABFChar:        /*-------------------------------------------------         * CHAR type         *------------------------------------------------*/        poFieldDefn = new UGKFieldDefn(pszCleanName, OFTString);        poFieldDefn->SetWidth(nWidth);        break;      case TABFInteger:        /*-------------------------------------------------         * INTEGER type         *------------------------------------------------*/        poFieldDefn = new UGKFieldDefn(pszCleanName, OFTInteger);        break;      case TABFSmallInt:        /*-------------------------------------------------         * SMALLINT type         *------------------------------------------------*/        poFieldDefn = new UGKFieldDefn(pszCleanName, OFTInteger);        break;      case TABFDecimal:        /*-------------------------------------------------         * DECIMAL type         *------------------------------------------------*/        poFieldDefn = new UGKFieldDefn(pszCleanName, OFTReal);        poFieldDefn->SetWidth(nWidth);        poFieldDefn->SetPrecision(nPrecision);        break;      case TABFFloat:        /*-------------------------------------------------         * FLOAT type         *------------------------------------------------*/        poFieldDefn = new UGKFieldDefn(pszCleanName, OFTReal);        break;      case TABFDate:        /*-------------------------------------------------         * DATE type (returned as a string: "DD/MM/YYYY")         *------------------------------------------------*/        poFieldDefn = new UGKFieldDefn(pszCleanName, OFTString);        poFieldDefn->SetWidth(10);        break;      case TABFLogical:        /*-------------------------------------------------         * LOGICAL type (value "T" or "F")         *------------------------------------------------*/        poFieldDefn = new UGKFieldDefn(pszCleanName, OFTString);        poFieldDefn->SetWidth(1);        break;      default:        UGKError(ET_Failure, UGKErr_NotSupported,                 "Unsupported type for field %s", pszName);        return -1;    }    /*-----------------------------------------------------     * Add the FieldDefn to the FeatureDefn      *----------------------------------------------------*/    m_poDefn->AddFieldDefn(poFieldDefn);    delete poFieldDefn;    /*-----------------------------------------------------------------     * Keep track of native field type     *----------------------------------------------------------------*/    m_paeFieldType = (TABFieldType *)UGK_Realloc(m_paeFieldType,                                                m_poDefn->GetFieldCount()*                                                sizeof(TABFieldType));    m_paeFieldType[m_poDefn->GetFieldCount()-1] = eMapInfoType;    /*-----------------------------------------------------------------     * Extend array of Indexed/Unique flags     *----------------------------------------------------------------*/    m_pabFieldIndexed = (UGKBool *)UGK_Realloc(m_pabFieldIndexed,                                            m_poDefn->GetFieldCount()*                                            sizeof(UGKBool));    m_pabFieldUnique  = (UGKBool *)UGK_Realloc(m_pabFieldUnique,                                            m_poDefn->GetFieldCount()*                                            sizeof(UGKBool));    m_pabFieldIndexed[m_poDefn->GetFieldCount()-1] = bIndexed;    m_pabFieldUnique[m_poDefn->GetFieldCount()-1] = bUnique;    UGK_Free(pszCleanName);    return nStatus;}/********************************************************************** *                   MIFFile::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. **********************************************************************/TABFieldType MIFFile::GetNativeFieldType(int nFieldId){    if ( m_poDefn==NULL || m_paeFieldType==NULL ||         nFieldId < 0 || nFieldId >= m_poDefn->GetFieldCount())        return TABFUnknown;    return m_paeFieldType[nFieldId];}/************************************************************************ *                       MIFFile::SetFieldIndexed() ************************************************************************/int MIFFile::SetFieldIndexed( int nFieldId ){    if ( m_poDefn==NULL || m_pabFieldIndexed==NULL ||         nFieldId < 0 || nFieldId >= m_poDefn->GetFieldCount())        return -1;    m_pabFieldIndexed[nFieldId] = TRUE;    return 0;}/************************************************************************ *                       MIFFile::IsFieldIndexed() ************************************************************************/UGKBool MIFFile::IsFieldIndexed( int nFieldId ){    if ( m_poDefn==NULL || m_pabFieldIndexed==NULL ||         nFieldId < 0 || nFieldId >= m_poDefn->GetFieldCount())        return FALSE;    return m_pabFieldIndexed[nFieldId];}/************************************************************************ *                       MIFFile::IsFieldUnique() ************************************************************************/UGKBool MIFFile::IsFieldUnique( int nFieldId ){    if ( m_poDefn==NULL || m_pabFieldUnique==NULL ||         nFieldId < 0 || nFieldId >= m_poDefn->GetFieldCount())        return FALSE;    return m_pabFieldUnique[nFieldId];}/************************************************************************//*                      MIFFile::SetMIFCoordSys()                       *//************************************************************************/int MIFFile::SetMIFCoordSys(const char * pszMIFCoordSys){    char        **papszFields, *pszCoordSys;    int         iBounds;    // Extract the word 'COORDSYS' if present    if (EQUALN(pszMIFCoordSys,"COORDSYS",8) )    {        pszCoordSys = UGKStrdup(pszMIFCoordSys + 9);    }    else    {        pszCoordSys = UGKStrdup(pszMIFCoordSys);    }    // Extract bounds if present    papszFields = TokenizeStringComplex(pszCoordSys, " ,()\t",                                           TRUE, FALSE );    iBounds = FindStrInList( papszFields, "Bounds" );    if (iBounds >= 0 && iBounds + 4 < CountOfList(papszFields))    {        m_dXMin = atof(papszFields[++iBounds]);        m_dYMin = atof(papszFields[++iBounds]);        m_dXMax = atof(papszFields[++iBounds]);        m_dYMax = atof(papszFields[++iBounds]);        m_bBoundsSet = TRUE;        pszCoordSys[strstr(pszCoordSys, "Bounds") - pszCoordSys] = '\0';    }    FreeStrList( papszFields );    // Assign the CoordSys    UGK_Free( m_pszCoordSys );    m_pszCoordSys = UGKStrdup(pszCoordSys);    UGK_Free(pszCoordSys);    return( m_pszCoordSys != NULL );}/********************************************************************** *                   MIFFile::UpdateExtents() * * Private Methode used to update the dataset extents **********************************************************************/void MIFFile::UpdateExtents(double dfX, double dfY){    if (m_bExtentsSet == FALSE)    {        m_bExtentsSet = TRUE;        m_sExtents.MinX = m_sExtents.MaxX = dfX;        m_sExtents.MinY = m_sExtents.MaxY = dfY;    }    else    {        if (dfX < m_sExtents.MinX)            m_sExtents.MinX = dfX;        if (dfX > m_sExtents.MaxX)          m_sExtents.MaxX = dfX;        if (dfY < m_sExtents.MinY)          m_sExtents.MinY = dfY;        if (dfY > m_sExtents.MaxY)          m_sExtents.MaxY = dfY;    }}/********************************************************************** *                   MIFFile::SetBounds() * * Set projection coordinates bounds of the newly created dataset. * * This function must be called after creating a new dataset and before any * feature can be written to it. * * Returns 0 on success, -1 on error. **********************************************************************/int MIFFile::SetBounds(double dXMin, double dYMin,                        double dXMax, double dYMax){    if (m_eAccessMode != TABWrite)    {        UGKError(ET_Failure, UGKErr_NotSupported,                 "SetBounds() can be used only with Write access.");        return -1;    }    m_dXMin = dXMin;    m_dXMax = dXMax;    m_dYMin = dYMin;    m_dYMax = dYMax;    m_bBoundsSet = TRUE;        return 0; }/********************************************************************** *                   MIFFile::GetFeatureCountByType() * * Return number of features of each type. * * NOTE: The current implementation always returns -1 for MIF files *       since this would require scanning the whole file. * * When properly implemented, the bForce flag will force scanning the * whole file by default. * * Returns 0 on success, or silently returns -1 (with no error) if this * information is not available. **********************************************************************/int MIFFile::GetFeatureCountByType(int &numPoints, int &numLines,                                   int &numRegions, int &numTexts,                                   UGKBool bForce ){    if( m_bPreParsed || bForce )    {        PreParseFile();        numPoints = m_nPoints;        numLines = m_nLines;        numRegions = m_nRegions;        numTexts = m_nTexts;        return 0;    }    else    {        numPoints = numLines = numRegions = numTexts = 0;        return -1;    }}/********************************************************************** *                   MIFFile::GetBounds() * * Fetch projection coordinates bounds of a dataset. * * Pass bForce=FALSE to avoid a scan of the whole file if the bounds * are not already available. * * Returns 0 on success, -1 on error or if bounds are not available and * bForce=FALSE. **********************************************************************/int MIFFile::GetBounds(double &dXMin, double &dYMin,                        double &dXMax, double &dYMax,                       UGKBool bForce /*= TRUE*/ ){        if (m_bBoundsSet == FALSE && bForce == FALSE)    {        return -1;    }    else if (m_bBoundsSet == FALSE)    {        PreParseFile();    }    if (m_bBoundsSet == FALSE)    {        return -1;    }    dXMin = m_dXMin;    dXMax = m_dXMax;    dYMin = m_dYMin;    dYMax = m_dYMax;        return 0;}/********************************************************************** *                   MIFFile::GetExtent() * * Fetch extent of the data currently stored in the dataset.  We collect * this information while preparsing the file ... often already done for * other reasons, and if not it is still faster than fully reading all * the features just to count them. * * Returns UGKERR_NONE/UGKRERR_FAILURE. **********************************************************************/UGKErr MIFFile::GetExtent (UGKEnvelope *psExtent, int bForce){    if (bForce == TRUE)        PreParseFile();    if (m_bPreParsed)    {        *psExtent = m_sExtents;        return UGKERR_NONE;    }    else        return UGKERR_FAILURE;}/************************************************************************//*                           TestCapability()                           *//************************************************************************/int MIFFile::TestCapability( const char * pszCap ){    if( EQUAL(pszCap,OLCRandomRead) )        return TRUE;    else if( EQUAL(pszCap,OLCSequentialWrite) )        return TRUE;    else if( EQUAL(pszCap,OLCSequentialWrite) )        return FALSE;    else if( EQUAL(pszCap,OLCFastFeatureCount) )        return m_bPreParsed;    else if( EQUAL(pszCap,OLCFastSpatialFilter) )        return FALSE;    else if( EQUAL(pszCap,OLCFastGetExtent) )        return m_bPreParsed;    else         return FALSE;}

⌨️ 快捷键说明

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