You are on page 1of 42

Lecture 10

Visual Basic: Data

This Lecture
 Data access.
Getting Maps and Layers Getting Attributes Selecting Features

 More GUIs : Forms  Help

Data Access
 The ArcMap Object model  Application
Document / MxDocument type Object Display / AppDisplay type Object

 Document
Maps, FocusMap, ActiveView, StyleGallery

 Well look at
Getting the Maps, Layers, Features. Getting Attribute data. Selecting and getting selected Features.

Getting data
 Various levels
Map AttributeTable
FID Data 234 875

Layer

1 2

Feature

234

Values

Getting a Map
 A Map contains all the data and features in the Data View or each Layout View Frame. Dim pDoc As IMxDocument Set pDoc = ThisDocument Dim pMap As IMap Set pMap = pDoc.FocusMap  FocusMap is the one visible in data view or selected in layout view.

Getting all Maps


 You can also get an Object containing all the Maps. Dim pDoc As IMxDocument Set pDoc = ThisDocument Dim pMaps As IMaps Set pMaps = pDoc.Maps  The Maps object is exposed by the IMxDocument Interface.  It returns an IMaps Interface type Object

IMaps
 You can loop through all the IMap Interface objects in an IMaps Interface object using its .count and .item Methods.
Dim pMap As IMap For i = 0 To pMaps.count -1 Set pMap = pMaps.item(i) Do stuff to pMap Next

 Other IMaps Methods include


 Add, Create, Remove(IMap), RemoveAt(index), Reset [Remove all].

Getting data
 Its rare we want to get data out of a Map. Its more usual to get data from a Layer ~ (a Coverage, FeatureDataset, Image etc.).
Map AttributeTable
FID Data 234 875

Layer

1 2

Feature

234

Values

Getting Layers
 If you know what the type of the Layers are, you can get them thus
Dim Set Dim Set pMxDocument As IMxDocument pMxDocument = Application.Document pMap As IMap pMap = pMxDocument.FocusMap

Dim pLayer As ILayer For i = 0 To pMap.LayerCount - 1 Set pLayer = pMap.Layer(i) Do something Next

Types of Layer
 Remember however that we can add many things as Layers (images, data, etc.).  If we dont know the data type we need to filter our Layers before we try to do some job.  We do this by knowing the number by which the system refers to the Layer Classes as.  This is quite painful.

Types of Layer
 IDataLayer
 {6CA416B1-E160-11D2-9F4E-00C04F6BC78E}

 IGeoFeatureLayer
 {E156D7E5-22AF-11D3-9F99-00C04F6BC78E}

 IGraphicsLayer
 {34B2EF81-F4AC-11D1-A245-080009B6F22B}

 IFDOGraphicsLayer
 {34B2EF85-F4AC-11D1-A245-080009B6F22B}

 ICoverageAnnotationLayer
 {0C22A4C7-DAFD-11D2-9F46-00C04F6BC78E}

 IGroupLayer
 {EDAD6644-1810-11D1-86AE-0000F8751720}

 Layers that implement IDataLayer include FeatureLayers, FDOGraphicsLayers (Annotation), TinLayer, RasterLayer, and CoverageAnnotationLayer.

Enumerations
 Objects containing lists of other objects. Like a 1D array.  Arc uses them to return arrays of data to you.  Have a next Method to get the next Object.  Also a reset Method to return to the start.  Arc Object types have different Enumerations.  IEnumFeature is the Interface for a set of Map IFeatures.

Standard use of Enumerations


Dim pEnumSomething As IEnumSomething Set pEnumSomething = someEnumGettingMethod pEnumSomething.Reset Dim variable As Something Set variable = pEnumSomething.Next Do While Not variable Is Nothing Do stuff with Layer Set variable = pEnumSomething.Next Loop

How to filter Layers

Dim Dim Set Set

pMxDoc As IMxDocument pMap As IMap pMxDoc = Application.Document pMap = pMxDoc.FocusMap

 Get an Enumeration of Layers


