Title: Visual Basic: Data
1Lecture 10
2This Lecture
- Data access.
- Getting Maps and Layers
- Getting Attributes
- Selecting Features
- More GUIs Forms
- Help
3Data 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.
4Getting data
Map
AttributeTable
Layer
Feature
234
Values
5Getting 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.
6Getting 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
7IMaps
- 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.
8Getting 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
Layer
Feature
234
Values
9Getting Layers
- If you know what the type of the Layers are, you
can get them thus - Dim pMxDocument As IMxDocument
- Set pMxDocument Application.Document
- Dim pMap As IMap
- Set pMap pMxDocument.FocusMap
- Dim pLayer As ILayer
- For i 0 To pMap.LayerCount - 1
- Set pLayer pMap.Layer(i)
- Do something
- Next
10Types 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.
11Types 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
.
12Enumerations
- 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.
13Standard 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
14How to filter Layers
Dim pMxDoc As IMxDocument Dim pMap As IMap Set
pMxDoc Application.Document Set 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
15The 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
16Getting data
- Once we have our Layer, we want to get data from
it.
Map
AttributeTable
Layer
Feature
234
Values
17Getting 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.
18Getting 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
19Getting the Attribute Table
- Assuming we have an Enumeration of Layers
- 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
20Getting data
- Alternative is to get data from a Feature.
Map
AttributeTable
Layer
Feature
234
Values
First though, we need to get only the features we
are interested in. We can search for these.
21Searching
- 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.
22Search 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.
23IQueryFilter 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.
24Getting 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")
25Setting/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.
26Getting data
Map
AttributeTable
Layer
Feature
234
Values
27Summary
- 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.
28So far
- Data access.
- Getting Maps and Layers
- Getting Attributes
- Selecting Features
- Now
- More GUIs Forms
29Forms
- 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.
30The 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
31Running Forms
- Add in a Macro to the document
- Sub startF ()
- MyForm.Show
- End Sub
32So far
- Data access.
- Getting Maps and Layers
- Getting Attributes
- Selecting Features
- More GUIs Forms
- Now
- Help
33Finding 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
34ESRI Object Browser
- The ESRI Object Browser EOBrowser.exe
- In C\Program Files\ArcGIS\DeveloperKit\tools
- Starts empty of references.
35ESRI Object Browser
- Click whether you want CoClasses, Interfaces etc.
- Search for a term.
- Pick a Class from the list and display it.
36The 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/
37The Help Files
- ArcGIS Desktop gt 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)
38Help Files
- List Classes / Interfaces and their methods /
variables
Access
Class List
Click for more info and example uses
39The Object Diagrams
- Notoriously vast pdf UML diagrams. Can get to
them via the Object Model Diagrams tree in the
Help or in Arcs DeveloperKit/Diagrams/
directory. - Best to open the AllDesktopOMDs.pdf diagram and
use Ctrl-F to search for an object class.
40Understanding UML
- http//edndoc.esri.com/arcobjects/8.3/
- ArcObjects online gt Object Models Diagrams gt How
to read
41Summary
- 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.
42Next Lecture
- Editing data, running commands.
- VB.Net, ActiveX/Com and ModelBuilder.
- Practical
- Data manipulation