GIS – Intro to MapObjects

Geography refers to real world objects described by their location using geographical coordinates. Information is the attributes of the object; this can include physical, biological, cultural, demographic or economic information. System is any computer system that can support the integration of these two. Thus, GIS are computerized systems for the storage, retrieval, manipulation, analysis and display of geographical data.

Environmental System Research Institute (ESRI) dominates the commercial world of GIS packages and software. ESRI provides ArcGIS software package, which includes software and tools for creating and analyzing geographic data. ESRI also provides MapObjects for developing custom GIS software depending on the client’s requirement. MapObjects is an ActiveX control (OCX) – 2.3 its latest avatar, called the Map control – and a set of over forty-five ActiveX automation objects. It is for use in industry standard Windows programming environments. You can download the MapObjects evaluation OCX from www.ESRI.com. Do not forget to download the MapObjects sample data.

You can implement these and other functions in programs built with MapObjects:

  • Display a map with multiple map layers, such as roads, streams and boundaries;
  • Pan and zoom throughout a map;
  • Draw graphic features such as points, lines, ellipses, rectangles and polygons;
  • Draw descriptive texts;
  • Identify features on a map by pointing at them;
  • Select features along lines and inside boxes, areas, polygons and circles;
  • Select features within a specified distance of other features;
  • Select features with an SQL expression;
  • Calculate basic statistics on selected features;
  • Query and update attribute data associated with selected features;
  • Render features with thematic methods such as value maps, class breaks, dot density, charts, events or by Z values;
  • Label features with text from field values;
  • Create new shapefiles;
  • Draw images from aerial photography or satellite imagery;
  • Dynamically display real-time or time-series data;
  • Type in an address and find a location on a map; and
  • Project your data into different coordinate systems.

Creating our first MapObjects application

We need to install MapObjects and the sample data before continuing. During installation, follow the default paths. Proceed once installation is finished.

Loading MapObjects

Start Visual Basic and select ‘New’ project from the dialog box. Now, right-click on the components or select Components under the Project menu. Refer figure 1.



Find ‘ESRI MapObjects 2.3’ in the list of available controls and check the box beside it. Click OK to close the dialog. Notice that a new tool appears in the Visual Basic Toolbox. This new tool is the MapObjects version 2.3 Map control (figure 2).



Adding a Map

Map control is used to display maps.

Add the map control to the form

1. Double-click the map control button in the Toolbox to add a new map to the form; and
2. Resize the map to fill the form (figure 3).




Select the data to be displayed on the Map

You can specify the data that is to be displayed in the map by setting properties in the Map control’s property sheet. 

1. Right-click the mouse on the map to display the context menu;
2. Choose Properties to display the property sheet (figure 4);
3. Click the Add button and locate the folder containing the States sample data. If you selected the defaults when you installed MapObjects, this will be in C:\Program Files\ ESRI\MapObjects2\Samples\Data;
4. Select the States.shp file and then click Open; and
5. Add the file USHigh.shp in the same manner.



Note: There is a more refined way to add different map layers. We need to organize Point maps à Segment maps à Polygon maps, in this order, so that all the layers are visible. Otherwise, if Polygon maps is the top layer, it may hide all the other layers

Set properties for the layers

1. Click the States layer in the Layers list and then click on Properties (figure 5);
2. Click the Color button to select a color for the States layer;
3. Click OK to close the dialog;




4. Select a color for the USHigh layer in the same manner; and
5. Click OK to close the property sheet.

Save the project and run. The output will look as shown in figure 6.



Next, we will add toolbar with pan and zoom functionality.

Adding a toolbar

Visual Basic includes a toolbar control that can be used in conjunction with an ImageList control to display a collection of buttons at the top of a form. See figure 7.

1. Double-click the Toolbox and select MS Windows Common Controls. You will notice that new tools are added to your Toolbox;
2. Double-click the Toolbar button in the Toolbox to add a Toolbar control to the form;
3. Double-click the ImageList button in the Toolbox to add an ImageList to the form; and
4. Resize the map so that it is not obscured by the toolbar.



The ImageList control may obscure the map; however, this is not a problem because the ImageList control will not be visible when your application is running.

Adding images to the ImageList control

