网站主页 新闻动态 产品介绍 软件下载 教学视频 资源中心 技术支持 产品销售 单位简介
 
欢迎光临本站!
今天是2024年4月28日星期日农历三月二十
 
 
 
MO基本代码(VC)
 

 
 
  作者:gis
发表时间:2004-4-18 18:52:56

////**********属性浏览**********
//
///////////////////////////////////////////////////////////////
//函数功能 : 选择地图要素
//处理过程 : 先得到鼠标点击的要素的Recordset,将鼠标点击的要素加入到Track层
//返 回 值 : void
//参数说明 : short Shift, //是否按下了Shift键
// long X, long Y
///////////////////////////////////////////////////////////////
void SelectFeatToSelection(CMap1& m_Map, short Shift, long X, long Y)
{
if(Shift==0) CLISTrackLayer::ClearTrack(m_Map); //清除所有Track层数据
long ShapeType;
CMoRecordset selSet;
if(SelectFeatureOnMap(m_Map,X,Y,m_SelTolerance,selSet,ShapeType))
// SelectFeatureOnMap函数参考如下
{
CMoFields MoFields = selSet.GetFields();
CMoField shapeField(MoFields.Item(COleVariant(TEXT("Shape"))));
if(ShapeType==moShapeTypePolygon)
{
CMoPolygon shape(shapeField.Getvalue().pdispVal);
CLISTrackLayer::AddTrack(m_Map,shape,TRACK_SYMBOL_BLOCK0);
}
if(ShapeType==moShapeTypeLine)
{
CMoLine shape(shapeField.Getvalue().pdispVal);
CLISTrackLayer::AddTrack(m_Map,shape,TRACK_SYMBOL_LINE0);
}
if(ShapeType==moShapeTypePoint)
{
CMoPoint shape(shapeField.Getvalue().pdispVal);
CLISTrackLayer::AddTrack(m_Map,shape,TRACK_SYMBOL_POINT0);
}
}
}
///////////////////////////////////////////////////////////////
//函数功能 : 选择地图要素,自动判断该要素属于那一层,返回该要素的Recordset和
// 要素所在图层的类型(点、线、面)
//返 回 值 : BOOL
//参数说明 : CMap1& map,
//long X, long Y,
//double ptSize, 选择范围
//CMoRecordset& set, 要素的Recordset(返回参数)
//long& ShapeType 要素所在图层的类型(返回参数)
///////////////////////////////////////////////////////////////
BOOL SelectFeatureOnMap(CMap1& map,long X,long Y,double ptSize,CMoRecordset
& set,long& ShapeType)
{
double tolerance = map.ToMapDistance((float)ptSize);
CMoPointmapPt(map.ToMapPoint((float)X, (float)Y));
CMoLayers layers(map.GetLayers());
for(long i=0;i<layers.GetCount();i++)
{
CMoMapLayer Itemlayer(layers.Item(COleVariant(i)));
if(!Itemlayer.GetVisible()) continue;
ShapeType = Itemlayer.GetShapeType();
if(Itemlayer.GetShapeType()==moShapeTypePolygon)
{
set = Itemlayer.SearchShape(mapPt,moPointInPolygon, TEXT(""));
if(!set.GetEof()) return TRUE;
}
if(Itemlayer.GetShapeType()==moShapeTypePoint||Itemlayer.GetShapeType()==mo
ShapeTypeLine)
{
set = Itemlayer.SearchByDistance(mapPt,tolerance, TEXT(""));
if(!set.GetEof()) return TRUE;
}
}
return FALSE;
}
///////////////////////////////////////////////////////////////
//函数功能 : 取消选择
//处理过程 : 清除所有Track层数据
//返 回 值 : void
//参数说明 : CMap1& map
///////////////////////////////////////////////////////////////
void DelTrack(CMap1& map)
{
CLISTrackLayer::ClearTrack(map);
}
///////////////////////////////////////////////////////////////
//函数功能 : 输入线、矩形、多边形,选择与其有相交或包含关系的要素
//处理过程 :
//返 回 值 : BOOL
//参数说明 : BOOL fromTrackShp, ;是否直接从Track层取几何图形来选择
// int nID ;要输入几何图形的类型
///////////////////////////////////////////////////////////////
BOOL ShowLayerFWAttrib(CMap1& m_Map, BOOL fromTrackShp, int nID)
{
CMoPolygon poly;
CMoLine line;
CMoRectangle rect;
CMoEllipse Ellipse;
int shapeType = 0;
if(fromTrackShp)
{
if(CLISTrackLayer::GetTrackCount(m_Map)!=1)
{
CLISTrackLayer::ClearTrack(m_Map);
return FALSE;
}
if(CLISTrackLayer::GetTrackShapeType(m_Map,0)==moShapeTypePolygon)
{
CLISTrackLayer::GetTrackShape(m_Map,0,poly); //获得Track层的第一个多边

if(!LPDISPATCH(poly)) return FALSE;
shapeType = moShapeTypePolygon;
}
else if(CLISTrackLayer::GetTrackShapeType(m_Map,0)==moShapeTypeLine)
{
CLISTrackLayer::GetTrackShape(m_Map,0,line); //获得Track层的第一个线
if(!LPDISPATCH(line)) return FALSE;
shapeType = moShapeTypeLine;
}
else
{
CLISTrackLayer::ClearTrack(m_Map);
return FALSE;
}
}
else
{
if(nID==ID_MAP_POLYFWATT) //输入多边形
{
poly = m_Map.TrackPolygon();
if(!LPDISPATCH(poly)) return FALSE;
CLISTrackLayer::AddTrack(m_Map,poly,TRACK_SYMBOL_BLOCK0);
shapeType = moShapeTypePolygon;
}
else if(nID==ID_MAP_RECTFWATT) //输入矩形
{
rect = m_Map.TrackRectangle();
if(!LPDISPATCH(rect)) return FALSE;
CLISTrackLayer::AddTrack(m_Map,rect,TRACK_SYMBOL_BLOCK0);
shapeType = moShapeTypeRectangle;
}
else if(nID==ID_MAP_CIRCLFWATT) //输入圆
{
Ellipse = m_Map.TrackCircle();
if(!LPDISPATCH(Ellipse)) return FALSE;
CLISTrackLayer::AddTrack(m_Map,Ellipse,TRACK_SYMBOL_BLOCK0);
shapeType = moShapeTypeEllipse;
}
else if(nID==ID_MAP_LINEFWATT) //输入线
{
line = m_Map.TrackLine();
if(!LPDISPATCH(line)) return FALSE;
CLISTrackLayer::AddTrack(m_Map,line,TRACK_SYMBOL_LINE0);
shapeType = moShapeTypeLine;
}
else
{

}
}
CString LayerCode = FormatSDELayerCode(*m_LayerManger.m_curLayer);
CMoLayers layers(m_Map.GetLayers());
CMoMapLayer Itemlayer(layers.Item(COleVariant(LayerCode)));
if(!LPDISPATCH(Itemlayer))
{
AfxMessageBox("打 不 开 选 中 的 层 !");
return TRUE;
}
long ShapeType = Itemlayer.GetShapeType();
CMoRecordset selSet;
if(shapeType == moShapeTypePolygon)
selSet = Itemlayer.SearchShape(poly,moAreaIntersect, TEXT(""));
else if(shapeType == moShapeTypeLine)
selSet = Itemlayer.SearchShape(line,moAreaIntersect, TEXT(""));
else if(shapeType == moShapeTypeRectangle)
selSet = Itemlayer.SearchShape(rect,moAreaIntersect, TEXT(""));
else if(shapeType == moShapeTypeEllipse)
selSet = Itemlayer.SearchShape(Ellipse,moAreaIntersect, TEXT(""));
else
return TRUE;
// 根据返回的Recordset对象在对话框中显示属性值,代码略
return TRUE;
}
///////////////////////////////////////////////////////////////
//函数功能 : 根据表达式查询地图要素
//处理过程 :
//备 注 :
//返 回 值 : BOOL
//参数说明 : CMap1& map,
//CString layerCode, //查询目标图层的名称
//CString SQL, //查询表达式,SQL形式
//CMoRecordset& selSet, //返回查询要素的Recordset
//long& ShapeType //返回查询目标图层的类型
///////////////////////////////////////////////////////////////
BOOL SQLSearchFeature(CMap1& map,CString layerCode,CString SQL,CMoRecordset&
selSet, long& ShapeType)
{
for(int i=0;i<m_LayerManger.m_layers.GetSize();i++)
{
if(!m_LayerManger.m_layers.m_Visible) continue;
if(m_LayerManger.m_layers.m_LayerCode.CompareNoCase(layerCode)!=0) con
tinue;
// 寻找目标图层
CMoLayers layers = map.GetLayers();
CMoMapLayer SearchLayer(layers.Item(COleVariant(FormatSDELayerCode(m_Lay
erManger.m_layers))));
if(!LPDISPATCH(SearchLayer)) return FALSE;
VARIANT va;
VariantInit(&va);
CMoRectangle Rect,RectCell;
Rect.CreateDispatch(TEXT("MapObjects2.Rectangle"));
RectCell.CreateDispatch(TEXT("MapObjects2.Rectangle"));
BOOL Flag = TRUE;
ShapeType = SearchLayer.GetShapeType();
selSet = SearchLayer.SearchExpression(SQL);
if(selSet.GetEof()) return FALSE;
while(!selSet.GetEof())
{
CMoFields MoFields = selSet.GetFields();
CMoField shapeField(MoFields.Item(COleVariant(TEXT("Shape"))));
if(ShapeType==moShapeTypePolygon)
{
CMoPolygon shape(shapeField.Getvalue().pdispVal);
CLISTrackLayer::AddTrack(map,shape,TRACK_SYMBOL_BLOCK1);
RectCell = shape.GetExtent();
}
if(ShapeType==moShapeTypeLine)
{
CMoLine shape(shapeField.Getvalue().pdispVal);
CLISTrackLayer::AddTrack(map,shape,TRACK_SYMBOL_LINE1);
RectCell = shape.GetExtent();
}
if(ShapeType==moShapeTypePoint)
{
CMoPoint shape(shapeField.Getvalue().pdispVal);
CLISTrackLayer::AddTrack(map,shape,TRACK_SYMBOL_POINT1);
RectCell.SetLeft(shape.GetX()-1);
RectCell.SetRight(shape.GetX()+1);
RectCell.SetTop(shape.GetY()+1);
RectCell.SetBottom(shape.GetY()-1);
}
if(Flag)
{
Rect = RectCell;
Flag = FALSE;
}
else
{
if(Rect.GetLeft()>RectCell.GetLeft()) Rect.SetLeft(RectCell.GetLeft())
;
if(Rect.GetRight()<RectCell.GetRight()) Rect.SetRight(RectCell.GetRigh
t());
if(Rect.GetBottom()>RectCell.GetBottom()) Rect.SetBottom(RectCell.GetB
ottom());
if(Rect.GetTop()<RectCell.GetTop()) Rect.SetTop(RectCell.GetTop());
}
selSet.MoveNext();
}
Rect.ScaleRectangle(1.2);
map.SetExtent(Rect);
map.Refresh();
break;
}
return TRUE;
}
///////////////////////////////////////////////////////////////
//函数功能 : 坐标点定位
//返 回 值 : void
//参数说明 : CMap1& m_Map,
// long X, long Y; 坐标点
///////////////////////////////////////////////////////////////
void SetPos(CMap1& m_Map, long X,long Y)
{
CMoRectangle moRect(m_Map.GetExtent());
CMoRectangle moRectNew;
moRectNew.CreateDispatch(TEXT("MapObjects2.Rectangle"));
moRectNew.SetLeft(X - moRect.GetWidth()/2.0);
moRectNew.SetRight(X + moRect.GetWidth()/2.0);
moRectNew.SetTop(Y + moRect.GetHeight()/2.0);
moRectNew.SetBottom(Y - moRect.GetHeight()/2.0);
m_Map.SetExtent(moRectNew);
m_Map.Refresh();
}

