MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Geocoding using Mathematica

On Nov 29, 6:33 am, "Coleman, Mark" <Mark.Cole... at>
> There are a number of freely available web-based geocoding
> services.  Would it possible to invoke them via Mathematica?

Sure, Mathematica has built in features that make this easy.

A good web-based geolocator will allow you to access its
data via a URL with a query string and give you the option
to retrieve it's output as XML.  Thus, you can Import from a
URL to symbolic XML, which Mathematica can easily parse
using pattern matching., for example, returns three results for "The
White House" in Washington, DC.

-- In --
result = Import[
#[[3, 1]] & /@ Cases[result,
  XMLElement["name", __], Infinity]

-- Out --
{The White House, The White House Visitor Center,
 The Hay Adams across from the White House}

It appears that the first of these is the actual Presidential
residence.  Here's the latitude and longitude.

-- In --
{latString, lngString} = First /@ Map[#[[3, 1]] &,
    {Cases[result, XMLElement["lat", __], Infinity],
    Cases[result, XMLElement["lng", __], Infinity]},

-- Out --
{38.8976118, -77.0363658}

These results are strings, so be sure to use ToExpression,
if you want to do any numeric work.  On the other hand, it
might be nice to leave them as strings.  For example, we can
pass them straight back to geonames to get the elevation.

-- In --
Import[""; <>
  latString <> "&lng=" <> lngString]

-- Out --

The result is a simple string indicating the elevation in

Have fun,

  • Prev by Date: Re: "vector" Map[] / functional outer product?
  • Next by Date: Re: Manipulate is sluggish?
  • Previous by thread: Re: Geocoding using Mathematica
  • Next by thread: Recursive function