Dim pUID As IUID Set pUID = New UID pUID = "{E156D7E5-22AF-11D3-9F99-00C04F6BC78E} Dim pEnumLayer As IEnumLayer Set pEnumLayer = pMap.Layers(pUID, True) pEnumLayer.Reset Dim pGeoFeatureLayer As IGeoFeatureLayer Set pGeoFeatureLayer = pEnumLayer.Next Do While Not pGeoFeatureLayer Is Nothing Do stuff with Layer Set pGeoFeatureLayer = pEnumLayer.Next Loop

The TypeOf Keyword


 You can check whether an Object implements an Interface using VBs TypeOf keyword.  For example, if the users selected something in ArcMap's tree of contents, you can test whether its a GeoFeatureLayer, thus

Dim pUnk As IUnknown Set pUnk = pEnumLayer.Next If TypeOf pUnk Is IGeoFeatureLayer Then Set pFeatLayer = pUnk 'Do something with pFeatLayer End If

Getting data
 Once we have our Layer, we want to get data from it.
Map AttributeTable
FID Data 234 875

Layer

1 2

Feature

234

Values

Getting Features from Layers


 Assign your Layer to an appropriate Interface.
 IGeoFeatureLayer : Treat as Geographical data  IFeatureLayer : Treat as a general Layer  IAttributeTable : Treat as an Attribute table

 Search the Layer, or get the Attribute Table.

Getting the Attribute Table


 Assuming we have a Layer Enumeration, we set the Layer to an IAttributeTable. Dim pAttributeTable As IAttributeTable Set pAttributeTable = pEnumLayer.Next Dim pTable As ITable Set pTable = pAttributeTable.AttributeTable Dim pRow As IRow For i = 0 To pTable.RowCount(Nothing) - 1 Set pRow = pTable.GetRow(i) Dim index As Long index = pTable.FindField("School") Debug.Print pRow.Value(index) Next i

Getting the Attribute Table

Get Layer as AttributeTable

 Assuming we have an Enumeration of Layers Dim pAttributeTable As IAttributeTable Set pAttributeTable = pEnumLayer.Next

Get actual Table


Dim pTable As ITable Set pTable = pAttributeTable.AttributeTable Dim pRow As IRow

Get each row

For i = 0 To pTable.RowCount(Nothing) - 1 Set pRow = pTable.GetRow(i) Dim index As Long index = pTable.FindField("School") Debug.Print pRow.Value(index) Next i

Get a particular Field

Getting data
 Alternative is to get data from a Feature.
Map AttributeTable
FID Data 234 875

Layer

1 2

Feature

234

Values

First though, we need to get only the features we are interested in. We can search for these.

Searching
 You need to get a reference to the data and a search cursor (ICursor / IFeatureCursor).  The search cursor jumps between records in a dataset that match some search criteria.  The cursor marks the row in the dataset that youre currently interested in.  They have a nextFeature Method which gets a Feature object appropriate to the Dataset.

Search Methods
 IFeatureLayer / IGeoFeatureLayer
Set pCursor = pFeatureLayer.Search(Q, False)

 ITable from an IAttributeTable


Set pCursor = pTable.Search (Q, False)

 Where pCursor is an IFeatureCursor Object  Q is an IQueryFilter Object  True / False determines how the records are allocated to the Cursor set to False.

IQueryFilter Objects
 You can get everything by setting these to Nothing.
Set pC = pFeatureLayer.Search(Nothing, False)

 IQueryFilter Objects store fields you want

returned and query strings.


pQueryFilter.SubFields = SCHOOL,LEA pQueryFilter.AddField (POSTCODE) pQueryFilter.WhereClause = LEA = Leeds

 By default the fields are set to * i.e. all fields, so if you use just AddFields youll have to set SubFields to first.

Getting Data from a Feature


 Use the cursor to get the next feature
Dim pFeature As IFeature Set pFeature = pC.NextFeature Do While Not pFeature Is Nothing Set pFeature = pC.NextFeature Do stuff to pFeature Loop
 The Feature Method getValue(i) takes in an integer number equalling the position of the field column.  If you dont know it, there are lookup methods that return integers

