Transfer.cpp
上传用户:bjslfz
上传日期:2022-07-25
资源大小:4430k
文件大小:42k
- // Transfer.cpp: implementation of the CTransfer class.
- //
- //////////////////////////////////////////////////////////////////////
- #include "stdafx.h"
- #include "SHP2VCT.h"
- #include "Transfer.h"
- #include "ShpFile.h"
- #include "MapRecordSet.h"
- #include "MapField.h"
- #include "MapFields.h"
- #ifdef _DEBUG
- #undef THIS_FILE
- static char THIS_FILE[]=__FILE__;
- #define new DEBUG_NEW
- #endif
- //////////////////////////////////////////////////////////////////////
- // Construction/Destruction
- //////////////////////////////////////////////////////////////////////
- CTransfer::CTransfer(CString source,CString target)
- {
- if(source.Right(4)==".shp" && target.Right(4)==".vct")
- {
- int i;
- m_bShpOpen = FALSE;
- m_bShxOpen = FALSE;
- m_shpType = NULLSHP;
-
- /* -------------------------------------------------------------------- */
- /* Establish the byte order on this machine. */
- /* -------------------------------------------------------------------- */
- i = 1;
- if( *((uchar *) &i) == 1 )
- m_bBigEndian = FALSE;
- else
- m_bBigEndian = TRUE;
-
- if(!SHP2VCT(source,target))
- AfxMessageBox("数据转换失败",MB_OK,MB_ICONINFORMATION);
- else
- AfxMessageBox("数据转换完成",MB_OK,MB_ICONINFORMATION);
- }
- AfxMessageBox("源文件:"+ source +"n目标文件:" + target,MB_OK,MB_ICONINFORMATION);
-
- }
- /*************************************************
- 描述: 读入DBF文件格式
- 输入: 文件名(全路径)
- 输出: 成功返回TRUE 失败返回FALSE
- *************************************************/
- CTransfer::~CTransfer()
- {
- CMapPoint* pPt;
- CMapPoints* pPts;
- CMapLine* pLine;
- CMapPolygon* pPolygon;
-
- while(!m_ObList.IsEmpty())
- {
- switch ( m_shpType )
- {
- case POINT:
- pPt =(CMapPoint*)m_ObList.RemoveTail();
- delete pPt;
- break;
- case MULTIPOINT:
- pPts =(CMapPoints*)m_ObList.RemoveTail();
- delete pPts;
- break;
-
- case POLYLINE:
- pLine =(CMapLine*)m_ObList.RemoveTail();
- delete pLine;
- break;
- case POLYGON:
- pPolygon =(CMapPolygon*)m_ObList.RemoveTail();
- delete pPolygon;
- break;
- default:
- m_ObList.RemoveTail();
- break;
-
- }
- }
- if ( m_bShpOpen )
- m_fShp.Close();
- if (m_bShxOpen)
- m_fShx.Close();
- m_pRecordSet->fDbf.Close();
- if ( m_pRecordSet )
- delete m_pRecordSet;
- if ( m_bVctOpen)
- fclose(m_pVctFile);
-
- }
- /*************************************************
- 描述: 读入DBF文件格式
- 输入: 文件名(全路径)
- 输出: 成功返回TRUE 失败返回FALSE
- *************************************************/
- BOOL CTransfer::ReadDBF(CString& csFileName)
- {
- CString csDbfName;
- BOOL bResult;
- //创建记录集对象
- m_pRecordSet = new CMapRecordSet;
- ASSERT ( m_pRecordSet != NULL );
- csDbfName = csFileName.Left(csFileName.GetLength() - 3);
- csDbfName = csDbfName + "dbf";
- //打开DBF文件
- bResult = m_pRecordSet->openDBF(csDbfName);
- if ( !bResult )
- delete m_pRecordSet;
- return bResult;
- }
- /*************************************************
- 描述: 打开要操作的文件,读文件头及记录
- 输入: 源文件名,目标文件名
- 输出: 成功返回TRUE 失败返回FALSE
- *************************************************/
- BOOL CTransfer::SHP2VCT(CString& sourceFileName,CString& targetFileName)
- {
-
- int iTemp;
- CString csShxName;
- CFileException fe;
- SHPHEADER varHeader;
-
- //打开主文件
- if ( !m_fShp.Open(sourceFileName, CFile::modeRead|CFile::shareDenyWrite,&fe))
- return FALSE;
- m_bShpOpen = TRUE;
-
- //打开索引文件
- csShxName = sourceFileName.Left(sourceFileName.GetLength() - 3);
- csShxName = csShxName + "shx";
- //AfxMessageBox(csShxName);
- if ( !m_fShx.Open(csShxName, CFile::modeRead|CFile::shareDenyWrite,&fe))
- return FALSE;
- m_bShxOpen = TRUE;
- /////////////////////////////////////////////////////////////////////////
- TRY
- {
- //读主文件头 长100字节
- if ( m_fShp.Read(&varHeader , sizeof(SHPHEADER))!= sizeof(SHPHEADER))
- return FILE_READERR;
- iTemp = varHeader.iFileCode;
- if ( !m_bBigEndian )
- SwapWord(sizeof(int),&iTemp);
- if ( iTemp != 9994 ) //是否是shp文件
- return FILE_CODEERR;
- if ( varHeader.iVersion != FILE_VERSION ) //文件版本是否正确
- return FILE_VERSIONERR;
- //shp 类型
- m_shpType = varHeader.iShpType;
- m_shpFileLength = varHeader.iFileLength;
- if ( !m_bBigEndian )
- SwapWord(sizeof(int),&m_shpFileLength);
- //保存数据最大矩形范围
- m_Extent.SetLeft(varHeader.dbXMin);
- m_Extent.SetRight(varHeader.dbXMax);
- m_Extent.SetTop(varHeader.dbYMin);
- m_Extent.SetBottom(varHeader.dbYMax);
-
- //读索引文件头 长100字节
- if ( m_fShx.Read(&varHeader , sizeof(SHPHEADER))!= sizeof(SHPHEADER))
- return FILE_READERR;
- iTemp = varHeader.iFileCode;
- if ( !m_bBigEndian )
- SwapWord(sizeof(int),&iTemp);
- if ( iTemp != 9994 ) //是否是shx文件
- return FILE_CODEERR;
- if ( varHeader.iVersion != FILE_VERSION ) //文件版本是否正确
- return FILE_VERSIONERR;
- m_shxFileLength = varHeader.iFileLength;
- if ( !m_bBigEndian )
- SwapWord(sizeof(int),&m_shxFileLength);
- //通过索引文件计算主文件记录个数 文件长度数值以16位计
- m_iRecordCount = ((m_shxFileLength - 50 )*2)/sizeof(SHXRECORD);
- if ( !ReadRecord() )
- return FILE_READERR;
- if ( !ReadDBF(sourceFileName))
- return FALSE;
- }
- CATCH(CFileException ,eload)
- {
- m_fShp.Abort();
- return FALSE;
- }
- END_CATCH
-
- if(!WriteVctData(targetFileName))
- {
- AfxMessageBox("无法转换!");
- }
-
- return TRUE;
-
- }
- /*************************************************
- 描述: 读入Shp对象坐标
- 输入: 无
- 输出: 成功返回TRUE 失败返回FALSE
- *************************************************/
- BOOL CTransfer::ReadRecord()
- {
- int i,j,k,m;
- int iDataLen,iLength,iIndex;
- int *pIParts;
- double dbTmp;
- char *pszBuffer,*lpVer;
- BOOL bEof;
- SHPINFO shpIn;
- SHPRECORDHEADER RecordHeader;
- CMapRectangle objRectangle;
- CMapPoint *pPoint;
- CMapPoints *pPoints;
- CMapParts *pParts;
- CMapLine *pLine;
- CMapPolygon *pPolygon;
-
-
- bEof = FALSE;
- switch ( m_shpType )
- {
- case NULLSHP:
- return FALSE;
- break;
- case POINT:
- {
-
- SHPPTCONTENT point;
- //读入点记录
- for ( i = 1 ; i <= m_iRecordCount ; i++ )
- {
- iLength = SetRecordPos(i);
- if ( iLength <= 0 )
- return FALSE;
-
- //获得记录头信息
- if (!GetRecordHeader(RecordHeader))
- return FALSE;
- //记录内容长度是否与shp实体大小一致,索引记录长度是否与记录内容长度一致
- if ( RecordHeader.iContentLength*2 != sizeof(point)||
- RecordHeader.iContentLength*2 != iLength)
- return FALSE;
- if(m_fShp.Read(&point,sizeof(point))!= sizeof(point))
- return FALSE;
- pPoint = new CMapPoint;
- iIndex = i - 1;
- pPoint->SetIndex(iIndex);
- if ( pPoint == NULL )
- return FALSE;
- //类型是否匹配
- if ( point.iShpType != m_shpType)
- return FALSE;
- pPoint->SetX(point.dbX );
- pPoint->SetY(point.dbY );
- m_ObList.AddTail(pPoint);
-
- }
- }
- break;
- case POLYLINE:
-
- pszBuffer = new char [MAX_BUFFER_SIZE]; //分配缓冲区
- if ( pszBuffer == NULL )
- return FALSE;
- memset(pszBuffer , 0 , MAX_BUFFER_SIZE);
-
- //读入线记录
- for ( i = 1 ; i <= m_iRecordCount ; i++ ) //m_iRecordCount
- {
-
- iLength = SetRecordPos(i);
- if ( iLength <= 0 )
- return FALSE;
-
- pLine = new CMapLine();
- iIndex = i - 1;
- pLine->SetIndex(iIndex);
- if ( pLine == NULL )
- return FALSE;
- //获得记录头信息
- if (!GetRecordHeader(RecordHeader))
- return FALSE;
- if ( !GetShpInfo(shpIn))
- return FALSE;
- if (shpIn.ishpType != POLYLINE )//类型不匹配
- {
- delete pLine;
- continue;
- }
- if ( shpIn.iNumParts*sizeof(int) > MAX_BUFFER_SIZE )
- {
- //多义线段数大于最大缓冲区长度,忽略该对象
- delete pLine;
- continue;
- }
- //计算对象内容实际长度
- j = sizeof(SHPINFO) + shpIn.iNumParts*sizeof(int) ;
- j += shpIn.iNumPoints*sizeof(SHPPOINT);
-
- //判断实际长度是否与索引文件中记录的一致
- if ( RecordHeader.iContentLength*2 != j )
- {
- delete pLine;
- continue;
- }
- //设置shp矩形范围
- objRectangle.SetLeft(shpIn.Box[0].dbX);
- objRectangle.SetTop(shpIn.Box[0].dbY);
- objRectangle.SetRight(shpIn.Box[1].dbX);
- objRectangle.SetBottom(shpIn.Box[1].dbY);
- pLine->SetExtent(objRectangle);
- pIParts = new int[shpIn.iNumParts];
- if ( pIParts == NULL )
- {
- delete pLine;
- return FALSE;
- }
- //读入多义线段索引
- if ( m_fShp.Read(pIParts,shpIn.iNumParts*4) != (uint)(shpIn.iNumParts*4))
- {
- delete pLine;
- return FALSE;
- }
-
- //点坐标存储所占字节数
- iLength = shpIn.iNumPoints*sizeof(SHPPOINT);
- //初始化缓冲区数据
- iDataLen = ReadPoint(pszBuffer,iLength,bEof);
- if ( iDataLen < 0 )
- {
- delete pLine;
- delete pIParts;
- return FALSE;
-
- }
- lpVer = pszBuffer;
- for ( j = 0 ; j < shpIn.iNumParts ; j++ )
- {
- pParts = new CMapParts();
- pPoints = new CMapPoints();
- if ( pParts == NULL || pPoints == NULL)
- return FALSE;
- if ( j == shpIn.iNumParts - 1 )
- {
- k = pIParts[j]; //本段第一个顶点索引
- m = shpIn.iNumPoints ; //下一个段第一个顶点索引
- }
- else
- {
- k = pIParts[j];
- m = pIParts[j+1];
- }
- //处理第i段的顶点
- for ( ; k < m ; k++)
- {
- pPoint = new CMapPoint();
- if ( pPoint == NULL )
- return FALSE;
-
- //需要读入数据更新缓冲区
- if ( lpVer == pszBuffer + iDataLen && !bEof)
- {
- iDataLen = ReadPoint(pszBuffer,iLength,bEof);
- if ( iDataLen < 0 )
- {
- delete pPoint;
- delete pPoints;
- delete pLine;
- delete pIParts;
-
- return FALSE;
-
- }
- lpVer = pszBuffer;
- }
- dbTmp = *(double*)lpVer;
- pPoint->SetX(dbTmp);
- lpVer += 8;
- //需要读入数据更新缓冲区
- if ( lpVer == pszBuffer + iDataLen && !bEof)
- {
- iDataLen = ReadPoint(pszBuffer,iLength,bEof);
- if ( iDataLen < 0 )
- {
- delete pPoint;
- delete pPoints;
- delete pLine;
- delete pIParts;
- return FALSE;
-
- }
- lpVer = pszBuffer;
- }
- dbTmp = *(double*)(lpVer);
- lpVer += 8;
- pPoint->SetY(dbTmp);
- pPoints->Add(pPoint);
-
- }
- pParts->Add(pPoints);
- pLine->Add(pParts);
-
- }
- m_ObList.AddTail( pLine);
- delete []pIParts;
-
- }
- delete []pszBuffer;
-
- break;
- case POLYGON:
- pszBuffer = new char [MAX_BUFFER_SIZE]; //分配缓冲区
- if ( pszBuffer == NULL )
- return FALSE;
- memset(pszBuffer , 0 , MAX_BUFFER_SIZE);
-
- //读入多边形记录
- for ( i = 1 ; i <= m_iRecordCount ; i++ ) //m_iRecordCount
- {
- iLength = SetRecordPos(i);
- if ( iLength <= 0 )
- return FALSE;
-
- pPolygon = new CMapPolygon();
- iIndex = i - 1;
- pPolygon->SetIndex(iIndex);
- if (pPolygon == NULL )
- return FALSE;
- //获得记录头信息
- if (!GetRecordHeader(RecordHeader))
- return FALSE;
- if ( !GetShpInfo(shpIn))
- return FALSE;
- if (shpIn.ishpType != POLYGON )//类型不匹配
- {
- delete pPolygon;
- continue;
- }
- if ( shpIn.iNumParts*sizeof(int) > MAX_BUFFER_SIZE )
- {
- //复合多边型中的多边形个数大于最大缓冲区长度,忽略该对象
- delete pPolygon;
- continue;
- }
- //计算对象内容实际长度
- j = sizeof(SHPINFO) + shpIn.iNumParts*sizeof(int) ;
- j += shpIn.iNumPoints*sizeof(SHPPOINT);
-
- //判断实际长度是否与索引文件中记录的一致
- if ( RecordHeader.iContentLength*2 != j )
- {
- delete pPolygon;
- continue;
- }
- //设置shp矩形范围
- objRectangle.SetLeft(shpIn.Box[0].dbX);
- objRectangle.SetTop(shpIn.Box[0].dbY);
- objRectangle.SetRight(shpIn.Box[1].dbX);
- objRectangle.SetBottom(shpIn.Box[1].dbY);
- pPolygon->SetExtent(objRectangle);
- pIParts = new int[shpIn.iNumParts];
- if ( pIParts == NULL )
- {
- delete pPolygon;
- return FALSE;
- }
- //读入复合多边型段索引
- if ( m_fShp.Read(pIParts,shpIn.iNumParts*4) != (uint)(shpIn.iNumParts*4))
- {
- delete pPolygon;
- return FALSE;
- }
-
- //点坐标存储所占字节数
- iLength = shpIn.iNumPoints*sizeof(SHPPOINT);
- //初始化缓冲区数据
- iDataLen = ReadPoint(pszBuffer,iLength,bEof);
- if ( iDataLen < 0 )
- {
- delete pPolygon;
- delete pIParts;
- return FALSE;
-
- }
- lpVer = pszBuffer;
- for ( j = 0 ; j < shpIn.iNumParts ; j++ )
- {
- pParts = new CMapParts();
- pPoints = new CMapPoints();
- if ( pParts == NULL || pPoints == NULL)
- return FALSE;
- if ( j == shpIn.iNumParts - 1 )
- {
- k = pIParts[j]; //本段第一个顶点索引
- m = shpIn.iNumPoints ; //下一个段第一个顶点索引
- }
- else
- {
- k = pIParts[j];
- m = pIParts[j+1];
- }
- //处理第i段的顶点
- for ( ; k < m ; k++)
- {
- pPoint = new CMapPoint();
- if ( pPoint == NULL )
- return FALSE;
-
- //需要读入数据更新缓冲区
- if ( lpVer == pszBuffer + iDataLen && !bEof)
- {
- iDataLen = ReadPoint(pszBuffer,iLength,bEof);
- if ( iDataLen < 0 )
- {
- delete pPolygon;
- delete pIParts;
- return FALSE;
-
- }
- lpVer = pszBuffer;
- }
- dbTmp = *(double*)lpVer;
- pPoint->SetX(dbTmp);
- lpVer += 8;
- //需要读入数据更新缓冲区
- if ( lpVer == pszBuffer + iDataLen && !bEof)
- {
- iDataLen = ReadPoint(pszBuffer,iLength,bEof);
- if ( iDataLen < 0 )
- {
- delete pPolygon;
- delete pIParts;
- return FALSE;
-
- }
- lpVer = pszBuffer;
- }
- dbTmp = *(double*)(lpVer);
- pPoint->SetY(dbTmp);
- pPoints->Add(pPoint);
- lpVer += 8;
-
- }
- pParts->Add(pPoints);
- pPolygon->Add(pParts);
-
- }
- m_ObList.AddTail( pPolygon);
- delete []pIParts;
- }
- delete []pszBuffer;
- break;
- default:
- return FALSE;
- break;
-
- }
- return TRUE;
-
- }
- /*************************************************
- 描述: 计算每条shp对象相对文件头的偏移量
- 输入: 记录索引值(从零开始)
- 输出: 该shp对象数据在文件中的位置
- *************************************************/
- int CTransfer::SetRecordPos( int iRecord )
- {
- unsigned int iOffset,iTmp;
- SHXRECORD shxRD;
-
- if ( iRecord < 0 )
- return 0;
- //获得索引文件记录偏移量相对文件头
- if (iRecord == 1 )
- iOffset = sizeof(SHPHEADER) ;
- else
- iOffset = sizeof(SHPHEADER) + (iRecord-1)*sizeof(shxRecord) ;
- if ( iOffset > m_shxFileLength*2 - sizeof(shxRecord) )
- return 0;
- m_fShx.Seek( iOffset , CFile::begin );
- int m = sizeof(shxRD);
- m_fShx.Read( &shxRD , sizeof(shxRD));
- iTmp = shxRD.iOffset;
- SwapWord(sizeof(int),&iTmp);
- m_fShp.Seek(iTmp*2 , CFile::begin );
- iTmp = shxRD.iContentLength;
- SwapWord(sizeof(int),&iTmp);
-
-
- return iTmp*2;
- }
- /*************************************************
- 描述: 获得每条shp对象记录记录头的信息
- 输入: 记录头结构对象
- 输出: 成功返回TRUE 失败返回FALSE
- *************************************************/
- BOOL CTransfer::GetRecordHeader(SHPRECORDHEADER& RecordHeader )
- {
- int iLength,iNum;
-
- if(m_fShp.Read(&RecordHeader,sizeof(RecordHeader))!= sizeof(RecordHeader))
- return FALSE;
- if ( !m_bBigEndian )
- {
- iNum = RecordHeader.iRecordNum;
- iLength = RecordHeader.iContentLength;
- SwapWord(sizeof(int),&iLength);
- SwapWord(sizeof(int),&iNum);
- RecordHeader.iRecordNum = iNum;
- RecordHeader.iContentLength = iLength;
- }
- return TRUE;
-
- }
- /*************************************************
- 描述: 获得每条shp对象描述信息
- 输入: 描述信息结构对象
- 输出: 成功返回TRUE 失败返回FALSE
- *************************************************/
- BOOL CTransfer::GetShpInfo(SHPINFO& varInfo)
- {
-
- if(m_fShp.Read(&varInfo,sizeof(varInfo))!= sizeof(varInfo))
- return FALSE;
- return TRUE;
- }
- /*************************************************
- 描述: 读入点对象数据
- 输入: 数据缓冲区指针 缓冲区最大32K
- 如果超出需要分多次读取,要读取的长度、
- 是否已读取完成
- 输出: 读取数据的实际长度
- *************************************************/
- int CTransfer::ReadPoint(char* pszBuffer,int& iLength,BOOL& bEof)
- {
- if ( iLength > MAX_BUFFER_SIZE)
- {
- iLength -= MAX_BUFFER_SIZE;
- if ( m_fShp.Read(pszBuffer,MAX_BUFFER_SIZE) != MAX_BUFFER_SIZE )
- return FILE_READERR;
- bEof = FALSE;
- return MAX_BUFFER_SIZE;
- }
- else
- {
- if ( m_fShp.Read(pszBuffer,iLength) != (uint)iLength )
- return FILE_READERR;
- bEof = TRUE;
- return iLength;
- }
- }
- /*************************************************
- 描述: 向目标文件写文件头数据
- 输入: 目标VCT文件名
- 输出: 无
- *************************************************/
- void CTransfer::WriteVctFileHead(FILE* pfile)
- {
- fprintf(pfile,"%s%sn","DataMark: ",m_VctFilehead.DataMark);
- fprintf(pfile,"%s%2.1fn","VerSion: ",m_VctFilehead.VerSion);
- fprintf(pfile,"%s%cn","Unit: ",m_VctFilehead.Unit);
- fprintf(pfile,"%s%dn","Dim: ",m_VctFilehead.Dim);
- fprintf(pfile,"%s%dn","Topo: ",m_VctFilehead.Topo);
- if(m_VctFilehead.MinX!=0)
- fprintf(pfile,"%s%9.6fn","MinX: ",m_VctFilehead.MinX);
- if(m_VctFilehead.MinY!=0)
- fprintf(pfile,"%s%9.6fn","MinY: ",m_VctFilehead.MinY);
- if(m_VctFilehead.MinZ!=0)
- fprintf(pfile,"%s%9.6fn","MinZ: ",m_VctFilehead.MinZ);
- if(m_VctFilehead.MaxX!=0)
- fprintf(pfile,"%s%9.6fn","MaxX: ",m_VctFilehead.MaxX);
- if(m_VctFilehead.MaxY!=0)
- fprintf(pfile,"%s%9.6fn","MaxY: ",m_VctFilehead.MaxY);
- if(m_VctFilehead.MaxZ!=0)
- fprintf(pfile,"%s%9.6fn","MaxZ: ",m_VctFilehead.MaxZ);
- if(m_VctFilehead.ScaleM!=0)
- fprintf(pfile,"%s%9.0fn","ScaleM: ",m_VctFilehead.ScaleM);
- if(m_VctFilehead.Projection!="NO")
- fprintf(pfile,"%s%sn","Projection: ",m_VctFilehead.Projection);
- if(m_VctFilehead.Spheroid!="NO")
- fprintf(pfile,"%s%sn","Spheroid: ",m_VctFilehead.Spheroid);
- if(m_VctFilehead.Parameters!="NO")
- fprintf(pfile,"%s%sn","Parameters: ",m_VctFilehead.Parameters);
- if(m_VctFilehead.Date!="NO")
- fprintf(pfile,"%s%sn","Date: ",m_VctFilehead.Date);
- fprintf(pfile,"%s%cn","Separator: ",m_VctFilehead.Separator);
- ///暂没有坐标系Coordinate
- }
- /*************************************************
- 描述: 向目标文件写要素类型参数
- 输入: 目标VCT文件名
- 输出: 无
- *************************************************/
- void CTransfer::WriteVctFeature(FILE *pfile)
- {
- if(m_pVctFeature==NULL)
- {
- AfxMessageBox("被转入的文件没有要素类型参数信息");
- return;
- }
- VCTFeature* pVisitFeature = m_pVctFeature;
- while(pVisitFeature!=NULL)
- {
- fprintf(pfile,"%s,",pVisitFeature->Code);
- fprintf(pfile,"%s,",pVisitFeature->Name);
- fprintf(pfile,"%s,",pVisitFeature->Shape);
- fprintf(pfile,"%d,",pVisitFeature->Color);
- fprintf(pfile,"%s",pVisitFeature->ListName);
- fputs("n",pfile);
- pVisitFeature=pVisitFeature->pNext;
- }
- }
- /*************************************************
- 描述: 向目标文件写属性数据结构
- 输入: 目标VCT文件名
- 输出: 无
- *************************************************/
- void CTransfer::WriteVctTableStructure(FILE *pfile)
- {
- if(m_pVctTblStruct==NULL)
- {
- AfxMessageBox("被转入的文件没有属性数据结构信息");
- return;
- }
- VCTTableStructure *pVisitTblStruct=m_pVctTblStruct;
- while(pVisitTblStruct!=NULL)
- {
- fprintf(pfile,"%s,",pVisitTblStruct->ListName);
- fprintf(pfile,"%dn",pVisitTblStruct->ItemNum);
- for(int i=0;i<pVisitTblStruct->ItemNum;i++)
- {
- fprintf(pfile,"%s,",pVisitTblStruct->pItem[i].Name);
- fprintf(pfile,"%s,",pVisitTblStruct->pItem[i].Type);
- if(pVisitTblStruct->pItem[i].Type=="Char"||pVisitTblStruct->pItem[i].Type=="Integer")
- fprintf(pfile,"%dn",pVisitTblStruct->pItem[i].Width);
- if(pVisitTblStruct->pItem[i].Type=="Float")
- {
- fprintf(pfile,"%d,",pVisitTblStruct->pItem[i].Width);
- fprintf(pfile,"%dn",pVisitTblStruct->pItem[i].Pric);
- }
- }
- pVisitTblStruct=pVisitTblStruct->pTblNext;
- }
- }
- /*************************************************
- 描述: 向目标文件写图形数据
- 输入: 目标VCT文件名
- 输出: 无
- *************************************************/
- void CTransfer::WriteVctGraphy(FILE *pfile)
- {
- fputs("PointBeginn",pfile);
- WriteVctPoint(pfile);
- fputs("PointEndn",pfile);
- fputs("n",pfile);
-
- fputs("LineBeginn",pfile);
- WriteVctLine(pfile);
- fputs("LineEndn",pfile);
- fputs("n",pfile);
-
- fputs("PolygonBeginn",pfile);
- WriteVctPolygon(pfile);
- fputs("PolygonEndn",pfile);
- fputs("n",pfile);
- }
- /*************************************************
- 描述: 向目标文件写图形数据点数据
- 输入: 目标VCT文件名
- 输出: 无
- *************************************************/
- void CTransfer::WriteVctPoint(FILE *pfile)
- {
- if(m_pVctPointData==NULL)
- {
- //AfxMessageBox("被转入的文件没有点数据信息");
- return;
- }
- VCTPointData *pVisitPoint=m_pVctPointData;
- while(pVisitPoint!=NULL)
- {
- fprintf(pfile,"%sn",pVisitPoint->AimCode);
- fprintf(pfile,"%sn",pVisitPoint->FeatureCode);
- fprintf(pfile,"%sn",pVisitPoint->LayerName);
- fprintf(pfile,"%dn",pVisitPoint->PointMark);
- fprintf(pfile,"%f,",pVisitPoint->X);
- fprintf(pfile,"%fn",pVisitPoint->Y);
- //////////////////////////坐标Z未加
- if(m_VctFilehead.Topo==2 && pVisitPoint->PointMark==2)
- {
- //////////待扩充的模块
- }
- fputs("n",pfile);
- pVisitPoint=pVisitPoint->pNext;
- }
- }
- /*************************************************
- 描述: 向目标文件写图形数据线数据
- 输入: 目标VCT文件名
- 输出: 无
- *************************************************/
- void CTransfer::WriteVctLine(FILE *pfile)
- {
- /////写线数据
- if(m_pVctLineData==NULL)
- {
- //AfxMessageBox("被转入的文件没有线数据信息");
- return;
- }
- VCTLineData *pVisitLine=m_pVctLineData;
- while(pVisitLine!=NULL)
- {
- fprintf(pfile,"%sn",pVisitLine->AimCode);
- fprintf(pfile,"%sn",pVisitLine->FeatureCode);
- fprintf(pfile,"%sn",pVisitLine->LayerName);
- fprintf(pfile,"%dn",pVisitLine->LineMark);
- if(pVisitLine->LineMark!=100)
- {
- fprintf(pfile,"%dn",pVisitLine->PointNum);
- for(int i=0;i<pVisitLine->PointNum;i++)
- {
- fprintf(pfile,"%f,",pVisitLine->pMemberPoint->X);
- fprintf(pfile,"%fn",pVisitLine->pMemberPoint->Y);
- ///坐标Z暂未加
- pVisitLine->pMemberPoint = pVisitLine->pMemberPoint->pNext;
- }
- }
- else
- {
- ///////写间接坐标
- fprintf(pfile,"%dn",pVisitLine->ChildNum);
- int cout=GetShang(pVisitLine->ChildNum,8);
- for(int i=0;i<cout+1;i++)
- {
- if(i!=cout)
- {
- for(int j=0;j<8;j++)
- {
- if(j==7)
- fprintf(pfile,"%d",pVisitLine->pMemberChild[i*8+j]);
- else
- fprintf(pfile,"%d,",pVisitLine->pMemberChild[i*8+j]);
- }
- }
- else
- {
- for(int j=0;j<pVisitLine->ChildNum%8;j++)
- {
- if(j==(pVisitLine->ChildNum%8)-1)
- fprintf(pfile,"%d",pVisitLine->pMemberChild[cout*8+j]);
- else
- fprintf(pfile,"%d,",pVisitLine->pMemberChild[cout*8+j]);
- }
- }
- fputs("n",pfile);
- }
- }
- fputs("n",pfile);
- pVisitLine=pVisitLine->pNext;
- }
- }
- /*************************************************
- 描述: 向目标文件写图形数据面数据
- 输入: 目标VCT文件名
- 输出: 无
- modify by xiejing 2008-6-5
- *************************************************/
- void CTransfer::WriteVctPolygon(FILE *pfile)
- {
- /////////写多边形
- if(m_pVctPolygon==NULL)
- {
- //AfxMessageBox("被转入的文件没有多边形数据信息");
- return;
- }
- VCTPolygonData* pVisitPolygon=m_pVctPolygon;
-
- while(pVisitPolygon!=NULL)
- {
- fprintf(pfile,"%sn",pVisitPolygon->AimCode);
- fprintf(pfile,"%sn",pVisitPolygon->FeatureCode);
- fprintf(pfile,"%sn",pVisitPolygon->LayerName);
- fprintf(pfile,"%f,",pVisitPolygon->labX);
- fprintf(pfile,"%fn",pVisitPolygon->labY);
- if(m_VctFilehead.Topo==0)
- { ///////写直接坐标
- fprintf(pfile,"%dn",pVisitPolygon->PointNum);
- while(pVisitPolygon->pMemberPoint != NULL)
- {
- fprintf(pfile,"%f,",pVisitPolygon->pMemberPoint->X);
- fprintf(pfile,"%fn",pVisitPolygon->pMemberPoint->Y);
- pVisitPolygon->pMemberPoint = pVisitPolygon->pMemberPoint->pNext;
- }
- fprintf(pfile,"%dn",0);
- }
- else
- {
- /////////写间接坐标
- fprintf(pfile,"%dn",pVisitPolygon->ChildNum);
- int cout=GetShang(pVisitPolygon->ChildNum,8);
- for(int i=0;i<cout+1;i++)
- {
- if(i!=cout)
- {
- for(int j=0;j<8;j++)
- {
- if(j==7)
- fprintf(pfile,"%d",pVisitPolygon->pMemberChild[i*8+j]);
- else
- fprintf(pfile,"%d,",pVisitPolygon->pMemberChild[i*8+j]);
- }
- }
- else
- {
- for(int j=0;j<(pVisitPolygon->ChildNum%8);j++)
- {
- if(j==(pVisitPolygon->ChildNum%8)-1)
- fprintf(pfile,"%d",pVisitPolygon->pMemberChild[cout*8+j]);
- else
- fprintf(pfile,"%d,",pVisitPolygon->pMemberChild[cout*8+j]);
- }
- }
- fputs("n",pfile);
- }
- }
- fputs("n",pfile);
- pVisitPolygon=pVisitPolygon->pNext;
- }
- }
- /*************************************************
- 描述: 向目标文件写注记
- 输入: 目标VCT文件名
- 输出: 无
- *************************************************/
- void CTransfer::WriteVctAnnotation(FILE *pfile)
- {
- /////////////写注记
- if(m_pVctAnnotation==NULL)
- {
- //AfxMessageBox("被转入的文件没有注记信息");
- return;
- }
- VCTAnnotation *pVisitAnnotation=m_pVctAnnotation;
- while(pVisitAnnotation!=NULL)
- {
- fprintf(pfile,"%sn",pVisitAnnotation->AimCode);
- fprintf(pfile,"%sn",pVisitAnnotation->FeatureCode);
- fprintf(pfile,"%sn",pVisitAnnotation->LayerName);
- fprintf(pfile,"%sn",pVisitAnnotation->Font);
- fprintf(pfile,"%dn",pVisitAnnotation->Color);
- fprintf(pfile,"%d,",pVisitAnnotation->Pound);
- fprintf(pfile,"%d,",pVisitAnnotation->Shape);
- fprintf(pfile,"%cn",pVisitAnnotation->Udline);
- if(pVisitAnnotation->Size>0&&pVisitAnnotation->Size<100)
- fprintf(pfile,"%f,",pVisitAnnotation->Size);
- else
- fprintf(pfile,"%f,",pVisitAnnotation->Width);
- fprintf(pfile,"%fn",pVisitAnnotation->High);
- fprintf(pfile,"%fn",pVisitAnnotation->Intevel);
- fprintf(pfile,"%sn",pVisitAnnotation->Content);
- fprintf(pfile,"%dn",pVisitAnnotation->AntnNum);
- for(int i=0;i<pVisitAnnotation->AntnNum;i++)
- {
- fprintf(pfile,"%f,",pVisitAnnotation->pMemberPoint[i].X);
- fprintf(pfile,"%fn",pVisitAnnotation->pMemberPoint[i].Y);
- //fprintf(pfile,"%dn",pVisitAnnotation->Z);
- }
- fputs("n",pfile);
- pVisitAnnotation=pVisitAnnotation->pNext;
- }
- }
- /*************************************************
- 描述: 向目标文件写属性数据
- 输入: 目标VCT文件名
- 输出: 无
- *************************************************/
- void CTransfer::WriteVctAttribute(FILE *pfile)
- {
- ///////写属性数据
- if(m_pVctAttribute==NULL)
- {
- AfxMessageBox("被转入的文件没有属性信息");
- return;
- }
- VCTAttribute *pVisitAbute=m_pVctAttribute;
- while( pVisitAbute != NULL)
- {
- fprintf(pfile,"%s,t",pVisitAbute->ListName);
-
- for(int i=1;i<pVisitAbute->ItemNum+1;i++)
- {
-
- if(i==pVisitAbute->ItemNum)
- {
-
- fprintf(pfile,"%sn",pVisitAbute->pRecordList->Record);
- }
- else
- {
- fprintf(pfile,"%s,t",pVisitAbute->pRecordList->Record);
- pVisitAbute->pRecordList = pVisitAbute->pRecordList->pNext;
- }
- }
-
- pVisitAbute=pVisitAbute->pNext;
- }
- fputs("TableEndn",pfile);
- }
- int CTransfer::GetShang(int dividend, int divisor)// 求商
- {
- int temp,result;
- temp=dividend%divisor;
- result=(dividend-temp)/divisor;
- return result;
- }
- /*************************************************
- 描述: 初始化VCT格式数据
- 输入: 源shp文件名
- 输出: 成功返回TRUE 失败返回FALSE
- *************************************************/
- BOOL CTransfer::InitVctData(CString& csTargetFileName)
- {
-
- //创建并打开目标vct文件
- m_pVctFile = fopen(csTargetFileName,"w");
- m_bVctOpen = TRUE;
- if(m_pVctFile==NULL)
- {
- AfxMessageBox("写入文件失败");
- return FALSE;
- }
-
- m_pVctFeature = NULL;
- m_pVctTblStruct = NULL;
- m_pVctPointData = NULL;
- m_pVctLineData = NULL;
- m_pVctPolygon = NULL;
- m_pVctAnnotation = NULL;
- m_pVctAttribute = NULL;
-
- //Vct文件头初始化
- m_VctFilehead.DataMark = "CNSDTF-VCT";
- m_VctFilehead.VerSion = (float) 1.0;
- m_VctFilehead.Unit = 'D';
- m_VctFilehead.Dim = 2;
- m_VctFilehead.Topo = 0;
- m_VctFilehead.MaxX = (float)0.0;
- m_VctFilehead.MaxY = (float)0.0;
- m_VctFilehead.MinX = (float)0.0;
- m_VctFilehead.MinY = (float)0.0;
- m_VctFilehead.MinZ = (float)0.0;
- m_VctFilehead.MaxZ = (float)0.0;
- m_VctFilehead.ScaleM = (float)10000.0;
- m_VctFilehead.Projection = "";
- m_VctFilehead.Spheroid = "";
- m_VctFilehead.Parameters = "";
- m_VctFilehead.Date = "NO";
- m_VctFilehead.Separator = ',';
-
-
- //取文件名
- CString tempName= csSourseFileName; //保存源文件的文件名
- tempName = tempName.Mid(tempName.ReverseFind('\')+1);
- m_strFileName = tempName.Left(tempName.ReverseFind('.'));
-
- //Vct要素类型初始化
- m_pVctFeature = new VCTFeature();
- m_pVctFeature->Code = m_pVctFeature->ListName
- = m_pVctFeature->Name
- = m_pVctFeature->Shape
- = m_strFileName;
- m_pVctFeature->Color = 0;
- m_pVctFeature->pNext = NULL;
-
- //Vct属性数据结构初始化
- m_pVctTblStruct = new VCTTableStructure();
- m_pVctTblStruct->ItemNum = 0;
- m_pVctTblStruct->ListName = m_strFileName;
- m_pVctTblStruct->pItem = NULL;
- m_pVctTblStruct->pTblNext = NULL;
-
- //Vct几何图形数据初始化
- m_pVctLineData = NULL;
- m_pVctPolygon = NULL;
- m_pVctPointData = NULL;
-
- //Vct注记初始化(未定义)
- m_pVctAnnotation = NULL;
-
- //属性数据初始化
- m_pVctAttribute = NULL;
-
- //SHP格式数据到VCT格式数据的转换
- DataTransfer();
- return TRUE;
- }
- /*************************************************
- 描述: 向目标vct文件写入数据
- 输入: 源shp文件名
- 输出: 成功返回TRUE 失败返回FALSE
- *************************************************/
- BOOL CTransfer::WriteVctData(CString& csFileName)
- {
- if (!InitVctData(csFileName))
- {
- return FALSE;
- }
- fputs("HeadBeginn",m_pVctFile);
- WriteVctFileHead(m_pVctFile);
- fputs("HeadEndn",m_pVctFile);
- fputs("n",m_pVctFile);
-
- fputs("FeatureCodeBeginn",m_pVctFile);
- WriteVctFeature(m_pVctFile);
- fputs("FeatureCodeEndn",m_pVctFile);
- fputs("n",m_pVctFile);
-
- fputs("TableStructureBeginn",m_pVctFile);
- WriteVctTableStructure(m_pVctFile);
- fputs("TableStructureEndn",m_pVctFile);
- fputs("n",m_pVctFile);
-
- WriteVctGraphy(m_pVctFile);
-
- fputs("AnnotationBeginn",m_pVctFile);
- WriteVctAnnotation(m_pVctFile);
- fputs("AnnotationEndn",m_pVctFile);
- fputs("n",m_pVctFile);
-
- fputs("AttributeBeginn",m_pVctFile);
- fputs(m_strFileName + "n",m_pVctFile);
- WriteVctAttribute(m_pVctFile);
- fputs("AttributeEndn",m_pVctFile);
- fclose(m_pVctFile);
- return TRUE;
- }
- /*************************************************
- 描述: 数据转换
- 输入: 无
- 输出: 无
- *************************************************/
- void CTransfer::DataTransfer()
- {
- ///////////文件头数据转换/////////////////////////////////////////////
- //边界
- m_VctFilehead.MaxX = (float) m_Extent.GetRight();
- m_VctFilehead.MinX = (float) m_Extent.GetLeft();
- m_VctFilehead.MaxY = (float) m_Extent.GetBottom();
- m_VctFilehead.MinY = (float) m_Extent.GetTop();
- //时间
- m_VctFilehead.Date.Format("%d%d%d",(int)m_pRecordSet->m_Header.yy,(int)m_pRecordSet->m_Header.mm,(int)m_pRecordSet->m_Header.dd);
-
- //要素类型参数转换///
- //直接加到初始化里了///////////////////
- //属性数据结构转换//////////////////////////////////////////
- TableStructureTransfer();
- //几何图形数据部分//////////////////////////////////////////
- GraphyDatatransfer();
- //注记转换部分(省略)//////////////////////////////////////
-
- //属性数据转换//////////////////////////////////////////////
- AttributeTransfer();
- }
- /*************************************************
- 描述: 文件头数据转换
- 部分在DataTransfer()方法中,部分在初始化方法中
- 输入: 无
- 输出: 无
- *************************************************/
- /*************************************************
- 描述: 要素类型参数转换
- (直接加到初始化里了)
- 输入: 无
- 输出: 无
- *************************************************/
- /*************************************************
- 描述: 属性数据结构转换
- 输入: 无
- 输出: 无
- *************************************************/
- void CTransfer::TableStructureTransfer()
- {
-
- int iSize = m_pRecordSet->m_TableDesc.m_fieldsDesc.GetSize();
- FIELD_ELEMENT* pFileElement;
- VCTTableStructure* pTableStructure;
-
- m_pVctTblStruct = pTableStructure = new VCTTableStructure();
- pTableStructure->pTblNext = NULL;
- pTableStructure->ItemNum = iSize;
- pTableStructure->ListName = m_strFileName;
- pTableStructure->pItem = new VCTItem[iSize];
- for(int i=0;i<iSize;i++)
- {
-
- pFileElement = m_pRecordSet->m_TableDesc.m_fieldsDesc.GetAt(i);
- pTableStructure->pItem[i].Name.Format("%s",pFileElement->szFieldName);
- pTableStructure->pItem[i].Width = (int)pFileElement->ucFieldLength;
- if(pFileElement->cFieldType == 'N' || pFileElement->cFieldType == 'F' )
- {
- if( pFileElement->ucFieldDecimal == 0 )
- pTableStructure->pItem[i].Type = "Integer";
-
- else
- {
- pTableStructure->pItem[i].Type = "Float";
- pTableStructure->pItem[i].Pric = (int) pFileElement->ucFieldDecimal;
- }
- }
- else if(pFileElement->cFieldType == 'C')
- {
- pTableStructure->pItem[i].Type = "Char";
- }
- else
- pTableStructure->pItem[i].Type = "Invaild";
- }
-
- }
- /*************************************************
- 描述: 几何图形数据部分转换
- 输入: 无
- 输出: 无
- *************************************************/
- void CTransfer::GraphyDatatransfer()
- {
-
- switch ( m_shpType )
- {
- case POINT:
- {
- m_pVctFeature->Shape = "Point";
- CMapPoint* pPoint;
- VCTPointData* pCurPointData;
-
- POSITION pos = m_ObList.GetHeadPosition();
- int iCount = 1;
- while(pos != NULL)
- {
- if(iCount == 1)
- {
- m_pVctPointData = pCurPointData = new VCTPointData();
- pCurPointData->pNext = NULL;
- }
- else
- {
- pCurPointData->pNext = new VCTPointData();
- pCurPointData = pCurPointData->pNext;
- pCurPointData->pNext = NULL;
- }
- pCurPointData->pNext = NULL;
- pPoint = (CMapPoint*) m_ObList.GetNext(pos);
- pCurPointData->FeatureCode = m_strFileName;
- pCurPointData->LayerName = m_strFileName;
- pCurPointData->X = (float) pPoint->GetX();
- pCurPointData->Y = (float) pPoint->GetY();
- pCurPointData->PointMark = 1;
- pCurPointData->AimCode.Format("%d",iCount);
-
- iCount ++;
- }
- break;
- }
- case POLYLINE:
- {
- m_pVctFeature->Shape = "Line";
- CMapLine* pMapLine = NULL;
- CMapParts* pMapParts;
- CMapPoints* pPoints;
- CMapPoint* pPoint;
- VCTLineData* pVctLine;
- POSITION pos = m_ObList.GetHeadPosition();
- int tempLineCount = 0;
- VCTPoint* pVctHeadPoint;
- while(pos != NULL)
- {
- if(tempLineCount == 0)
- {
- m_pVctLineData = pVctLine = new VCTLineData();
- pVctLine->pNext = NULL;
- }
- else
- {
- pVctLine->pNext = new VCTLineData();
- pVctLine = pVctLine->pNext;
- pVctLine->pNext = NULL;
- }
-
- pMapLine = (CMapLine*)m_ObList.GetNext(pos);
- int temPointCount = 0;
- for(int i=0;i<pMapLine->m_Line.GetSize();i++)//线
- {
- temPointCount = 0;
- pMapParts = pMapLine->m_Line[i];
-
- for(int j=0;j<pMapParts->m_Parts.GetSize();j++)//段
- {
-
- pPoints = pMapParts->m_Parts[j];
- for(int k=0;k<pPoints->m_Points.GetSize();k++)//点
- {
- pPoint = pPoints->m_Points[k];
- if(temPointCount == 0)
- {
- pVctHeadPoint = pVctLine->pMemberPoint = new VCTPoint();
- pVctLine->pMemberPoint->pNext = NULL;
- }
- else
- {
- pVctLine->pMemberPoint->pNext = new VCTPoint();
- pVctLine->pMemberPoint = pVctLine->pMemberPoint->pNext;
- pVctLine->pMemberPoint->pNext = NULL;
- }
- pVctLine->pMemberPoint->X = (float)pPoint->GetX();
- pVctLine->pMemberPoint->Y = (float)pPoint->GetY();
- temPointCount++;
- }
- }
- }
- pVctLine->AimCode.Format("%d",++tempLineCount);
- pVctLine->FeatureCode = m_strFileName;
- pVctLine->LayerName = m_strFileName;
- pVctLine->LineMark = 1;
- pVctLine->PointNum = temPointCount;
- pVctLine->pMemberPoint = pVctHeadPoint;
-
- }
- break;
- }
- case POLYGON:
- {
- m_pVctFeature->Shape = "Polygon";
- CMapPolygon* pMapPoiygon = NULL;
- CMapParts* pMapParts;
- CMapPoints* pPoints;
- CMapPoint* pPoint;
- VCTPolygonData* pVctPolygon;
- POSITION pos = m_ObList.GetHeadPosition();
- int tempPolygonCount = 0;
- VCTPoint* pVctHeadPoint;
- while(pos != NULL)
- {
- if(tempPolygonCount == 0)
- {
- m_pVctPolygon = pVctPolygon = new VCTPolygonData();
- pVctPolygon->pNext = NULL;
- }
- else
- {
- pVctPolygon->pNext = new VCTPolygonData();
- pVctPolygon = pVctPolygon->pNext;
- pVctPolygon->pNext = NULL;
- }
- pMapPoiygon = (CMapPolygon*) m_ObList.GetNext(pos);
- pVctPolygon->AimCode.Format("%d",++tempPolygonCount);
- pVctPolygon->FeatureCode = m_strFileName;
- pVctPolygon->LayerName = m_strFileName;
- // pVctPolygon->labX = (float) pMapPoiygon->m_Extent.GetLeft(); //why ()
- // pVctPolygon->labY = (float) pMapPoiygon->m_Extent.GetTop();
- pVctPolygon->labX = (float) 0.000000; //why ()
- pVctPolygon->labY = (float) atof("7836836683315036600000000000000000000.000000");
-
- int iPointCount = 0;
- for(int i=0;i<pMapPoiygon->m_Polygon.GetSize();i++)//线
- {
- pMapParts = pMapPoiygon->m_Polygon[i];
- for(int j=0;j<pMapParts->m_Parts.GetSize();j++)//段
- {
-
- pPoints = pMapParts->m_Parts[j];
- for(int k=0;k<pPoints->m_Points.GetSize();k++)//点
- {
- pPoint = pPoints->m_Points[k];
- if(iPointCount == 0)
- {
- pVctHeadPoint = pVctPolygon->pMemberPoint = new VCTPoint();
- pVctPolygon->pMemberPoint->pNext = NULL;
- }
- else
- {
- pVctPolygon->pMemberPoint->pNext = new VCTPoint();
- pVctPolygon->pMemberPoint = pVctPolygon->pMemberPoint->pNext;
- pVctPolygon->pMemberPoint->pNext = NULL;
- }
-
- pVctPolygon->pMemberPoint->X = (float)pPoint->GetX();
- pVctPolygon->pMemberPoint->Y = (float)pPoint->GetY();
- iPointCount++;
- }
- }
- }
- pVctPolygon->PointNum = iPointCount;
- pVctPolygon->pMemberPoint = pVctHeadPoint;
- }
-
- break;
- }
- default:
- break;
- }
- }
- /*************************************************
- 描述: 注记转换部分(忽略)
- 输入: 无
- 输出: 无
- *************************************************/
- /*************************************************
- 描述: 属性数据转换
- 输入: 无
- 输出: 无
- *************************************************/
- void CTransfer::AttributeTransfer()
- {
-
- LONG iRecordOffset;
- char *pszBuffer;
- char szBuff[255];
- double dbValue;
- FIELD_ELEMENT *pField;
- CMapField *pMapField;
- VCTAttribute* pAttribute;
- VCTRecordList* pHeadRecordList;
- VARIANT varValue;
- //逐条读取属性记录
- for(unsigned long recs=0;recs<m_pRecordSet->m_Header.no_recs;recs++)
- {
- if( recs < 0 || recs >= m_pRecordSet->m_Header.no_recs)
- {
- AfxMessageBox("无效的索引值!");
- return; //无效的索引值
- }
- iRecordOffset = recs*m_pRecordSet->m_Header.rec_len + m_pRecordSet->m_Header.head_len;
- pszBuffer = new char[ m_pRecordSet->m_Header.rec_len];
- m_pRecordSet->fDbf.Seek(iRecordOffset , CFile::begin);
- if ( m_pRecordSet->fDbf.Read(pszBuffer,m_pRecordSet->m_Header.rec_len)
- != m_pRecordSet->m_Header.rec_len)
- {
- delete []pszBuffer;
- AfxMessageBox("读取属性记录错误!");
- return ;
- }
-
- if( recs == 0)
- {
- m_pVctAttribute = pAttribute = new VCTAttribute();
- pAttribute->pNext = NULL;
- }
- else
- {
- pAttribute->pNext = new VCTAttribute();
- pAttribute = pAttribute->pNext;
- pAttribute->pNext = NULL;
- }
- pAttribute->ListName.Format("%d",recs+1);
- pAttribute->ItemNum = m_pRecordSet->m_TableDesc.GetFieldCount();
-
-
- for ( int j = 0 ; j < m_pRecordSet->m_TableDesc.GetFieldCount() ; j++)
- {
- pMapField = new CMapField();
- pField = m_pRecordSet->m_TableDesc.GetDesc(j);
- pMapField->SetName( pField->szFieldName );
- pMapField->SetType( pField->cFieldType );
- memset(szBuff , 0 , 255);
- //略过该记录是否删除标记字节pszBuffer+1
- strncpy(szBuff, pszBuffer+1+pField->ulOffset , pField->ucFieldLength);
- if ( pField->cFieldType == 'N' || pField->cFieldType == 'F' )
- {
- ::VariantInit(&varValue);
- dbValue = atof(szBuff );
- if ( pField->ucFieldDecimal == 0 )
- {
- varValue.bVal = VT_I4;
- varValue.lVal = (int)dbValue;
- pMapField->SetType(fdInteger);
- }
- else
- {
- varValue.bVal = VT_R8;
- varValue.dblVal = dbValue;
- pMapField->SetType(fdDouble);
- }
- pMapField->SetValue(varValue);
- }
- else if ( pField->cFieldType == 'C' )
- {
- pMapField->SetValueAsString(szBuff);
- pMapField->SetType(fdString);
- }
- else
- pMapField->SetType(fdInvaild);
- if( j == 0)
- {
- pHeadRecordList = pAttribute->pRecordList = new VCTRecordList();
- pAttribute->pRecordList->pNext = NULL;
- }
- else
- {
- pAttribute->pRecordList->pNext = new VCTRecordList();
- pAttribute->pRecordList = pAttribute->pRecordList->pNext;
- pAttribute->pRecordList->pNext = NULL;
- }
- pAttribute->pRecordList->Record = pMapField->GetValueAsString();
- delete pMapField;
- pMapField = NULL;
-
- }
- delete []pszBuffer;
- pszBuffer = NULL;
- pAttribute->pRecordList = pHeadRecordList;
- }
- }