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

📄 shapefile.cpp

📁 用vc和cj60lib做的读取shape文件的源代码.还有些小问题没做完。
💻 CPP
字号:
// ShapeFile.cpp: implementation of the CShapeFile class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "GeoGis.h"
#include "ShapeFile.h"
#include  "MapLines.h"
#include   "Attribute.h"
#include   "Coord.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
inline  void   Swap(double &A,double &B)
{
	double  C=A;
	A=B;
	B=C;
}
inline  void  Swap(int  &A,int &B)
{
	int  C=A;
	A=B;
	B=C;
}
CShapeFile::CShapeFile()
{
	m_pFileIn = NULL;
	m_pFileOut = NULL;
	m_bLatLon = FALSE;
}

CShapeFile::~CShapeFile()
{

}

CPolyLine::CPolyLine()
{
	for(int  i = 0; i< 4; i++)
		m_dBox[i] = 0;
	m_nParts= 0;
	m_nPoints = 0;
}

BOOL CShapeFile::ImportShapeFile(CLongLines *pMapLines, LPCSTR sFileName)
{
	CMainHeader mainheader;
	CCoordArray aCoord;
	return ImportShapeFile(pMapLines, &aCoord, mainheader, sFileName, PolyLine);
}

BOOL CShapeFile::ExportShapeFile(CLongLines *pMapLines, LPCSTR sFileName, int nFlags)
{

}

int CShapeFile::ImportShapeFileType(LPCSTR sFileName)
{
	CMainHeader   mainheader;
	CLongLines    maplines;
	CCoordArray   aCoord;
	BOOL   bOK =ImportShapeFile(&maplines,&aCoord,&mainheader,sFileName,Header);
	if(bOK)return  mainheader.m_nShapeType;
	else
		return FALSE;
}

BOOL CShapeFile::ExportShapeFile(CMapLayer *pMapLayer, LPCSTR sFileName, int nFlags)
{

}

BOOL CShapeFile::ImportShapeFile(CMapLayer *pMapLayer, LPCSTR sFileName)
{

}

void CShapeFile::Convert()
{

}

BOOL CShapeFile::ImportMultMapLines(CLongLines *pMapLines, CLongCoord, CLongLines *&pMapLinesNew)
{
	return  TRUE;
}

BOOL CShapeFile::ImportShapeFile(CLongLines *pMapLines, CCoordArray *pCoords, CMainHeader &mainheader, LPCSTR sFileName, int iFlag)
{
	BOOL bOK = TRUE;   
	CRecordHeader recordheader;
	DWORD nShape;   
	BOOL bPolyLine = FALSE;
	int nBytes = sizeof(CMainHeader);
	
	// Open the file as binary
	
	FILE* pFile = fopen(sFileName, "rb");
	if (pFile != NULL)
	{
		// Read the file header
		
		if (fread(&mainheader, sizeof(CMainHeader),1,pFile) == 1)
		{
			mainheader.m_nFileCode = ReverseBytes(mainheader.m_nFileCode);
			mainheader.m_nFileLength = ReverseBytes(mainheader.m_nFileLength);
			
			// Determine if lat/long or coordinates
			
			if (mainheader.m_dXMin >= -180 && mainheader.m_dXMax <= 180 &&
				mainheader.m_dYMax <= 90 && mainheader.m_dYMin >= -90)
			{
				iFlag |= LatLon;
			}
			
			
			// Read the record header
			
			//BDProgressRange(0, mainheader.m_nFileLength);
			
			while (bOK && fread(&recordheader, sizeof(CRecordHeader),1,pFile) == 1 && 
				!(iFlag & Header))
			{
				// Read the shape type
				if (fread(&nShape,sizeof(nShape),1,pFile) == 1)
				{
					// Import only polylines
					
					if (nShape == SHPPolyLine || nShape == SHPPolygon)
					{
						bPolyLine = TRUE;
						bOK = ImportPolyLine(pMapLines, pFile, iFlag & LatLon);
					} 
					
					else if (nShape == SHPPoint)
					{
						bOK = ImportPoints(pCoords, pFile, iFlag & LatLon);
					}
					// Skip the record
					
					else
					{
						// Handles corrupt shapefiles
						
						if (nShape != 0) bOK = FALSE;
						
						ASSERT(nShape >= 0 && nShape <= 31);
						int nContentLength = ReverseBytes(recordheader.m_nContentLength);
						for (int i = 0; bOK && i < nContentLength-2; i++)
						{
							WORD word;
							if (fread(&word,sizeof(word),1,pFile) != 1)
							{
								bOK = FALSE;
							}
						}
					}
					
					// Update progress bar
					nBytes += ReverseBytes(recordheader.m_nContentLength);
					//BDProgressPos(nBytes);
					
				} else
				{
					bOK = FALSE;
				}
			}
		} else
		{
			bOK = FALSE;
		}
	}
	if (pFile != NULL) fclose(pFile);
	
	if (bOK && !bPolyLine && (iFlag & PolyLine) && !(iFlag & Points))
	{
		//AfxMessageBox(BDString(IDS_SHAPEFILENOPOLYLINES));
		bOK = FALSE;
	}
	
	//BDProgressPos(0);
	
	return bOK;
}

