class RangeManager
{
#region 类成员变量
public bool oprFlag = false;
static private string pFilePath = null;
static private string rangeName = null;
static private string listName = "";
static private AxMapControl MapControl = null;
static private List<string> List_village = new List<string>();
#endregion
#region 类构造方法
/// <summary>
/// 打开shp文件
/// </summary>
public static Response OpenFile(AxMapControl mapControl)
{
MapControl = mapControl;
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Title = "打开文件";
openFileDialog.Filter = "shp文件(*.shp)|*.shp";//|CAD文件(*.cad)|*.cad";
openFileDialog.Multiselect = true;
openFileDialog.InitialDirectory = Configuration.Environment.WorkPath;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string path = "";
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
//打开多个文件
for (int i = 0; i < openFileDialog.FileNames.Length; i++)
{
path = openFileDialog.FileNames.GetValue(i).ToString();
pFilePath = System.IO.Path.GetDirectoryName(path);
string pFileName = System.IO.Path.GetFileName(path);
IFeatureWorkspace pFeaWorkspace = pWorkspaceFactory.OpenFromFile(pFilePath, 0) as IFeatureWorkspace;
IFeatureClass pFeatureClass = pFeaWorkspace.OpenFeatureClass(pFileName);
AddLayerToMap(pFeatureClass);
}
return new Response(Constant.Success, "");
}
return new Response(Constant.Nothing, "");
}
/// <summary>
/// 绘制范围线
/// </summary>
public static Response DrawRange()
{
ILayer pLayer = GetLayerByName(MapControl, Constant.Range_DLTB);
IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
if (pFeatureClass != null)
{
MainForm frmMain = new MainForm();
BarButtonItem btiDrawBoundary = frmMain.btiDrawBoundary;
if (btiDrawBoundary.Caption == "绘范围线")
{
btiDrawBoundary.Caption = "停止绘线";
oprFlag = true;
}
else
{
btiDrawBoundary.Caption = "绘范围线";
oprFlag = false;
}
//更改鼠标指针样式
MapControl.MousePointer = esriControlsMousePointer.esriPointerCrosshair;
}
else
{
MessageBox.Show("未发现可编辑图层!", "提示");
return new Response(Constant.Nothing, "");
}
return new Response(Constant.Success, "");
}
/// <summary>
/// 开始绘制
/// </summary>
public static Response CreateDrawPolygon()
{
//axMapControl1控件的当前地图工具为空
MapControl.CurrentTool = null;
//绘制多边形事件
IGeometry pGeometry = MapControl.TrackLine();
//通过AddFeature函数的两个参数, sLayer——绘制折线的图层; pGeometry——绘制几何的图层
AddFeature("DLTB", pGeometry);
return new Response(Constant.Success, "");
}
/// <summary>
/// 添加实体对象到地图图层(添加线、面要素)
/// </summary>
private void AddFeature(string layerName, IGeometry pGeometry)
{
//ILayer pLayer = GetLayerByName(MapControl,layerName);
ILayer pLayer = MapControl.get_Layer(0);
//得到要添加地物的图层
IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
if (pFeatureLayer != null)
{
//定义一个地物类, 把要编辑的图层转化为定义的地物类
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
//先定义一个编辑的工作空间, 然后将其转化为数据集, 最后转化为编辑工作空间
IWorkspaceEdit pWorkspaceEdit = (pFeatureClass as IDataset).Workspace as IWorkspaceEdit;
IFeature pFeature;
//开始事务操作
pWorkspaceEdit.StartEditing(false);
//开始编辑
pWorkspaceEdit.StartEditOperation();
//在内存创建一个用于暂时存放编辑数据的要素(FeatureBuffer)
IFeatureBuffer pFeatureBuffer = pFeatureClass.CreateFeatureBuffer();
//定义游标
IFeatureCursor pFtCursor;
//查找到最后一条记录, 游标指向该记录后再进行插入操作
pFtCursor = pFeatureClass.Search(null, true);
pFeature = pFtCursor.NextFeature();
//开始插入新的实体对象(插入对象要使用Insert游标)
pFtCursor = pFeatureClass.Insert(true);
try
{
//向缓存游标的Shape属性赋值
pFeatureBuffer.Shape = pGeometry;
}
catch (COMException ex)
{
MessageBox.Show("绘制的几何图形超出了边界!");
return;
}
object featureOID = pFtCursor.InsertFeature(pFeatureBuffer);
//保存实体
pFtCursor.Flush();
//结束编辑
pWorkspaceEdit.StopEditOperation();
//结束事务操作
pWorkspaceEdit.StopEditing(true);
//释放游标
Marshal.ReleaseComObject(pFtCursor);
MapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, pLayer, null);
//更改编辑状态
oprFlag = false;
}
else
{
MessageBox.Show("未发现" + layerName + "图层");
}
}
/// <summary>
/// 选择村落
/// </summary>
public static Response LocationRange()
{
if(MapControl != null)
{
ILayer pLayer = GetLayerByName(Constant.Range_DLTB);
if (pLayer != null)
{
IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
//判断是否找到村落字段
int Judge = 0;
IFields pFields = pFeatureClass.Fields;
for (int i = 0; i < pFields.FieldCount; i++)
{
IField ifield = pFields.get_Field(i);
if (ifield.Name == Constant.Range_Shp_Field)
{
Judge = 1;
List<string> List = new List<string>();
IFeatureCursor pFeaCursor = pFeatureClass.Search(null, true);
IFeature pFeature = pFeaCursor.NextFeature();
while (pFeature != null)
{
string fieldValue = pFeature.get_Value(i).ToString();//获取指定字段的值
List.Add(fieldValue);
pFeature = pFeaCursor.NextFeature();
}
List = List.Distinct().ToList(); //列表去除重复
//实例化窗体
SelectRange selectRange = new SelectRange(List);
selectRange.ShowDialog();
}
}
if (Judge == 0)
{
MessageBox.Show("未找到村落名称字段,请重新加载地图!", "提示");
return new Response(Constant.Nothing, "");
}
}
else
{
MessageBox.Show("请先加DLTB!", "提示");
return new Response(Constant.Nothing, "");
}
}
else
{
MessageBox.Show("请先加载数据!", "提示");
return new Response(Constant.Nothing, "");
}
return new Response(Constant.Success, "");
}
/// <summary>
/// 根据定位村落绘制范围线
/// </summary>
public static Response drawLocationRange(List<string> List)
{
listName = null;
rangeName = null;
List_village.Clear();
//实例化进度条
ProcessBar processBar = new ProcessBar();
//通过名字获取图层
ILayer pLayer = GetLayerByName(Constant.Range_DLTB);
IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
//根据勾选信息进行属性筛选
IFeatureSelection pFeatureSelection = pFeatureLayer as IFeatureSelection;
IQueryFilter queryFilter = new QueryFilterClass();
for (int i = 0; i < List.Count; i++)
{
queryFilter.WhereClause = Constant.Range_Shp_Field + Constant.File_Ext_equal + "'" + List[i] + "'";
pFeatureSelection.SelectFeatures(queryFilter, esriSelectionResultEnum.esriSelectionResultAdd, false);
listName += Constant.File_Ext_Char1 + List[i];
List_village.Add(List[i]);
}
rangeName = Constant.Project_File_Range + listName + Constant.File_Ext_Char + Constant.Project_File_Shp;
//要素数据集路径
string pPath = pFilePath + Constant.File_Ext_Char2;
//创建工作空间
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
IFeatureWorkspace pFeaWorkspace = pWorkspaceFactory.OpenFromFile(pFilePath, 0) as IFeatureWorkspace;
//判断文件是否存在
if (System.IO.File.Exists(pPath + rangeName))
{
if (MessageBox.Show("文件已存在,是否覆盖?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes)
{
IFeatureClass pFCChecker = pFeaWorkspace.OpenFeatureClass(rangeName);
if (pFCChecker != null)
{
IDataset pds = pFCChecker as IDataset;
pds.Delete();
}
}
else
{
MapControl.Map.ClearSelection();
return new Response(Constant.Nothing, "");
}
}
processBar.Show();
processBar.Update();
//绘制范围线
Geoprocessor GP_Tool = new Geoprocessor();
ESRI.ArcGIS.DataManagementTools.Dissolve GP_Dissolve = new Dissolve();
GP_Dissolve.in_features = pFeatureSelection as FeatureClass;
GP_Dissolve.out_feature_class = pPath + rangeName;
try
{
GP_Tool.Execute(GP_Dissolve, null);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "GP Error");
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 0; i < GP_Tool.MessageCount; i++)
sb.AppendLine(GP_Tool.GetMessage(i));
if (sb.Capacity > 0) MessageBox.Show(sb.ToString(), "GP Messages");
}
MapControl.Map.ClearSelection();
IFeatureClass lFeatureClass = pFeaWorkspace.OpenFeatureClass(rangeName);
processBar.Close();
//将要素添加到图层
AddLayerToMap(lFeatureClass);
return new Response(Constant.Success, "");
}
/// <summary>
/// 生成边框
/// </summary>
public static Response CreateFrame()
{
CreateEnvelope(pFilePath, rangeName, 1000, 1000);
return new Response(Constant.Success, "");
}
/// <summary>
/// 根据范围线生成边框
/// </summary>
public static Response CreateEnvelope(String pFilePath, String rangeName, double x, double y)
{
if (pFilePath != null)
{
if (rangeName != null)
{
//创建工作空间
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
IFeatureWorkspace pFeaWorkspace = pWorkspaceFactory.OpenFromFile(pFilePath, 0) as IFeatureWorkspace;
//根据范围线范围进行矩形裁剪
IFeatureClass lFeatureClass = pFeaWorkspace.OpenFeatureClass(rangeName);
//判断文件是否存在
if (System.IO.File.Exists(pFilePath + Constant.File_Ext_Char2 + Constant.Project_File_Envelope + listName + Constant.File_Ext_Char + Constant.Project_File_Shp))
{
if (MessageBox.Show("文件已存在,是否覆盖?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes)
{
IFeatureClass pFCChecker = pFeaWorkspace.OpenFeatureClass(Constant.Project_File_Envelope + listName + Constant.File_Ext_Char + Constant.Project_File_Shp);
if (pFCChecker != null)
{
IDataset pds = pFCChecker as IDataset;
pds.Delete();
}
}
else
{
return new Response(Constant.Nothing, "");
}
}
ProcessBar processBar = new ProcessBar();
processBar.Show();
processBar.Update();
IFeature pFeature = lFeatureClass.GetFeature(0);
IPolygon pPolygon = pFeature.ShapeCopy as IPolygon;
IEnvelope envelope = pPolygon.Envelope;
//扩大矩形,false=按长度,true=按比例
envelope.Expand(x, y, false);
//将矩形转换为IPolygon
IPolygon pPolygon_E = EnvelopeToPolygon(envelope);
IGeometry pGeometry = pPolygon_E;
ITopologicalOperator2 pTopoOperator = pGeometry as ITopologicalOperator2;
pTopoOperator.IsKnownSimple_2 = false;
pTopoOperator.Simplify();
//将矩形要素存入新的FeatureClass
IFields pFields = lFeatureClass.Fields;
IFeatureClass eFeatureClass = pFeaWorkspace.CreateFeatureClass(Constant.Project_File_Envelope + listName, pFields, null, null, esriFeatureType.esriFTSimple, "SHAPE", null);
IFeature pFeature1 = eFeatureClass.CreateFeature();
pFeature1.Shape = pGeometry;
pFeature1.Store();
processBar.Close();
//添加图框要素到图层
AddLayerToMap(eFeatureClass);
}
else
{
MessageBox.Show("请先绘制范围线!", "提示");
return new Response(Constant.Nothing, "");
}
}
else
{
MessageBox.Show("请先加载数据!", "提示");
return new Response(Constant.Nothing, "");
}
return new Response(Constant.Success, "");
}
/// <summary>
/// 数据处理
/// </summary>
public static Response DataProcessor()
{
if (pFilePath != null)
{
//创建工作空间
IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
IFeatureWorkspace pFeaWorkspace = pWorkspaceFactory.OpenFromFile(pFilePath, 0) as IFeatureWorkspace;
//通过名字获取图层
ILayer pLayer = GetLayerByName(Constant.Range_DLTB);
if (pLayer != null)
{
//要素数据集路径
string pPath = pFilePath + Constant.File_Ext_Char2;
IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
if (rangeName != null)
{
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
IFeatureClass lFeatureClass = pFeaWorkspace.OpenFeatureClass(rangeName);
if (System.IO.File.Exists(pPath + Constant.Project_File_Envelope + listName + Constant.File_Ext_Char + Constant.Project_File_Shp))
{
IFeatureClass eFeatureClass = pFeaWorkspace.OpenFeatureClass(Constant.Project_File_Envelope + listName + Constant.File_Ext_Char + Constant.Project_File_Shp);
//判断文件是否存在
if (System.IO.File.Exists(pPath + pFeatureClass.AliasName + Constant.Project_File_Range_out + listName + Constant.File_Ext_Char + Constant.Project_File_Shp))
{
if (MessageBox.Show("文件已存在,是否覆盖?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk) == DialogResult.Yes)
{
IFeatureClass pFCChecker = pFeaWorkspace.OpenFeatureClass(pFeatureClass.AliasName + Constant.Project_File_Range_out + listName);
if (pFCChecker != null)
{
IDataset pds = pFCChecker as IDataset;
pds.Delete();
}
}
else
{
return new Response(Constant.Nothing, "");
}
}
ProcessBar processBar = new ProcessBar();
processBar.Show();
//移除上层范围线
DeleteLayerByName(lFeatureClass.AliasName);
//进行DLTB裁剪操作
ClipTool(pFeaWorkspace, pFeatureClass, lFeatureClass, eFeatureClass, pPath, listName);
DeleteLayerByName(Constant.Range_DLTB);
AddLayer(pFeaWorkspace, pFeatureClass);
//进行XZDW裁剪操作
ILayer pLayer1 = GetLayerByName(Constant.Range_XZDW);
if (pLayer1 != null)
{
IFeatureLayer pFeatureLayer1 = pLayer1 as IFeatureLayer;
IFeatureClass pFeatureClass1 = pFeatureLayer1.FeatureClass;
ClipTool(pFeaWorkspace, pFeatureClass1, lFeatureClass, eFeatureClass, pPath, listName);
DeleteLayerByName(Constant.Range_XZDW);
AddLayer(pFeaWorkspace, pFeatureClass1);
}
//进行XZQJX裁剪操作
ILayer pLayer2 = GetLayerByName(Constant.Range_XZQJX);
if (pLayer2 != null)
{
IFeatureLayer pFeatureLayer2 = pLayer2 as IFeatureLayer;
IFeatureClass pFeatureClass2 = pFeatureLayer2.FeatureClass;
ClipTool(pFeaWorkspace, pFeatureClass2, lFeatureClass, eFeatureClass, pPath, listName);
DeleteLayerByName( Constant.Range_XZQJX);
AddLayer(pFeaWorkspace, pFeatureClass2);
}
processBar.Close();
//添加范围线到图层
AddLayerToMap(lFeatureClass);
}
else
{
MessageBox.Show("请先绘制图框!", "提示");
return new Response(Constant.Nothing, "");
}
}
else
{
MessageBox.Show("请先绘制范围线!", "提示");
return new Response(Constant.Nothing, "");
}
}
else
{
MessageBox.Show("请先加载地图!", "提示");
return new Response(Constant.Nothing, "");
}
}
else
{
MessageBox.Show("请先加载数据!", "提示");
return new Response(Constant.Nothing, "");
}
return new Response(Constant.Success, "");
}
/// <summary>
/// 裁剪工具
/// </summary>
private static void ClipTool(IFeatureWorkspace pFeaWorkspace, IFeatureClass pFeatureClass, IFeatureClass lFeatureClass, IFeatureClass eFeatureClass, string pPath, string listName)
{
//判断文件是否存在
if (System.IO.File.Exists(pPath + pFeatureClass.AliasName + Constant.Project_File_Range_out + listName + Constant.File_Ext_Char + Constant.Project_File_Shp))
{
IFeatureClass pFCChecker = pFeaWorkspace.OpenFeatureClass(pFeatureClass.AliasName + Constant.Project_File_Range_out + listName);
if (pFCChecker != null)
{
IDataset pds = pFCChecker as IDataset;
pds.Delete();
}
}
//矩形裁剪
Geoprocessor GP_Tool = new Geoprocessor();
ESRI.ArcGIS.AnalysisTools.Clip GP_Clip = new ESRI.ArcGIS.AnalysisTools.Clip();
GP_Clip.clip_features = eFeatureClass;
GP_Clip.in_features = pFeatureClass;
GP_Clip.out_feature_class = pPath + pFeatureClass.AliasName + Constant.Project_File_Range_out + listName;
try
{
GP_Tool.Execute(GP_Clip, null);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "GP_Clip_envelope Error");
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 0; i < GP_Tool.MessageCount; i++)
sb.AppendLine(GP_Tool.GetMessage(i));
if (sb.Capacity > 0) MessageBox.Show(sb.ToString(), "GP Messages");
}
IFeatureClass eFeatureClass_out = pFeaWorkspace.OpenFeatureClass(pFeatureClass.AliasName + Constant.Project_File_Range_out + listName);
//判断文件是否存在
if (System.IO.File.Exists(pPath + pFeatureClass.AliasName + Constant.File_Ext_Char1 + listName + Constant.File_Ext_Char + Constant.Project_File_Shp))
{
IFeatureClass pFCChecker = pFeaWorkspace.OpenFeatureClass(pFeatureClass.AliasName + Constant.File_Ext_Char1 + listName);
if (pFCChecker != null)
{
IDataset pds = pFCChecker as IDataset;
pds.Delete();
}
}
//内裁剪
ESRI.ArcGIS.AnalysisTools.Clip GP_Clip1 = new ESRI.ArcGIS.AnalysisTools.Clip();
GP_Clip1.clip_features = lFeatureClass;
GP_Clip1.in_features = eFeatureClass_out;
GP_Clip1.out_feature_class = pPath + pFeatureClass.AliasName + Constant.File_Ext_Char1 + listName;
try
{
GP_Tool.Execute(GP_Clip1, null);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "GP_Clip_Range Error");
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for (int i = 0; i < GP_Tool.MessageCount; i++)
sb.AppendLine(GP_Tool.GetMessage(i));
if (sb.Capacity > 0) MessageBox.Show(sb.ToString(), "GP Messages");
}
//外裁剪
ISpatialFilter pSpatialFilter = new SpatialFilterClass();
IGeometry pGeometry = lFeatureClass.GetFeature(0).ShapeCopy as IGeometry;
pSpatialFilter.Geometry = pGeometry;
pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
//求出与裁切框相交要素
IFeatureCursor pFeatureCursor = eFeatureClass_out.Search(pSpatialFilter, false);
IFeature pFeature = pFeatureCursor.NextFeature();
while (pFeature != null)
{
IGeometry tGeo = pFeature.ShapeCopy;
ITopologicalOperator tTope = tGeo as ITopologicalOperator;
tTope.Simplify();
//用裁切出来的要素,再与其源要素进行求差处理,即得到外围要素
IGeometry tGeoDe = tTope.Difference(pGeometry);
//把外围要素赋予源要素
pFeature.Shape = tGeoDe;
pFeature.Store();
pFeature = pFeatureCursor.NextFeature();
}
}
/// <summary>
/// 面积统计获取地类
/// </summary>
public static Response AreaSum()
{
if(MapControl != null)
{
ILayer pLayer = GetLayerByName(Constant.Range_DLTB);
if (pLayer != null)
{
IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
//判断是否找到地类名称字段
int Judge = 0;
IFields pFields = pFeatureClass.Fields;
for (int i = 0; i < pFields.FieldCount; i++)
{
IField ifield = pFields.get_Field(i);
if (ifield.Name == Constant.Range_Field_DLMC)
{
Judge = 1;
List<string> List = new List<string>();
IFeatureCursor pFeaCursor = pFeatureClass.Search(null, true);
IFeature pFeature = pFeaCursor.NextFeature();
while (pFeature != null)
{
string fieldValue = pFeature.get_Value(i).ToString();//获取指定字段的值
List.Add(fieldValue);
pFeature = pFeaCursor.NextFeature();
}
List = List.Distinct().ToList(); //列表去除重复
//实例化窗体
AreaSum areaSum = new AreaSum(List);
areaSum.ShowDialog();
}
}
if (Judge == 0)
{
MessageBox.Show("未找到地类名称字段,请重新加载地图!", "提示");
return new Response(Constant.Nothing, "");
}
}
else
{
MessageBox.Show("请先加载DLTB!", "提示");
return new Response(Constant.Nothing, "");
}
}
else
{
MessageBox.Show("请先加载数据!", "提示");
return new Response(Constant.Nothing, "");
}
return new Response(Constant.Success, "");
}
/// <summary>
/// 根据选择地类进行面积统计
/// </summary>
public static Response AreaShow(List<string> List)
{
//实例化窗体
AreaShow areaShow = new AreaShow();
GridControl gridControl = areaShow.dataGrid;
GridView gridView = areaShow.gridView;
//新建表格
DataTable dataTable = new DataTable();
gridControl.DataSource = dataTable;
//通过名字获取图层
ILayer pLayer = GetLayerByName(Constant.Range_DLTB);
IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
IFeatureSelection pFeatureSelection = pFeatureLayer as IFeatureSelection;
//根据勾选信息进行属性筛选
if (List.Count != 0)
{
dataTable.Columns.Add("村名", typeof(string));
for (int l = 0; l < List_village.Count+1; l++)
{
dataTable.Rows.Add();
}
for (int i = 0; i < List.Count; i++)
{
dataTable.Columns.Add(List[i], typeof(string));
IDataStatistics dataStatistics = new DataStatisticsClass();
IQueryFilter queryFilter = new QueryFilterClass();
queryFilter.WhereClause = Constant.Range_Field_DLMC + Constant.File_Ext_equal + "'" + List[i] + "'";
ICursor cursor = pFeatureClass.Search(queryFilter, false) as ICursor;
dataStatistics.Field = Constant.Range_Field_TBMJ;//面积字段
dataStatistics.Cursor = cursor;
IStatisticsResults results = dataStatistics.Statistics;
double sum = results.Sum;
double Sum = Math.Round(sum, 2);//保留两位小数
dataTable.Rows[List_village.Count][0] = "总计:";
dataTable.Rows[List_village.Count][i+1] = Sum;
}
//数据查询
if (List_village.Count != 0)
{
for (int l = 0; l < List_village.Count; l++)
{
dataTable.Rows[l][0] = List_village[l];
for (int i = 0; i < List.Count; i++)
{
IDataStatistics dataStatistics = new DataStatisticsClass();
IQueryFilter queryFilter = new QueryFilterClass();
//"ZLDWMC = '村名'AND DLMC = '类型'"
string SQL = Constant.Range_Shp_Field + Constant.File_Ext_equal + "'" + List_village[l] + "'" + "AND" + ' ' + Constant.Range_Field_DLMC + Constant.File_Ext_equal + "'" + List[i] + "'";
queryFilter.WhereClause = SQL;
ICursor cursor = pFeatureClass.Search(queryFilter, false) as ICursor;
dataStatistics.Field = Constant.Range_Field_TBMJ;//面积字段
dataStatistics.Cursor = cursor;
IStatisticsResults results = dataStatistics.Statistics;
double sum = results.Sum;
double Sum = Math.Round(sum, 2);//保留两位小数
dataTable.Rows[l][i+1] = Sum;
}
}
}
gridView.PopulateColumns();
areaShow.ShowDialog();
}
else
{
MessageBox.Show("请勾选地类!", "提示");
return new Response(Constant.Nothing, "");
}
return new Response(Constant.Success, "");
}
/// <summary>
/// 添加图层
/// </summary>
private static void AddLayerToMap(IFeatureClass pFeatureClass)
{
//去除后缀
String LayerName = pFeatureClass.AliasName;
LayerName = LayerName.Split('_')[0];
//将要素添加到显示
IFeatureLayer pFeatureLyr = new FeatureLayer();
pFeatureLyr.FeatureClass = pFeatureClass;
pFeatureLyr.Name = LayerName;
if (LayerName == Constant.Project_File_Range || LayerName == Constant.Project_File_Envelope)
{
SimpleFillSymbol(pFeatureLyr, esriSimpleFillStyle.esriSFSHollow, null, new RgbColorClass() { Red = 255, Green = 0, Blue = 0 }, 2);
}
MapControl.Map.AddLayer(pFeatureLyr);
MapControl.ActiveView.Refresh();
}
/// <summary>
/// 添加内、外图层显示
/// </summary>
private static void AddLayer(IFeatureWorkspace pFeaWorkspace, IFeatureClass pFeatureClass)
{
//添加内、外层要素到图层
IFeatureClass open_pFeatureClass = pFeaWorkspace.OpenFeatureClass(pFeatureClass.AliasName + Constant.File_Ext_Char1 + listName);
IFeatureClass open_pFeatureClass_out = pFeaWorkspace.OpenFeatureClass(pFeatureClass.AliasName + Constant.Project_File_Range_out + listName);
AddLayerToMap(open_pFeatureClass);
AddLayerToMap(open_pFeatureClass_out);
}
/// <summary>
/// 根据图层名获取图层
/// </summary>
private static ILayer GetLayerByName(string IN_LayerName)
{
ILayer pLayer = null;
for (int i = 0; i < MapControl.LayerCount; i++)
{
if (MapControl.get_Layer(i).Name == IN_LayerName)
{
pLayer = MapControl.get_Layer(i);
break;
}
}
return pLayer;
}
/// <summary>
/// 根据图层名删除图层
/// </summary>
private static void DeleteLayerByName(string Delete_LayerName)
{
for (int i = 0; i < MapControl.LayerCount; i++)
{
if (MapControl.get_Layer(i).Name == Delete_LayerName)
{
MapControl.DeleteLayer(i);
}
}
}
/// <summary>
/// 符号化
/// </summary>
private static void SimpleFillSymbol(IFeatureLayer pFeatureLayer, esriSimpleFillStyle FillStyle, IColor pFillColor, IColor pOutLineColor, int nOutLineWidth = 1)
{
//填充风格、颜色
ISimpleFillSymbol pSimpleFillSymbol = new SimpleFillSymbolClass
{
Style = FillStyle
};
if (null != pFillColor)
{
pSimpleFillSymbol.Color = pFillColor;
}
ISimpleLineSymbol pSimpleLineSymbol = new SimpleLineSymbolClass
{
Style = esriSimpleLineStyle.esriSLSSolid,
Color = pOutLineColor,//边线颜色
Width = nOutLineWidth > 0 ? nOutLineWidth : 1 //线宽
};
pSimpleFillSymbol.Outline = pSimpleLineSymbol;
ISimpleRenderer simpleRender = new SimpleRendererClass
{
Symbol = pSimpleFillSymbol as ISymbol,
};
IGeoFeatureLayer geoLayer = pFeatureLayer as IGeoFeatureLayer;
if (geoLayer != null)
{
geoLayer.Renderer = simpleRender as IFeatureRenderer;
}
}
/// <summary>
/// EnvelopeToPolygon
/// </summary>
private static IPolygon EnvelopeToPolygon(IEnvelope envelope)
{
IPointCollection pointColl = new PolygonClass();
IPoint point = new PointClass();
point.PutCoords(envelope.XMin, envelope.YMin);
pointColl.AddPoint(point);
point = new PointClass();
point.PutCoords(envelope.XMax, envelope.YMin);
pointColl.AddPoint(point);
point = new PointClass();
point.PutCoords(envelope.XMax, envelope.YMax);
pointColl.AddPoint(point);
point = new PointClass();
point.PutCoords(envelope.XMin, envelope.YMax);
pointColl.AddPoint(point);
point = new PointClass();
point.PutCoords(envelope.XMin, envelope.YMin);
pointColl.AddPoint(point);
return pointColl as IPolygon;
}
#endregion
}
}
ArcGIS Engine - 绘制范围线
于 2020-10-21 15:57:02 首次发布