///
/// 地图加载
///
///
///
///
public bool
ArcEngineShapeMapLoad(AxESRI.ArcGIS.Controls.AxMapControl
mAxMapControl, string shapeMapPath, string shapeMapName)
{
bool Flag
= false;
if
(File.Exists(shapeMapPath + shapeMapName))
{
mAxMapControl.AddShapeFile(shapeMapPath, shapeMapName);
Flag = true;
}
else
{
Flag = false;
}
return
Flag;
}
///
/// 地图加载(MXD)
///
///
public bool
ArcEngineMXDMapLoad(AxESRI.ArcGIS.Controls.AxMapControl
mAxMapControl, string shapeMapPath)
{
bool Flag
= false;
if
(File.Exists(shapeMapPath))
{
IMapDocument pMapDocument =
new MapDocumentClass();
pMapDocument.Open(shapeMapPath, "");
for (int i = 0; i <=
pMapDocument.MapCount - 1; i++)
{
//一个IMapDocument对象中可能有多个Map对象,遍历每个map对象
mAxMapControl.Map =
pMapDocument.get_Map(i);
}
mAxMapControl.Refresh();
Flag = true;
}
else
{
Flag = false;
}
return
Flag;
}
///
/// 文字标注
///
///
///
///
///
///
///
///
public bool ArcEngineMapTextNoted(ILayer layer,
string fieldName, int rgbRed, int rgbGreen, int rgbBlue, int
fontSize)
{
bool Flag
= false;
IGeoFeatureLayer mIGeoFeatureLayer = layer as
IGeoFeatureLayer;
IAnnotateLayerPropertiesCollection
mIAnnotateLayerPropertiesCollection =
mIGeoFeatureLayer.AnnotationProperties;
mIAnnotateLayerPropertiesCollection.Clear();
IRgbColor
mIRgbColor = ArcEngineCreateAlgorithmicColorRamp(rgbRed, rgbGreen,
rgbBlue);
IFontDisp
mIFontDisp = new StdFont()
{
Name = "宋体",
Bold = true
} as
IFontDisp;
ITextSymbol mITextSymbol = new TextSymbolClass()
{
Color = mIRgbColor,
Font = mIFontDisp,
Size = fontSize
};
//用来控制标注和要素的相对位置关系
ILineLabelPosition mILineLabelPosition = new
LineLabelPositionClass()
{
Parallel = false,
//修改标注的属性
Perpendicular = true,
InLine = true
};
//用来控制标注冲突
ILineLabelPlacementPriorities mILineLabelPlacementPriorities = new
LineLabelPlacementPrioritiesClass()
{
AboveStart = 5, //让above
和start的优先级为5
BelowAfter = 4
};
//用来实现对ILineLabelPosition 和
ILineLabelPlacementPriorities以及更高级属性的控制
IBasicOverposterLayerProperties mIBasicOverposterLayerProperties =
new BasicOverposterLayerPropertiesClass()
{
FeatureType =
esriBasicOverposterFeatureType.esriOverposterPolygon,
LineLabelPlacementPriorities
= mILineLabelPlacementPriorities,
LineLabelPosition =
mILineLabelPosition
};
//创建标注对象
ILabelEngineLayerProperties mILabelEngineLayerProperties = new
LabelEngineLayerPropertiesClass()
{
Symbol = mITextSymbol,
BasicOverposterLayerProperties =
mIBasicOverposterLayerProperties,
IsExpressionSimple =
true,
Expression = "[" + fieldName
+ "]"
};
//设置标注的参考比例尺
IAnnotateLayerTransformationProperties
mIAnnotateLayerTransformationProperties =
mILabelEngineLayerProperties as
IAnnotateLayerTransformationProperties;
mIAnnotateLayerTransformationProperties.ReferenceScale =
2500000;
IAnnotateLayerProperties mIAnnotateLayerProperties =
mILabelEngineLayerProperties as IAnnotateLayerProperties;
mIAnnotateLayerProperties.AnnotationMinimumScale = 10000000;
mIAnnotateLayerPropertiesCollection.Add(mIAnnotateLayerProperties);
mIGeoFeatureLayer.DisplayAnnotation = true;
return
Flag;
}
///
/// 文字标注(简单标注)
///
///
///
///
///
///
public void
ArcEngineMapTextNoted(AxESRI.ArcGIS.Controls.AxMapControl
mAxMapControl, string fieldName, string layerName, IRgbColor
mIRgbColor)
{
try
{
IMap mImap;
IFeatureLayer
mIFeatureLayer;
IFeatureClass
mIFeatureClass;
stdole.IFontDisp
mIFontDisp;
mImap =
mAxMapControl.Map;
mImap.Layers.Reset();
for (int i = 0; i <
mImap.LayerCount; i++)
{
ILayer mILayer = mImap.get_Layer(i);
if (mILayer is IFeatureLayer)
{
mIFeatureLayer = mILayer as IFeatureLayer;
mIFeatureClass = mIFeatureLayer.FeatureClass;
if
(mIFeatureLayer.Name == layerName)
{
mIFontDisp = new
stdole.StdFontClass() as stdole.IFontDisp;
mIFontDisp.Name =
"arial";
//设置为粗体
mIFontDisp.Bold = true;
mIFontDisp.Italic =
false;
mIFontDisp.Strikethrough =
false;
ArcEngineMapTextNoted(mImap,
mIFeatureClass, fieldName, mIFontDisp, mIRgbColor);
}
}
}
}
catch
(Exception ex)
{
throw ex;
}
}
///
/// 文字标注(简单标注)
///
///
///
///
///
///
///
///
public void ArcEngineMapTextNoted(IMap pMap,
IFeatureClass pFeatClass, string strName, stdole.IFontDisp fontDis,
IColor fontColor)
{
IFields
pFields;
pFields =
pFeatClass.Fields;
int i =
pFields.FindField(strName);
ITextSymbol pTextSymbol;
pTextSymbol = new TextSymbolClass();
pTextSymbol.Size = 3;
pTextSymbol.Font = fontDis;
pTextSymbol.Color = fontColor;
IFeatureCursor pFeatCursor;
pFeatCursor = pFeatClass.Search(null, true);
IFeature
pFeat;
pFeat =
pFeatCursor.NextFeature();
IEnvelope
pEnv;
while
(pFeat != null)
{
try
{
pEnv = pFeat.Extent;
IPoint pPoint;
pPoint = new PointClass();
pPoint.PutCoords(pEnv.XMin + pEnv.Width / 2,
pEnv.YMin + pEnv.Height / 2);
ITextElement pTextEle;
IElement pEle;
pTextEle = new TextElementClass();
//pTextEle.Text =
pFeat.get_Value(i).ToString();
pTextEle.ScaleText = true;
pTextEle.Symbol = pTextSymbol;
pEle = pTextEle as IElement;
pEle.Geometry = pPoint;
IActiveView pActiveView;
IGraphicsContainer pGraphicsContainer;
pActiveView = pMap as IActiveView;
pGraphicsContainer = pMap as
IGraphicsContainer;
pGraphicsContainer.AddElement(pEle, 0);
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,
null, null);
pPoint = null;
pEle = null;
pFeat = pFeatCursor.NextFeature();
}
catch
{
break;
}
}
}
///
/// 获取颜色对象
///
///
///
///
///
public IRgbColor
ArcEngineCreateAlgorithmicColorRamp(int r, int g, int b)
{
IRgbColor
pColor;
pColor =
new RgbColorClass();
pColor.Red
= r;
pColor.Green = g;
pColor.Blue = b;
return
pColor;
}
///
/// 生成颜色带
///
///
///
public IColorRamp
ArcEngineCreateAlgorithmicColorRamp(int colorRampCount)
{
IEnumColors pEnumRamp;
AlgorithmicColorRamp pColorRamp;
pColorRamp
= new AlgorithmicColorRampClass();
pColorRamp.FromColor = ArcEngineCreateAlgorithmicColorRamp(222,
222, 222);
pColorRamp.ToColor = ArcEngineCreateAlgorithmicColorRamp(32, 200,
150);
pColorRamp.Size = colorRampCount;
bool ok =
true;
pColorRamp.CreateRamp(out ok);
pEnumRamp
= pColorRamp.Colors;
return
pColorRamp;
}
///
/// 获取图层
///
///
///
public IGeoFeatureLayer
ArcEngineGetGeoFeature(AxESRI.ArcGIS.Controls.AxMapControl
mAxMapControl, string layerName)
{
ILayer
layer;
IGeoFeatureLayer geoFeatureLayer;
for (int i
= 0; i < mAxMapControl.LayerCount; i++)
{
layer =
mAxMapControl.get_Layer(i);
if (layer != null &&
layer.Name == layerName)
{
geoFeatureLayer = layer as
IGeoFeatureLayer;
return geoFeatureLayer;
}
}
return
null;
}
///
/// 为PageLayout对象添加经纬网格
///
///
///
public void
ArcEngineCreateGraticuleMapGrid(IPageLayout mIPageLayout)
{
//获取MapFrame对象
IActiveView mIActiveView = mIPageLayout as IActiveView;
IMap mIMap
= mIActiveView.FocusMap;
IGraphicsContainer mIGraphicsContainer = mIActiveView as
IGraphicsContainer;
IMapFrame
mIMapFrame = mIGraphicsContainer.FindFrame(mIMap) as
IMapFrame;
IMapGrids
mIMapGrids = mIMapFrame as IMapGrids;
//创建网格对象
IMapGrid
mIMapGrid = new GraticuleClass();//
(IGraticule)pServerContext.CreateObject("esriCarto.Graticule");
mIMapGrid.Name = "Gd";
//设置网格线的符号样式
ICartographicLineSymbol mICartographicLineSymbol;
mICartographicLineSymbol = new CartographicLineSymbolClass();
mICartographicLineSymbol.Cap = esriLineCapStyle.esriLCSButt;
mICartographicLineSymbol.Width = 1;
IRgbColor
mIRgbColor = new RgbColor();
mIRgbColor.Red = 166;
mIRgbColor.Green = 187;
mIRgbColor.Blue = 208;
IColor
mIColor = mIRgbColor as IColor;
mICartographicLineSymbol.Color = mIColor;
mIMapGrid.LineSymbol = mICartographicLineSymbol;
//设置网格的边框样式,为简单边框样式
ISimpleMapGridBorder mISimpleMapGridBorder = new
SimpleMapGridBorderClass();
ISimpleLineSymbol mISimpleLineSymbol = new
SimpleLineSymbolClass();
mISimpleLineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;
mISimpleLineSymbol.Color = ArcEngineCreateAlgorithmicColorRamp(0,
0, 0);
mISimpleLineSymbol.Width = 2;
mISimpleMapGridBorder.LineSymbol = mISimpleLineSymbol as
ILineSymbol;
mIMapGrid.Border = mISimpleMapGridBorder as IMapGridBorder;
mIMapGrid.SetTickVisibility(true, true, true, true);
//设置网格的主刻度的样式和可见性
mIMapGrid.TickLength = 15;
mICartographicLineSymbol = new CartographicLineSymbolClass();
mICartographicLineSymbol.Cap = esriLineCapStyle.esriLCSButt;
mICartographicLineSymbol.Width = 1;
mICartographicLineSymbol.Color = mIColor;
mIMapGrid.TickMarkSymbol = null;
mIMapGrid.TickLineSymbol = mICartographicLineSymbol;
mIMapGrid.SetTickVisibility(true, true, true, true);
//设置网格的次一级刻度的样式和可见性
mIMapGrid.SubTickCount = 5;
mIMapGrid.SubTickLength = 10;
mICartographicLineSymbol = new CartographicLineSymbolClass();
mICartographicLineSymbol.Cap = esriLineCapStyle.esriLCSButt;
mICartographicLineSymbol.Width = 0.1;
mICartographicLineSymbol.Color = mIColor;
mIMapGrid.SubTickLineSymbol = mICartographicLineSymbol;
mIMapGrid.SetSubTickVisibility(true, true, true, true);
//设置网格的标签的样式和可见性
IGridLabel
mIGridLabel;
mIGridLabel = mIMapGrid.LabelFormat;
mIGridLabel.LabelOffset = 15;
stdole.StdFont mFont = new stdole.StdFont();
mFont.Name
= "Arial";
mFont.Size
= 16;
mIMapGrid.LabelFormat.Font = mFont as stdole.IFontDisp;
//设置网格的可见性
mIMapGrid.Visible = true;
//创建IMeasuredGrid对象
IMeasuredGrid mIMeasuredGrid;
mIMeasuredGrid = mIMapGrid as IMeasuredGrid;
mIMeasuredGrid.FixedOrigin = true;
mIMeasuredGrid.XIntervalSize = 0.5; //meridian interval
mIMeasuredGrid.XOrigin = 109; //shift grid 5
mIMeasuredGrid.YIntervalSize = 0.5; //parallel interval
mIMeasuredGrid.YOrigin = 40; //shift grid 5
//将网格对象添加到地图控件中
mIMapGrids.AddMapGrid(mIMapGrid);
//刷新地图
mIActiveView.PartialRefresh(esriViewDrawPhase.esriViewBackground,
null, null);
}
///
/// 指定IFeatureLayer的显示样式(颜色)
///
///
public void
ArcEngineIFeatureLayerStyle(AxESRI.ArcGIS.Controls.AxMapControl
mAxMapControl, IFeatureLayer mIFeatureLayer)
{
IFeatureLayer featureLayer = mIFeatureLayer;
IGeoFeatureLayer mIGeoFeatureLayer =
(IGeoFeatureLayer)featureLayer;
ISimpleRenderer mISimpleRenderer;
ISimpleLineSymbol mISimpleLineSymbol = new
SimpleLineSymbolClass();
mISimpleLineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;
mISimpleLineSymbol.Width = 1.5;
IRgbColor
mIRgbColor = new RgbColorClass();
mIRgbColor.Red = 255;
mIRgbColor.Green = 0;
mIRgbColor.Blue = 0;
mISimpleLineSymbol.Color = mIRgbColor;
mISimpleRenderer = new SimpleRendererClass();
mISimpleRenderer.Symbol = (ISymbol)mISimpleLineSymbol;
mIGeoFeatureLayer.Renderer =
(IFeatureRenderer)mISimpleRenderer;
mAxMapControl.ActiveView.ContentsChanged();
mAxMapControl.Refresh(esriViewDrawPhase.esriViewGeography, null,
null);
}
///
/// 指定IFeatureLayer的显示样式(颜色,大小)
///
///
public void
ArcEngineIFeatureLayerStyle(AxESRI.ArcGIS.Controls.AxMapControl
mAxMapControl, IFeatureLayer mIFeatureLayer, IRgbColor rgbColor,
double with)
{
IFeatureLayer featureLayer = mIFeatureLayer;
IGeoFeatureLayer mIGeoFeatureLayer =
(IGeoFeatureLayer)featureLayer;
ISimpleRenderer mISimpleRenderer;
ISimpleLineSymbol mISimpleLineSymbol = new
SimpleLineSymbolClass();
mISimpleLineSymbol.Style = esriSimpleLineStyle.esriSLSSolid;
mISimpleLineSymbol.Width = with;
mISimpleLineSymbol.Color = rgbColor;
mISimpleRenderer = new SimpleRendererClass();
mISimpleRenderer.Symbol = (ISymbol)mISimpleLineSymbol;
mIGeoFeatureLayer.Renderer =
(IFeatureRenderer)mISimpleRenderer;
mAxMapControl.ActiveView.ContentsChanged();
mAxMapControl.Refresh(esriViewDrawPhase.esriViewGeography, null,
null);
}
///
/// 地图输出
///
///
///
///
///
public string ExportMapToImage(IActiveView
pActiveView, string fileName, int filterIndex)
{
string
exportPath = "";
try
{
IExport mIExport =
null;
switch (filterIndex)
{
case 1:
mIExport =
new ExportJPEGClass();
break;
case 2:
mIExport =
new ExportBMPClass();
break;
case 3:
mIExport =
new ExportEMFClass();
break;
case 4:
mIExport =
new ExportGIFClass();
break;
case 5:
mIExport =
new ExportAIClass();
break;
case 6:
mIExport =
new ExportPDFClass();
break;
case 7:
mIExport =
new ExportPNGClass();
break;
case 8:
mIExport =
new ExportPSClass();
break;
case 9:
mIExport =
new ExportSVGClass();
break;
case 10:
mIExport =
new ExportTIFFClass();
break;
default:
break;
}
ESRI.ArcGIS.Geometry.IEnvelope pEnvelope = new
ESRI.ArcGIS.Geometry.EnvelopeClass();
ITrackCancel pTrackCancel =
new CancelTrackerClass();
tagRECT ptagRECT =
pActiveView.ExportFrame;
int pResolution =
(int)(pActiveView.ScreenDisplay.DisplayTransformation.Resolution);
pEnvelope.PutCoords(ptagRECT.left, ptagRECT.bottom, ptagRECT.right,
ptagRECT.top);
mIExport.Resolution =
pResolution;
mIExport.ExportFileName =
fileName;
mIExport.PixelBounds =
pEnvelope;
pActiveView.Output(mIExport.StartExporting(), pResolution, ref
ptagRECT, pActiveView.Extent, pTrackCancel);
mIExport.FinishExporting();
//释放资源
System.Runtime.InteropServices.Marshal.ReleaseComObject(mIExport);
}
catch
(Exception ex)
{
exportPath = "";
throw ex;
}
finally
{
exportPath = fileName;
}
return
exportPath;
}
///
/// 鹰眼OnMouseDown
///
///
public void
MapControlHawkEyeOnMouseDown(AxESRI.ArcGIS.Controls.AxMapControl
mAxMapControl, AxESRI.ArcGIS.Controls.AxMapControl
HawkEyeMapCOntrol,
AxESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
{
if
(e.button == 1)//捕捉鼠标左键
{
ESRI.ArcGIS.Geometry.IPoint
pPt = new ESRI.ArcGIS.Geometry.PointClass();
pPt.X = e.mapX;
pPt.Y = e.mapY;
ESRI.ArcGIS.Geometry.IEnvelope pEnvelope = mAxMapControl.Extent as
ESRI.ArcGIS.Geometry.IEnvelope;
pEnvelope.CenterAt(pPt);
mAxMapControl.Extent =
pEnvelope;
mAxMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography,
null, null);
}
else if
(e.button == 2)//捕捉鼠标右键按下
{
ESRI.ArcGIS.Geometry.IEnvelope pEnvelope =
HawkEyeMapCOntrol.TrackRectangle();
mAxMapControl.Extent =
pEnvelope;
mAxMapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography,
null, null);
}
}
///
/// 鹰眼OnMouseMove
///
///
public void
MapControlHawkEyeOnMouseMove(AxESRI.ArcGIS.Controls.AxMapControl
mAxMapControl, AxESRI.ArcGIS.Controls.AxMapControl
HawkEyeMapCOntrol,
AxESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseMoveEvent e)
{
if
(e.button != 1)
return;
ESRI.ArcGIS.Geometry.IPoint pPt = new
ESRI.ArcGIS.Geometry.PointClass();
pPt.X =
e.mapX;
pPt.Y =
e.mapY;
mAxMapControl.CenterAt(pPt);
HawkEyeMapCOntrol.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,
null, null);
}
///
/// 鹰眼OnExtentUpdated
///
///
public void
MapControlHawkEyeOnExtentUpdated(AxESRI.ArcGIS.Controls.AxMapControl
HawkEyeMapCOntrol,
AxESRI.ArcGIS.Controls.IMapControlEvents2_OnExtentUpdatedEvent
e)
{
int rgbRed
= 200;
int
rgbGreen = 0;
int
rgbBlue = 0;
int
isysmbolWith = 1;
IGraphicsContainer pGraphicsContainer = HawkEyeMapCOntrol.Map as
IGraphicsContainer;//以axMapControl4为图形容器
IActiveView pAv = pGraphicsContainer as IActiveView;
//在绘制前,清除axMapControl4中的任何图形元素
pGraphicsContainer.DeleteAllElements();
IRectangleElement Ire = new RectangleElementClass();
IElement
pEle = Ire as IElement;
ESRI.ArcGIS.Geometry.IEnvelope pEnv;
pEnv =
e.newEnvelope as ESRI.ArcGIS.Geometry.IEnvelope;
pEle.Geometry = pEnv;
//设置颜色
IRgbColor
Icolor = new RgbColorClass();
Icolor.Red
= rgbRed;// 200;
Icolor.Green = rgbGreen;// 0;
Icolor.Blue = rgbBlue;// 0;
Icolor.Transparency = 255;
//产生一个线性符号
ILineSymbol Isysmbol = new SimpleLineSymbolClass();
Isysmbol.Width = isysmbolWith;
Isysmbol.Color = Icolor;
//设置填充符号的属性
IFillSymbol pFillSysmbol = new SimpleFillSymbolClass();
//设置透明颜色
Icolor.Transparency = 0;
pFillSysmbol.Color = Icolor;
pFillSysmbol.Outline = Isysmbol;
IFillShapeElement pFillShapeElement = Ire as
IFillShapeElement;
pFillShapeElement.Symbol = pFillSysmbol;
pGraphicsContainer.AddElement(pEle, 0);
HawkEyeMapCOntrol.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,
null, null);
}
///
/// 鹰眼OnExtentUpdated(设置大小和颜色)
///
///
public void
MapControlHawkEyeOnExtentUpdated(IRgbColor mIRgbColor, int with,
AxESRI.ArcGIS.Controls.AxMapControl HawkEyeMapCOntrol,
AxESRI.ArcGIS.Controls.IMapControlEvents2_OnExtentUpdatedEvent
e)
{
IGraphicsContainer mIGraphicsContainer = HawkEyeMapCOntrol.Map as
IGraphicsContainer;//以axMapControl4为图形容器
IActiveView mIActiveView = mIGraphicsContainer as
IActiveView;
//在绘制前,清除axMapControl4中的任何图形元素
mIGraphicsContainer.DeleteAllElements();
IRectangleElement mIRectangleElement = new
RectangleElementClass();
IElement
mIElement = mIRectangleElement as IElement;
ESRI.ArcGIS.Geometry.IEnvelope mIEnvelope;
mIEnvelope
= e.newEnvelope as ESRI.ArcGIS.Geometry.IEnvelope;
mIElement.Geometry = mIEnvelope;
//设置颜色
IRgbColor
Icolor = new RgbColorClass();
Icolor =
mIRgbColor;
//产生一个线性符号
ILineSymbol Isysmbol = new SimpleLineSymbolClass();
Isysmbol.Width = with;
Isysmbol.Color = Icolor;
//设置填充符号的属性
IFillSymbol mIFillSymbol = new SimpleFillSymbolClass();
//设置透明颜色
Icolor.Transparency = 0;
mIFillSymbol.Color = Icolor;
mIFillSymbol.Outline = Isysmbol;
IFillShapeElement mIFillShapeElement = mIRectangleElement as
IFillShapeElement;
mIFillShapeElement.Symbol = mIFillSymbol;
mIGraphicsContainer.AddElement(mIElement, 0);
HawkEyeMapCOntrol.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,
null, null);
}
///
/// 打开Raster文件
///
///
///
public IRaster ArcEngineGetRasterFile(string
filePath, string fileName)
{
return
null;
}