📄 mitab_miffile.cpp
字号:
{ 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 */ /*----------------------------------------------------------------- * 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(); } /*----------------------------------------------------------------- * 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 *------------------------------------------------*/ poFieldDefn = new OGRFieldDefn(pszCleanName, OFTReal); poFieldDefn->SetWidth(nWidth); poFieldDefn->SetPrecision(nPrecision); break; case TABFFloat: /*------------------------------------------------- * FLOAT type *------------------------------------------------*/ poFieldDefn = new OGRFieldDefn(pszCleanName, OFTReal); break; case TABFDate: /*------------------------------------------------- * DATE type (returned as a string: "DD/MM/YYYY") *------------------------------------------------*/ poFieldDefn = new OGRFieldDefn(pszCleanName, OFTString); poFieldDefn->SetWidth(10); break; case TABFLogical: /*------------------------------------------------- * LOGICAL type (value "T" or "F") *------------------------------------------------*/ poFieldDefn = new OGRFieldDefn(pszCleanName, OFTString); poFieldDefn->SetWidth(1); break; default: CPLError(CE_Failure, CPLE_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 *)CPLRealloc(m_paeFieldType, m_poDefn->GetFieldCount()* sizeof(TABFieldType)); m_paeFieldType[m_poDefn->GetFieldCount()-1] = eMapInfoType; /*----------------------------------------------------------------- * Extend array of Indexed/Unique flags *----------------------------------------------------------------*/ m_pabFieldIndexed = (GBool *)CPLRealloc(m_pabFieldIndexed, m_poDefn->GetFieldCount()* sizeof(GBool)); m_pabFieldUnique = (GBool *)CPLRealloc(m_pabFieldUnique, m_poDefn->GetFieldCount()* sizeof(GBool)); m_pabFieldIndexed[m_poDefn->GetFieldCount()-1] = bIndexed; m_pabFieldUnique[m_poDefn->GetFieldCount()-1] = bUnique; CPLFree(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() ************************************************************************/GBool 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() ************************************************************************/GBool MIFFile::IsFieldUnique( int nFieldId ){ if ( m_poDefn==NULL || m_pabFieldUnique==NULL || nFieldId < 0 || nFieldId >= m_poDefn->GetFieldCount()) return FALSE; return m_pabFieldUnique[nFieldId];}/************************************************************************//* MIFFile::SetSpatialRef() *//************************************************************************/int MIFFile::SetSpatialRef( OGRSpatialReference * poSpatialRef ){ CPLFree( m_pszCoordSys ); m_pszCoordSys = MITABSpatialRef2CoordSys( poSpatialRef ); return( m_pszCoordSys != NULL );}/************************************************************************//* 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 = CPLStrdup(pszMIFCoordSys + 9); } else { pszCoordSys = CPLStrdup(pszMIFCoordSys); } // Extract bounds if present papszFields = CSLTokenizeStringComplex(pszCoordSys, " ,()\t", TRUE, FALSE ); iBounds = CSLFindString( papszFields, "Bounds" ); if (iBounds >= 0 && iBounds + 4 < CSLCount(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'; } CSLDestroy( papszFields ); // Assign the CoordSys CPLFree( m_pszCoordSys ); m_pszCoordSys = CPLStrdup(pszCoordSys); CPLFree(pszCoordSys); return( m_pszCoordSys != NULL );}/************************************************************************//* MIFFile::GetSpatialRef() *//************************************************************************/OGRSpatialReference *MIFFile::GetSpatialRef(){ if( m_poSpatialRef == NULL ) m_poSpatialRef = MITABCoordSys2SpatialRef( m_pszCoordSys ); return m_poSpatialRef;}/********************************************************************** * 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) { CPLError(CE_Failure, CPLE_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, GBool 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, GBool 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. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -