Engine10.0轻松入门级教程(3).doc_第1页
Engine10.0轻松入门级教程(3).doc_第2页
Engine10.0轻松入门级教程(3).doc_第3页
Engine10.0轻松入门级教程(3).doc_第4页
Engine10.0轻松入门级教程(3).doc_第5页
已阅读5页,还剩42页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

ArcGIS Engine10.0轻松入门级教程(3)ArcEngine10.0查询分析功能目录(?)-1. 属性及空间查询2. 缓冲区查询3. 叠置分析4. 网络分析 GIS中的查询分析功能是非常重要的,本节将实现这些功能。1属性及空间查询在Forms文件夹右击点击“添加”“Windows窗体”,添加两个窗体,分别用于空间查询和属性查询,参数设置如下表。窗体名称(Name)Text属性描述SpatialQueryForm空间查询用于空间查询参数设置AttributeQueryForm属性查询用于属性查询参数设置同时在主窗体上的菜单项上添加一二级菜单。 查询(menuQuery) 属性查询(menuAttributeQuery) 空间查询(menuSpatialQuery) 实现属性查询,首先打开“属性查询”窗体的设计器。添加三个Label控件,两个ComboBox,两个Button和一个TextBox。各控件属性设置如下:名称(Name)Text属性描述lblLayer选择图层:标签lblField字段名称:标签lblFind查找内容:标签cboLayerMapControl中的图层名称cboFieldcboLayer选中图层的所有字段名称txtValue输入的查询对象名称btnOk查找查询按钮btnCancel取消取消查询按钮界面效果如下:进入窗体的代码编辑界面,首先添加三个引用:usingESRI.ArcGIS.Controls; usingESRI.ArcGIS.Carto; usingESRI.ArcGIS.Geodatabase; 然后定义两个成员变量,一个用于存储地图数据,一个用于存储当前选中图层,如下/地图数据privateAxMapControlmMapControl;/选中图层privateIFeatureLayermFeatureLayer; 然后修改其构造函数,构造函数中添加一个参数MapControl,用于获取MapControl中的数据,如下所示:publicAttributeQueryForm(AxMapControlmapControl)InitializeComponent();this.mMapControl=mapControl; 在窗体的Load事件中添加代码,用于初始化cboLayer,获取MapControl中的图层名称,如下:/MapControl中没有图层时返回if(this.mMapControl.LayerCount=0)return;/获取MapControl中的全部图层名称,并加入ComboBox/图层ILayerpLayer;/图层名称stringstrLayerName;for(inti=0;ithis.mMapControl.LayerCount;i+)pLayer=this.mMapControl.get_Layer(i);strLayerName=pLayer.Name;/图层名称加入cboLayerthis.cboLayer.Items.Add(strLayerName);/默认显示第一个选项this.cboLayer.SelectedIndex=0; 在CboLayer的SelectedIndexChanged事件中添加代码,当选中图层发生变化时,cboField中的字段名称重新获取,代码如下:/获取cboLayer中选中的图层mFeatureLayer=mMapControl.get_Layer(cboLayer.SelectedIndex)asIFeatureLayer;IFeatureClasspFeatureClass=mFeatureLayer.FeatureClass;/字段名称stringstrFldName;for(inti=0;ipFeatureClass.Fields.FieldCount;i+)strFldName=pFeatureClass.Fields.get_Field(i).Name;/图层名称加入cboFieldthis.cboField.Items.Add(strFldName);/默认显示第一个选项this.cboField.SelectedIndex=0; 查找按钮添加事件: private void button1_Click(object sender, EventArgs e) /定义图层,要素游标,查询过滤器,要素 IFeatureCursor pFeatureCursor; IQueryFilter pQueryFilter; IFeature pFeature; IPoint pPoint; IEnvelope pEnv; pEnv = mMapControl.ActiveView.Extent; pPoint = new PointClass(); pPoint.X = pEnv.XMin + pEnv.Width / 2; pPoint.Y = pEnv.YMin + pEnv.Height / 2; if (this.mMapControl.LayerCount = 0) return; /获取图层 mFeatureLayer = mMapControl.get_Layer(cboLayer.SelectedIndex) as IFeatureLayer; /清除上次查询结果 this.mMapControl.Map.ClearSelection(); this.mMapControl.ActiveView.Refresh(); /pQueryFilter的实例化 pQueryFilter = new QueryFilterClass(); /设置查询过滤条件 pQueryFilter.WhereClause = cboField.Text + = + txtValue.Text; /查询 pFeatureCursor = mFeatureLayer.Search(pQueryFilter, true); /获取查询到的要素 pFeature = pFeatureCursor.NextFeature(); /判断是否获取到要素 if (pFeature != null) /选择要素 this.mMapControl.Map.SelectFeature(mFeatureLayer, pFeature); /放大到要素 pFeature.Shape.Envelope.CenterAt(pPoint); this.mMapControl.Extent = pFeature.Shape.Envelope; else /没有得到pFeature的提示 MessageBox.Show(没有找到相关要素!, 提示); 取消按钮添加事件: private void btnCancel_Click(object sender, EventArgs e) this.Close(); 点击运行,这样就实现了属性查询的功能。运行效果如下图: 这一小结,我们进一步实现空间查询窗体的设计实现,我们的设想是通过该窗体选择查询的图层和查询的方式,然后将这两个参数传递给主窗体,主窗体实现查询,将查询得到的要素的属性显示在DataGridView控件中,下面开始动手吧。首先打开“属性查询”窗体的设计器。添加两个Label控件,两个ComboBox,两个Button。各控件属性设置如下:名称(Name)Text属性描述lblLayer选择图层:标签lblMode查询方式:标签cboLayerMapControl中的图层名称cboMode空间查询的方式btnOk确定确定查询按钮btnCancel取消取消查询按钮 进入窗体的代码编辑界面,首先添加三个引用: usingESRI.ArcGIS.Controls; usingESRI.ArcGIS.Carto; 然后定义两个成员变量,一个用于存储地图数据,一个用于存储当前选中图层,如下/获取主界面的MapControl对象privateAxMapControlmMapControl;/查询方式publicintmQueryMode;/图层索引publicintmLayerIndex; 然后修改其构造函数,构造函数中添加一个参数MapControl,用于获取MapControl中的数据,如下所示:publicSpatialQueryForm(AxMapControlmapControl)InitializeComponent();this.mMapControl=mapControl;在窗体的Load事件中添加代码,用于初始化cboLayer,获取MapControl中的图层名称,并初始化查询方式,代码如下:/MapControl中没有图层时返回if(this.mMapControl.LayerCount=0)return;/获取MapControl中的全部图层名称,并加入ComboBox/图层ILayerpLayer;/图层名称stringstrLayerName;for(inti=0;ithis.mMapControl.LayerCount;i+)pLayer=this.mMapControl.get_Layer(i);strLayerName=pLayer.Name;/图层名称加入ComboBoxthis.cboLayer.Items.Add(strLayerName);/加载查询方式this.cboMode.Items.Add(矩形查询);this.cboMode.Items.Add(线查询);this.cboMode.Items.Add(点查询);this.cboMode.Items.Add(圆查询);/初始化ComboBox默认值this.cboLayer.SelectedIndex=0;this.cboMode.SelectedIndex=0; 在“确定”按钮添加代码如下:/设置鼠标点击时窗体的结果this.DialogResult=DialogResult.OK;/判断是否存在图层if(this.cboLayer.Items.Count=0)MessageBox.Show(当前MapControl没有添加图层!,提示);return;/获取选中的查询方式和图层索引this.mLayerIndex=this.cboLayer.SelectedIndex;this.mQueryMode=this.cboMode.SelectedIndex; 这样我们就完成了空间查询窗体的设计。由于空间查询的结果需要借助于DataGridView进行显示,我们首先需要添加一个方法LoadQueryResult(AxMapControlmapControl,IFeatureLayerfeatureLayer,IGeometrygeometry),用于获取空间查询得到的要素的属性。在这个方法的参数中,IGeometry是用于空间查询的几何对象,IFeatureLayer是查询要素所在的要素图层,AxMapControl是当前MapControl。我们使用DataTable来存储要素的属性,然后将DataTable中的数据添加到DataGridView进行显示。为了显示效果在主窗体上添加一个Panel控件,把DataGridView控件添加上去,并在Panel控件上添加一个“关闭”按钮。然后把Panel控件Visible属性设置为False。 在这个方法实现过程中,首先利用IFeatureClass的属性字段初始化DataTable,然后利用IGeometry对IFeatureLayer图层进行空间查询返回到要素游标IFeatureCursor中,然后逐个变量要素,将值添加到DataTable。代码如下:privateDataTableLoadQueryResult(AxMapControlmapControl,IFeatureLayerfeatureLayer,IGeometrygeometry)IFeatureClasspFeatureClass=featureLayer.FeatureClass;/根据图层属性字段初始化DataTableIFieldspFields=pFeatureClass.Fields;DataTablepDataTable=newDataTable();for(inti=0;ipFields.FieldCount;i+)stringstrFldName;strFldName=pFields.get_Field(i).AliasName;pDataTable.Columns.Add(strFldName);/空间过滤器ISpatialFilterpSpatialFilter=newSpatialFilterClass();pSpatialFilter.Geometry=geometry;/根据图层类型选择缓冲方式switch(pFeatureClass.ShapeType)caseesriGeometryType.esriGeometryPoint:pSpatialFilter.SpatialRel=esriSpatialRelEnum.esriSpatialRelContains;break;caseesriGeometryType.esriGeometryPolyline:pSpatialFilter.SpatialRel=esriSpatialRelEnum.esriSpatialRelCrosses;break;caseesriGeometryType.esriGeometryPolygon:pSpatialFilter.SpatialRel=esriSpatialRelEnum.esriSpatialRelIntersects;break;/定义空间过滤器的空间字段pSpatialFilter.GeometryField=pFeatureClass.ShapeFieldName;IQueryFilterpQueryFilter;IFeatureCursorpFeatureCursor;IFeaturepFeature;/利用要素过滤器查询要素pQueryFilter=pSpatialFilterasIQueryFilter;pFeatureCursor=featureLayer.Search(pQueryFilter,true);pFeature=pFeatureCursor.NextFeature();while(pFeature!=null)stringstrFldValue=null;DataRowdr=pDataTable.NewRow();/遍历图层属性表字段值,并加入pDataTablefor(inti=0;ipFields.FieldCount;i+)stringstrFldName=pFields.get_Field(i).Name;if(strFldName=Shape)strFldValue=Convert.ToString(pFeature.Shape.GeometryType);elsestrFldValue=Convert.ToString(pFeature.get_Value(i);dri=strFldValue;pDataTable.Rows.Add(dr);/高亮选择要素mapControl.Map.SelectFeature(ILayer)featureLayer,pFeature);mapControl.ActiveView.Refresh();pFeature=pFeatureCursor.NextFeature();returnpDataTable; 定义两个成员变量,分别用于标记空间查询的查询方式和选中图层的索引(Index)。/空间查询的查询方式privateintmQueryMode;/图层索引privateintmLayerIndex; 然后单击菜单中的“查询”选项,选择“空间查询”,双击进入代码编辑界面,添加代码如下:/初始化空间查询窗体SpatialQueryFormspatialQueryForm=newSpatialQueryForm(this.axMapControl1);if(spatialQueryForm.ShowDialog()=DialogResult.OK)/标记为“空间查询”this.mTool=SpaceQuery;/获取查询方式和图层this.mQueryMode=spatialQueryForm.mQueryMode;this.mLayerIndex=spatialQueryForm.mLayerIndex;/定义鼠标形状this.axMapControl1.MousePointer=ESRI.ArcGIS.Controls.esriControlsMousePointer.esriPointerCrosshair; 然后进入MapControl的OnMouseDown事件,添加代码如下:this.axMapControl1.Map.ClearSelection();/获取当前视图IActiveViewpActiveView=this.axMapControl1.ActiveView;/获取鼠标点IPointpPoint=pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(e.x,e.y);switch(mTool)caseZoomIn:this.mZoomIn.OnMouseDown(e.button,e.shift,e.x,e.y);break;caseZoomOut:this.mZoomOut.OnMouseDown(e.button,e.shift,e.x,e.y);break;casePan:/设置鼠标形状this.axMapControl1.MousePointer=esriControlsMousePointer.esriPointerPanning;this.mPan.OnMouseDown(e.button,e.shift,e.x,e.y);break;caseSpaceQuery:panel1.Visible = true;IGeometrypGeometry=null;if(this.mQueryMode=0)/矩形查询pGeometry=this.axMapControl1.TrackRectangle();elseif(this.mQueryMode=1)/线查询pGeometry=this.axMapControl1.TrackLine();elseif(this.mQueryMode=2)/点查询ITopologicalOperatorpTopo;IGeometrypBuffer;pGeometry=pPoint;pTopo=pGeometryasITopologicalOperator;/根据点位创建缓冲区,缓冲半径为0.1,可修改pBuffer=pTopo.Buffer(0.1);pGeometry=pBuffer.Envelope;elseif(this.mQueryMode=3)/圆查询pGeometry=this.axMapControl1.TrackCircle();IFeatureLayerpFeatureLayer=this.axMapControl1.get_Layer(this.mLayerIndex)asIFeatureLayer;DataTablepDataTable=this.LoadQueryResult(this.axMapControl1,pFeatureLayer,pGeometry);this.dataGridView1.DataSource=pDataTable.DefaultView;this.dataGridView1.Refresh();break;default:break; 最后在“关闭”按钮函数下写入panel.Visible=false;至此,我们完成了空间查询代码的编写,运行效果如下:2缓冲区查询我们在使用缓冲区分析时,需要设定原始的图层,缓冲半径以及生成缓冲区的保存路径。添加控件打开项目ZZUMap,在ZZUMap的主菜单添加一个新的菜单项“分析”,并添加子菜单“缓冲区分析”,Name属性修改为“menuBuffer”。项目中添加一个新的窗体,名称为“BufferForm”,Name属性设为“缓冲区分析”,添加四个Label、一个ComboBox、两个TextBox、三个Button控件,控件属性设置如下:控件类型Name属性Text属性控件说明Label选择图层:Label缓冲半径:LabellblUnit地图单位标示当前地图的地图单位Label输出图层:ComboBoxcboLayers所有图层的名称TextBoxtxtBufferDistance1.0生成缓冲区的缓冲半径TextBoxtxtOutputPath缓冲区文件的输出路径,其ReadOnly属性设为TrueButtonbtnOutputLayer选择缓冲区文件的输出路径ButtonbtnBuffer分析进行缓冲区分析ButtonbtnCancel取消取消 该项目需添加如下引用: usingESRI.ArcGIS.Controls; usingESRI.ArcGIS.Geoprocessor; usingESRI.ArcGIS.Carto; usingESRI.ArcGIS.Geoprocessing; usingESRI.ArcGIS.esriSystem; 首先声明两个成员变量,用于保存地图数据和输出文件的路径。/接收MapControl中的数据privateIHookHelpermHookHelper=newHookHelperClass();/缓冲区文件输出路径publicstringstrOutputPath; 重写BufferForm的构造函数,添加一个参数,用于接收MapControl中的数据。/重写构造函数,添加参数hook,用于传入MapControl中的数据publicBufferForm(objecthook)InitializeComponent();this.mHookHelper.Hook=hook; 添加一个自定义函数,用于根据图层名称获取要素图层并返回。privateIFeatureLayerGetFeatureLayer(stringlayerName)IFeatureLayerpFeatureLayer=null;/遍历图层,获取与名称匹配的图层for(inti=0;ithis.mHookHelper.FocusMap.LayerCount;i+)ILayerpLayer=this.mHookHelper.FocusMap.get_Layer(i);if(pLayer.Name=layerName)pFeatureLayer=pLayerasIFeatureLayer;if(pFeatureLayer!=null)returnpFeatureLayer;elsereturnnull; BufferForm在载入时需要加载当前MapControl中的图层名称到cboLayers,读取当前地图的地图单位,设置缓冲区文件的默认输出路径,这里我们将默认输出路径设为“D:Temp”。privatevoidBufferForm_Load(objectsender,EventArgse)/传入数据为空时返回if(null=mHookHelper|null=mHookHelper.Hook|0=mHookHelper.FocusMap.LayerCount)return;/获取图层名称并加入cboLayersfor(inti=0;i0)cboLayers.SelectedIndex=0;/设置生成文件的默认输出路径和名称stringtempDir=D:Temp;txtOutputPath.Text=System.IO.Path.Combine(tempDir,(string)cboLayers.SelectedItem+_buffer.shp);/设置默认地图单位lblUnits.Text=Convert.ToString(mHookHelper.FocusMap.MapUnits); 双击路径设置按钮,进入代码编辑界面,添加如下代码:privatevoidbtnOutputLayer_Click(objectsender,EventArgse)/定义输出文件路径SaveFileDialogsaveDlg=newSaveFileDialog();/检查路径是否存在saveDlg.CheckPathExists=true;saveDlg.Filter=Shapefile(*.shp)|*.shp;/保存时覆盖同名文件saveDlg.OverwritePrompt=true;saveDlg.Title=输出路径;/对话框关闭前还原当前目录saveDlg.RestoreDirectory=true;saveDlg.FileName=(string)cboLayers.SelectedItem+_buffer.shp;/读取文件输出路径到txtOutputPathDialogResultdr=saveDlg.ShowDialog();if(dr=DialogResult.OK)txtOutputPath.Text=saveDlg.FileName; 双击“分析”按钮,添加代码如下:privatevoidbtnBuffer_Click(objectsender,EventArgse)/缓冲距离doublebufferDistance;/输入的缓冲距离转换为doubledouble.TryParse(txtBufferDistance.Text.ToString(),outbufferDistance);/判断输出路径是否合法if(!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(txtOutputPath.Text)|.shp!=System.IO.Path.GetExtension(txtOutputPath.Text)MessageBox.Show(输出路径错误!);return;/判断图层个数if(mHookHelper.FocusMap.LayerCount=0)return;/获取图层IFeatureLayerpFeatureLayer=GetFeatureLayer(string)cboLayers.SelectedItem);if(null=pFeatureLayer)MessageBox.Show(图层+(string)cboLayers.SelectedItem+不存在!rn);return;/获取一个geoprocessor的实例Geoprocessorgp=newGeoprocessor();/OverwriteOutput为真时,

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论