Upload
others
View
6
Download
0
Embed Size (px)
Citation preview
Using ArcObjects
Instructor: Dr. Wei Ding
CS 697 Spatial Data MiningCS 697 Spatial Data Mining
Spring 2009
1
Adding layers to a mapAdding layers to a map•Adding a geodatabase feature class•Adding a raster datasetExercise 14 (ex14a.mxd & ex14b.mxd)
2
Adding a Layer to a MapAdding a Layer to a MapFour steps:
Step 1. Create a layer object using a correspondent layer class
S 2 G d f l Step 2. Get a dataset from a storage location
Step 3. Associate the data set with the layer object
St 4 Add th l t Step 4. Add the layer to a map
3
Step 1: Create a New Layer ObjectStep 1: Create a New Layer ObjectThe type of layer you create depends on the data source you are going to associate with it.
Example ExampleExample
Say the layer’s data source will be an ArcInfo Graid. A grid is a raster data
Example
Or say the layer’s data source is a shaoe file. Shapefiles are feature data
set.
Dim pRLayer As IRasterLayerSet pRLayer = New RasterLayer
sets.
Dim pFLayer As IFeatureLayerSet pFLayer = New FeatureLayerSet pRLayer New RasterLayer Set pFLayer New FeatureLayer
4
Step 2: Get a Data Set From a Storage L tiLocation
A storage location is called a workspace. To get a dataset, you first get its workspace. You get a workspace from a class called a workspace factory.
The process is to create a workspace factory use it to get a The process is to create a workspace factory, use it to get a workspace, and then get the dataset you want from the workspace. p
ExampleSay a personal geodatabase is stored in Microsoft Access file format. You need a Microsoft Access workspace factory. y
Dim pAWFactory As IWorkspaceFactorySet pAWFactory = New AccessWorkspaceFactoryDim pFWorkspace As IFeatureWorkspace
5
Dim pFWorkspace As IFeatureWorkspaceSet pFWorkspace = pAWFactory.OpenFromFile (“D:\data\city.mdb”,0)
Example DiscussionExample DiscussionExamplepSay a personal geodatabase is stored in Microsoft Access file format. You need a Microsoft Access workspace factory.
Dim pAWFactory As IWorkspaceFactoryDim pAWFactory As IWorkspaceFactorySet pAWFactory = New AccessWorkspaceFactoryDim pFWorkspace As IFeatureWorkspaceSet pFWorkspace = pAWFactory.OpenFromFile (“D:\data\city.mdb”,0)
• Although you are creating an object of AccessWorkspaceFactory. The variable is declared to IWorkspaceFactory. So why is this still correct?p y y
• IWorkspaceFactory contains the OpenFromFile method.• AccessWorkspaceFactory is a subclass of IWorkspaceFactory.
6
Example DiscussionExample DiscussionExamplepSay a personal geodatabase is stored in Microsoft Access file format. You need a Microsoft Access workspace factory.
Dim pAWFactory As IWorkspaceFactoryDim pAWFactory As IWorkspaceFactorySet pAWFactory = New AccessWorkspaceFactoryDim pFWorkspace As IFeatureWorkspaceSet pFWorkspace = pAWFactory.OpenFromFile (“D:\data\city.mdb”,0)
• OpenFromFile method returns IWorkspace. Then why is the variable declared to IFeatureWorkspace? p
• IWorkspace and IFeatureWorkspace both belong to the class Workspace. • You need IFeatureWorkspace, which has an OpenFeatureClass method to get the data
set from the workspace. • So ou declare the ariable to the interface ou ant and let VBA do Quer Interface
7
• So you declare the variable to the interface you want and let VBA do QueryInterfacefor you.
To Finish Step 2To Finish Step 2ExamplepNow that you have the workspace, you can get the data set by running the OpenFeatureClassmethod.
Dim pFLayer As IFeatureLayerDim pFLayer As IFeatureLayerSet pFLayer = New FeatureLayerDim pAWFactory As IWorkspaceFactorySet pAWFactory = New AccessWorkspaceFactoryDim pFWorkspace As IFeatureWorkspaceSet pFWorkspace = pAWFactory.OpenFromFile (“D:\data\city.mdb”,0)Dim pFClass As IFeatureClassSet pFclass = pFWorkspace OpenFeatureClass(“Streets”) Set pFclass pFWorkspace.OpenFeatureClass( Streets )
8
Step 3: Associate the data set with the l bj tlayer object
ExamplepYou associate a feature class with a layer by setting the FeatureClass property on IFeatureLayer.
Dim pFLayer As IFeatureLayerDim pFLayer As IFeatureLayerSet pFLayer = New FeatureLayerDim pAWFactory As IWorkspaceFactorySet pAWFactory = New AccessWorkspaceFactoryDim pFWorkspace As IFeatureWorkspaceSet pFWorkspace = pAWFactory.OpenFromFile (“D:\data\city.mdb”,0)Dim pFClass As IFeatureClassSet pFClass = pFWorkspace OpenFeatureClass(“Streets”) Set pFClass pFWorkspace.OpenFeatureClass( Streets ) Set pFlayer.FeatureClass = pFClass
9
Step 4: Add the layer to a map`Step 4: Add the layer to a mapExampleFinally, you add the layer to a map. You set a map with the FocusMap property on Finally, you add the layer to a map. You set a map with the FocusMap property on IMxDocument.
Dim pFLayer As IFeatureLayerS FL N F LSet pFLayer = New FeatureLayerDim pAWFactory As IWorkspaceFactorySet pAWFactory = New AccessWorkspaceFactoryDim pFWorkspace As IFeatureWorkspacep p pSet pFWorkspace = pAWFactory.OpenFromFile (“D:\data\city.mdb”,0)Dim pFClass As IFeatureClassSet pFClass = pFWorkspace.OpenFeatureClass(“Streets”) S t Fl F t Cl = FClSet pFlayer.FeatureClass = pFClassDim pMxDoc As IMxDocumentSet pMxDoc = ThisDocumentDim pMap As IMap
10
Set pMap = pMxDoc.FocusMappMap.AddLayer pFlayer
Exercise 14a: Add Layers Directly From Ma Menu.
Dim pActiveView As IActiveViewSet pActiveView = pMxDoc ActiveView pMxDoc.ActiveView.RefreshSet pActiveView pMxDoc.ActiveViewpActiveView.Refresh
p
chaining
11
Adding a Raster Data SetAdding a Raster Data SetWorking with raster layers is similar to working with feature layers. In both cases,
Step 1. Create a layer object using a correspondent layer classclass
Step 2. Get a dataset from a storage location
Step 3 Associate the data set with the layer objectStep 3. Associate the data set with the layer object
Step 4. Add the layer to a map
12
Create a raster workspace factory: Dim pRWFactory As IWorkspaceFactorySet pRWFactory = New RasterWorkspaceFactory
Declare and set a variable to get the workspace using the OpenFromFile method: Dim pRasterWorkspace As IRasterWorkspaceSet pRasterWorkspace = pRWFactory.OpenFromFile ("..\Data\Wilson_NC\AirPhotos", 0)
Get the raster data set using the OpenRasterDataset method: Dim pRDataset As IRasterDatasetSet pRDataset = pRasterWorkspace OpenRasterDataset ("wilsonwest sid")Set pRDataset = pRasterWorkspace.OpenRasterDataset ( wilsonwest.sid )
Create a new raster layer: y :Dim pRLayer As IRasterLayerSet pRLayer = New RasterLayer
13
Associate the raster data set with the raster layer:pRLayer.CreateFromDataset pRDataset
Set the layer’s name:pRLayer.Name = "Wilson West"
Declare and set an IMxDocument variable and get the active map and add the layer to it:Declare and set an IMxDocument variable and get the active map and add the layer to it:Dim pMxDoc As IMxDocumentSet pMxDoc = ThisDocument
Dim pMap As IMapSet pMap = pMxDoc.FocusMappMap.AddLayer pRLayer
Use the Map’s MoveLayer method to move the raster layer to the bottom of the table of content:pMap.MoveLayer pRLayer, pMap.LayerCount - 1
This code sets the visibility of the legend so the classes don't drawDim pLegendInfo As ILegendInfoSet pLegendInfo = pRLayerpLegendInfo LegendGroup(0) Visible = False
otherwise
14
pLegendInfo.LegendGroup(0).Visible = False
Setting Layer SymbologySetting Layer Symbology
•Setting layer colorg y
•Setting layer symbols
•Creating a class breaks renderer
Exercise 15 (ex15a.mxd & ex15b.mxd & ex15c.mxd)
15
Setting Layer SymbologySetting Layer SymbologyLayers on a map have instructions that define the symbols and colors used to draw features.
You use a renderer and control it with code to work with layer legends legends.
16
InheritanceInheritanceA class has all the interfaces of its superclasses.
For example, the FeatureLayer class has all the interfaces of its superclass Layer.
With interface inheritance one interface inherits the With interface inheritance, one interface inherits the properties and methods of another interface.
For example, ISimpleLineSymbol inherits from IlineSymbol. p p y yThe properties on ILineSymbol can be used as if they were on ISimpleLineSymbol.
17
Example of Interface InheritanceExample of Interface Inheritance
l b l l b lDim pSimpleLineSymbol As ISimpleLineSymbolSet pSimpleLineSymbol = New SimpleLineSymbolpSimpleLineSymbol.Width = 3
18
Setting Layer ColorSetting Layer ColorWhen a user adds a feature layer to a map, ArcMap assigns a simple renderer that draws in a single randomly selected color (of course, you can set a color of your choice using ArcObjects)ArcObjects).
Every feature layers has a renderer, every renderer is composed of symbols, and every symbol has a color. p y y y
Color Symbol Renderer FeatureLayer
19
ColorS b l R d F t LSymbol Renderer FeatureLayer
Dim pSalmon As IRgbColorSet pSalmon = New RgbColor
S l R d = 255pSalmon.Red = 255pSalmon.Green = 160pSalmon.Blue = 122
20
Color S b l R d F t LSymbol Renderer FeatureLayer
Dim pLine As ISimpleLineSymbolSet pLine = New SimpleLineSymbolpLine.Color = pSalmonpLine.Color pSalmonpLine.Width = 3
21
Interface Inheritance
Color S b l R d F t LSymbol Renderer FeatureLayer
d l dDim pRender As ISimpleRendererSet pRender = new SimpleRendererSet pRender.Symbol = pLineSympRender.Label=“Salmon Streams”p
22
Color S b l R d F t LSymbol Renderer FeatureLayer
Dim pGFlayer As IgoFeatureLayerSet pGFLayer = New FeatureLayerSet pGFlayer.Renderer = pRendererp y p
b f h l f k h f h Note: before you can see the results of your work, you have to refresh the map display and update the table of contents. For example, pMxDoc.ActiveView.RefreshpMxDoc.UpdateContentsp p
23
Setting Layer SymbolsSetting Layer SymbolsWhen you make a feature layer’s renderer, you can create your
b l f i ( j di d) Y l own symbology for it (as we just discussed). You can also use symbols and colors created by others.
Style gallery itemsStyles
Style gallery classes
24
U S b l F E i ti g St lUse a Symbol From an Existing StyleWhen you want to use a symbol from an existing style, you follow there three steps:
Step 1: you get the style gallery. Step 2: you get a list or enumeration of style gallery itemsStep 2: you get a list, or enumeration, of style gallery items.Step 3: You get the specific style gallery item you want.
25
The IMxDocument inteface’s StyleGallery property is used to get the style gallery:
Dim pMxDoc As IMxDocumentpSet pMxDoc = ThisDocument
Dim pStyleGallery As IStyleGallerySet pSt leGaller = pM Doc St leGallerSet pStyleGallery = pMxDoc.StyleGallery
The Items property on IStyleGallery returns an Enum of symbols. The Items property has three arguments for specifying the style of gallery class, the style, and the symbol category.
Dim pEnumMarkers As IEnumStyleGalleryItemSet pEnumMarkers = pStyleGallery.Items _("Marker Symbols", "Hazmat.style", "Hazmat“)( y , y , )
Marker symbols in the Hazmat style that belong to the Hazmat categorycategory
26
Reset the Enum to be sure that the pointer points to the top of the list of marker symbols. p p p y
pEnumMarkers.Reset
D l i bl h ld l ll i d i N h fi k Declare a variable to hold a style gallery item, and issue a Next to get the first marker symbol from the Enum.
Dim pStyleItem As IStyleGalleryItemp y y ySet pStyleItem = pEnumMarkers.Next
Declare a marker symbol variable You will set this variable when the loop you are about to Declare a marker symbol variable. You will set this variable when the loop you are about to write finds Fire Hydrant 4.
Dim pMarker As IMarkerSymbol
27
Start a loop to get each marker symbol in the Enum. p g yThe loop will stop when there are no more items, because the last Next on an Enum returns Nothing.
Do Until pStyleItem Is NothingDo Until pStyleItem Is NothingIf pStyleItem.Name = "Fire Hydrant 4" Then
Set pMarker = pStyleItem.ItempMarker.Size = 14Exit Do
End IfSet pStyleItem = pEnumMarkers.NextLoopLoop
After the loop, create a simple renderer and set is Symbol property to the Fire Hydrant 4 marker symboly
Dim pRenderer As ISimpleRendererSet pRenderer = New SimpleRendererSet pRenderer S mbol = pMarker
28
Set pRenderer.Symbol = pMarker
Creating a Class Breaks RenderCreating a Class Breaks RenderWhen you map numeric attributes, such as population or income, you divide the range of values into classes and assign a different symbol to each class.
Three classes of parcel values
Each class in a legend has a range of beginning and ending values called break points.
p
p
Records whose value falls within a class’s break points belong to that class and are symbolized with that class’s symbol.
29
Create a class breaks renderer
Dim pCBR As IClassBreaksRendererSet pCBR = New ClassBreaksRenderer
Set the classification field to ParceValue and the number of breaks to 3. You are creating three classes because you want to show high, medium, and low values.
pCBR.Field = "ParcelValue“pCBR.BreakCount = 3
Set the break point values and the legend label values for the three classes.
pCBR.Break(0) = 50000pCBR Break(1) = 100000pCBR.Break(1) = 100000pCBR.Break(2) = 9000000
pCBR.Label(0) = "Low"
30
pCBR.Label(1) = "Medium"pCBR.Label(2) = "High"
Make three RGB color objects.jSet the colors’ RGB property with the RGB function to make three shades of green.
Dim pGreenLight As IRgbColorDim pGreenMedium As IRgbColorDim pGreenMedium As IRgbColorDim pGreenDark As IRgbColor
Set pGreenLight = New RgbColorSet pGreenMedium = New RgbColorSet pGreenDark = New RgbColor
pGreenLight RGB = RGB(220 245 233)pGreenLight.RGB RGB(220, 245, 233)pGreenMedium.RGB = RGB(118, 168, 130)pGreenDark.RGB = RGB(34, 102, 51)
31
Set the fill symbol for each class with a shade of green
Dim pFill As ISimpleFillSymbolSet pFill = New SimpleFillSymbolSet pFill New SimpleFillSymbol
pFill.Color = pGreenLightpCBR.Symbol(0) = pFill
pFill.Color = pGreenMediumpCBR.Symbol(1) = pFill
pFill.Color = pGreenDarkpCBR.Symbol(2) = pFill
32