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

📄 shpreaderdoc.cpp

📁 读取shp文件源码。
💻 CPP
字号:
/*
读取ShapeFile格式
工程:ShpReader
作者:醍醐灌顶
创建时间:星期一,四月,04,2005
*/
// ShpReaderDoc.cpp : implementation of the CShpReaderDoc class
//

#include "stdafx.h"
#include "ShpReader.h"

#include "ShpReaderDoc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CShpReaderDoc

IMPLEMENT_DYNCREATE(CShpReaderDoc, CDocument)

BEGIN_MESSAGE_MAP(CShpReaderDoc, CDocument)
	//{{AFX_MSG_MAP(CShpReaderDoc)
	ON_COMMAND(ID_MENUITEM_OPEN, OnMenuitemOpen)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CShpReaderDoc construction/destruction

CShpReaderDoc::CShpReaderDoc()
{
	// TODO: add one-time construction code here
	xmin = xmax = ymin = ymax = -1;
}

CShpReaderDoc::~CShpReaderDoc()
{
}

BOOL CShpReaderDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CShpReaderDoc serialization

void CShpReaderDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CShpReaderDoc diagnostics

#ifdef _DEBUG
void CShpReaderDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CShpReaderDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CShpReaderDoc commands

void CShpReaderDoc::SwapWord(int length, void *pword)
{
	uchar temp;
	for(int i=0;i<length/2;i++)
	{
		temp = ((uchar *)pword)[i];
		((uchar *)pword)[i] = ((uchar *)pword)[length-i-1];
		((uchar *)pword)[length-i-1] = temp;
	}
}

void CShpReaderDoc::ReadHeadInfo(FILE *fp)
{
	rewind(fp);
	uchar *buffer;
	double _xmin,_xmax,_ymin,_ymax;
	buffer = (uchar *)malloc(100);
	fread(buffer,100,1,fp);
	SwapWord(4,buffer+24);
	memcpy(&fileSize,buffer+24,4);
	fileSize *= 2;
	memcpy(&shapeType,buffer+32,4);
	memcpy(&_xmin,buffer+36,8);
	memcpy(&_ymin,buffer+44,8);
	memcpy(&_xmax,buffer+52,8);
	memcpy(&_ymax,buffer+60,8);
	free(buffer);
	xmin = (xmin==-1)?_xmin:(_xmin<xmin?_xmin:xmin);
	xmax = (xmax==-1)?_xmax:(_xmax>xmax?_xmax:xmax);
	ymin = (ymin==-1)?_ymin:(_ymin<ymin?_ymin:ymin);
	ymax = (ymax==-1)?_ymax:(_ymax>ymax?_ymax:ymax);
}

void CShpReaderDoc::OnMenuitemOpen() 
{
	// TODO: Add your command handler code here
	CFileDialog dlg(true,NULL,NULL,NULL,"ShapeFile(*.shp)|*.shp||");
	if(dlg.DoModal() == IDOK)
	{
		CString str = dlg.GetPathName();
		FILE *fp;
		fp = fopen(str,"rb");
		ReadHeadInfo(fp);
		switch(shapeType)
		{
			case 1:
				ReadPoints(fp);
				break;
			case 3:
				ReadPolyLines(fp);
				break;
			case 5:
				ReadPolygons(fp);
				break;
		}
		InvalidateRect(NULL,NULL,true);
	}
}

int CShpReaderDoc::ReadPoints(FILE *fp)
{
	int count = 0;
	fseek(fp,100,0);
	int pos = 100;
	while(!feof(fp))
	{
		count ++ ;
		ShpPoint pt;
		uchar *buffer;
		buffer = (uchar *)malloc(28);
		fread(buffer,28,1,fp);
		SwapWord(4,buffer);
		memcpy(&pt.id,buffer,4);
		memcpy(&pt.x,buffer+12,8);
		memcpy(&pt.y,buffer+20,8);
		free(buffer);
		pointV.push_back(pt);
		pos += 28;
		if(pos >= fileSize)
			break;
	}
	return count;
}

int CShpReaderDoc::ReadPolyLines(FILE *fp)
{
	int count = 0;
	fseek(fp,100,0);
	int pos = 100;
	while(!feof(fp))
	{
		count ++ ;
		ShpPolyLine pl;
		uchar *buffer;
		buffer = (uchar *)malloc(52);
		fread(buffer,52,1,fp);
		SwapWord(4,buffer);
		memcpy(&pl.id,buffer,4);
		memcpy(&pl.partnum,buffer+44,4);
		memcpy(&pl.pointnum,buffer+48,4);
		free(buffer);
		//buffer = (uchar *)malloc(pl.partnum*4);
		pl.parts = (int *)malloc(sizeof(int)*pl.partnum);
		fread(pl.parts,4,pl.partnum,fp);
		pl.points = (Point *)malloc(sizeof(Point)*pl.pointnum);
		fread(pl.points,sizeof(Point),pl.pointnum,fp);
		polylineV.push_back(pl);
		pos = pos + 52 + 4*pl.partnum + sizeof(Point)*pl.pointnum;
		if(pos >= fileSize)
			break;
	}
	return count;
}

int CShpReaderDoc::ReadPolygons(FILE *fp)
{
	int count = 0;
	fseek(fp,100,0);
	int pos = 100;
	while(!feof(fp))
	{
		count ++ ;
		ShpPolygon pg;
		uchar *buffer;
		buffer = (uchar *)malloc(52);
		fread(buffer,52,1,fp);
		SwapWord(4,buffer);
		memcpy(&pg.id,buffer,4);
		memcpy(&pg.partnum,buffer+44,4);
		memcpy(&pg.pointnum,buffer+48,4);
		free(buffer);
		pg.parts = (int *)malloc(sizeof(int)*pg.partnum);
		fread(pg.parts,4,pg.partnum,fp);
		pg.points = (Point *)malloc(sizeof(Point)*pg.pointnum);
		fread(pg.points,sizeof(Point),pg.pointnum,fp);
		polygonV.push_back(pg);
		pos = pos + 52 + 4*pg.partnum + sizeof(Point)*pg.pointnum;
		if(pos >= fileSize)
			break;
	}
	return count;
}

⌨️ 快捷键说明

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