pFeature.Fields.FindField("SCHOOL")

Setting/Getting Features Selected


 An alternative is to get the selected Features.  IMaps SelectFeature method takes in an ILayer and IFeature.  Refresh the display using the MxDocuments refresh.
pDoc.ActiveView.Refresh

 To get the Features you need IMaps FeatureSelection Method, or an ISelectionSet Object see handout for example.

Getting data
 Various levels
Map AttributeTable
FID Data 234 875

Layer

1 2

Feature

234

Values

Summary

 Get the Applications Document.  Get the Maps from it.  Pick the one you want or loop through them.  Get a Layer from the Map, or Loop through all of them.  Generate an Attribute table and use Rows / Fields to get data.  Or use the Layers search routine to search for Features.

So far
 Data access.
Getting Maps and Layers Getting Attributes Selecting Features

Now:
 More GUIs : Forms

Forms

 Even easier than UIControls. Instead of a Module, add a Form.  Brings up the Form editor drag stuff in.  Change its properties (which are variables you can alter on the fly)  Double click on Objects to alter Event Methods.

The Text Box


 One useful item you get with Forms is a TextBox.  For multiple lines you need to set MultiLine as True in its Behaviour properties and give it scrollbars.  You can then set the text in an Event handling method, thus
MyForm.TextBox1.Text = Its huge & vbCrLf & Its green

Running Forms

 Add in a Macro to the document


Sub startF () MyForm.Show End Sub

So far
 Data access.
Getting Maps and Layers Getting Attributes Selecting Features

 More GUIs : Forms

Now:
 Help

Finding out about Objects


 The Object Browser / Autocompletion lists
Unfortunately, these dont display methods of Class Objects whos default Interface is IUnknown. As this is most of the ESRI Objects this is not so useful.

 Alternatives
The ESRI Object Browser The Object Help Files / Online Help The Object Diagrams

ESRI Object Browser


 The ESRI Object Browser: EOBrowser.exe  In C:\Program Files\ArcGIS\DeveloperKit\tools  Starts empty of references.

ESRI Object Browser


 Click whether you want CoClasses, Interfaces etc.  Search for a term.  Pick a Class from the list and display it.

The Object Help Files / Online Help


 The standard VB Editor help files help you with VBA but not ArcObjects.  Two ways to access the ArcObjects help
Click in a Class or Object and hit the F1 key: brings up help about that thing. Go to the DeveloperKit/help/ directory: all the help files.

 However, just as easy, and more up to date are the online help files at ArcGIS Developer Online
http://edndoc.esri.com/arcobjects/9.1/

The Help Files


 ArcGIS Desktop > Developer Guide
A good starting point for good practice when using ArcObjects naming standards, code to avoid etc.

 Object Model / Overview


Basic descriptions of the branches of the Framework and their major classes.

 Library Reference
Lists of all the Interfaces and Classes for the Core bits (Application, ArcMap/Editor, ArcCatalog)

Help Files

 List Classes / Interfaces and their methods / variables

Access Class List

Click for more info and example uses

The Object Diagrams


 Notoriously vast pdf UML diagrams. Can get to them via the Object Model Diagrams tree in the Help or in Arcs DeveloperKit/Diagram s/ directory.  Best to open the AllDesktopOMDs.pdf diagram and use Ctrl-F to search for an object class.

Understanding UML
 http://edndoc.esri.com/arco bjects/8.3/  ArcObjects online > Object Models Diagrams > How to read

Summary

 Get the Applications Document.  Get the Maps from it.  Pick the one you want or loop through them.  Get a Layer from the Map, or Loop through all of them.  Generate an Attribute table and use Rows / Fields to get data.  Or use the Layers search routine to search for Features.

Next Lecture
 Editing data, running commands.  VB.Net, ActiveX/Com and ModelBuilder.

Practical
 Data manipulation

You might also like