AE开发实例代码总结_第1页
AE开发实例代码总结_第2页
AE开发实例代码总结_第3页
AE开发实例代码总结_第4页
AE开发实例代码总结_第5页
已阅读5页,还剩19页未读, 继续免费阅读

下载本文档

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

文档简介

-.z.1、AE开发技术文档一、数据加载问题1、加载个人数据库个人数据库是保存在Access中的数据库。加载方式有两种:通过名字和通过属性加载〔也许不只是这两种,AE中实现同一功能可以有多种方式〕A、通过设置属性加载个人数据库首先通过IPropertySet接口定义要连接数据库的一些相关属性,在个人数据库中为数据库的路径,例如:IPropertySetPropset=newPropertySetClass();Propset.SetProperty(“DATABASE〞,〞D:\test\Ao\data\sh\Mapdata.mdb〞);当定义完属性并设置属性后就可以进展翻开数据库的操作了,在ArcEngine开发中存在IWorkspaceFactory、IFeatureWorkspace、IFeatureClass、IFeatureLayer等几个常用的用于翻开和操作数据空间地物的接口。IWorkspaceFactory是一个用于创立和翻开工作空间的接口,它是一个抽象的接口,我们在具体应用时要用对应的工作空间实例化它,如下:IWorkspaceFactoryFact=newAccessWorkspaceFactoryClass();如果我们翻开的是SDE数据库就要用SdeWorkspaceFactoryClass实例化Fact。当我们完成了工作空间的实例化后就可以根据上边设置的属性翻开对应的Access数据库了。翻开方式如下:IFeatureWorkspaceWorkspace=Fact.Open(Propset,0)asIFeatureWorkspace;翻开Access工作空间后接下来的事情是做什么了,很简单,找到对应的地物类,赋给相应的层,通过MapControl控件添加对应的层,然后刷新地图。以下为添加*一层的代码:IFeatureClassFcls=Workspace.OpenFeatureClass("District");//找到对应的地物类IFeatureLayerFly=newFeatureLayerClass();//建立新图层Fly.FeatureClass=Fcls;//将地物赋给相应的层MapCtr.Map.AddLayer(Fly);//添加层MapCtr.ActiveView.Refresh();//刷新地图其中District为地物类的名字,MapCtr为AE中MapControl的对象。上边的通过属性设置加载数据空间的方式还可以用于SDE数据库,在SDE数据库加载时会介绍。以下为通过设置属性加载Access数据库的完整C*代码:publicvoidAddAccessDBByPro(){IPropertySetPropset=newPropertySetClass();Propset.SetProperty("DATABASE","D:\test\Ao\data\sh\MapData.mdb");IWorkspaceFactoryFact=newAccessWorkspaceFactoryClass();IFeatureWorkspaceWorkspace=Fact.Open(Propset,0)asIFeatureWorkspace;IFeatureClassFcls=Workspace.OpenFeatureClass("District");IFeatureLayerFly=newFeatureLayerClass();Fly.FeatureClass=Fcls;MapCtr.Map.AddLayer(Fly);MapCtr.ActiveView.Refresh();}B、通过数据库名字加载个人数据库publicvoidAddAccessDBByName〔〕{IworkspaceNamepWorkspaceName=newWorkspaceNameClass〔〕;pWorkspaceName.WorkspaceFactoryProgID=“esriDataSourcesGDB.AccessWorkspaceFactory〞;pWorkspaceName.PathName=“D:\test\Ao\data\sh\MapData.mdb〞;INamen=pWorkspaceNameasIName;IFeatureWorkspaceWorkspace=n.Open()asIFeatureWorkspace;IFeatureClassFcls=Workspace.OpenFeatureClass("District");IFeatureLayerFly=newFeatureLayerClass();Fly.FeatureClass=Fcls;MapCtr.Map.AddLayer(Fly);MapCtr.ActiveView.Refresh();}首先创立一个个人数据库工作空间名,再指定工作空间名的ProgID,以确定翻开的是什么类型的工作空间,例如在翻开Access个人数据库时,使用下面代码;IworkspaceNamepWorkspaceName=newWorkspaceNameClass〔〕;pWorkspaceName.WorkspaceFactoryProgID=“esriDataSourcesGDB.AccessWorkspaceFactory〞;pWorkspaceName.PathName=“D:\test\Ao\data\sh\MapData.mdb〞;属性WorkspaceFactoryProgID可以确保工作空间是AccessWorkspaceFactory,即个人数据库,同时要指定翻开数据库路径。为了翻开数据库,翻开工作空间必须使用IName接口〔思考有没有其他方法〕,,所以接着定义IName、对象,并把工作空间名转换成IName类型,并赋值给IName对象,然后通过IName对象的open方法翻开相应的工作空间,代码如下:INamen=pWorkspaceNameasIName;IFeatureWorkspaceWorkspace=n.Open()asIFeatureWorkspace;2、AE开发编辑功能数据编辑问题需要了解的概念长事务短事务编辑空间抽象类,类,和组件对象类2、最根本的添加点线面功能添加点〔方法有多种,根本思路一样,只是少量的接口有变化〕通过FeatureClass的CreatFeature函数来添加地物。publicvoidAddPointByStore〔〕{//得到要添加地物的图层IFeatureLayerl=MapCtr.Map.get_Layer(0)asIFeatureLayer;//定义一个地物类,把要编辑的图层转化为定义的地物类IFeatureClassfc=l.FeatureClass;//先定义一个编辑的工作空间,然后把它转化为数据集,最后转化为编辑工作空间,IWorkspaceEditw=(fcasIDataset).WorkspaceasIWorkspaceEdit;IFeaturef;IPointp;//开场事务操作w.StartEditing(false);//////""""""""""""""""""""""""//开场编辑w.StartEditOperation();for(inti=0;i<100;i++){//创立一个地物f=fc.CreateFeature();p=newPointClass();//设置点的坐标p.PutCoord(i,i);//确定图形类型f.Shape=p;//保存地物f.Store()}//完毕编辑w.StopEditOperation();//完毕事务操作w.StopEditing(true);}二、添加线方法跟添加点一样,不同的只是地物类型不一样而已,AE中各类库的介绍问题一:什么是UI组件?类库介绍空间拓扑运算1、高级几何对象:点〔point〕、多点〔multipoint〕、线〔polyline〕、面〔polygon〕、multipatch等。〔multipatch:多片,三维地形图中的地图符号包括点状符号、线状符号、面状符号、体状符号以及多片〔MultiPatch〕符号等〕2、拓扑:空间拓扑描述的事自然界地理对象的空间位置关系,即相邻、重合、连通等。是在同一个要素集下的要素类之间的关系的集合,所以要参与一个拓扑的所有要素类,必须在同一个要素集〔也就是具有同一个空间参考〕,这样进展的拓扑检查才是准确的。3、一个要素集可以有多个拓扑,但每个要素类最多只能参与一个拓扑,一个拓扑中可以定义多个规则,是地理对象空间属性的一局部,在目前ESRI提供的数据存储方式中,Coverage和GeoDatabase能够建立拓扑,而Shape格式的数据不能建立拓扑。esriTRTAny————任何拓扑规则,查询拓扑的时候用〔ESRI提供的拓扑关系〕5、AE中提供了ITopologicalOperator接口用于拓扑运算,ITopologicalOperator接口用来通过对已存在的几何对象做空间拓扑运算,以产生新的集合对象。ITopologicalOperator接口在GIS开发中的使用非常广泛,通常GIS系统中的缓冲区分析、裁剪几何图形、几何图形差分操作、几何图形合并操作等都需要此接口。Boundary方法用于几何图形对象的边界。Polygon的几何对象的Boundary是组成它的Polyline几何对象,Polyline几何对象的Boundary是组成它的顶点Point几何对象,而Point的几何对象的Boundary是空对象。Buffer用于集合对象缓冲区分析拓扑操作,该方法用于给一个高级几何对象产生一个缓冲区,无论是Polygon、Polyline、还是Point,他们的缓冲区都是具有面积的几何对象。Clip用于对几何对象进展裁剪空间拓扑操作。Clip方法可用一个Envelope对象对一个几何对象进展裁剪,结果是几何对象被Envelope对象所包围的局部。ConstructUion用于将多个枚举对象与单个几何对象合并为单个几何对象。Conve*Hull用于构建几何对象的凸多边形。Conve*Hull方法用于产生一个几何图形的最小的边框凸多边形〔没有凹面包含几何图形的最小多边形〕Cut用于切割几何对象,不支持GeometryBags几何对象,它可以指定一条切割曲线和一个几何图形,经过切割运算后把几何图形分为左右两局部,左右两局部是相对曲线的方向而言的。点与多点不可被分割。Polyline和Polygon只有与切割曲线相交时才能执行Cut方法。Difference用于从一个几何图形中减去其与另一个图形相交的局部,产生两个几何对象的差集。Intersection方法用于两个同纬度几何对象的交集局部。Simplify用于使几何对象拓扑一致。SymmetricDifference对称差分可将两个几何图形的并集局部减去两个几何图形交集的局部。开发实例鹰眼功能的实现:分析:鹰眼图的操作主要由以下几个动作,当在一个控件中移动一幅图的时候另一控件中的图也发生变化,当在主控件中重新加载一幅图的时候,另外一个控件的图也发生相应的变化,同时我们在鸟瞰的控件中参加一红色边框,注意这个其实是一个面,只是填充的颜色是透明的而已。通过分析我们知道,我们要添加两个MapControl控件,名字分别是a*MapControl1和a*MapControl2,其中a*MapControl1为主图,而a*MapControl2为鸟瞰图。方法:步骤一:对于名称为a*MapControl1的MapControl控件,只需要在a*MapControl1的OnE*tentUpdated和OnMapReplaced中分别添加以下代码:privatevoida*MapControl1_OnE*tentUpdated(objectsender,IMapControlEvents2_OnE*tentUpdatedEvente){//得到新围IEnvelopepEnvelope=(IEnvelope)e.newEnvelope;IGraphicsContainerpGraphicsContainer=a*MapControl2.MapasIGraphicsContainer;IActiveViewpActiveView=pGraphicsContainerasIActiveView;//在绘制前,去除a*MapControl2中的任何图形元素pGraphicsContainer.DeleteAllElements();IRectangleElementpRectangleEle=newRectangleElementClass();IElementpElement=pRectangleEleasIElement;pElement.Geometry=pEnvelope;//设置鹰眼图中的红线框IRgbColorpColor=newRgbColorClass();pColor.Red=255;pColor.Green=0;pColor.Blue=0;pColor.Transparency=255;//产生一个线符号对象ILineSymbolpOutline=newSimpleLineSymbolClass();pOutline.Width=3;的属性IFillSymbolpFillSymbol=newSimpleFillSymbolClass();pFillSymbol.Color=pColor;pFillSymbol.Outline=pOutline;IFillShapeElementpFillShapeEle=pElementasIFillShapeElement;pFillShapeEle.Symbol=pFillSymbol;pGraphicsContainer.AddElement((IElement)pFillShapeEle,0);pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,null,null);}privatevoida*MapControl1_OnMapReplaced(objectsender,IMapControlEvents2_OnMapReplacedEvente){if(a*MapControl1.LayerCount>0){a*MapControl2.Map=newMapClass();for(inti=0;i<=a*MapControl1.Map.LayerCount-1;i++){a*MapControl2.AddLayer(a*MapControl1.get_Layer(i));}a*MapControl2.E*tent=a*MapControl1.E*tent;a*MapControl2.Refresh();}}步骤二:对于名称为a*MapControl2的MapControl控件,只需要在a*MapControl2的OnMouseMove和OnMouseDown中分别添加以下代码:privatevoida*MapControl2_OnMouseMove(objectsender,IMapControlEvents2_OnMouseMoveEvente){if(e.button==1){IPointpPoint=newPointClass();pPoint.PutCoords(e.map*,e.mapY);a*MapControl1.CenterAt(pPoint);a*MapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography,null,null);}}privatevoida*MapControl2_OnMouseDown(objectsender,IMapControlEvents2_OnMouseDownEvente){if(a*MapControl2.Map.LayerCount>0){if(e.button==1){IPointpPoint=newPointClass();pPoint.PutCoords(e.map*,e.mapY);a*MapControl1.CenterAt(pPoint);a*MapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography,null,null);}elseif(e.button==2){IEnvelopepEnv=a*MapControl2.TrackRectangle();a*MapControl1.E*tent=pEnv;a*MapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography,null,null);}}}显示属性表的信息我们知道ArcMap中的TableofContents有很多功能,如下列图:而ArcGISEngine提供的TOCControl控件几乎没有提供,则这些都是需要自己开发的,在这里我做一个显示属性表的功能。分析:要显示*一个图层的属性表,首先要将这个图层选中,然后在另外一个Form中将选中的这个图层的属性信息进展显示。方法:添加一个上下文菜单,添加一个新的Form窗体,在这个新的窗体上添加GridView控件,并在TOCControl控件的OnMouseDown事件下添加如下代码〔pGlobalFeatureLayer是我定义的一个全局变量〕:privatevoida*TOCControl1_OnMouseDown(objectsender,ESRI.ArcGIS.Controls.ITOCControlEvents_OnMouseDownEvente){if(a*MapControl1.LayerCount>0){esriTOCControlItempItem=newesriTOCControlItem();pGlobalFeatureLayer=newFeatureLayerClass();IBasicMappBasicMap=newMapClass();objectpOther=newobject();objectpInde*=newobject();a*TOCControl1.HitTest(e.*,e.y,refpItem,refpBasicMap,refpGlobalFeatureLayer,refpOther,refpInde*);}if(e.button==2){conte*t.Show(a*TOCControl1,e.*,e.y);}}在上下文菜单的翻开属性表的Click事件中添加如下代码:privatevoid翻开属性表ToolStripMenuItem_Click(objectsender,EventArgse){FormTableFt=newFormTable(pGlobalFeatureLayerasIFeatureLayer);Ft.Show();}在新的窗体中添加一个将属性表显示到GridView控件中的函数,如下:publicvoidItable2Dtable(){IFieldspFields;pFields=pFeatureLayer.FeatureClass.Fields;dtGridView.ColumnCount=pFields.FieldCount;for(inti=0;i<pFields.FieldCount;i++){stringfldName=pFields.get_Field(i).Name;dtGridView.Columns[i].Name=fldName;dtGridView.Columns[i].ValueType=System.Type.GetType(ParseFieldType(pFields.get_Field(i).Type));}IFeatureCursorpFeatureCursor;pFeatureCursor=pFeatureLayer.FeatureClass.Search(null,false);IFeaturepFeature;pFeature=pFeatureCursor.Ne*tFeature();while(pFeature!=null){string[]fldValue=newstring[pFields.FieldCount];for(inti=0;i<pFields.FieldCount;i++){stringfldName;fldName=pFields.get_Field(i).Name;if(fldName==pFeatureLayer.FeatureClass.ShapeFieldName){fldValue[i]=Convert.ToString(pFeature.Shape.GeometryType);}elsefldValue[i]=Convert.ToString(pFeature.get_Value(i));}dtGridView.Rows.Add(fldValue);pFeature=pFeatureCursor.Ne*tFeature();}}数据库知识:DB2DB2、Informi*、PostgreSQL数据库翻开方式以及方法:翻开个人数据库:publicIWorkspaceGetMDBWorkspace(String_pGDBName){IWorkspaceFactorypWsFac=newAccessWorkspaceFactoryClass();IWorkspacepWs=pWsFac.OpenFromFile(_pGDBName,0);returnpWs;}翻开文件数据库:publicIWorkspaceGetFGDBWorkspace(String_pGDBName){IWorkspaceFactorypWsFac=newFileGDBWorkspaceFactoryClass();IWorkspacepWs=pWsFac.OpenFromFile(_pGDBName,0);returnpWs;}翻开SDE数据库:翻开SDE数据库我们使用的是Open方法,要用这个方法,我们就要对IPropertySet对象设置,要翻开SDE数据库,我们要获取SDE数据库的效劳器地址,数据库实例,数据库,用户,密码等参数。而IPropertySet就好比一个Key-Value的对象,用来帮组我们设置这些,然后传到Open方法中。publicIWorkspaceGetSDEWorkspace(String_pServerIP,String_pInstance,String_pUser,String_pPassword,String_pDatabase,String_pVersion){ESRI.ArcGIS.esriSystem.IPropertySetpPropertySet=newESRI.ArcGIS.esriSystem.PropertySetClass();pPropertySet.SetProperty("SERVER",_pServerIP);pPropertySet.SetProperty("INSTANCE",_pInstance);pPropertySet.SetProperty("DATABASE",_pDatabase);pPropertySet.SetProperty("USER",_pUser);pPropertySet.SetProperty("PASSWORD",_pPassword);pPropertySet.SetProperty("VERSION",_pVersion);ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2workspaceFactory;workspaceFactory=(ESRI.ArcGIS.Geodatabase.IWorkspaceFactory2)newESRI.ArcGIS.DataSourcesGDB.SdeWorkspaceFactoryClass();returnworkspaceFactory.Open(pPropertySet,0);}注意,翻开SDE数据库需要用到ArcGISEngine运行时的企业级许可。〔即此时的License需要特别设置——ArcEngine的Lisence应使用企业数据库类型,在From1_Load事件中初始化lisence,不能使用lisence控件设置,否则提示没有许可lisence。〕具体的解决代码如下:privatevoidFrom1_Load(objectsender,EventArge){IAoInitializepao=newAoInitializeClass();pao.Initialize(esriLisenceProductCode.esriLisenceProductCodeEngineGeoDB);}4.4.2获取数据库中的要素类在ArcGISEngine中,要得到*一个类,首要要获取工作空间,然后进入工作空间再得到相应的东西,也就是以下两个步骤:获取工作空间;获取相应的要素类。我们定义一个函数用来获取个人数据库的路径publicstringWsPath(){stringWsFileName="";OpenFileDialogOpenFile=newOpenFileDialog();OpenFile.Filter="个人数据库(MDB)|*.mdb";DialogResultDialogR=OpenFile.ShowDialog();if(DialogR==DialogResult.Cancel){}else{WsFileName=OpenFile.FileName;}returnWsFileName;}要获取要素类,首先获取工作空间,然后对工作空间中的要素类进展遍历,代码如下:privatevoidbutton2_Click(objectsender,EventArgse){stringWsName=WsPath();if(WsName!=""){IWorkspaceFactorypWsFt=newAccessWorkspaceFactoryClass();IWorkspacepWs=pWsFt.OpenFromFile(WsName,0);IEnumDatasetpEDataset=pWs.get_Datasets(esriDatasetType.esriDTAny);IDatasetpDataset=pEDataset.Ne*t();while(pDataset!=null){if(pDataset.Type==esriDatasetType.esriDTFeatureClass){FeatureClassBo*.Items.Add(pDataset.Name);}//如果是数据集elseif(pDataset.Type==esriDatasetType.esriDTFeatureDataset){IEnumDatasetpESubDataset=pDataset.Subsets;IDatasetpSubDataset=pESubDataset.Ne*t();while(pSubDataset!=null){FeatureClassBo*.Items.Add(pSubDataset.Name);//----这是从哪里来的???------------pSubDataset=pESubDataset.Ne*t();}}pDataset=pEDataset.Ne*t();}}FeatureClassBo*.Te*t=FeatureClassBo*.Items[0].ToString();}判断要素是否被编辑:ArcGISEngine提供了一个IDatasetEdit的接口用来判断我们的数据是否处于编辑状态,该接口只有一个方法,如下:例如代码如下:publicboolISEdit(IFeatureClasspFeatureClass){IDatasetEditpDataEdit=pFeatureClassasIDatasetEdit;returnpDataEdit.IsBeingEdited();}如何删除要素类:IFeatureWorkspace这个接口主要是用于管理基于矢量数据的,如表,要素类,要素数据集等。要想删除一个要素类,则必须先得到这个。,如要翻开一个名称为PointTest的要素类,只需要在OpenFeatureClass中传入这个要素类的名称,代码如下:IWorkspaceFactorypWsFt=newAccessWorkspaceFactoryClass();IWorkspacepWs=pWsFt.OpenFromFile(WsName,0);IFeatureWorkspacepFWs=pWsasIFeatureWorkspace;IFeatureClasspFClass=pFWs.OpenFeatureClass("PointTest");如果是在ArcMap中,我们会切换到Catalog中然后进入相应的数据库,然后删除相应的要素类,这种操作会让我们想到FeatureClas这个对象会提供删除的方法,其实不然,这个删除的方法是定义在Dataset这个对象中。privatevoidbutton1_Click(objectsender,EventArgse){stringWsName=WsPath();if(WsName!=""){IWorkspaceFactorypWsFt=newAccessWorkspaceFactoryClass();IWorkspacepWs=pWsFt.OpenFromFile(WsName,0);IFeatureWorkspacepFWs=pWsasIFeatureWorkspace;IFeatureClasspFClass=pFWs.OpenFeatureClass("PointTest");IDatasetpDatset=pFClassasIDataset;pDatset.Delete();}}删除前:删除后:创立要素类创立要素类用到了IFeatureWorkspace.CreateFeatureClass方法。所需接口:IField,IFieldEdit,IFields,IFieldsEdit,IGeometryDef,IGeometryDefEdit接口〔注意在NET中,会遇到以“_2〞结尾的属性,这些属性是可写的。〕//定义一个几何字段,类型为点类型ISpatialReferencepSpatialReference=a*MapControl1.ActiveView.FocusMap.SpatialReference;IGeometryDefEditpGeoDef=newGeometryDefClass();IGeometryDefEditpGeoDefEdit=pGeoDefasIGeometryDefEdit;pGeoDefEdit.GeometryType_2=esriGeometryType.esriGeometryPoint;pGeoDefEdit.SpatialReference_2=pSpatialReference;//定义一个字段集合对象IFieldspFields=newFieldsClass();IFieldsEditpFieldsEdit=(IFieldsEdit)pFields;//定义单个的字段IFieldpField=newFieldClass();IFieldEditpFieldEdit=(IFieldEdit)pField;pFieldEdit.Name_2="SHAPE";pFieldEdit.Type_2=esriFieldType.esriFieldTypeGeometry;pFieldsEdit.AddField(pField);pFieldEdit.GeometryDef_2=pGeoDef;//定义单个的字段,并添加到字段集合中pField=newFieldClass();pFieldEdit=(IFieldEdit)pField;pFieldEdit.Name_2="STCD";pFieldEdit.Type_2=esriFieldType.esriFieldTypeString;pFieldsEdit.AddField(pField);//定义单个的字段,并添加到字段集合中pField=newFieldClass();pFieldEdit=(IFieldEdit)pField;pFieldEdit.Name_2="SLM10";pFieldEdit.Type_2=esriFieldType.esriFieldTypeString;pFieldsEdit.AddField(pField);//定义单个的字段,并添加到字段集合中pField=newFieldClass();pFieldEdit=(IFieldEdit)pField;pFieldEdit.Name_2="SLM20";pFieldEdit.Type_2=esriFieldType.esriFieldTypeString;pFieldsEdit.AddField(pField);//定义单个的字段,并添加到字段集合中pField=newFieldClass();pFieldEdit=(IFieldEdit)pField;pFieldEdit.Name_2="SLM40";pFieldEdit.Type_2=esriFieldType.esriFieldTypeString;pFieldsEdit.AddField(pField);IWorkspaceFactorypFtWsFct=newAccessWorkspaceFactory();IFeatureWorkspacepWs=pFtWsFct.OpenFromFile("E:\arcgis\Engine\s.mdb",0)asIFeatureWorkspace;IFeatureClasspFtClass=pWs.CreateFeatureClass("Test",pFields,null,null,esriFeatureType.esriFTSimple,"SHAPE",null)——————//////"""""""""""""如何改变字段的别名?publicvoidChangeFieldAliasName(ITablepTable,stringpOriFieldName,stringpDesFieldName){IClassSchemaEditpClassSchemaEdit=(IClassSchemaEdit)pTable;//给对象加上锁ISchemaLockpSchemaLock=(ISchemaLock)pTable;pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriE*clusiveSchemaLock);if(pTable.FindField(pOriFieldName)!=-1){pClassSchemaEdit.AlterFieldAliasName(pOriFieldName,pDesFieldName);pSchemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);使用IFeatureSelection接口高亮显示在介绍IMap接口那一节,我们用IMap的IMap.SelectFeature方法实现了对查询的要素高亮显示,现在我们用IFeatureSelection接口实现查询高亮显示IMappMap=a*MapControl1.Map;IFeatureLayerpFeaturelayer=GetLayer(pMap,"Roads")asIFeatureLayer;IFeatureSelectionpFeatureSelection=pFeaturelayerasIFeatureSelection;IQueryFilterpQuery=newQueryFilterClass();pQuery.WhereClause="TYPE="+"'paved'";pFeatureSelection.SelectFeatures(pQuery,esriSelectionResultEnum.esriSelectionResultNew,false);a*MapControl1.ActiveView.Refresh();其中GetLayer函数是我们写的一个根据图层的名称获取图层的方法,代码如下privateILayerGetLayer(IMappMap,stringLayerName){IEnumLayerpEnunLayer;pEnunLayer=pMap.get_Layers(null,false);pEnunLayer.Reset();ILayerpRetureLayer;pRetureLayer=pEnunLayer.Ne*t();while(pRetureLayer!=null){if(pRetureLayer.Name==LayerName){break;}pRetureLayer=pEnunLayer.Ne*t();}returnpRetureLayer;}提问:以下三种方式的区别在哪里?a*MapControl1.Refresh(esriViewDrawPhase.esriViewGeoSelection,null,null);a*MapControl1.ActiveView.Refresh();a*MapControl1.Refresh();创立符合要求的表:publicITableCreateTable(string_TablePath,string_TableName){IWorkspaceFactorypWks=newShapefileWorkspaceFactoryClass();IFeatureWorkspacepFwk=pWks.OpenFromFile(_TablePath,0)asIFeatureWorkspace;//用于记录面中的ID;IFieldpFieldID=newFieldClass();IFieldEditpFieldIID=pFieldIDasIFieldEdit;pFieldIID.Type_2=esriFieldType.esriFieldTypeInteger;pFieldIID.Name_2="面ID";//用于记录个数的;IFieldpFieldCount=newFieldClass();IFieldEditpFieldICount=pFieldCountasIFieldEdit;pFieldICount.Type_2=esriFieldType.esriFieldTypeInteger;pFieldICount.Name_2="个数";//用于添加表中的必要字段ESRI.ArcGIS.Geodatabase.IObjectClassDescriptionobjectClassDescription=newESRI.ArcGIS.Geodatabase.ObjectClassDescriptionClass();IFieldspTableFields=objectClassDescription.RequiredFields;IFieldsEditpTableFieldsEdit=pTableFieldsasIFieldsEdit;pTableFieldsEdit.AddField(pFieldID);pTableFieldsEdit.AddField(pFieldCount);ITablepTable=pFwk.CreateTable(_TableName,pTableFields,null,null,"");returnpTable;}统计需要的数据:///<summary>///第一个参数为面数据,第二个参数为点数据,第三个为输出的表///</summary>///<paramname="_pPolygonFClass"></param>///<paramname="_pPointFClass"></param>///<paramname="_pTable"></param>publicvoidStatisticPointCount(IFeatureClass_pPolygonFClass,IFeatureClass_pPointFClass,ITable_pTable){IFeatureCursorpPolyCursor=_pPolygonFClass.Search(null,false);IFeaturepPolyFeature=pPolyCursor.Ne*tFeature();while(pPolyFeature!=null){IGeometrypPolGeo=pPolyFeature.Shape;intCount=0;ISpatialFilterspatialFilter=newSpatialFilterClass();spatialFilter.Geometry=pPolGeo;spatialFilter.SpatialRel=esriSpatialRelEnum.esriSpatialRelContains;IFeatureCursorpPointCur=_pPointFClass.Search(spatialFilter,false);if(pPointCur!=null){IFeaturepPointFeature=pPointCur.Ne*tFeature();while(pPointFeature!=null){pPointFeature=pPointCur.Ne*tFeature();Count++;}}if(Count!=0){IRowpRow=_pTable.CreateRow();pRow.set_Value(1,pPolyFeature.get_Value(0));pRow.set_Value(2,Count);pRow.Store();}pPolyFeature=pPolyCursor.Ne*tFeature();}}效果如下:上面这个例子只是用了空间过滤,没有用到属性过滤,我们将上面的代码稍微改动下,加上一句代码即可,结果对照:使用查询图层快速浏览:我们可以按照以下步骤来执行一个QueryLayer。1〕获取SqlWorkspaceFactory2〕获取SqlWorkspace3〕构造查询语句4〕执行查询5〕获取结果publicIFeatureLayerOracleQueryLayer(){//创立SqlWorkspaceFactory的对象TypepFactoryType=Type.GetTypeFromProgID("esriDataSourcesGDB.SqlWorkspaceFactory");IWorkspaceFactorypWorkspaceFactory=(IWorkspaceFactory)Activator.CreateInstance(pFactoryType);//构造连接数据库的参数IPropertySetpConnectionProps=newPropertySetClass();pConnectionProps.SetProperty("dbclient","Oracle11g");pConnectionProps.SetProperty("serverinstance","esri");pConnectionProps.SetProperty("authentication_mode","DBMS");pConnectionProps.SetProperty("user","scott");pConnectionProps.SetProperty("password","arcgis");//翻开工作空间IWorkspaceworkspace=pWorkspaceFactory.Open(pConnectionProps,0);ISqlWorkspacepSQLWorkspace=workspaceasISqlWorkspace;//获取数据库中的所有表的名称IStringArraypStringArray=pSQLWorkspace.GetTables();for(inti=0;i<pStringArray.Count;i++){MessageBo*.Show(pStringArray.get_Element(i));}//构造过滤条件SELECT*FROMPointQueryLayerIQueryDescriptionqueryDescription=pSQLWorkspace.GetQueryDescription("SELECT*FROMPointQueryLayer");ITablepTable=pSQLWorkspace.OpenQueryClass("QueryLayerTest",queryDescription);IFeatureLayerpFeatureLayer=newFeatureLayerClass();pFeatureLayer.FeatureClass=pTableasIFeatureClass;returnpFeatureLayer;}点几何对象:Point是一个0维的几何图形,具有*,Y坐标值,以及一些可选的属性:如高程值(Z值),度量值(M值)M这个属性在线性参考和动态分段中经常用到和ID号,点对象用于描述准确定位的对象。同时,点对象还可以有Z和M两个可选属性,。以下代码演示如何创立一个Point对象:///<summary>///获取点///</summary>///<paramname="*"></param>///<paramname="y"></param>///<returns></returns>privateIPointConstructPoint(double*,doubley){IPointpPoint=newPointClass();pPoint.PutCoords(*,y);returnpPoint;}MultiPoint对象是一系列无序的点的群集,这些点具有一样的属性信息。例如可以用一个点集来表示整个城市天然气调压站。如下列图所示:一个Multipoint对象由8个Point对象组成。以下代码片段演示如何构建Multipoint对象:privateobjectpMissing=Type.Missing;publicIGeometryGetMultipointGeometry(){constdoubleMultipointPointCount=25;IPointCollectionpPointCollection=newMultipointClass();for(inti=0;i<MultipointPointCount;i++){pPointCollection.AddPoint(GetPoint(),refpMissing,refpMissing);}returnpPointCollectionasIGeometry;}privateIPointGetPoint(){constdoubleMin=-10;constdoubleMa*=10;RandompRandom=newRandom();double*=Min+(Ma*-Min)*pRandom.Ne*tDouble();doubley=Min+(Ma*-Min)*pRandom.Ne*tDouble();returnConstructPoint(*,y);}Segment对象Segment对象是一个有起点和终点的“线“,也就是说Segement只有两个点,至于两点之间的线是直的,还是曲的,需要其余的参数定义。所以Segment是由起点,终点和参数三个方面决定的。Segment有4个子类,它的4个子类〔直线,圆弧,椭圆弧,贝赛尔曲线〕。ISegment有两个很有用的方法如下列图:这两个方法用于将该Segment进展分割成小的SegementPath几何对象Path是连续的Segment的集合,除了路径的第一个Segment和最后一个Segment外其余的Segment的起始点都是前一个Segment的终止点,即Path对象的中的Segment不能出现别离,Path可以是任意数的Segment子类的组合。该Path对象有很多我们经常用到的方法,如平滑曲线,对曲线抽稀等操作Ring几何对象Ring是一个封闭的Path即起始和终止点有一样的坐标值,它有部和外部属性。Polyline几何对象Polyline对象是由一个或多个相连或者不相连的path对象的有序集合,通常用来代表线状地物如道路,河流,管线等等.该对象在ArcGISEngine中的模型图如下:这并不意味着用户必须按照这种层次去构造polyline,实际上Point集合直接构成Polyline,组成Polyline的这些路径既可以是连续的,也可以是不连续的。Polyline是有序path组成的集合,可以拥有M、Z和ID属性值,Polyline对象的IPointCollection接口包含了所有的节点信息,IGeometryCollection接口可以获取polyline的paths,ISegmentCollection接口可以获取polyline的segments。一个Polyline对象必须满足以下准则:1.组成Polyline对象的所有Path对象必须是有效的。2.组成Polyline对象的所有Path对象不能重合,相交或自相交。3.组成Polyline对象的多个Path对象可以连接与*一点,也可以别离。4.Path对象的长度不能为0.IPolyline是Polyline类的主要接口,IPolyline的Reshape方法可以使用一个Path对象为一个Polyline对象整形,IPolyline的SimplifyNetwork方法用于简化网络。Polyline对象可以使用IGeometryCollection接口添加Path对象的方法来创立,使用该接口需注意以下情况:1.每一个Path对象必须是有效的,或使用IPath::Simplify方法后有效。2.由于Polyline是Path对象的有序集合,所以添加Path对象时必须注意顺序和方向。3.为了保证Polyline是有效的,可以创立完Polyline对象后使用ITopologicalOperator接口的Simplify方法。下面代码片段演示了一个Polyline的构成:privateobjectpMissing=Type.Missing;publicIGeometryGetPolylineGeometry(){constdoublePathCount=3;constdoublePathVerte*Count=3;IGeometryCollectionpGeometryCollection=newPolylineClass();for(inti=0;i<PathCount;i++){IPointCollectionpPointCollection=newPathClass();for(intj=0;j<PathVerte*Count;j++){pPointCollection.AddPoint(GetPoint(),refpMissing,refpMissing);}pGeometryCollection.AddGeometry(pPointCollectionasIGeometry,refpMissing,refpMissing);}returnpGeometryCollectionasIGeometry;}privateIPointGetPoint(){constdoubleMin=-10;constdoubleMa*=10;Randomrandom=newRandom();double*=Min+(Ma*-Min)*random.Ne*tDouble();doubley=Min+(Ma*-Min)*random.Ne*tDouble();returnConstructPoint(*,y);}Segment,Path,Ring和Polyline的区别在这四者当中Segment是最小的单位具体的构成路线可以分为两个条:Segment-Path-Ring〔封闭的Path〕Segment-Path-Polyline我们可以这样说Segment是Path,只不过是这个Path由一个Segment组成,Ring也是一种Path,只不过是一个起点和终点重合的Path,至于Polyline那就很明显了,他们的区别可以从下列图看出:Polygon对象Polylgon对象是由一个或多个Ring对象的有序集合,它可以是由单个Ring对象构成,也可以使用多个Ring组成。Polygon通常用来代表有面积的多边形矢量对象,如行政区,建筑物等。Polygon的组成构造可以看下列图:从这个图上可以看出Polygon是由Rings构成,而Ring又是又Segment构成,但是这并不意味着用户必须按照这种层次去构造Polygon,实际上用Point的集合可以构成Polygon.///<summary>///通过点构造面///</summary>///<paramname="pPointCollection"></param>///<returns></returns>publicIPolygonCreatePolygonByPoints(IPointCollectionpPointCollection){IGeometryBridge2pGeometryBridge2=newGeometryEnvironmentClass();IPointCollection4pPolygon=newPolygonClass();WKSPoint[]pWKSPoint=newWKSPoint[pPointCollection.PointCount];for(inti=0;i<pPointCollection.PointCount;i++){pWKSPoint[i].*=pPointCollection.get_Point(i).*;pWKSPoint[i].Y=pPointCollection.get_Point(i).Y;}pGeometryBridge2.SetWKSPoints(pPolygon,refpWKSPoint);IPolygonpPoly=pPolygonasIPolygon;pPoly.close();returnpPoly;}组成Polygon的是Ring,其中Ring可以分为OuterRing(外环)和InnerRing(环)之分。外环和环都是有方向的,它们的区别是外环的方向是顺时针的,环的方向是逆时针。Polygon对象实现了一个接口IArea,而该接口用来对Poylgon的中心,重心,以及面积进展访问,下面片段用来获取Polygoe的面积:IAreapArea=pPolygonasIArea;DoubleS=pArea.AreaIGeometryCollection接口通过IGeometryCollection创立一个Polygon对象的代码片段如下:privateIPolygonConstructorPolygon(List<IRing>pRingList){try{IGeometryCollectionpGCollection=newPolygonClass();objecto=Type.Missing;for(inti=0;i<pRingList.Count;i++){//通过IGeometryCollection接口的AddGeometry方法向Polygon对象中添加Ring子对象pGCollection.AddGeometry(pRingList[i],refo,refo);}//QI至ITopologicalOperatorITopologicalOperatorpTopological=pGCollectionasITopologicalOperator;//执行Simplify操作pTopological.Simplify();IPolygonpPolygon=pGCollectionasIPolygon;//返回Polygon对象returnpPolygon;}catch(E*ceptionErr){returnnull;}}privateIPolygonMergePolygons(IPolygonfirstPolygon,IPolygonSecondPolygon){try{//创立一个Polygon对象IGeometryCollectionpGCollection1=newPolygonClass();IGeometryCollectionpGCollection2=firstPolygonasIGeometryCollection;IGeometryCollectionpGCollection3=SecondPolygonasIGeometryCollection;//添加firstPolygonpGCollection1.AddGeometryCollection(pGCollection2);//添加SecondPolygonpGCollection1.AddGeometryC

温馨提示

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

评论

0/150

提交评论