📄 enghelper.cpp
字号:
#include "stdafx.h"
void Eng_AddLayerMap(CMap1 &map,const CString& path,COLORREF color)
{
CMoDataConnection conn;
conn.CreateDispatch(TEXT("MapObjects2.DataConnection"));
CString sDir;
Eng_GetPathOfPathFile(path,sDir); //不包含驱动器存在问题。
conn.SetDatabase(sDir);
if(!conn.Connect())
throw new CFileException(CFileException::fileNotFound);
CMoLayers layers(map.GetLayers());
CMoMapLayer layer;
layer.CreateDispatch(TEXT("MapObjects2.MapLayer"));
CString sTitle;
Eng_GetFileOfPathFile(path,sTitle);
CMoGeoDataset geoDataset(conn.FindGeoDataset(sTitle));
layer.SetGeoDataset(geoDataset);
if(color!=-1)
{
CMoSymbol layerSymbol(layer.GetSymbol());
layerSymbol.SetColor(color);
}
layer.SetTag("[1]"+path);
//默认增加的在最上面
layers.Add(layer);
//将新增加的放在最后
//layers.MoveToBottom(0);
}
void Eng_AddLayerCad(CMap1 &map,const CString& path,COLORREF color)
{
CMoDataConnection conn;
conn.CreateDispatch(TEXT("MapObjects2.DataConnection"));
conn.SetDatabase("[CAD]"+GetFileDirectory(path));
if(!conn.Connect())
throw new CFileException(CFileException::fileNotFound);
CMoLayers layers(map.GetLayers());
CMoMapLayer layer;
layer.CreateDispatch(TEXT("MapObjects2.MapLayer"));
CMoGeoDataset geoDataset(conn.FindGeoDataset(GetFileName(path)));
layer.SetGeoDataset(geoDataset);
if(color!=-1)
{
CMoSymbol layerSymbol(layer.GetSymbol());
layerSymbol.SetColor(color);
}
layer.SetTag("[3]"+path);
//默认增加的在最上面
layers.Add(layer);
}
void Eng_AddLayerImage(CMap1 &map,const CString& path,COLORREF color)
{
// Establish connection to data
CMoDataConnection conn;
if (!conn.CreateDispatch(TEXT("MapObjects2.DataConnection")))
throw "unable to create MapObjects2.DataConnection";
CString sDir;
Eng_GetPathOfPathFile(path,sDir);
conn.SetDatabase(sDir);
if (!conn.Connect())
throw "unable to connect to database";
// Add image layer
CMoLayers layers(map.GetLayers());
CMoImageLayer image;
if (!image.CreateDispatch(TEXT("MapObjects2.ImageLayer")))
throw "unable to create MapObjects2.ImageLayer";
image.SetFile(path);
image.SetTransparent(TRUE);
image.SetTransparentColor(RGB(255,255,255));
image.SetTag("[2]"+path);
layers.Add(image);
}
void Eng_AddLayerArc(CMap1 &map,const CString& path,COLORREF color)
{
int n=0;
CString sPath,sFolder,sDir,sFile;
CString sName,sArc;
n=path.ReverseFind('\\');
sPath=path.Left(n);
sFile=path.Mid(n+1);
sName=sFile.Left(sFile.GetLength()-4);
n=sPath.ReverseFind('\\');
sFolder=sPath.Mid(n+1);
sDir=sPath.Left(n);
sArc=sFolder+"."+sName;
CMoDataConnection conn;
conn.CreateDispatch(TEXT("MapObjects2.DataConnection"));
conn.SetDatabase("[arc]"+sDir);
if(!conn.Connect())
throw new CFileException(CFileException::fileNotFound);
CMoLayers layers(map.GetLayers());
CMoMapLayer layer;
layer.CreateDispatch(TEXT("MapObjects2.MapLayer"));
CMoGeoDataset geoDataset(conn.FindGeoDataset(sArc));
layer.SetGeoDataset(geoDataset);
if(color!=-1)
{
CMoSymbol layerSymbol(layer.GetSymbol());
layerSymbol.SetColor(color);
}
layer.SetTag("[4]"+path);
//默认增加的在最上面
layers.Add(layer);
//将新增加的放在最后
//layers.MoveToBottom(0);
/*
CMoDataConnection conn;
conn.CreateDispatch(TEXT("MapObjects2.DataConnection"));
// conn.SetDatabase("[arc]"+GetFileDirectory(path));
conn.SetDatabase("[arc]d:\\pub\\usa_arc");
if(!conn.Connect())
throw new CFileException(CFileException::fileNotFound);
CMoLayers layers(map.GetLayers());
CMoMapLayer layer;
layer.CreateDispatch(TEXT("MapObjects2.MapLayer"));
CMoGeoDataset geoDataset(conn.FindGeoDataset("usa.aat"));
layer.SetGeoDataset(geoDataset);
if(color!=-1)
{
CMoSymbol layerSymbol(layer.GetSymbol());
layerSymbol.SetColor(color);
}
//默认增加的在最上面
layers.Add(layer);
*/
}
void Eng_AddLayerSde(CMap1 &map,const CString& path,COLORREF color)
{
CMoDataConnection conn;
conn.CreateDispatch(TEXT("MapObjects2.DataConnection"));
CString sDir;
Eng_GetPathOfPathFile(path,sDir);
conn.SetDatabase(sDir);
if(!conn.Connect())
throw new CFileException(CFileException::fileNotFound);
CMoLayers layers(map.GetLayers());
CMoMapLayer layer;
layer.CreateDispatch(TEXT("MapObjects2.MapLayer"));
CString sTitle;
Eng_GetFileOfPathFile(path,sTitle);
CMoGeoDataset geoDataset(conn.FindGeoDataset(sTitle));
layer.SetGeoDataset(geoDataset);
if(color!=-1)
{
CMoSymbol layerSymbol(layer.GetSymbol());
layerSymbol.SetColor(color);
}
layer.SetTag("[5]"+path);
//默认增加的在最上面
layers.Add(layer);
//将新增加的放在最后
//layers.MoveToBottom(0);
}
void Eng_GetRunPath(CString &sPath)
{
TCHAR szPath[MAX_PATH];
::GetModuleFileName(NULL,szPath,MAX_PATH);
CString s=szPath;
sPath=s.Left(s.ReverseFind('\\'));
}
void Eng_GetRandInt(int n0,int n1,int &r,int &g,int &b)
{
int n;
double rate;
// srand((unsigned)time(NULL));
n=rand();
rate=(double)n/(double)RAND_MAX;
r=n0+(int)(rate*(double)(n1-n0));
n=rand();
rate=(double)n/(double)RAND_MAX;
g=n0+(int)(rate*(double)(n1-n0));
n=rand();
rate=(double)n/(double)RAND_MAX;
b=n0+(int)(rate*(double)(n1-n0));
}
void Eng_GetPathOfPathFile(CString sPathFile,CString &sPath)
{
char szDrive[MAX_PATH],szDir[MAX_PATH],szFile[MAX_PATH],szExt[MAX_PATH];
_tsplitpath(sPathFile,szDrive,szDir,szFile,szExt);
sPath=szDrive;
sPath+=szDir;
}
void Eng_GetDirOfPathFile(CString sPathFile,CString &sDir)
{
char szDrive[MAX_PATH],szDir[MAX_PATH],szFile[MAX_PATH],szExt[MAX_PATH];
_tsplitpath(sPathFile,szDrive,szDir,szFile,szExt);
sDir=szDir;
}
void Eng_GetFileOfPathFile(CString sPathFile,CString &sFile)
{
char szDrive[MAX_PATH],szDir[MAX_PATH],szFile[MAX_PATH],szExt[MAX_PATH];
_tsplitpath(sPathFile,szDrive,szDir,szFile,szExt);
// sFile=szDir;
sFile=szFile;
// sFile+=".";
sFile+=szExt;
}
void Eng_GetFileExtOfPathFile(CString sPathFile,CString &sExt)
{
char szDrive[MAX_PATH],szDir[MAX_PATH],szFile[MAX_PATH],szExt[MAX_PATH];
_tsplitpath(sPathFile,szDrive,szDir,szFile,szExt);
sExt=szDir;
}
void Eng_GetLayerDataPara(CMap1 &map,short iLayer, CStringArray &saPara)
{
CMoLayers layers(map.GetLayers());
CMoMapLayer layer(layers.Item(COleVariant(iLayer)));
CMoProjCoordSys projSys(layer.GetCoordinateSystem().pdispVal);
CString s;
if(projSys.GetName()!="")
{
s="投影坐标系统:"+projSys.GetName();
saPara.Add(s);
CMoGeoCoordSys geoSys=projSys.GetGeoCoordSys();
s="地理坐标系统:"+geoSys.GetName();
saPara.Add(s);
CMoUnit unit=projSys.GetUnit();
s="单位:"+unit.GetName();
saPara.Add(s);
CMoProjection proj(projSys.GetProjection());
s="投影:"+proj.GetName();
saPara.Add(s);
}
else
{
s="投影坐标系统:无";
saPara.Add(s);
s="地理坐标系统:无";
saPara.Add(s);
s="单位:?";
saPara.Add(s);
s="投影:?";
saPara.Add(s);
}
}
void Eng_GetLayerField(CMap1 &map,short iLayer, CLayerFieldArray &taLayerFields)
{
CMoLayers layers(map.GetLayers());
CMoMapLayer layer(layers.Item(COleVariant(iLayer)));
CMoRecordset rs(layer.GetRecords());
CMoTableDesc table=rs.GetTableDesc();
//统计数据
CString s;
CMoStatistics st;
SEngLayerFields elf;
long type;
short i,count=table.GetFieldCount();
for(i=0;i<count;i++)
{
memset(&elf,0,sizeof(SEngLayerFields));
s=table.GetFieldName(i);
strcpy(elf.szLayer,"layer");
strcpy(elf.szField,s);
type=table.GetFieldType(i);
switch(type)
{
case moNone: //= 0
strcpy(elf.szDataType,"无");
break;
case moLong:// = 3,
strcpy(elf.szDataType,"长整数");
st=rs.CalculateStatistics(s);
elf.count=st.GetCount();
elf.dev=st.GetStdDev();
elf.max=st.GetMax();
elf.mean=st.GetMean();
elf.min=st.GetMin();
elf.sum=st.GetSum();
break;
case moDouble: // = 5,
strcpy(elf.szDataType,"双精度");
st=rs.CalculateStatistics(s);
elf.count=st.GetCount();
elf.dev=st.GetStdDev();
elf.max=st.GetMax();
elf.mean=st.GetMean();
elf.min=st.GetMin();
elf.sum=st.GetSum();
break;
case moDate: // = 7,
strcpy(elf.szDataType,"日期");
st=rs.CalculateStatistics(s);
elf.count=st.GetCount();
break;
case moString: // = 8,
strcpy(elf.szDataType,"字符串");
st=rs.CalculateStatistics(s);
elf.count=st.GetCount();
break;
case moBoolean: // = 11,
strcpy(elf.szDataType,"布尔");
st=rs.CalculateStatistics(s);
elf.count=st.GetCount();
break;
case moPoint: //= 21,
strcpy(elf.szDataType,"点");
st=rs.CalculateStatistics(s);
elf.count=st.GetCount();
break;
case moLine: // = 22,
strcpy(elf.szDataType,"线");
st=rs.CalculateStatistics(s);
elf.count=st.GetCount();
break;
case moPolygon: // = 23,
strcpy(elf.szDataType,"多边形");
st=rs.CalculateStatistics(s);
elf.count=st.GetCount();
break;
case moPoints: // = 24
strcpy(elf.szDataType,"多点");
st=rs.CalculateStatistics(s);
elf.count=st.GetCount();
break;
default:
strcpy(elf.szDataType,"错误");
break;
}
taLayerFields.Add(elf);
}
}
void Eng_SetLegend(CMap1 &map,C_legend &legend)
{
LPUNKNOWN pUnknown = map.GetControlUnknown();
LPDISPATCH pDispatch = 0;
// QI for the IDispatch pointer
if (pUnknown)
pUnknown->QueryInterface(IID_IDispatch, (void**)&pDispatch);
// Link the legend with the map control
if (pDispatch)
legend.setMapSource(&pDispatch);
int pbool = 1;
legend.LoadLegend(&pbool);
// Release the dispatch pointer
if (pDispatch)
pDispatch->Release();
}
void Eng_GetRsStatistic(CMoRecordset &rs,CLayerFieldArray &taLayerFields)
{
CMoTableDesc table=rs.GetTableDesc();
//统计数据
CString s;
CMoStatistics st;
SEngLayerFields elf;
long type;
short i,count=table.GetFieldCount();
for(i=0;i<count;i++)
{
memset(&elf,0,sizeof(SEngLayerFields));
s=table.GetFieldName(i);
strcpy(elf.szLayer,"layer");
strcpy(elf.szField,s);
type=table.GetFieldType(i);
switch(type)
{
case moNone: //= 0
strcpy(elf.szDataType,"无");
break;
case moLong:// = 3,
strcpy(elf.szDataType,"长整数");
st=rs.CalculateStatistics(s);
elf.count=st.GetCount();
elf.dev=st.GetStdDev();
elf.max=st.GetMax();
elf.mean=st.GetMean();
elf.min=st.GetMin();
elf.sum=st.GetSum();
break;
case moDouble: // = 5,
strcpy(elf.szDataType,"双精度");
st=rs.CalculateStatistics(s);
elf.count=st.GetCount();
elf.dev=st.GetStdDev();
elf.max=st.GetMax();
elf.mean=st.GetMean();
elf.min=st.GetMin();
elf.sum=st.GetSum();
break;
case moDate: // = 7,
strcpy(elf.szDataType,"日期");
st=rs.CalculateStatistics(s);
elf.count=st.GetCount();
break;
case moString: // = 8,
strcpy(elf.szDataType,"字符串");
st=rs.CalculateStatistics(s);
elf.count=st.GetCount();
break;
case moBoolean: // = 11,
strcpy(elf.szDataType,"布尔");
st=rs.CalculateStatistics(s);
elf.count=st.GetCount();
break;
case moPoint: //= 21,
strcpy(elf.szDataType,"点");
st=rs.CalculateStatistics(s);
elf.count=st.GetCount();
break;
case moLine: // = 22,
strcpy(elf.szDataType,"线");
st=rs.CalculateStatistics(s);
elf.count=st.GetCount();
break;
case moPolygon: // = 23,
strcpy(elf.szDataType,"多边形");
st=rs.CalculateStatistics(s);
elf.count=st.GetCount();
break;
case moPoints: // = 24
strcpy(elf.szDataType,"多点");
st=rs.CalculateStatistics(s);
elf.count=st.GetCount();
break;
default:
strcpy(elf.szDataType,"错误");
break;
}
taLayerFields.Add(elf);
}
}
void Eng_SetLegend(CMap1 &map,C_ScaleBar &scaleBar)
{
}
//BOOL Eng_RoateLine(CMoLine &line,CMoPoint &pt,double dAngle)
BOOL Eng_RoatePoly(CMoPolygon &poly,double dAngle)
{
CMoPoint pt;
pt=poly.GetCentroid(); //重心
double xs,ys;
CMoPoints pts,pts1;
double x1, y1, a1, c, a2, x2, y2;
CMoPoint pt0;
long i;
xs =pt.GetX();
ys =pt.GetY();
pts=poly.GetParts().Item(COleVariant(((short)0)));
pts1.CreateDispatch(TEXT("MapObjects2.Points"));
for(i=0;i<pts.GetCount();i++)
{
pt0=pts.Item(COleVariant(i));
x1 =pt0.GetX() - xs;
y1 =pt0.GetY() - ys;
a1 = atan(y1 / x1);
c = y1 / sin(a1);
a2 = (dAngle * 0.01745329) + a1;
x2 = cos(a2) * c;
y2 = sin(a2) * c;
pt0.SetX(xs + x2);
pt0.SetY(ys + y2);
pts1.Add(pt0);
}
short j;
for(j=0;j<pts.GetCount();j++)
poly.GetParts().Remove(j);
poly.GetParts().Add(pts1);
return TRUE;
}
void Eng_GetLayers(CMap1 &map,CStringArray &saLayer)
{
CString s;
short i,count;
CMoLayers layers(map.GetLayers());
count=layers.GetCount();
for(i=0;i<count;i++)
{
switch(GetLayerType(layers,i))
{
case moMapLayer:
{
CMoMapLayer layer=layers.Item(COleVariant(i));
s=layer.GetTag();
s.Replace("[1]","");
saLayer.Add(s);
}
break;
case moImageLayer:
{
CMoImageLayer layer=layers.Item(COleVariant(i));
s=layer.GetTag();
s.Replace("[2]","");
saLayer.Add(s);
}
break;
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -