Profile | Register | Active Topics | Search | FAQ
You are welcome to post questions, comments and tips in all forums and topics.
And if you think you have relevant information, do not hesitate to make a contribution!

Advanced Search
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 Franson GpsTools
 GpsTools
 Guide # 2 - Add Labels to ShapeFile
 New Topic  Reply to Topic
 Printer Friendly
Digg this
Author Previous Topic Topic Next Topic  

bokelund
Senior Member

121 Posts

Posted - 07/21/2006 :  03:43:24  Show Profile  Reply with Quote

GpsTools 2.30 or later

GpsTools Guide # 2 - How to Add Labels to Your ShapeFile Application

1. Introduction

This guide will attempt to show how to make use of the .dbf-file that comes with the ShapeFile and view the information as label on the Map.

2. Getting Started

First we need to open and view a ShapeFile on a Map using the MapShapeFile.
If you are not familiar with this, have a look at Guide #1 - How to draw a shapefile with GpsTools

3. The .dbf-file

The .dbf-file (dBASE format) stores attribute information about the shapes found in a ShapFile.
The attributes can contain almost any kind of information. However, typical attributes are country name, city names and area.

4. Labels on the Map

The first thing we need to do is to enable the labels. This is done with the ShowLabels property on the MapShapeFile object.
Let's say we have created a MapShapeFile object like this:

// Create a MapShapeFile to draw the contents
mapShapeFile = map1.NewMapShapeFile(shapeFile);

We just need to set the ShowLabels property to true like this:

// Enable to labels. 
mapShapeFile.ShowLabels = true;

4.1. Decide Which Attribute to Show

Each shape can have several attributes. For example a shape describing a country can have attributes of both the name of the country and the population.
To find out what attributes there are to show, we can use the following code:

// Get an attribute ( any attribute will do)
GpsShapeNET.DataRecord record = sf.GetAttribute(1);

if ( record != null )
{
// Loop through the column names
for ( int iColumn = 1; iColumn <= record.Count; iColumn++ )
{
// Add the column names to a ListBox
this.listBox1.Items.Add(record.GetColumnNameByIndex(iColumn));
}
}
}

Now the column names have been added to the ListBox. We are ready to selected an attribute to show.

4.2. Create a LabelTemplate

The next step is to set the LabelTemplate on the MapShapeFile object. The LabelTemplate is used to set the appearance and behavior of the labels.

4.2.1. ColumnIndex

We will begin with setting the ColumnIndex on the LabelTemplate. The ColumnIndex is used to select which of the attributes associated with
this shape to show as a label.

A good way set the ColumnIndex is to use the SelectedIndexChanged event on the list box like this:

private void listBox_SelectedIndexChanged(object sender, System.EventArgs e)
{
// Set the attribute (column) to show
// Note! The ColumnIndex property is 1-based.
mapShapeFile.LabelTemplate.ColumnIndex = this.listBox.SelectedIndex + 1;

// The changes do not take until we have updated the Map.
map1.Update();
}

4.2.2. Transparent

The Transparent property decides if there will be a box around the label or not (default).
We can use a CheckBox to set this property and again use an event.

private void cboxTransparent_CheckedChanged(object sender, System.EventArgs e)
{
// Decides if there will be a box around the labels or not. 56
mapShapeFile.LabelTemplate.Transparent = this.cboxTransparent.Checked;
map1.Update();
}

If the Transparent is set to false. The Background property is used to set the background color of the box and the Border is used to set the
color of the border surrounding the box.

Here is an example on how to update the border color using a ColorDialog

private void btnBorder_Click(object sender, System.EventArgs e)
{
this.colorDialogBorder.ShowDialog();

// Set the 3 colors from the Colordialog.
mapShapeFile.LabelTemplate.Border.Red = this.colorDialogBorder.Color.R;
mapShapeFile.LabelTemplate.Border.Blue = this.colorDialogBorder.Color.B;
mapShapeFile.LabelTemplate.Border.Green = this.colorDialogBorder.Color.G;

// The changes do not take until we have updated the Map.
map1.Update();
}

4.2.4. Other Properties.

There are two properties that sets the behavior of the label drawing, namely the Overlapping and the Duplicates properties.
You can use a CheckBox and the CheckedChanged event to set theses properties.

5. The Sample

All the code in this guide is demonstrated in ShapFileViwer_LabelGuide sample which can be downloaded at the end of this guide.

Feedback
Feel free to commnet this guide in this topic. What was good and what was bad?

Resources

1. GpsTools SDK (at least version 2.30).
2. A ESRI compatible shapefile with a .dbf-file.
3. The ShapeFileViewer_LabelGuide sample. Download here!

Allegro
New Member

4 Posts

Posted - 07/21/2007 :  19:33:37  Show Profile  Reply with Quote
Hi bokelund,

super n00b here... try to run thru' this guide and get the following error when "Build":

'GpsViewNET.Map' does not contain a definition for 'CancelDrawOnUpdate' - line 112, column 14.

what's wrong?

any suggestion/help is greatly appreciated. TIA

allegro

Allegro
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
GpsGate Forum © 2010 Franson Technology AB Go To Top Of Page
Powered By: Snitz Forums 2000 Version 3.4.04