Anda di halaman 1dari 21

Advanced GIS Customizing the user interface in ArcGIS

Fall 2003

Where to write your code in ArcGIS?


Test code and write final code are two different things. Start with VBA macro in ArcGIS and either leave your code there or move to different places. 3 ways to write ArcObject codes:
VBA macro in ArcGIS ActiveX COM component such as DLL or OCX As a standard EXE program

Find a related sample


Start ArcObjects Developler Help from Start>Programs>ArcGIS>ArcObjects Developer Help Stay in the Tab Contents and click Samples then ArcMap. Select Add a Shapefile programmatically Copy the code listed on the window to clipboard Open ArcMap and start with an empty project. Open Marco (or VB Editor) and past this code onto the code window. If you open from VB Editor, you will need to Insert a Module to the VB project. Change the directory to your local c drive, such as Set pFeatureWorkspace = pWorkspaceFactory.OpenFromFile("c: \esri\esridata\World", 0) Run the code and see what happens. It should add layer Country to your ArcMap project.

AddShapeFile()
Public Sub AddShapeFile() Dim pWorkspaceFactory As IWorkspaceFactory Dim pFeatureWorkspace As IFeatureWorkspace Dim pFeatureLayer As IFeatureLayer Dim pMxDocument As IMxDocument Dim pMap As IMap 'Create a new ShapefileWorkspaceFactory object and open a shapefile folder Set pWorkspaceFactory = New ShapefileWorkspaceFactory Set pFeatureWorkspace = pWorkspaceFactory.OpenFromFile(c:\esri\esridata\world", 0) 'Create a new FeatureLayer and assign a shapefile to it Set pFeatureLayer = New FeatureLayer Set pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(country") pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName 'Add the FeatureLayer to the focus map Set pMxDocument = Application.Document Set pMap = pMxDocument.FocusMap pMap.AddLayer pFeatureLayer 5 End Sub

Exercise 5 bonus points


Add cities shapefile to your project using InputBox.

Private Sub UIToolControl1_MouseDown(ByVal button As Long, ByVal shift As Long, ByVal x As Long, ByVal y As Long) Dim pMxApp As IMxApplication Dim pMxDoc As IMxDocument Dim pMap As IMap Dim pActiveView As IActiveView Dim pEnvelope As IEnvelope Set pMxApp = Application Set pMxDoc = Application.Document Set pMap = pMxDoc.FocusMap Set pActiveView = pMap Set pEnvelope = pMxDoc.CurrentLocation.Envelope pEnvelope.Expand pMxDoc.SearchTolerance, pMxDoc.SearchTolerance, False 'Refresh the old selection to erase it pActiveView.PartialRefresh esriViewGeoSelection, Nothing, Nothing 'Perform the selection using a point created on mouse down pMap.SelectByShape pEnvelope, pMxApp.SelectionEnvironment, True 7 'Refresh again to draw the new selection

Selection by shape (or try next slide, select by attribute)

AttributeQuery
Public Sub AttributeQuery() Dim pQueryAttributes As IQueryAttributes 'Create a new Query Attribute Dialog and set 'some necessary properties before launching it Set pQueryAttributes = New QueryAttributes Set pQueryAttributes.Application = Application pQueryAttributes.SelectFeaturesInLayerOnOK = True 'Provide a default expression if desired 'pQueryAttributes.Expression = """NAME"" = 'Halifax'" 'Lauch the dialog pQueryAttributes.DoModal Application.hWnd End Sub

Run Macro from the project


Run Selection from the Macro Try to find Population >= 1 billion.

Macro in ArcMap
Go to Tools>Macro and select Normal in Macro in Type in MyZoomIn in Macro Name and click Create Go to File>Close Return to ArcMap In ArcMap, go to Tools>Macro> and select Module1.MyZoomIn macro and click Run (make sure your macro settings is in Normal) The display zoomed in 50% smaller.

10

Selection/Buffer
Add Buffer Feature and create graphic elements by clicking on left window and copy to your new module in existing macro Add Select Features Using An Attribute Query to next module in same macro. Select Name = Kroger in business layer and buffer this feature using 1000 meters (go to View > Data Frame Properties and select Predefined > Projected Coordinate System > UTM > NAD 1983 > NAD 1983 UTM Zone 16N. This will set your projected system to UTM 1983)

11

Open a ArcGIS file and create a macro with the following code and run it (switch your view between map and PageLayout window
Sub PageLayout() Dim pActiveView As IActiveView ' Define interface pointer Dim pMxDoc As IMxDocument Set pMxDoc = ThisDocument Set pActiveView = pMxDoc.ActiveView ' Get active view of document If TypeOf pActiveView Is IMap Then ' Test the view object type MsgBox "This page is a map window" Else MsgBox "It's a PageLayout object" End If End Sub
12

Add Table
Public Sub AddTable() Dim pMxDoc As IMxDocument Dim pMap As IMap Dim pTableCollection As ITableCollection Set pMxDoc = ThisDocument Set pMap = pMxDoc.FocusMap Set pTableCollection = pMap Set pTable = OpenTable(c:\esri\esridata\usa\", "States.dbf") If pTable Is Nothing Then Exit Sub pTableCollection.AddTable pTable pMxDoc.UpdateContents End Sub
13

Function for Add Table


Private Function OpenTable(strWorkspace As String, strTableName As String) As ITable On Error GoTo ErrorHandler Dim pShpWorkspaceName As IWorkspaceName Dim pDatasetName As IDatasetName Dim pName As IName 'Create the workspace name object Set pShpWorkspaceName = New WorkspaceName pShpWorkspaceName.PathName = strWorkspace pShpWorkspaceName.WorkspaceFactoryProgID = _ "esriCore.shapefileworkspacefactory.1"

14

Function..
'Create the table name object Set pDatasetName = New TableName pDatasetName.Name = strTableName Set pDatasetName.WorkspaceName = pShpWorkspaceName 'Open the table Set pName = pDatasetName Set OpenTable = pName.Open Exit Function 'exit to avoid error handler ErrorHandler: Set OpenTable = Nothing End Function
15

Map Components in ArcMap


A map document is organized as a hierarchy of maps, layers, and elements. MxDocument.Maps is the collection of all maps in the current ArcMap document. MxDocument.FocusMap is the map that is currently selected for user interaction. A Map object corresponds to a data frame entry in ArcMap's Table of Contents window. It maintains a collection of map layers (e.g., geodatabase features) and map surrounds (e.g., legends and scalebars). Map.Name is the data frame name Individual map layers may be accessed via Layer() and an index value, or by Layers() (plural) and the UID of a layer type-specific interface. The BasicGraphicsLayer contains "basic graphics" - that is, graphic elements not directly described in a database. These include features that have been converted to graphics and elements added by drawing tools.
16

Map objects
When a new map object is instantiated, two other related objects are created too. ScreenDisplay and CompositeGraphicsLayer. IMAP is the starting point for Map. (for example, add/ delete/access map layers, associate with Map surround :legend,scale bar..)

17

ArcMap objects
MapSurround() provides the list of map surround objects. Scale and SpatialReference are metric properties common to all layers of a given Map. Each map has its own list of bookmarks, available through the IMapBookmarks interface. Bookmarks managed through the main menu options are of the AOIBookmark type (AOI = "area of interest").
18

Map layer
Map layers are represented by various types of layer objects according to the type of data they present. FeatureLayer - geodata-defined map elements (features) stored as feature class data in a geodatabase. FDOGraphicsLayer (FDO = "feature data object") annotation feature class data. Annotation features are graphical elements used to annotate members of a feature layer. FDOGraphicsLayer data is also stored in a geodatabase. CompositeGraphicsLayer - used by Map for its BasicGraphicsLayer property. CoverageAnnotationLayer - for display of ArcInfo coverage files. RasterLayer and TinLayer - for raster or TIN data. GroupLayer - allows treating multiple layers as single unit.
19

Layer type
A given map may contain layers of different types. All layer objects implement the ILayer interface, and this is what is returned by Map's layer selection functions. To determine the exact layer type, you need to try querying the object for type-specific interfaces. For example, if the layer object supports the IFeatureLayer interface, then you know it is a FeatureLayer object. In VisualBasic, this might be coded as follows (Add this code to the previous macro) Dim pLayer As ILayer Dim pMap As IMap Set pMap = pMxDoc.FocusMap Set pLayer = pMap.Layer(0) ' (pMap points to Map object) If TypeOf pLayer Is IFeatureLayer Then MsgBox "pLayer is a FeatureLayer object" 20 End If

Layer
GraphicElement objects are used for the display of nonfeature data (a FeatureLayer displays itself using a feature renderer acting on each record of the layer dataset). Graphic elements include text, primitive geometric shapes, and pictures. (TIP: Graphs and reports are stored as EmfPictureElements.) The MapSurround class includes legends, scale bars, and north arrows. Map grid support is provided by objects derived from MapGrid, MapGridBorder, and GridLabel. Members of the NumberFormat class assist in grid labeling with a variety of numeric display format options. FrameElements serve as graphical containers for other objects. In addition to providing backgrounds and borders, they perform other functions according to their type: A MapFrame houses a Map object. It stores map grids, locator rectangles, and clipping parameters. A MapSurroundFrame houses a MapSurround object.
21

Anda mungkin juga menyukai