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

MapPoint 2011

Programming MapPoint in .NET

MapPoint Book

  Spatial Community
SVG Tutorials
MapPoint

Map Visitors

  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
-bingmaps: RT @ChrisPendleton: In case you missed my #location2012 talk, @OrdnanceSurvey has posted a recording: http://t.co/p5dcMTpN
-Upcoming WhereCamp Berlin 2012
-MapPoint for World Maps?
-Beginner stuck-what am I doing wrong?
-Mappoint 2011 Europe -> tooltip and update possibilities
-bingmaps: FREE BING MAPS EVENTS IN GERMANY: München https://t.co/pFJZxDdE Köln https://t.co/zerD99GK #bingmaps
-bingmaps: FREE EVENT: @BingMaps and @Beyond present a vision of the London 2012 Games: The Most Connected Games Ever. https://t.co/yEJ493OR
-Need Several Radius imported into a map
-Automatic generation of Drive Time zones around PushPins
-C# Code To Load Older COM Version
-Successful MapPoint Web Service (MWS) to Bing Maps Conversion
-bingmaps: @Sonray Please direct your question to our technical forum which is monitored by our developers: http://t.co/dEfOwFBC
-bingmaps: New Bird’s Eye Imagery & Streetside Coverage on Bing Maps! http://t.co/iJPu0McQ
-I have an excel spreadsheet that has number of houses on roads, can I link this to mappoint?
-Microsoft MapPoint 2011 Construction Data Update May 4th, 2012
-Mappoint 2011 and the built in database
-Using Excel and mappoint
-Extracting ZipCode With Lat/Long
-DataLink Problem!
-bingmaps: Integrating Bing Maps With WPF:http://t.co/HzdriaMs
-Mapping Bears
-Route Segments do not add up to complete route
-plotting pie charts by sales numbers
-Importing Live Data into Mapoint
-number of houses on a road?
 Resources
Browse GIS books and periodicals
Find a MapPoint Partner or Consultant
Real Estate Thornbrook Homes for Sale


Want Your Site To Appear Here?

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