AE开发
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using System.Collections;
namespace 考试
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.axTOCControl1.SetBuddyControl(this.axMapControl1);
}
List<IFeature> sFeature;
private List<IFeature> RecSelectFetures(IFeatureLayer pFeatureLayer)
{
List<IFeature> pFeatures = new List<IFeature>();
if (pFeatureLayer != null)
{
IFeatureSelection pFeatureSelection = pFeatureLayer as IFeatureSelection;
pFeatureSelection.Clear();
axMapControl1.Refresh();
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
IGeometry pGeometry = axMapControl1.TrackRectangle();
ISpatialFilter pSpatialFilter = new SpatialFilterClass();
pSpatialFilter.Geometry = pGeometry;
pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
IFeatureCursor pFeatureCursor = pFeatureLayer.Search(pSpatialFilter, false);
IFeature pFeature = pFeatureCursor.NextFeature();
pFeatureSelection.CombinationMethod = esriSelectionResultEnum.esriSelectionResultNew;
while (pFeature != null)
{
pFeatureSelection.Add(pFeature);
pFeatures.Add(pFeature);
pFeature = pFeatureCursor.NextFeature();
}
axMapControl1.ActiveView.Refresh();
}
return pFeatures;
}
private double ConvertPixelsToMapUnits(IActiveView pActiveView, double pixelUnits)
{
IPoint p1 = pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.UpperLeft;
IPoint p2 = pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.UpperRight;
int x1, x2, y1, y2;
pActiveView.ScreenDisplay.DisplayTransformation.FromMapPoint(p1, out x1, out y1);
pActiveView.ScreenDisplay.DisplayTransformation.FromMapPoint(p2, out x2, out y2);
double pixelExtent = x2 - x1;
double realWorldDisplayExtent = pActiveView.ScreenDisplay.DisplayTransformation.VisibleBounds.Width;
double sizeOfOnePixel = realWorldDisplayExtent / pixelExtent;
return pixelUnits * sizeOfOnePixel;
}
private void CopyAttribute(IFeature pSourceFea, IFeature pTargetFea)
{
IFields pSourceFlds = pSourceFea.Fields;
for (int i = 0; i < pSourceFlds.FieldCount; i++)
{
IField pSourceFld = pSourceFlds.get_Field(i);
if ((pSourceFld.Type == esriFieldType.esriFieldTypeOID) ||
(pSourceFld.Type == esriFieldType.esriFieldTypeGeometry) ||
(pSourceFld.Type == esriFieldType.esriFieldTypeBlob))
continue;
string sSourceVal = pSourceFea.get_Value(i).ToString();
if (string.IsNullOrEmpty(sSourceVal))
continue;
int iTargetFldIndex = pTargetFea.Fields.FindField(pSourceFld.Name);
if (iTargetFldIndex >= 0)
pTargetFea.set_Value(iTargetFldIndex, sSourceVal);
}
}
private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "打开文件";
dlg.Filter = "(*.mdb)|*.mdb|All Files(*.*)|*.* ";
dlg.ShowDialog();
string path = dlg.FileName;
IWorkspaceFactory pAccessWorkspaceFactory;
IFeatureWorkspace pFeatureWorkspace;
IFeatureLayer pFeatureLayer;
IFeatureDataset pFeatureDataset;
pAccessWorkspaceFactory = new AccessWorkspaceFactoryClass();
IWorkspace pWorkspace = pAccessWorkspaceFactory.OpenFromFile(path, 0);
IEnumDataset pEnumDataset = pWorkspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);
pEnumDataset.Reset();
IDataset pDataset = pEnumDataset.Next();
if (pDataset is IFeatureDataset)
{
pFeatureWorkspace = (IFeatureWorkspace)pAccessWorkspaceFactory.OpenFromFile(path, 0);
pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset(pDataset.Name);
IEnumDataset pEnumDataset1 = pFeatureDataset.Subsets;
pEnumDataset1.Reset();
IDataset pDataset1 = pEnumDataset1.Next();
if (pDataset1 is IFeatureClass)
{
pFeatureLayer = new FeatureLayerClass();
pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(pDataset1.Name);
pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
axMapControl1.Map.AddLayer(pFeatureLayer);
axMapControl1.ActiveView.Refresh();
}
}
else
{
pFeatureWorkspace = (IFeatureWorkspace)pWorkspace;
pFeatureLayer = new FeatureLayerClass();
pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(pDataset.Name);
pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
axMapControl1.Map.AddLayer(pFeatureLayer);
axMapControl1.ActiveView.Refresh();
}
}
private void axMapControl1_OnMouseDown(object sender, ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvent e)
{
IMap pMap = axMapControl1.Map;
IActiveView pActiveView = pMap as IActiveView;
IFeatureLayer pFeatureLayer = pMap.get_Layer(0) as IFeatureLayer;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
IPoint point = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(e.x, e.y);
ITopologicalOperator pTOpo = point as ITopologicalOperator;
double length;
length = ConvertPixelsToMapUnits(pActiveView, 4);
IGeometry pBuffer = pTOpo.Buffer(length);
IGeometry pGeomentry = pBuffer.Envelope;
ISelectionEnvironment pSelEnv = new SelectionEnvironment();//新建选择环境
IRgbColor pColor = new RgbColor();
pColor.Red = 255;
pSelEnv.DefaultColor = pColor;//设置高亮颜色
pMap.SelectByShape(pBuffer, pSelEnv, false);//选择图
axMapControl1.Refresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
List<IFeature> S = RecSelectFetures(pFeatureLayer);
sFeature = S;
if (S.Count > 0)
{
treeList1.ClearNodes();
try
{
treeList1.BeginUpdate();
int k = 0;
for (int i = 0; i < S.Count; i++)
{
for (int j = 0; j < S[i].Fields.FieldCount; j++)
{
IField pFiled = S[i].Fields.get_Field(j);
treeList1.Columns.Add();
treeList1.Columns[j].Caption = pFiled.Name;
treeList1.Columns[j].VisibleIndex = j;
}
string sFirseFidValue = S[i].get_Value(0).ToString();
treeList1.AppendNode(new object[] { sFirseFidValue }, null);
for (int m = 1; m < S[i].Fields.FieldCount; m++)
{
object FieldValue;
if (S[i].Class.Fields.get_Field(m).Name == "SHAPE")
{
FieldValue = S[i].Shape.GeometryType.ToString();
}
else
{
FieldValue = S[i].get_Value(m).ToString();
}
treeList1.Nodes[k][m] = FieldValue;
}
k = k + 1;
}
}
catch (Exception exc)
{
MessageBox.Show("错误" + exc.ToString());
}
finally
{
treeList1.EndUpdate();
}
}
}
private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
string serror = "";
for (int i = 0; i < sFeature.Count; i++)
{
IFeature pFeature = sFeature[i];
IFeatureClass pSourceFeaCls = pFeature.Class as IFeatureClass;
if (pSourceFeaCls == null)
return;
IWorkspace pSourceWks = (pSourceFeaCls as IDataset).Workspace;
ISpatialReference pSourceSpr = ((pSourceFeaCls as IDataset) as IGeoDataset).SpatialReference;
//string localFilePath, fileNameExt, newFileName, FilePath;
SaveFileDialog sfd = new SaveFileDialog();
//设置文件类型
sfd.Filter = "Shape(*.shp)|*.shp|All Files(*.*)|*.*";
sfd.CreatePrompt = false;
if (sfd.ShowDialog() != DialogResult.OK)
return;
//得到文件名和路径
string strShppath = System.IO.Path.GetDirectoryName(sfd.FileName);
string strShpFile = System.IO.Path.GetFileNameWithoutExtension(sfd.FileName);
//创建shp工作空间
IWorkspaceFactory pWksFac = new ShapefileWorkspaceFactory();
IFeatureWorkspace pFeaWks = (IFeatureWorkspace)pWksFac.OpenFromFile(strShppath, 0);
//创建字段集
IFields pFlds = new FieldsClass();
IFieldsEdit pFldsEdt = (IFieldsEdit)pFlds;
//创建基础字段
IField pFld = new FieldClass();
IFieldEdit pFldEdt = (IFieldEdit)pFld;
string strShapeFieldName = "shape";
pFldEdt.Name_2 = strShapeFieldName;
pFldEdt.Type_2 = esriFieldType.esriFieldTypeGeometry;
//为esriFieldTypeGeometry类型的字段创建几何定义
IGeometryDef pGeoDef = new GeometryDefClass();
IGeometryDefEdit pGeoDefEdit = (IGeometryDefEdit)pGeoDef;
pGeoDefEdit.GeometryType_2 = pSourceFeaCls.Fields.get_Field(1).GeometryDef.GeometryType;//设置shp文件的几何类型
pGeoDefEdit.SpatialReference_2 = pSourceSpr;//设置shp文件的空间参考
pFldEdt.GeometryDef_2 = pGeoDef;
pFldsEdt.AddField(pFld);
//创建其他字段
for (int j = 2; j < pSourceFeaCls.Fields.FieldCount; j++)
{
pFld = new FieldClass();
pFldEdt = (IFieldEdit)pFld;
pFldEdt.Name_2 = pSourceFeaCls.Fields.get_Field(j).Name;
pFldEdt.Type_2 = pSourceFeaCls.Fields.get_Field(j).Type;
pFldsEdt.AddField(pFld);
}
//创建空白shp文件
pFeaWks.CreateFeatureClass(strShpFile, pFlds, null, null, esriFeatureType.esriFTSimple, strShapeFieldName, "");
//打开Shapefile,获取FeaCls
IWorkspace pTargetWks = pWksFac.OpenFromFile(strShppath, 0);
if (pTargetWks == null)
return;
IFeatureClass pTargetFeaCls = (pTargetWks as IFeatureWorkspace).OpenFeatureClass(strShpFile);
int ifeaCount = pSourceFeaCls.FeatureCount(null);
IFeatureCursor pFeaCur = pSourceFeaCls.Search(null, false);
IFeature pSourceFea = pFeaCur.NextFeature();
if (pSourceFea == null)
return;
bool blok = false;
IWorkspaceEdit pWksEdt = pTargetWks as IWorkspaceEdit;
pWksEdt.StartEditing(false);
pWksEdt.RedoEditOperation();
try
{
while (pSourceFea != null)
{
IFeature pTargetFea = pTargetFeaCls.CreateFeature();
if (pTargetFea != null)
{
pTargetFea.Shape = pSourceFea.Shape;
CopyAttribute(pSourceFea, pTargetFea);
pTargetFea.Store();
}
pSourceFea = pFeaCur.NextFeature();
}
blok = true;
}
catch (Exception exc)
{
serror = exc.Message;
}
finally
{
if (blok)
pWksEdt.StopEditOperation();
else
pWksEdt.AbortEditOperation();
pWksEdt.StopEditing(blok);
if (blok)
MessageBox.Show("导出成功,共计导出" + ifeaCount + "条记录");
else
MessageBox.Show("导出失败" + serror);
}
}
}
{
ISymbol pSymbol;
ISimpleFillSymbol pSymbolFillSymbol = new SimpleFillSymbolClass();
pSymbolFillSymbol.Color = pColor;
pSymbolFillSymbol.Outline.Width = 0.4;
pSymbol = pSymbolFillSymbol as ISymbol;
return pSymbol;
}
private IRandomColorRamp GetColorRamp(int size)
{
IRandomColorRamp pRandomColorRamp = new RandomColorRampClass();
pRandomColorRamp.StartHue = 10;
pRandomColorRamp.EndHue = 300;
pRandomColorRamp.MaxSaturation = 100;
pRandomColorRamp.MinSaturation = 0;
pRandomColorRamp.MaxValue = 100;
pRandomColorRamp.MinValue = 0;
pRandomColorRamp.Size = size;
bool ok = true;
pRandomColorRamp.CreateRamp(out ok);
return pRandomColorRamp;
}
private void barButtonItem4_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
IMap pMap = this.axMapControl1.ActiveView.FocusMap;
if (pMap.LayerCount == 0)
{
MessageBox.Show("地图为空,请加载地图!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
return;
}
IGeoFeatureLayer pGeoLayer = this.axMapControl1.get_Layer(0) as IGeoFeatureLayer;
ITable pTable = pGeoLayer.FeatureClass as ITable;
ICursor pCursor;
IQueryFilter pQueryFilter = new QueryFilter();
pQueryFilter.AddField("OBJECTID");
pCursor = pTable.Search(pQueryFilter, true);//获取字段
IEnumerator pEnumreator;
//获取字段中各要素属性唯一值
IDataStatistics pDataStatistics = new DataStatisticsClass();
pDataStatistics.Field = "OBJECTID";//获取统计字段
pDataStatistics.Cursor = pCursor;
pEnumreator = pDataStatistics.UniqueValues;
int fieldcount = pDataStatistics.UniqueValueCount;//唯一值个数,以此确定颜色带范围
IUniqueValueRenderer pUniqueValueR = new UniqueValueRendererClass();
pUniqueValueR.FieldCount = 1;//单值渲染
pUniqueValueR.set_Field(0, "OBJECTID");//渲染字段
IEnumColors pEnumColor = GetColorRamp(fieldcount).Colors;
pEnumColor.Reset();
while (pEnumreator.MoveNext())
{
string value = pEnumreator.Current.ToString();
if (value != null)
{
IColor pColor = pEnumColor.Next();
ISymbol pSymbol = GetSymbol(pColor);
pUniqueValueR.AddValue(value, "OBJECTID", pSymbol);
//pUniqueValueR.set_Symbol(value, pSymbol);
}
}
pGeoLayer.Renderer = pUniqueValueR as IFeatureRenderer;
this.axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
this.axTOCControl1.Update();
}
}
private void barButtonItem3_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
{
}
private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
{
}
}
}