




已阅读5页,还剩23页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
实验六:基于最邻近模型的服务区分析题目要求:1)Form1主界面搭建: 2)Form2用于邻近分析和唯一值渲染的界面搭建:*要注意的几点:2.1首先将实验数据存放在系统工程文件夹binDebug文件夹中,如图:2.2后面需要添加一个EngineFunction.cs类:结果如下:2.3如果引用命名空间时出现了红波浪线,则需在解决方案资源管理器中添加引用,选择.NET环境下的以ESRI.ArcGIS开头的命名空间的引用,则代码中的红波浪则会消除,如图:3)如果用于Visual2010+ArCGIS10.0所组成的ArcGIS Engine中:Program.cs 中需添加一句话用于格式转换:using System;using System.Collections.Generic;using System.Linq;using System.Windows.Forms;namespace 六 static class Program STAThread static void Main() Application.EnableVisualStyles(); ESRI.ArcGIS.RuntimeManager.Bind(ESRI.ArcGIS.ProductCode.EngineOrDesktop); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1(); 4)Form1代码:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using ESRI.ArcGIS.Controls;using ESRI.ArcGIS.Carto;using ESRI.ArcGIS.Geodatabase;using System.IO;using ESRI.ArcGIS.Display;using ESRI.ArcGIS.esriSystem;using ESRI.ArcGIS.DataSourcesFile;using ESRI.ArcGIS.Geometry;namespace 六 public partial class Form1 : Form public Form1() InitializeComponent(); private void Form1_Load(object sender, EventArgs e) private void 文件tToolStripMenuItem_Click(object sender, EventArgs e) IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactoryClass(); string ShpPath = Application.StartupPath + 服务区分析数据; IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(ShpPath, 0); IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace; IFeatureClass pFeat1 = pFeatureWorkspace.OpenFeatureClass(cubsoxaddr_prj.shp); IFeatureLayer pFLayerPoint = new FeatureLayerClass(); pFLayerPoint.FeatureClass = pFeat1; pFLayerPoint.Name = pFeat1.AliasName; ILayer pLayerPoint = pFLayerPoint as ILayer; IFeatureClass pFeat2 = pFeatureWorkspace.OpenFeatureClass(chitrt_cent.shp); IFeatureLayer pFLayerPeoplePoint = new FeatureLayerClass(); pFLayerPeoplePoint.FeatureClass = pFeat2; pFLayerPeoplePoint.Name = pFeat2.AliasName; ILayer pLayerPeoplePoint = pFLayerPeoplePoint as ILayer; IFeatureClass pFCLine = pFeatureWorkspace.OpenFeatureClass(chitrt_polygon.shp); IFeatureLayer pFLayerLine = new FeatureLayerClass(); pFLayerLine.FeatureClass = pFCLine; pFLayerLine.Name = pFCLine.AliasName; ILayer pLayerLine = pFLayerLine as ILayer; IMap pMap = axMapControl1.Map; pMap.AddLayer(pLayerLine); pMap.AddLayer(pLayerPeoplePoint); pMap.AddLayer(pLayerPoint); axMapControl1.ActiveView.Refresh(); 邻近分析ToolStripMenuItem.Enabled = true; 打开文件ToolStripMenuItem.Enabled = false; private void 邻近分析_Click(object sender, EventArgs e) IObjectCopy objectCopy = new ObjectCopyClass(); object toCopyMap = axMapControl1.Map; object copiedMap = objectCopy.Copy(toCopyMap); IMap pMap = copiedMap as IMap; Form2 AccFrom = new Form2(pMap); AccFrom.ShowDialog(); 5)Form2代码:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using ESRI.ArcGIS.Carto;using ESRI.ArcGIS.Geodatabase;using ESRI.ArcGIS.Display;using ESRI.ArcGIS.Geometry;using System.Threading;namespace 六 public partial class Form2 : Form IMap MainMap = null; IFeatureLayer ServiceLayer = null; IFeatureLayer ResidualLayer = null; IFeatureLayer ResidulLayerPoint = null; string DmdFieldName = null; public Form2(IMap mainMap) this.MainMap = mainMap; ILayer pLayer = EngineFunction.GetLayerByName(MainMap, chitrt_polygon); IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer; EngineFunction.AddField(pFeatureLayer, 结果字段, esriFieldType.esriFieldTypeInteger); /添加一个字段 结果字段,用来保存计算结果 InitializeComponent(); for (int i = 0; i MainMap.LayerCount; i+) comboBox1.Items.Add(MainMap.get_Layer(i).Name); comboBox2.Items.Add(MainMap.get_Layer(i).Name); comboBox3.Items.Add(MainMap.get_Layer(i).Name); comboBox1.SelectedIndex = 0; comboBox2.SelectedIndex = 1; comboBox3.SelectedIndex = 2; comboBox4.SelectedIndex = 12; private void Form2_Load(object sender, EventArgs e) axMapControl1.Map = MainMap; button1.Enabled = false; private void comboBox3_SelectedIndexChanged(object sender, EventArgs e) comboBox4.Items.Clear(); ILayer pLayer = EngineFunction.GetLayerByName(MainMap, comboBox3.Text); IFeatureLayer pFeatureLayer = pLayer as IFeatureLayer; IFields pFields = pFeatureLayer.FeatureClass.Fields; for (int i = 0; i pFields.FieldCount; i+) comboBox4.Items.Add(pFields.get_Field(i).Name); comboBox4.SelectedIndex = 0; public static Thread CountThread = null; private void btn_Count_Click(object sender, EventArgs e) label5.Visible = true; ServiceLayer = EngineFunction.GetLayerByName(MainMap, comboBox1.Text) as IFeatureLayer; ResidualLayer = EngineFunction.GetLayerByName(MainMap, comboBox3.Text) as IFeatureLayer; ResidulLayerPoint = EngineFunction.GetLayerByName(MainMap, comboBox2.Text) as IFeatureLayer; DmdFieldName = comboBox4.Text; btn_Count.Enabled = false; comboBox1.Enabled = false; comboBox2.Enabled = false; comboBox3.Enabled = false; comboBox4.Enabled = false; CountThread = new Thread(new ThreadStart(StartCount); /防止计算时,界面处于假死未响应状态,申请一个线程单独执行运算。 CountThread.Start(); public void StartCount() JustContains(ServiceLayer, ResidualLayer, DmdFieldName, ResidulLayerPoint); IWorkspaceEdit pWorkspaceEdit; / ServiceLayer 就是服务区图层,里面有两个点,分别代表两个队 该图层文件名cubsoxaddr_prj /ResiduaLayer 是居民区图层,因为最后要对它进行渲染,所有要把计算的结果保留到这个图层里,所以在这个图层里新建了一个字段,“计算结果”,也就是DmdFieldName变量 该图层的文件名chitrt_polygon /ResidulLayerPoint 在计算服务区与点之间的距离时,因为面和点的距离无法计算,所以就用面图层中,人口的矢量质心代替,每个面都对应一个点,这些点保存在ResidulLayerPoint图层中,该图层文件名 chitrt_cent private void JustContains(IFeatureLayer ServiceLayer, IFeatureLayer ResidualLayer, string DmdFieldName, IFeatureLayer ResidulLayerPoint) IFeatureClass pResidulFeatureClass = ResidulLayerPoint.FeatureClass; int CountPoint = pResidulFeatureClass.FeatureCount(null); SetMaxNum(CountPoint); IQueryFilter pQueryFilter = new QueryFilterClass(); IFeatureCursor pResidulFeatCursor = pResidulFeatureClass.Search(null, false);/获取居民点图层中的所有要素,每个要素的FID和面图层的FID对应,由此关系来找到面图层中对应的要素 IFeature pResidulFeature = pResidulFeatCursor.NextFeature();/ 读取第一个要素 int ResidualFieldIndex = ResidualLayer.FeatureClass.Fields.FindField(DmdFieldName); /在居民要素图层中找到“结果字段对应的索引 Index” IQueryFilter pQueryFilterServer = new QueryFilterClass(); pQueryFilterServer.WhereClause = FID = 0; IFeature Cubs = ServiceLayer.FeatureClass.Search(pQueryFilterServer, false).NextFeature(); /获取小熊队的要素 pQueryFilterServer.WhereClause = FID = 1; IFeature WhiteSox = ServiceLayer.FeatureClass.Search(pQueryFilterServer, false).NextFeature();/获取白袜队的要素 IPoint CubsPoint = Cubs.Shape as IPoint; /把小熊队要素转化为点 IPoint WhiteSoxPoint = WhiteSox.Shape as IPoint;/把白袜队要素转化为点 IDataset pDataSet = ResidualLayer.FeatureClass as IDataset; /建立居民区图层的数据表 IWorkspace pWorkspace = pDataSet.Workspace;/获取数据表的工作空间 pWorkspaceEdit = pWorkspace as IWorkspaceEdit; / 建立工作空间编辑器 pWorkspaceEdit.StartEditing(false); / 开始编辑 pWorkspaceEdit.StartEditOperation(); /开启应用程序编辑操作 int iStep = 1; while (pResidulFeature != null) /循环遍历 居民区点要素图层中的每一个要素 int FID = ResidulLayerPoint.FeatureClass.Fields.FindField(FID); /获取要素 的FID字段在表结构中的索引值 int FIDResult = Convert.ToInt32(pResidulFeature.get_Value(FID);/通过索引值获取对应要素的FID值 IQueryFilter pQueryFilterEdit = new QueryFilterClass(); pQueryFilterEdit.WhereClause = FID= + FIDResult; /通过居民区点要素图层中的FID找到居民区要素中相对应的要素 IFeatureCursor pEditCursor = ResidualLayer.FeatureClass.Search(pQueryFilterEdit, false);/ 获取该居民区要素集合 IFeature EditFeature = pEditCursor.NextFeature(); /因为只可能找到一个值,所以直接读取第一个要素就可以了 IPoint pPoint = pResidulFeature.Shape as IPoint; /居民区点图层要素转化为点符号 double DX1 = MainMap.ComputeDistance(pPoint, CubsPoint); /计算该点符号与小熊队的距离 double DX2 = MainMap.ComputeDistance(pPoint, WhiteSoxPoint);/计算该点符号与白袜队的距离 if (DX1 DX2) /如果与小熊队比较接近 EditFeature.set_Value(ResidualFieldIndex, 0); /把结果字段标记为 0 else /如果与白袜队比较接近 EditFeature.set_Value(ResidualFieldIndex, 1);/ 把结果字段标记为1 EditFeature.Store(); /存储编辑 SetStepNum(iStep); /此处与实验无关,只是加载进度条显示而已 iStep+; pResidulFeature = pResidulFeatCursor.NextFeature(); /继续读取下一个居民区点要素图层 pWorkspaceEdit.StopEditOperation(); pWorkspaceEdit.StopEditing(true); / 编辑完成 Application.DoEvents(); MessageBox.Show(计算完成!); SetVisable(); private void button1_Click(object sender, EventArgs e) / 这里 就是单一值渲染 IUniqueValueRenderer pRender = new UniqueValueRendererClass(); pRender.FieldCount = 1; pRender.set_Field(0, 结果字段); pRender.set_FieldType(0, false); IRgbColor pRGB = new RgbColorClass(); pRGB.Red = 0; pRGB.Green = 255; pRGB.Blue = 127; IFillSymbol pSymbol1 = new SimpleFillSymbolClass(); pSymbol1.Outline.Width = 0.4; pSymbol1.Color = pRGB; pRender.AddValue(0, 小熊队, pSymbol1 as ISymbol); IRgbColor pRGB2 = new RgbColorClass(); pRGB2.Red = 255; pRGB2.Green = 97; pRGB2.Blue = 0; IFillSymbol pSymbol2 = new SimpleFillSymbolClass(); pSymbol2.Outline.Width = 0.4; pSymbol2.Color = pRGB2; pRender.AddValue(1, 白袜队, pSymbol2 as ISymbol); ILayer ppp = GetLayerByName(MainMap, chitrt_polygon); IGeoFeatureLayer pGeoFLayer = ppp as IGeoFeatureLayer; pGeoFLayer.Renderer = pRender as IFeatureRenderer; IActiveView pView = axMapControl1.ActiveView; pView.ContentsChanged(); pView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null); public static ILayer GetLayerByName(IMap pMap, String layerName) ILayer layer = null; for (int i = 0; i pMap.LayerCount; i+) if (pMap.get_Layer(i).Name = layerName) layer = pMap.get_Layer(i); break; return layer; public delegate void MyInvoke(int Num); public delegate void CountrolInvoke(); private void SetMaxNum(int Num) if (progressBar1.InvokeRequired) MyInvoke _myInvoke = new MyInvoke(SetMaxNum); this.Invoke(_myInvoke, new object Num ); else gressBar1.Maximum = Num; private void SetStepNum(int Num) if (progressBar1.InvokeRequired) MyInvoke _myInvoke = new MyInvoke(SetStepNum); this.Invoke(_myInvoke, new object Num ); else gressBar1.Value = Num; private void SetVisable() if (progressBar1.InvokeRequired) CountrolInvoke _myInvoke = new CountrolInvoke(SetVisable); this.Invoke(_myInvoke, new object ); else this.button1.Enabled = true; if (label5.InvokeRequired) CountrolInvoke _myInvoke = new CountrolInvoke(SetVisable); this.Invoke(_myInvoke, new object ); else this.label5.Visible = false; private void accessibilityForm_FormClosing(object sender, FormClosingEventArgs e) if (CountThread != null) if (CountThread.IsAlive) pWorkspaceEdit.StopEditOperation(); pWorkspaceEdit.StopEditing(true); / 编辑完成 CountThread.Abort(); 6)在解决方案资源管理器中添加一个新类EngineFunction.cs,其中主要存储一些方法,代码如下:using System;using System.Collections.Generic;using System.Linq;using System.Text;using ESRI.ArcGIS.Controls;using ESRI.ArcGIS.Carto;using ESRI.ArcGIS.Geodatabase;using System.IO;using ESRI.ArcGIS.Display;using ESRI.ArcGIS.esriSystem;using ESRI.ArcGIS.DataSourcesFile;using ESRI.ArcGIS.Geometry;namespace 六 class EngineFunction / / 获取图层几何类型的属性字段值 / / / public static string GetShpType(ILayer n_Layer) IFeatureLayer pFeatureLayer = n_Layer as IFeatureLayer; switch (pFeatureLayer.FeatureClass.ShapeType) case esriGeometryType.esriGeometryPoint: return Point; break; case esriGeometryType.esriGeometryPolyline: return Polyline; break; case esriGeometryType.esriGeometryPolygon: return Polygon; break; default: return ; break; / / 向属性表中添加字段 / 如果存在,则先删除后添加 / / / / public static void AddField(ILayer pLayer, string FieldName, esriFieldType Type) ITable LayerTable; LayerTable = pLayer as ITable; IField pField = new FieldClass(); IFieldEdit pFieldEdit; pFieldEdit = pField as IFieldEdit; pFieldEdit.Name_2 = FieldName; pFieldEdit.Type_2 = Type; IField tmpField = null; for (int i = 0; i LayerTable.Fields.FieldCount; i+) tmpField = LayerTable.Fields.get_Field(i); if (FieldName = tmpField.Name) LayerTable.DeleteField(tmpField); break; LayerTable.AddField(pField); public static void AddField(IFeatureLayer pFeatureLayer, string FieldName, esriFieldType Type) ISchemaLock pLock = pFeatureLayer.FeatureClass as ISchemaLock; ITable LayerTable = pFeatureLayer as ITable; IField tmpField = null; for (int i = 0; i LayerTable.Fields.FieldCount; i+) tmpField = LayerTable.Fields.get_Field(i); if (FieldName = tmpField.Name) LayerTable.DeleteField(tmpField); break; IField pField = new FieldClass();
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2025农产品销售合同模板
- 2025版租赁合同范本:房屋租赁合同
- 2025年县域高速公路监控员招聘面试预测题详解
- 2025存量房买卖合同协议书样本下载
- 2025标准企业劳动合同模板
- 2025年专业素养培养中级语言培训师面试大纲及模拟题解析
- 中职高考护理面试题及答案
- 2025年AI编程师考试模拟题详解及答题技巧
- 2025年人工智能算法工程师面试预测题及难点解析
- 2025年学校教师招聘面试技巧与预测题集
- 2025-2026学年广美版(2024)小学美术二年级上册教学计划及进度表
- 2025年手电筒行业研究报告及未来行业发展趋势预测
- 酒店客户服务质量提升培训课件
- GB/T 9258.2-2025涂附磨具用磨料粒度组成的检测和标记第2部分:粗磨粒P12~P220
- 2025山西太原西山生态文旅投资建设有限公司及子公司招聘13人笔试参考题库附带答案详解
- 2025 年小升初吕梁市初一新生分班考试语文试卷(带答案解析)-(部编版)
- 2025秋全体教师大会上,德育副校长讲话:德为根,安为本,心为灯,家为桥-这场开学讲话,句句都是育人的方向
- 2025年政工师考试试题及参考答案
- (2025年标准)个人转款协议书
- 2025兵团连队职工考试试题及答案解析
- 2025-2026学年接力版(2024)小学英语四年级上册(全册)教学设计(附目录)
评论
0/150
提交评论