📄 mifdoc.cpp
字号:
// MIFDoc.cpp : implementation of the CMIFDoc class
//
#include "stdafx.h"
#include "MIF.h"
#include "fstream.h"
#include "MIFDoc.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMIFDoc
IMPLEMENT_DYNCREATE(CMIFDoc, CDocument)
BEGIN_MESSAGE_MAP(CMIFDoc, CDocument)
//{{AFX_MSG_MAP(CMIFDoc)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMIFDoc construction/destruction
CMIFDoc::CMIFDoc()
{
// TODO: add one-time construction code here
num_pt_type = 0;
num_ln_type = 0;
num_pg_type = 0;
pt_type = NULL;
ln_type = NULL;
pg_type = NULL;
redraw = false;
}
CMIFDoc::~CMIFDoc()
{
//释放各类目标空间
free_pt_type();
free_ln_type();
free_pg_type();
//delete对象数组
for(int i = 0;i < num_pg_type;i++)
{
PolygonType * type = &pg_type[i];
for(int j = 0;j < type->pg[j].num_pt; j++)
{
delete [](char*)type->pg[j].pt ;
type->pg[j].pt = NULL;
}
}
for(int k = 0;k < num_ln_type;k++)
{
LineType * type = &ln_type[k];
for(int l = 0;l < type->ln[k].num_pt; l++)
{
delete [](char*)type->ln[l].pt ;
type->ln[l].pt = NULL;
}
}
}
BOOL CMIFDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
// TODO: add reinitialization code here
// (SDI documents will reuse this document)
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CMIFDoc serialization
void CMIFDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
// TODO: add storing code here
}
else
{
// TODO: add loading code here
}
}
/////////////////////////////////////////////////////////////////////////////
// CMIFDoc diagnostics
#ifdef _DEBUG
void CMIFDoc::AssertValid() const
{
CDocument::AssertValid();
}
void CMIFDoc::Dump(CDumpContext& dc) const
{
CDocument::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CMIFDoc commands
void CMIFDoc::OnFileOpen()
{
// TODO: Add your command handler code here
//获得文件名,存入filename字符串中
OPENFILENAME ofn;
char filename[2048] = "";
//初始化OPENFILENAME结构
ZeroMemory(&ofn, sizeof(OPENFILENAME));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = NULL;
ofn.lpstrFile = filename;
ofn.nMaxFile = sizeof(filename);
ofn.lpstrFilter = "MapInfo MIF索引文件(*.mif)\0*.mid\0All(*.*)\0*.*\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
//打开资源管理器选择文件
if (!GetOpenFileName(&ofn))
{
return;
}
//从filename中提取路径名
char path[1024] = "",title[1024] = "";
char file[1024] = "",tfile[1024] = "";
int i = strlen(filename)-1;
while (i >= 0 && filename[i] != '\\') i--;
strncpy(path, filename, i);
path[i]=0;
//从filename中提取扩展名
CString FileName((LPCSTR)filename);
CString ext=FileName.Right(4);
//比较文件后缀名 .mid
if(!ext.CompareNoCase(".mid"))
{
//打开文件并读取
ifstream in(filename);
if (!in) return;
while (!in.eof())
{
in >> title;
if (strncmp(title,"点文件",6)==0 )
{
in >> file;
sprintf(tfile, "%s\\%s", path, file);
//将path,file一起读入tfile,得到新的文件名tfile
load_pt_mapinfo(tfile);
}
else if (strncmp(title, "线文件", 6) == 0)
{
in >> file;
sprintf(tfile, "%s\\%s", path, file);
load_ln_mapinfo(tfile);
}
else if (strncmp(title, "多边形文件", 10) == 0)
{
in >> file;
sprintf(tfile, "%s\\%s", path, file);
load_pg_mapinfo(tfile);
}
}
in.close();
}
this->UpdateAllViews(NULL);
}
void CMIFDoc::load_pt_mapinfo(char * filename)
{
//颜色表处理
unsigned int color;
float red,green,blue,alpha,size;
red=1.0;
green=0.5;
blue=0.5;
alpha=1.0;
size=5;
color = PACK_COLOR((int)(red*255), (int)(green*255), (int)(blue*255), (int)(alpha*255));
//从filename中提取路径名
char datapath[256] = "";//文件路径名
char datafile[256] = "";//文件名
char line[128] = "";//文件中一行字符串
char str[32] = ""; //行中一个字符串
char name[16] = ""; //文件名
int k = strlen(filename)-1;
while (k >= 0 && filename[k] != '\\') k--;
strncpy(datapath, filename, k);
datapath[k]=0;
//数据文件索引文件
ifstream in2(filename);
if (!in2) return ;
//首先释放以前的点信息,然后读取新的点地理信息
free_pt_type();
//读取点文件数目,存入num_pt_type
in2.getline(line, sizeof(line));
sscanf(line, "%d", &num_pt_type);
if (num_pt_type == 0)
{
AfxMessageBox("mif point file open error");
return ;
}
//建立num_pt_type个点对象
//下面是2种建立对象指针数组的方法:
//pt_type = new PointType[num_pt_type];
///下面建立pt_type的点对象数组的方法比较好,建议以后都采用这种方式!!!
pt_type = (PointType*) new char[sizeof(PointType) * num_pt_type];
for(int i = 0;i<num_pt_type;i++)
{
PointType * type = &pt_type[i];
//读取真正的数据文件名,存在datafile
in2.getline(datafile,sizeof(datafile));
//读取颜色表信息
in2.getline(line, sizeof(line));
sscanf(line, "%f %f %f %f %f", &red, &green, &blue, &alpha, &size);
//获取真正的全路径文件名,覆盖datafile,此时的datafile是文件全路径名
strcpy(line, datafile);
strcpy(datafile, datapath);
strcat(datafile, "\\");
strcat(datafile, line);
//用读取的文件名作为该类点文件名
int k = strlen(line)-1;
while (k >= 0 && line[k] != '.') k--;
strncpy(name, line, k);
name[k]=0;
strcpy(type->name,name);
//开始读真正的数据文件,对数据文件进行操作
//换用iostream来读文件!!!
ifstream in3(datafile);
if (!in3)
{
AfxMessageBox("mif point file open error");
return ;
}
//////////////////////////////////以下只读取了"version 300"一行,其余行都可以按照下面的方法读取
//读取文件版本
int ver=0;
in3.getline(line,sizeof(line));
sscanf(line,"%s %d",str,&ver);
//获得文件中点数目
int pointnum=0;
//当文件未结束且文件未出现错误执行
while(!in3.eof())
{
in3.getline(line,sizeof(line));
sscanf(line,"%s",str);
if((strcmp(str,"Text")==0)||(strcmp(str,"text")==0)||(strcmp(str,"Point")==0)||(strcmp(str,"point")==0))
{
pointnum++;
}
}
type->num_pt = pointnum;
//关闭in3
in3.close();
//新文件指针指到文件起点
ifstream in4(datafile);
in4.getline(line,sizeof(line));
//type->pt=new Point[pointnum];
type->pt = (Point*) new char[sizeof(Point) * pointnum];
//用j来表示依次读取文件内各数据点的操作
int j = 0;
while(!in4.eof())
{
in4.getline(line,sizeof(line));
sscanf(line,"%s",str);
while(j < (pointnum))
{
in4.getline(line,sizeof(line));
sscanf(line,"%s ",str);
//如果是带地名的点
if((strcmp(str,"Text")==0)||(strcmp(str,"text")==0))
{
//读取点名称
in4.getline(line,sizeof(line));
sscanf(line,"%s",str);
//将点名称读入点类型指针数组的名字域
strcpy(type->pt[j].name, str);
//读取各点的经纬度
in4.getline(line,sizeof(line));
sscanf(line,"%f %f",&type->pt[j].lat,&type->pt[j].lon);
type->pt[j].hgt=0.0;
j++;
}
else if((strcmp(str,"Point")==0)||(strcmp(str,"point")==0))
{
//将该点名赋值为TIC点
strcpy(type->pt[j].name, "TIC点");
//直接读取点的经纬度
sscanf(line,"%s %f %f",str,&type->pt[j].lat,&type->pt[j].lon);
type->pt[j].hgt = 0.0;
j++;
}
}
//关闭真正的数据文件
in4.close();
}
}
//关闭mif索引文件
in2.close();
AfxMessageBox("读取mif点文件成功!!!");
redraw = true;
return ;
}
void CMIFDoc::load_ln_mapinfo(char * filename)
{
//颜色表处理
unsigned int color;
float red,green,blue,alpha,size;
red=1.0;
green=0.5;
blue=0.5;
alpha=1.0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -