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!