////**********图形浏览**********
//
///////////////////////////////////////////////////////////////
//函数功能 : 拉框放大
//返 回 值 : void
//参数说明 : CMap1& map
///////////////////////////////////////////////////////////////
void ZoomIn(CMap1& map)
{
CMoRectangle r(map.TrackRectangle());
if (LPDISPATCH(r))
map.SetExtent(r);
}
///////////////////////////////////////////////////////////////
//函数功能 : 点击缩小
//返 回 值 : void
//参数说明 : CMap1& map
///////////////////////////////////////////////////////////////
void ZoomOut(CMap1& map)
{
CMoRectangle r(map.GetExtent());
ASSERT(LPDISPATCH(r));
r.ScaleRectangle(1.5); //1.5为每次缩小比例
map.SetExtent(r);
}
///////////////////////////////////////////////////////////////
//函数功能 : 漫游
//返 回 值 : void
//参数说明 : CMap1& map
///////////////////////////////////////////////////////////////
void Pan(CMap1& map)
{
map.Pan();
}
///////////////////////////////////////////////////////////////
//函数功能 : 刷新
//返 回 值 : void
//参数说明 : CMap1& map
///////////////////////////////////////////////////////////////
void Refresh(CMap1& map)
{
map.Refresh();
}
///////////////////////////////////////////////////////////////
//函数功能 : 全图显示
//返 回 值 : void
//参数说明 : CMap1& map
///////////////////////////////////////////////////////////////
void ZoomFull(CMap1& map)
{
CMoRectangle r(map.GetFullExtent()); //获得地图全图范围
ASSERT(LPDISPATCH(r));
map.SetExtent(r);
}
///////////////////////////////////////////////////////////////
//函 数 名 : Identify
//函数功能 : 属性查询
//处理过程 :
//返 回 值 : void
//参数说明 : long X, long Y ;X,Y为鼠标点击地图屏幕时,鼠标的屏幕位置
///////////////////////////////////////////////////////////////
void Identify(long X,long Y)
{
long ShapeType;
CMoRecordset selSet;
CMoMapLayer selLayer;
if(SelectFeatureOnMap(*m_Map,X,Y,m_SelTolerance/* 咬合范围的大小 */,selSet
,ShapeType,selLayer))
//SelectFeatureOnMap函数参考如下
{
CMoFields MoFields = selSet.GetFields();
FlashMoFields(*m_Map,MoFields,ShapeType); //使所选择的要素闪烁
// 根据返回的Recordset对象在无模式对话框中显示属性值,代码略
}
}

