• 1. ArcSDE编程基础——空间数据库原理与设计课程实践
  • 2. 内容简介 ArcObject简介 对象模型图分类介绍 如何连接SDE 如何获取数据 查询分析与编辑操作 简单的AO开发实例
  • 3. ArcGIS介绍ArcGIS概述 统一的地理信息系统平台 灵活性 信息技术标准 功能强大 ArcGIS由三个重要部分组成 ArcGIS 桌面软件 ,一个一体化的高级的GIS应用ArcSDE通路,一个用数据库管理系统管理空间数据库的接口ArcIMS软件,基于Internet的分布式数据和服务的GIS
  • 4. ArcGIS桌面软件 ArcView ArcEditor ArcInfo
  • 5. ArcSDE 是在数据库管理系统中存储和管理多用户空间数据库的通路。ArcGIS Desktop通过它来应用和管理Geodatabase数据。 ArcIMS 为ArcGIS系统增加了Internet地图服务能力 所有的ArcGIS桌面客户端可以通过Web从ArcIMS服务器动态地获取矢量数据流。
  • 6. ArcGIS桌面系统ArcMapArcCatalogArcToolBox
  • 7. ArcObject简介ArcObject是一套ArcGIS的可重用的通用的二次开发组件集ArcGISArcObject
  • 8. ArcObject发展历程1999年:ArcInfo8 2001年:ArcGIS8.1 2004年:ArcGIS9 ArcGIS Engine是嵌入式GIS产品 它将ArcObject中的一些组件单独打包出来,灵活性高,费用低 AO是指ArcGIS Desktop版本的组件集ArcObject,即ArcObject Desktop Development Kit(最完整版)
  • 9. ArcObject对象模型图ArcGIS9.0的Desktop版本中AO的枚举对象是647个,结构对象是35个 3073个接口,组件类2389个 一共是6144个对象,而且随着新版本的出现在不断增加
  • 10. (本页无文本内容)
  • 11. AO编程基础基于接口的编程,基础是COM COM(Component Object Model)不是接口也不是类,而是一种二进制基本的组件通信标准,因此它是语言独立的。COM组件间所有通讯都通过组件的接口实现,一旦一个接口被添加,就不能被删除。 什么是接口?为什么要使用接口? 过程、函数 类 接口
  • 12. 接口和类 接口是一种用来定义程序的协定。它可以包含方法、属性和事件,但它本身并不提供它所定义的成员的实现,而只是指定实现该接口的类或接口必须提供的成员。 接口可以看作是一个特殊的类形式,除了不能被实例化为一个对象外,它可以实现类能够完成的任何任务,如声明对象为某种接口类型,接口也可以继承等。
  • 13. 接口和类的关系DOGBarkGrowlColorCallPetNameIPetIDogClassProperties and MethodsInterfaces
  • 14. 对象模型图的标记组件类抽象类例化类继承关联组成BirdChickenNestFeatherWingEgg2*方法属性通过赋值来给对象属性分配一个值通过引用设置对象的属性
  • 15. 对象模型图中的类在OMD中有三类class,分别是抽象类(AbstractClass)、组件类(CoClass)和普通类(Class)。抽象类的主要目的是为它的子类定义公共接口,一个抽象类将把它的部分或全部实现延迟到子类中,因此,一个抽象类不能被实例化。一个组件类对象可以被直接创建,普通类对象虽然不能直接创建,但它可以作为其它类的一个属性或者从其它类的实例化来创建。Dim pFeatureClass As IFeatureClass Set pFeatureClass = pFeatureLayer.FeatureClass Dim pMap As IMap Set pMap = pMxDoc.FocusMap
  • 16. 类的实例化A和B为普通类对象,C为组件类对象,D为一普通类,如下面代码: A = B.writeA() A = C.writeA() A = New D X 普通类不能通过New方法产生 A的生命周期是由产生它的对象B和C控制的
  • 17. 类的继承接口类型继承,并非继承其实现
  • 18. 接口继承和调用父类接口所具有的方法和属性对派生接口都有效 IFeatureClass继承IClass接口 一个类同时支持这两个接口,则接口之间可相互调用 Inherit
  • 19. 类和接口的关系接口和类的关系 一个类可以支持多个接口,一个接口也可以由多个类实现。在接口中仅定义了接口需要实现的属性和方法,属性和方法的具体实现是在类中完成的。 查询接口:是指在同一个对象上使用附加的接口 Polygon类支持IPolygon和IArea接口,通过IPolygon接口中的Length属性可以得到其周长,通过IArea接口的Area属性可以得到其面积。 set pArea=New Polygon (X) set pArea=pPolygon (√)
  • 20. 连接SDE数据源Private Sub UIButtonControl1_Click() Dim pWS As IWorkspace Dim pPropSet As IPropertySet Dim pSdeFact As IWorkspaceFactory Set pPropSet = New PropertySet With pPropSet .SetProperty "SERVER", "legend" .SetProperty "INSTANCE", "esri_sde" .SetProperty "DATABASE", “sde" .SetProperty "USER", "zhj" .SetProperty "PASSWORD", "zhj" .SetProperty "VERSION", "SDE.DEFAULT" End With Set pSdeFact = New SdeWorkspaceFactory Set pWS = pSdeFact.Open(pPropSet, 0)   If Not pWS Is Nothing Then MsgBox "OK" Else MsgBox "NO" End If End Sub
  • 21. Getting a workspaceWorkspaceFactory Factories return Workspaces VBA 0 for hWnd Dim pWFactory As IWorkspaceFactory Dim pWork As IWorkspace Set pWFactory = New AccessWorkspaceFactory Set pWork = pWFactory.OpenFromFile("C:\data\CityData.mdb", 0)WorkspaceWorkspace FactoryShapefileWorkspaceFactoryArcInfoWorkspaceFactoryAccessWorkspaceFactoryArcSDEWorkspaceFactory
  • 22. Workspace种类ArcInfo workspaces For ArcInfo™ workspaces enter the full pathname to the workspace directory. MyFolder is a workspace that contains coverage directories and an INFO subdirectory. Set pWork = _ pArcInfoWFactory.OpenFromFile("C:\MyFolder", 0) Shapefile workspaces For shapefile workspaces, enter the folder that contains the shapefile as the workspace file and the full pathname to that folder. Set pWork = _ pShapefileWFactory.OpenFromFile("d:\abc\CityData", 0)
  • 23. Getting an ArcSDE workspaceArcSDEWorkspaceFactory Connection file (ArcSDE) or Connection properties Dim pSDEFactory As IWorkspaceFactory Dim pWork As IWorkspace Set pSDEFactory = New SDEWorkspaceFactory Set pWork = pSDEFactory.OpenFromFile _ ("C:\WINNT\Profiles\Student10\Application Data \ESRI\ArcCatalog\Redlands.SDE", 0)
  • 24. Getting a data setQI IFeatureWorkspace from IWorkspace OpenFeatureDataset returns IFeatureDataset Use the name of the data setDim pFWorkspace As IFeatureWorkspace Dim pFData As IFeatureDataset Set pFWorkspace = pWork ‘QI pFWorkspace from pWorkspace Set pFData = pFWorkspace.OpenFeatureDataset("Transportation")WorkspaceWorkspace FactoryData set
  • 25. Getting a feature classGet IFeatureClassContainer Use ClassByName Returns feature class Dim pFCC As IFeatureClassContainer Dim pFC As IFeatureClass Set pFCC = pFData Set pFC = pFCC.ClassByName("Streets")WorkspaceTableFeature class*IFeatureClassContainerIFeatureDatasetData setFeature data setGeodataset
  • 26. Instantiating a feature layerFeature layer is a CoClass Create a new one Set the feature layer’s feature class 'Make a new layer Dim pFLayer As IFeatureLayer Set pFLayer = New FeatureLayer 'Set the layer’s FeatureClass (streets) Set pFLayer.FeatureClass = pFCFeature classFeature layer
  • 27. Adding a layer to a mapGet feature class data set Use data set name as the new layer name Add layer to map This layer has name and source'Get the dataset Dim pDataset As IDataset Set pDataset = pFC 'Set the layer name to the dataset name pFLayer.Name = pDataset.Name 'Add the layer to the map pMap.AddLayer pFLayerMap*Layer
  • 28. 对象模型图分类介绍按照功能划分:地图加载:地图、图层、要素等地图编辑:要素的图形、属性查询分析:图层、要素等地图输出:Pagelayout地图显示:图层属性、要素属性等
  • 29. AO中的核心对象类MapLayerFeatureShapeFieldsGeometryElement
  • 30. 地图和图层LayerApplicationMap0 .. 1*MxDocumentFeatureLayerFeatureClassTableDataSetFeatureDataset*ArcMapMap LayerGeodatabase
  • 31. 从地图中获取图层1、根据图层在Map中的顺序得到指定图层 Dim pLayer as ILayer Set pLayer = pMap.Layer(3)2、根据图层属性在图层集合中得到指定图层 Dim pEnumLayer As IEnumLayer Dim pLayer As ILayer Set pEnumLayer = pMap.Layers pEnumLayer.Reset Set pLayer = pEnumLayer.Next Do While Not pLayer Is Nothing If TypeOf pLayer Is IFeatureLayer Then Exit Do (pLayer.Name = “States”) Set pLayer = pEnumLayer.Next Loop 图层的枚举与集合相似,但只有两个方法,没有方法通过位置顺序引用枚举中的对象,也没有Count属性Dim i As Integer For i=0 to pMap.LayerCount-1 Set pLayer=pMap .Layer(i) MsgBox pLayer.Name Next i
  • 32. 在地图中对图层的其他操作 添加 删除 改变显示顺序AddLayer (Layer ) AddLayers (Layers, autoArrange )ClearLayers DeleteLayer (Layer )MoveLayer (Layer, toIndex ) 若toIndex无效则移到最后
  • 33. 图层属性ILayer Name: String MaximumScale: Double MinimumScale: Double Visible: Boolean AreaOfInterest: Ienvelope(只读) ShowTips: Boolean
  • 34. 图层属性Dim pQueryFilt As IQueryFilter Dim pFeatCursor As IFeatureCursor Dim pFeature As IFeature Set pQueryFilt = New QueryFilter pQueryFilt.WhereClause = "DEMOG.Pop1991 > 1000000" 'Perform the search and report name of each feature in results Set pFeatCursor = pLayer.SearchDisplayFeatures(pQueryFilt, True) Set pFeature = pFeatCursor.NextFeature MsgBox pFeature.Value(pFeatCursor.FindField("Province.Name")) Do Until pFeature Is Nothing Set pFeature = pFeatCursor.NextFeature If Not pFeature Is Nothing Then MsgBox pFeature.Value(pFeatCursor.FindField("Province.Name")) End If Loop
  • 35. 图层属性IFeatureSelection : IUnknown BufferDistance: Double CombinationMethod:esriSelectionResultEnum SelectionColor: IColor SelectionSet: ISelectionSet SelectionSymbol: ISymbol SetSelectionSymbol: Boolean 方法Add (in Feature: IFeature) 方法Clear 方法SelectFeatures (in Filter: IQueryFilter, in Method: esriSelectionResultEnum, in justOne: Boolean)Dim pFeatSel As IFeatureSelection Dim pQueryFilt As IQueryFilter Dim pSelSet As ISelectionSet Set pFeatSel = pMap.Layer(0) ' Make the query filter Set pQueryFilt = New QueryFilter pQueryFilt.WhereClause = "AREA > 350000" 'Perform the selection and refresh the view pFeatSel.SelectFeatures pQueryFilt, esriSelectionResultNew, False pMapControl.ActiveView.PartialRefresh esriViewGeography, _ Nothing, Nothing 'Report how many features were selected Set pSelSet = pFeatSel.SelectionSet MsgBox pSelSet.Count
  • 36. 查询和选择集SelectionSetQueryFilterSpatialFilterTableFeatureClassRowFeatureCursorFeatureCursor
  • 37. 查询和选择集 Search方法 IFeatureLayer和IFeatureClass的方法 Search (Filter:IQueryFilter, Recycling: Boolean ):IFeatureCursor ISelectionSet的方法 Search (pQueryFilter, Recycling, pCursor ) 注:支持IQueryFilter常用的两类QueryFilter和SpatialFilter(看帮助)Dim pPoint As IPoint Set pPoint = New esriCore.Point pPoint.PutCoords -117.946, 35.823 Dim pFilter As IspatialFilter Set pFilter = New SpatialFilter With pFilter Set .Geometry = pPoint .GeometryField = "SHAPE" .SpatialRel = esriSpatialRelIntersects End With Dim pFeatureCursor As IFeatureCursor Set pFeatureCursor = pFeatureClass.Search(pFilter, False) esriSpatialRelEnum Constants 只有NextFeature方法,要将指针回到Cursor头,不能用Reset方法,必须重新用Search方法得到Cursor
  • 38. 查询和选择集 IFeatureSelection 的SelectFeatures 方法 SelectFeatures (Filter, Method:esriSelectionResultEnum , justOne :Boolean) 再通过SelectionSet 属性得到查询结果集(ISelectionSet)
  • 39. 查询和选择集 IMap的SelectByShape方法 SelectByShape (Shape:IGeometry , env :ISelectionEnvironment, justOne ) 再通过FeatureSelection属性得到选择集(ISelection) esriSelectionResultEnum Constants Dim pEnumFeat As IEnumFeature Dim pFeat As IFeature Set pEnumFeat = pMap.FeatureSelection Set pFeat = pEnumFeat.Next Do While (Not pFeat Is Nothing) Debug.Print pFeat.Value(pFeat.Fields.FindField("Name")) Set pFeat = pEnumFeat.Next Loop 通过ISelection查询接口IEnumFeature
  • 40. 几何形体GeometryEnvelope*Geometry collectionCurvePolycurvePointMultiPoint*SegmentPathPolylinePolygon**LineCircularArcBezierCurveRing*
  • 41. 点和点群点是最基本的几何形体,利用IGeometryCollection或IPointCollection接口,任何类型的集合形体都可以由点的集合构成。Dim pPoint As IPoint Set pPoint = New Point pPoint.X = 300 pPoint.Y = 450 Dim pMultiPts As IPointCollection Set pMultiPts = New MultiPoint pMultiPts.AddPoint pPoint
  • 42. 要素和几何形体Feature的Shape属性返回要素几何形体,Shape的Evenlope属性返回一个形体的最小外接矩形,这个属性只读。 Mapcontrol.Extent = pFeature.Shape.EnvelopeGeometryEnvelopeFeature
  • 43. 显示几何形体StartDrawing:为绘图显示作准备 DrawPoint,DrawMultipoint,DrawPolyline,DrawPolgon FinishDrawing:显示所画几何形体 注意:该方法只是进行快速绘画,并未保存画出的几何形体,当刷新显示时,几何形体本身及符号不再显示。 Dim pDisplay As IScreenDisplay Set pDisplay = mapcontrol.ActiveView.ScreenDisplay IScreenDisplay接口的绘图方法 pDisplay.StartDrawing pDisplay.Hdc, esriNoScreenCache pDisplay.SetSymbol pSym pDisplay.DrawPolygon pPolygon pDisplay.FinishDrawing
  • 44. 用VB和ESRI Map Control开发初步显示包含多个图层的地图 控制图层的漫游和放大 创建工具条 根据设置的比例尺范围显示地图 完成空间和属性查询 通过编程实现图层的加载
  • 45. 在VB中加载 Map Control控件安装了ArcGIS桌面软件或ArcEngine后在VB中可使用它们提供的控件 MapControl和PageLayout Control
  • 46. 引用相应的类库为了保证能使用所有可用的ArcObject组件,必须添加对相应类库的引用
  • 47. 使用Map control控件2. 向Map control地图中添加数据1. 在窗体中添加Map control控件3. 设置图层显示属性4. 保存工程,检查运行效果
  • 48. 实现漫游和放大功能添加漫游功能添加全景按钮
  • 49. 添加工具条1. 在窗体中添加Toolbar和ImageList控件2. 向ImageList控件中添加图片(ArcGIS\bin)3. 建立Toolbar与ImageList控件之间的关联4. 在Toolbar上添加按钮5. 修改MouseDown事件6. 响应Toolbar的ButtonClick事件
  • 50. 添加属性查询工具1. 在窗体中添加Label和Textbox控件2. 在Textbox的KeyPress事件中添加代码If KeyAscii = vbKeyReturn Then ' Find the States layer Dim i As Integer Dim pFeatLyr As IFeatureLayer For i = 0 To MapControl1.LayerCount - 1 Set pFeatLyr = MapControl1.Layer(i) If pFeatLyr.Name = "states" Then ' Found it and exit loop Exit For End If Next i ' Create a string to use in the query Dim queryStr As String queryStr = "STATE_NAME = '" & Text1.Text & "'" ' Create the query filter Dim pQueryFltr As IQueryFilter Set pQueryFltr = New QueryFilter pQueryFltr.WhereClause = queryStr ' Perform the selection Dim pFeatSeln As IFeatureSelection ' QI for IFeatureSelection from the ' IFeatureLayer Set pFeatSeln = pFeatLyr pFeatSeln.SelectFeatures _ pQueryFltr, esriSelectionResultNew, False ' Get the selection set Dim pSelSet As ISelectionSet Set pSelSet = pFeatSeln.SelectionSet ' Get the cursor from the selection set Dim pFeatCursor As IFeatureCursor pSelSet.Search Nothing, True, pFeatCursor ' Assume only one feature Dim pFeature As IFeature Set pFeature = pFeatCursor.NextFeature If Not pFeature Is Nothing Then ' Get the extent of the selected feature Dim pExtent As IEnvelope Set pExtent = pFeature.Shape.Envelope ' And set the Map control's extent MapControl1.Extent = pFeature.Shape.Envelope End If End If End Sub
  • 51. 设置图层显示的比例尺范围1. 在Map control属性页中添加图层,调整图层显示顺序2. 根据指定字段值渲染图层3. 在Form_Load事件中设置图层显示比例尺范围Dim pLayer As ILayer Dim i As Integer ' Iterate through all layers For i = 0 To MapControl1.LayerCount - 1 Set pLayer = MapControl1.Layer(i) ' Note case sensitivity If UCase(pLayer.Name) = "COUNTIES" Then pLayer.MaximumScale = 0 pLayer.MinimumScale = 15000000 ElseIf UCase(pLayer.Name) = "STATES" Then pLayer.MaximumScale = 14999999 pLayer.MinimumScale = 0 End If Next i
  • 52. 实现空间查询功能在Map control的Mouse Down事件中添加代码With Toolbar1.Buttons If .Item("Zoom").Value = tbrPressed Then MapControl1.Extent = MapControl1.TrackRectangle ElseIf .Item("Pan").Value = tbrPressed Then MapControl1.Pan ElseIf .Item("SelectByPolygon").Value = tbrPressed Then Dim pSearchShape As IPolygon ' Create the search shape Set pSearchShape = MapControl1.TrackPolygon ' Do the actual selection With MapControl1 .Map.ClearSelection .Refresh esriViewGeoSelection .Map.SelectByShape pSearchShape, Nothing, False ' And refresh the map .Refresh esriViewGeoSelection End With End If End With
  • 53. 通过编程实现图层加载AddLayerFromFile:layer文件(.lyr) AddShapeFile:Shapefile文件 AddLayer:Coverages, TIN, Raster, or ArcSDE ' Change the paths to reflect your installation Dim pFactory As IWorkspaceFactory Dim pWorkspace As IFeatureWorkspace Dim pFeatLayer As IFeatureLayer ' Create a new ShapefileWorkspaceFactory object Set pFactory = New ShapefileWorkspaceFactory Set pWorkspace = pFactory.OpenFromFile("C:\Data\USA", 0) ' Create a new FeatureLayer and assign a shapefile to it Set pFeatLayer = New FeatureLayerSet pFeatLayer.FeatureClass = pWorkspace.OpenFeatureClass("States") pFeatLayer.Name = pFeatLayer.FeatureClass.AliasName ' Add the FeatureLayer to the map With MapControl1 .AddLayer pFeatLayer .AddShapeFile "C:\Data\USA", "counties" .AddShapeFile "C:\Data\USA", "USHigh" End With