BOOL CShapeFile::ImportPolyLine(CLongLines *pMapLines, FILE *pFile, BOOL bLatLon, CRectDbl *pRect)
{
	return TRUE;
}

BOOL CShapeFile::ImportPoints(CCoordArray *pPoints, FILE *pFile, BOOL bLatLon)
{
	return TRUE;

}

BOOL CShapeFile::ExportPoints(CMapLayer *pMapLayer, FILE *pFile, BOOL bLatLon)
{
	return TRUE;

}

BOOL CShapeFile::ExportPolyLine(CMapLayer *pMapLayer, FILE *pFile, BOOL bLatLon, DWORD nShape)
{
	return TRUE;

}

BOOL CShapeFile::ExportShapeFilePoint(CMapLayer *pMapLayer, LPCSTR sFileName, BOOL bLatLon)
{
	return TRUE;

}

BOOL CShapeFile::ExportShapeFilePoly(CMapLayer *pMapLayer, LPCSTR sFileName, BOOL bLatLon)
{

}

BOOL CShapeFile::ExportIndexFile(CPolyLine &polyline, CMapLayer *pMapLayer, LPCSTR sFileName, CMainHeader &mainheader)
{
	return TRUE;

}

BOOL CShapeFile::ExportIndexFile(CMapLayer *pMapLayer, LPCSTR sFileName, CMainHeader &mainheader)
{
	return TRUE;

}

BOOL CShapeFile::ExportDBaseQuery(CMapLayer *pMapLayer, LPCSTR sFileName)
{
	return TRUE;

}

BOOL CShapeFile::ExportDBase(CMapLayer *pMapLayer, LPCSTR sFileName)
{
	return TRUE;

}

BOOL CShapeFile::IsValidMapObject(CAttrArray &aAttr, CArrayAttrSel &aAttrSel)
{
	return TRUE;

}

int CShapeFile::ReverseBytes(int)
{
	return 0;

}

CPolyLine CShapeFile::GetBoundingBoxPoly(CLongLines *pMapLines, BOOL bLatLon, int i1, int j2)
{

}

CPolyLine CShapeFile::GetBoundingBox(CMapLayer *pMapLayer, BOOL bLatLon)
{

}

CPolyLine CShapeFile::GetNumParts(CLongLines *pMapLines)
{

}

CString CShapeFile::GetLineText(CMapLayer *, CMapLayerObj *)
{

}

void CShapeFile::fwritex(void *p, size_t size, DWORD n, FILE *pFile)
{
	if(fwrite(p,size,n,pFile)!=n)
	{
		AfxThrowUserException();
	}		
}

BOOL CShapeFile::ConvertShapeFile()
{
	return TRUE;

}

BOOL CShapeFile::ConvertPolyLine()
{
	return TRUE;

}

BOOL CShapeFile::ConvertPoints()
{
	return TRUE;

}

BOOL CShapeFile::ConvertIndex()
{
	return TRUE;

}

BOOL CShapeFile::ConvertDBaseFile()
{
	return TRUE;

}

BOOL CShapeFile::PolylineToPolygon(CMapLayer *)
{
	return TRUE;

}


⌨️ 快捷键说明

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