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);
}
}
}

  private ISymbol GetSymbol(IColor pColor)
        {
            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)
{

}

}
}

AE开发的更多相关文章

  1. AE开发使用内存图层

    AE开发中,有时需要从磁盘中读取一些文件信息如坐标点转为图层并进行分析,此过程并不需要坐标点入库之类的操作,就可以创建一个内存图层解决问题.创建内存图层需要用到InMemoryWorkspaceFac ...

  2. AE开发能否实现TOC Control里添加多个Data Frame

    问题: 在ArcMap中,菜单Insert下Data Frame,可以在TOC中增加Data Frame,在MapControl或者PageLayoutControl下都可以正常显示多个Data Fr ...

  3. 将AE开发的专题图制作功能发布为WPS

    AE开发可以定制化实现ArcGIS的地理处理功能,并实际运用于其他方面的工作,有时候我们还希望将AE开发的功能发布为网络地理信息处理服务(WPS),从而能在Web端更自由便利地调用所需要的地学处理算法 ...

  4. 【转】.NET+AE开发中常见几种非托管对象的释放

    尝试读取或写入受保护的内存.这通常指示其他内存已损坏. 今天在开发时遇到一个问题:" 未处理 System.AccessViolationException Message="尝试 ...

  5. AE开发技术文档--8种数据访问方法

    转自原文 AE开发技术文档--8种数据访问方法 1.shapefile IWorkspaceFactory pWorkspaceFactory; pWorkspaceFactory = new Sha ...

  6. AE开发概念辨析

    樱木 原文 AE开发之概念辨析2,AE开发涉及相关概念,AE开发相关概念 1 AE中的类库 AE总共包括了21个子库,分别是SYSTEM,SYSTEMUI,GEOMETRY,DISPLAY,SERVE ...

  7. c# 进行AE开发时,如何在地图上定位出一个点

    一.文本形式的气泡提示框 由于本人是初学,所以具体的含义尚未弄清楚,直接给出代码吧!

  8. AE开发 入门教程

    此过程说明适合那些使用.NET建立和部署应用的开发者,它描述了使用ArcGIS控件建立和部署应用的方法和步骤. 你可以在下面的目录下找到相应的样例程序: <安装目录>/DeveloperK ...

  9. WPF+AE开发小结--TOCControl右键菜单删除图层

    1.WPF项目中添加toccontrol控件,如何添加,网上有很多方法,可自行搜索,这里不再赘述,代码如下 <Window x:Class="AE.MainWindow" x ...

  10. AE开发流程

    using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using Sy ...

随机推荐

  1. Android-Kotlin-抽象类与多态的表现

    选择包名,然后右键: 选择Class类型,会有class:  选择File类型,不会自动有class: 目录结构: 定义描述抽象类 Person人类: package cn.kotlin.kotlin ...

  2. PCA in MLLib

    SVD分解: \(A=U\Sigma V^T\),变换:\(\hat{A}=A\cdot V=U\Sigma\) 分解时先计算\(A^TA=U\Sigma^2U^T\),再进行SVD分解 /** * ...

  3. Elasticsearch 系列4 --- Windows10安装Kibana

    Kibana是Elastic Stack家族内的一部分,它是一个管理网站,与ES(Elastic Search)集成可以用来管理ES的索引,除ES外它还可以跟Elastic家族的其他组件进行整合如lo ...

  4. Python 学习第三部分函数——第一章函数基础

    函数是python 为了代码最大程度的重用和最小代码冗余而提供的最基本的程序结构.使用它我们可以将复杂的系统分解为可管理的部件. 函数相关语句 def...          创建一个对象并将其赋值给 ...

  5. bower 安装后 jade 引用404问题

    这个问题困扰我接近2小时,这是我在stackoveflow 上面挖到的 原文地址:http://stackoverflow.com/questions/21821773/configure-node- ...

  6. Android sharedUserId 和系统权限

    sharedUserId 给不同的应用使用同一个 sharedUserId 可以运行在这几个应用间互相访问数据(数据库,SharedPreferences,文件). sharedUserId 一旦使用 ...

  7. Python3.5 学习十七

    jQuery 模块=类库 jQuery就是DOM .BOM.Javascript的封装成的类库 一.查找元素.DOM只有10种左右选择器 jQuery有很多选择器和筛选器 PS:jQuery 推荐1系 ...

  8. 微服务 - Eureka注册中心

    我们来解决微服务的第一问题,服务的管理. 服务中心对外提供服务,需要对外暴露自己的地址.而consumer(调用者)需要记录服务提供者的地址.将来地址出现变更,还需要及时更新.这在服务较少的时候并不觉 ...

  9. urllib2 的使用与介绍

    爬虫简介  什么是爬虫? 爬虫:就是抓取网页数据的程序. HTTP和HTTPS HTTP协议(HyperText Transfer Protocol,超文本传输协议):是一种发布和接收 HTML页面的 ...

  10. Redis---List(链表)

    1. 基本结构 Redis 早期版本存储 list 列表数据结构使用的是压缩列表 ziplist 和普通的双向链表 linkedlist,也就是元素少时用 ziplist,元素多时用 linkedli ...