spacer1
spacer2 1_1 1_2
2_1
 Subscribe
 The MP2K Update!
 
 
 
 Magazine
Front Cover
What's New
Articles
Toolbar
News
Sample Data
Gallery
Advertise
About
 Features
MapPoint 2006
Press Releases
MapPoint Forums
Companies
Link to MP2Kmag
Wish List
MapPoint Trial
Authors
 Sponsors
 Order

MapPoint 2006

Programming MapPoint in .NET

MapPoint Book

  Spatial Community
ViaVirtualEarth
SVG Tutorials
MapPoint

Map Visitors

 Direct Media

SMALL BUSINESS IT
In a world of faceless organisations, Connect provides a personal service to help you get rid of any IT frustrations.

printer cartridges
Don't settle for any old supplier. Get the best ink for the best price. See our great range of printer cartridges at competitive prices.

Cheap flights to Greece
Check Holiday Hypermarket for the latest cheap flights to Greece. Save your money for memorable souvenirs. See online today!

Holiday to Greece
Book a holiday to Greece today! There is something for everyone with a famous ancient history but also fantastic weather.

property Bulgaria
Ski resort and beach apartments for sale in beautiful Bulgaria. Visit online For further details.

Holiday Deals
Don't miss out on our fantastic holiday deals to a range of exotic destinations. We use over twenty top travel agents and tour operators to bring you great value holiday deals.

Cheap Turkey Holidays
The best time of year to sneak in cheap Turkey holidays is during the off-peak season. Check out the great deals available during April, May or October. For other tips on how to bag a great value holiday simply check online at ulookubook.com

  ARTICLES  


Formatting Coordinates with DmsFormat

If you noticed you're rewriting that coordinate formatting routine each time you need a slightly different output, DmsFormat is for you - next time, you'll simply use a different format string.

Introduction

In a nutshell, what DmsFormat does is accept a coordinate value (latitude or longitude in MapPoints internal decimal degree notation - a double, that is) and convert it into a string, given a pattern string that controls formatting and conversion.

Signature and parameters

The actual signature of the function is as follows:


Public Function DmsFormat(dblVal As Double, strPattern As String, _
                          blnIsLatitude As Boolean, _
                          blnForceDp As Boolean) As String

Let's have a quick look at the parameters:
  • dblVal - this is the latitude or longitude value to be converted

  • strPattern - the format string - will be explained in detail below

  • blnIsLatitude - pass True if the coordinate is a latitude - used to format the compass point

  • blnForceDp - pass True to force a decimal point instead of a comma. If your country settings in control panel specify a comma as decimal separator, DmsFormat will format respect this, unless you force it to be a period. Use this e.g. if you want to write DmsFormat data to file in a format that is not locale-dependent.

  • Return value - DmsFormat returns its result in string format, for further processing.

Source code

The source code for DmsFormat can be downloaded here. It is available in the form of a .BAS module that you can add to your own projects (alternatively, just paste the routine into your code). To download the file, right-click on the link above, and select "Save target as ..."

The pattern string

The most important parameter is the pattern string. DmsFormat will scan this string, replacing two-character placeholders in angular brackets with certain values, and leaving the rest as it is.

For example, in the following pattern string:

"A latitude of <Di> degrees, <Mi> minutes and <Sd> seconds <Cp>"

<Di> will be replaced by integer degrees, <Mi> by integer minutes, and <Sd> by decimal seconds. <Cp> will be replaced by the compass point, i.e. S or N depending whether dblVal is negative or not. (And assuming blnIsLatitude is passed as True)

With a dblValue of 49.00220, DmsFormat would return the following result:


"A latitude of 49 degrees, 0 minutes and 7.9 seconds N"
The following table lists the placeholders recognized by DmsFormat (see "Format specification" below for an explanation of the "default format" column):

PlaceholderDescriptionResult for dblVal = -1.23456Default format
<Di> Integer (whole) degrees, unsigned 1 "##0"
<Mi> Integer (whole) degrees, unsigned 14 "#0"
<Si> Integer (whole) seconds, unsigned 4 "#0"
<Dd> Decimal degrees, unsigned 1.23456 "##0.0####"
<Md> Decimal minutes, unsigned 14.074 "#0.0##"
<Sd> Decimal seconds, unsigned 4.4 "#0.0"
<Cp> Compass point (W, E, N, or S) S (assuming blnIsLatitude passed as True) "W|E|S|N"

Format specification

DmsFormat provides "sensible" formatting of the values that get substituted for the two-character placeholders. In general, integer values do not get any decimal places nor a decimal point, and the decimal values get "enough" of them - five for Dd, three for Md, and one for Sd.

But if you want, you can override these default formats, and provide your own. Simply append an equals sign to the two-character placeholder (inside the angular brackets), and add a format specification in the form understood by the VB " Format" function.

Example: You'd like to output integer degrees and decimal minutes, with a compass point. The degrees should be three digits with leading zeroes, the decimal minutes two digits (with leading zeroes) before, and three after the decimal point. This is the pattern string you would use:

"<Di=000> degrees and <Md=00.000> minutes <Cp>"

and this is what the result would look like, assuming dblVal = -1.23456 and blnIsLatitude set:

"001 degrees and 14.074 minutes S"

A special case: The Compass point format

All placeholders represent numerical values, except for the compass point. It has a special format string too. Instead of the defaults W,E,N, and S, you can provide other names for the compass strings by listing them, separated by pipe characters, after the "Cp".

Example: to get more verbose compass points, you could use the following:

"<Cp=West|East|North|South>"

Note that which of those four names is used depends on the sign of dblVal, as well as the blnIsLatitude flag. The names have to be in the exact order shown above: WENS.

Default formats

Every placeholder has a default format that will be used if you do not override it using the assignment syntax just described. The placeholder table above has a column that lists the default format specifications for every placeholder.

Controlling field widths

The desired width of a replacement can be specified by appending a colon (:) and the width to the format specification. For example, to output integer degrees without leading zeroes right-justified in a field that is always 5 characters wide, use the following format:

"---<Di=##0:5>---"

(The dashes are used to make the alignment more visible)
The output would be the following (for dblVal = -1.23456):

"---    1---" (Note the additional four spaces in front of the 1.)

To left-justify the value, use a negative width. For example:

"---<Di=##0:-5>---"

(The dashes are used to make the left justification more visible)
The output would be the following (for dblVal = -1.23456):

"---1    ---" (Note the additional four spaces after the 1.)

Note that the field width that you specify is always observed - if the resulting value is too big to fit, it will be truncated.

Special escape placeholders

If you need to include the less-than (<) and greater-than (>) characters themselves inside your pattern string, use <lt> and <gt> to represent them.

Tips, tricks, and pitfalls

  • The placeholders are case-insensitive - it doesn't matter if you use <cp>, <CP>, or <Cp>.

  • Undefinded placeholders (e.g. <xy>) will be replaced by 99.99

  • In the format specification part, anything the VB Format function understands can be used - for example, <Dd=General number> will work.

  • If you need a signed version of the coordinates (negative for west and south, positive for east and north), use e.g. <Cp=-|+|-|+><Dd>;

  • If you would like to use the default format specification, but want to specify a width, add the equals sign immediately followed by a colon. Example: <Dd=:8>

  • If you specify a width, make sure it is large enough for the value to be displayed, otherwise, the value will be truncated.

  • Avoid using thousands separators in the format specification, as they can create havoc in combination with blnForceDp

Discuss this story in the forum.

Author: Gilles Kohl
Email: gilles(AT)_deletethisincludingunderlines_compuserve.com
URL: http://www.procad.de
Gilles Kohl, a native of Luxembourg living in Germany, is a software development lead with PROCAD GmbH of Karlsruhe, Germany.

Mapping and especially GPS-related topics are a hobby - Gilles enjoys developing solutions for Microsoft MapPoint and his favorite outdoor occupation is confluence hunting.

As always, please direct questions to the newsgroup.



Google
 
MP2Kmag Internet


 Recent Discussion
-Virtual Earth on CSI: New York
-Helping out after the China quakes
-Coastal Boundaries
-Mapping Earthquakes in China
-An invalid argument was encountered.
-Navman Wireless Oem Solutions Enhances Leading Line Of Gps Receiver Modules
-T-mobile UK
-Virtual Earth and OpenLayers
-Reminder: Microsoft Virtual Earth Partner Webinar and Offer for Free Training
-Virtual Earth Quarterly Technical Briefing
-Customize User Page
-Information about area
-Territory information
-Gps task pane in ActiveX
-Worldwide Telescope Now Available!
-c++ - setting the color of a line
-Initial Costa Rica MapServer map is up & running!
-Satellite Images of Myanmar Cyclone Damage
-Virtual Earth Loyalist: Aerial View
-Leaving messages on the Virtual Earth for Government blog site
-Virtual Earth Webcast: Adding Mapping Capabilities to your Applications with Virtual Earth
-Get new Lat & Lon from speed
-Integrating Virtual Earth Maps and Excel 2007
-Zillow.com
-ADODB Recordset to MapPoint.DataSet
 Resources
Browse GIS books and periodicals
Best Car Contract Hire Lease Save
Find a MapPoint Partner or Consultant
Real Estate Columbia MO Homes for Sale


Want Your Site To Appear Here?

   © 1999-2008 MP2K. Questions and comments to: website@mp2kmag.com
  Microsoft and MapPoint 2002/2004/2006 are either trademarks or registered trademarks of Microsoft.

• Replacement Cordless Phone Batteries from DSMiller
Improve your career with a computer degree LocalEDU.com
• Tim Cohn's Marketing Strategy Consultancy
• Buy Toner, Ink Cartridges & Drums at Marquee Office Solutions
• Looking for Dry Erase Boards? See Dry Erase Boards
• 5339 - We Know Bikes: Road Bikes / Triathlon Bikes / Mountain Bikes / BMX Bikes
• Buy Your Car Used Cars, Car Lease and Contract Hire