///////////////////////////////////////////////////////////////
//函数功能 : 返回要素属性
//处理过程 :
//返 回 值 : BOOL
//参数说明 : CMap1& map, long X, long Y, double ptSize,
// CMoRecordset& set, 该要素的Recordset (返回参数)
// long& ShapeType, 要素所在图层的类型(返回参数)
// CMoMapLayer& layer 要素所在图层 (返回参数)
///////////////////////////////////////////////////////////////
BOOL SelectFeatureOnMap(CMap1& map,long X,long Y,double ptSize,CMoRecordset
& set,long& ShapeType,CMoMapLayer& layer)
{
double tolerance = map.ToMapDistance((float)ptSize);
CMoPointmapPt(map.ToMapPoint((float)X, (float)Y));
CMoLayers layers(map.GetLayers());
for(long i=0;i<layers.GetCount();i++)
{
CMoMapLayer Itemlayer(layers.Item(COleVariant(i)));
if(!Itemlayer.GetVisible()) continue;
ShapeType = Itemlayer.GetShapeType();
if(Itemlayer.GetShapeType()==moShapeTypePolygon)
{
set = Itemlayer.SearchShape(mapPt,moPointInPolygon, TEXT(""));
layer = Itemlayer;
if(!set.GetEof()) return TRUE;
}
if(Itemlayer.GetShapeType()==moShapeTypePoint||Itemlayer.GetShapeType()==mo
ShapeTypeLine)
{
set = Itemlayer.SearchByDistance(mapPt,tolerance, TEXT(""));
layer = Itemlayer;
if(!set.GetEof()) return TRUE;
}
}
return FALSE;
}
///////////////////////////////////////////////////////////////
//函数功能 : 使Recordset对应的要素闪烁
//返 回 值 : void
//参数说明 : CMap1& map, CMoFields MoFields, MoFields为要素的Recordset对应的
Fields
// long ShapeType
///////////////////////////////////////////////////////////////
void FlashMoFields(CMap1& map,CMoFields MoFields,long ShapeType)
{
CMoField shapeField(MoFields.Item(COleVariant(TEXT("Shape"))));
if(ShapeType==moShapeTypePolygon)
{
CMoPolygon shape(shapeField.Getvalue().pdispVal);
map.FlashShape(shape,2); // 2为闪烁次数
}
if(ShapeType==moShapeTypeLine)
{
CMoLine shape(shapeField.Getvalue().pdispVal);
map.FlashShape(shape,2);
}
if(ShapeType==moShapeTypePoint)
{
CMoPoint shape(shapeField.Getvalue().pdispVal);
map.FlashShape(shape,2);
}
}
///////////////////////////////////////////////////////////////
//函数功能 : 设置地图显示比例尺
//处理过程 : 先得到屏幕上一米包含多少个象素点,地图的宽度(象素点值)除以该值
得到
// 地图的显示实际宽度(米),地图的显示宽度(米)乘地图显示的比例尺
即得到该比例尺
// 下地图宽度(象素点值)
//返 回 值 : void
//参数说明 : CMap1& map, double scale ;scale为地图显示比例尺
///////////////////////////////////////////////////////////////
void SetMapScale(CMap1& map,double scale)
{
CDC* pDC = map.GetDC();
double MiterPointNum = (1000.0/25.4)*GetDeviceCaps(pDC->m_hDC,LOGPIXELSX);
//MiterPointNum为屏幕上一米有多少个象素点
//GetDeviceCaps(pDC->m_hDC,LOGPIXELSX) 返回屏幕横轴方向上每英寸长度包含的象
素个数
CRect r = GetDeviceRect(pDC);
double widthX = r.Width()*scale/MiterPointNum;
double widthY = r.Height()*scale/MiterPointNum;
CMoRectangle moRect(map.GetExtent());
CMoPoint pt(moRect.GetCenter());
CMoRectangle moRectNew;
if(!moRectNew.CreateDispatch(TEXT("MapObjects2.Rectangle")))
return;
moRectNew.SetLeft(pt.GetX()-widthX/2.0);
moRectNew.SetRight(pt.GetX()+widthX/2.0);
moRectNew.SetTop(pt.GetY()+widthY/2.0);
moRectNew.SetBottom(pt.GetY()-widthY/2.0);
map.SetExtent(moRectNew);
map.Refresh();
map.ReleaseDC(pDC);
}
 
     
 

您是第 2107448 位来宾  官方QQ群:916871934  电话:0731-84186551
网站指南 | 欢迎合作 | 刊登广告 | 人才引进 |  网站管理 |  会员退出
Copyright 2018 版权所有,All Rights Reserved,Tel:0731-84186551
长沙市规划勘测设计研究院 湘ICP备10002110号