📄 mitab_miffile.cpp
字号:
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)
{
CPLError(CE_Failure, CPLE_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;
}
CPLAssert(m_nPreloadedId == nFeatureId);
return 0;
}
}
/**********************************************************************
* MIFFile::NextFeature()
**********************************************************************/
GBool 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)
{
CPLError(CE_Failure, CPLE_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)
{
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 (EQUALN(pszLine,"COLLECTION",10))
{
m_poCurFeature = new TABCollection(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;
}
/**********************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -