5,783,659 members and growing! (16,392 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Communication Foundation » General     Intermediate License: The MIT License

GeoNames .NET WCF Client

By Dennis JD Myren

A .NET WCF client for accessing the GeoNames services
C# 3.0, C#, WCF, Dev

Posted: 2 Nov 2008
Updated: 5 Nov 2008
Views: 4,144
Bookmarked: 29 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
8 votes for this Article.
Popularity: 3.37 Rating: 3.74 out of 5
0 votes, 0.0%
1
1 vote, 12.5%
2
1 vote, 12.5%
3
1 vote, 12.5%
4
5 votes, 62.5%
5

Introduction

GeoNames is a geographical database available under a Creative Commons attribution license. This data is accessible free of charge through a number (30+) of Web services.

The Web services include direct and reverse geocoding, finding places through postal codes, finding places next to a given place, weather observations for a given location, and finding Wikipedia articles about neighbouring places, etc.

Background

I have recently been working with the GeoNames services, both from JavaScript and from .NET. Among other things, we have been working with postal code auto-completion on the Web using JavaScript, and server-side postal code validation using .NET.

All these cool GeoNames services could definitely be used in many different contexts and applications. I have some ideas myself of UI controls and widgets that I would like to develop using GeoNames as data provider.

Therefore, I developed this WCF client for accessing the GeoNames services from .NET in a uniform, easy object-oriented manner.

Using the Code

These are just a few C# examples of using the client.

All the services are available through a single class, the GeoNamesClient (inherits System.ServiceModel.ClientBase<T>). We first create an instance of this object:

  using ( GeoNamesClient client = new GeoNamesClient ( ) )
  {
    ...
  }

Once the GeoNamesClient is created, any service may be called through this object. The first example shows how to retrieve all the cities within a geographical bounding box:

  double north = 44.1, south = -9.9, east = -22.4, west = 55.2;
  var cities = client.GetCities ( north, south, east, west );
  foreach ( GeoCity city in cities )
  {
    // GeoCity has many more properties to choose from...
    Console.WriteLine ( "City, Name={0}, Population={1}", city.Name, city.Population );
  }

The next example shows how to find weather observation for a given location:

  double latitude = 42, longitude = -2;
  var response = client.FindNearbyWeather ( latitude, longitude );
  GeoWeatherObservation weather = response.WeatherObservation;

  // GeoWeatherObservation has many more properties to choose from...
  Console.WriteLine ( "Current temperature is {0} degrees
		(delivered from the {1} weather station)",
    weather.Temperature, weather.StationName );

The client also provides overloaded methods for specifying culture, service verbosity, and controlling the max number of records to return for each service. The example shows how to search any geographical toponym that matches "london," with Spanish culture settings (place names, etc.) and limit the number of returned results to 10:

  GeoToponymSearchCriteria criteria = new GeoToponymSearchCriteria ( );
  criteria.Query = "london";
  criteria.Culture = "es";
  criteria.MaxRows = 10;
  var response = client.SearchToponyms ( criteria );
  foreach ( GeoToponym toponym in response )
  {
    // GeoToponym has many more properties to choose from...
    Console.WriteLine ( "Found toponym match, name:{0}", toponym.Name );
  }

Updated: Added support for geocoding addresses. This new example shows geocoding an address to determine it's spatial coordinate, and then pass coordinate to FindNearbyPostalCodes in order to find nearby postal codes for this address:

  string address = "1600 Amphitheatre Parkway Mountain View, CA  94043";
  double latitude, longitude;
  if ( client.TryGeocode ( address, out latitude, out longitude ) )
  {
    var response = client.FindNearbyPostalCodes ( latitude, longitude, 10 );

    foreach ( GeoPostalCode code in response )
    {
      ...
    }
  }
  else
    Console.WriteLine ( "-- Geocoding failed --" );

Conclusion

If you need to access GeoNames services from your .NET application, you might want to look into my client. It supports all the available services, through a nice, object-oriented API.

History

  • 2008-11-02 1.0.0.0
  • 2008-11-04 1.5.0.0 Added support for geocoding

License

This article, along with any associated source code and files, is licensed under The MIT License

About the Author

Dennis JD Myren


Swedish Software Engineer based in Oslo, Norway.
Background in C++/PASCAL.
Working with .NET technologies along with XML since 2001.
Occupation: Software Developer (Senior)
Location: Norway Norway

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralGreat work! A possible simple enhancementmembersandeep_111:12 3 Nov '08  
GeneralRe: Great work! A possible simple enhancementmemberDennis JD Myren12:43 3 Nov '08  
GeneralRe: Great work! A possible simple enhancementmemberDennis JD Myren10:14 5 Nov '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 5 Nov 2008
Editor: Sean Ewington
Copyright 2008 by Dennis JD Myren
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project