1. Right-click the ImageList control to display the context menu;
2. Click Properties to display the property sheet;
3. Click the Images tab (figure 8);
4. Click Insert Picture and locate the folder that contains the sample bitmaps – “C:\ program files\ESRI\MapObjects2\Samples\BitMaps”; 
5. Select the Zoom.bmp file and then click Open; and
6. Add the files Pan.bmp, and Globe.bmp in the same manner.



Set the MaskColor of the ImageList

Setting the MaskColor property of an ImageList control specifies a color that will act as a mask for any images contained by the control. The mask color will not be drawn, resulting in an image with a transparent background.

1. Click the Colors tab (figure 9);
2. In the Properties list, click the arrow and then click MaskColor;



3. Click Dark Cyan in the Color Palette list; and
4. Click OK to dismiss the property sheet.

Associate the ImageList with the toolbar

You can associate the Toolbar control with an ImageList control to provide graphic images for the buttons. 

1. Right-click the Toolbar control to display the context menu;
2. Click Properties to display the property sheet; and
3. In the ImageList box, click the arrow and then click ImageList1. This associates the toolbar with the ImageList control. See figure 10.

Adding buttons to the Toolbar control

In this section, you will add five buttons and a separator to the toolbar. You will set the style of two buttons to Placeholder. Placeholders will be used later.

1. In the Toolbar Control Properties dialog, click on the Buttons tab and then click Insert Button (figure 11);
2. Set the button’s style to Button Group, Image to 1 and its Value to Pressed;
3. Add a second button and set its Style to Button Group and Image to 2;
4. Add two more buttons, the third and fourth buttons, and set their Style to Placeholder.
5. Add a fifth button and set its Style to Separator;



7. Add a sixth button and set its Image to 3; and
8. Click OK to dismiss the property sheet.

Change the MouseDown event

1. Double-click the map to display the Visual Basic Code window; and
2. Add the following code 1 to MouseDown Procedure.

Code 1


Private Sub Map1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Toolbar1.Buttons(1).Value = tbrPressed Then
Set Map1.Extent = Map1.TrackRectangle
ElseIf Toolbar1.Buttons(2).Value = tbrPressed Then
Map1.Pan
End If
End Sub

Selecting the first button in the toolbar sets the mouse to be a zoom tool; selecting the second button lets you use the mouse to pan.

Implement the FullExtent button

1. Double-click the toolbar to display the Code window; and
2. Add code 2 to Toolbar1’s ButtonClick event.

Code 2

Private Sub Toolbar1_ButtonClick(ByVal Button As MSComctlLib.Button)
If Button.Index = 6 Then
Map1.Extent = Map1.FullExtent
End If
End Sub

The ButtonClick event is generated whenever a click occurs on a button in the toolbar. If the index of the button is 6, then the map is zoomed to its full extent.

Test the application. The result would appear just like in figure 12.



Creating ‘Find’ tool

Though the application is capable of displaying maps, there may be instances when we may have to find a certain state on the map. For this purpose, let us build a ‘Find’ tool. The .shp (called as shape file) format is actually a combination of three files, namely:

a. Shape file – which contains different types of polygons, lines and points;
b. Database file – which are the attributes of these shapes; and
c. Index file – which works as a connection between the other two files.

So, we can actually make an SQL query to find the area of our interest. Just add a label “state” and a text box next to it. Add the following code 3 to the text box.

Code 3

Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = vbKeyReturn Then
Dim recs As MapObjects2.Recordset
Dim shp As Object
Dim rect As MapObjects2.Rectangle
Dim exp As String

'build a search expression
exp = "STATE_NAME = '" & Text1.Text & "'"
' perform search
Set recs = Map1.Layers("States").SearchExpression(exp)
'show the results if any
If Not recs.EOF Then
Set shp = recs.Fields("Shape").Value
Set rect = shp.Extent
rect.ScaleRectangle 2
Set Map1.Extent = rect
Map1.Refresh
Map1.FlashShape shp, 3
End If
End If
End Sub

The final application will appear as in figure 13.



The author is studying at the Symbiosis Institute of GeoInformatics. He can be reached at: patil_prasadc@yahoo.com.


Language: VB
Platform: Windows




Added on July 20, 2007 Comment

Comments

Post a comment