GeographyWizard

This class aims to provide you with answers to most geographical questions you frequently encounter on a website. It is US and Canada centric now, but can be expanded to include other geographical regions.

How to use

Step 1: Import translation table data into a MySQL database

mysql MyDatabase < Setup\MySql\SetupGeographyWizard.sql

Step 2: Provide a connection string to the translation table

<?xml version="1.0"?>
<configuration>
	<connectionStrings>
		<add name="SharpNick.GeographyWizard"
			connectionString="Datasource=localhost;Database=MyDatabase"
			providerName="MySql.Data.MySqlClient"/>
	</connectionStrings>
</configuration>

Step 3: Use!

Console.WriteLine(GeographyWizard.GetCountryName("US"); // United States
Console.WriteLine(GeographyWizard.TranslateState("Ontario"); // ON
Console.WriteLine(GeographyWizard.IsStateInCountry("Alberta"), "US")); // false

Methods

string GetCountryCodeByState(string state)

Console.WriteLine(GeographyWizard.GetCountryCodeByState("Ontario")); // CA

string GetCountryName(string countryCode)

Console.WriteLine(GeographyWizard.GetCountryName("DE")); // Germany

bool IsMilitaryAddress(string state, string country)

Console.WriteLine(GeographyWizard.IsMilitaryAddress("AF", "US")); // true

bool IsPostalBox(string street, string country)

Console.WriteLine(GeographyWizard.IsPostalBox("P.O. Box #1234", "US")); // true
Console.WriteLine(GeographyWizard.IsPostalBox("P.O. Box #1234", "DE")); // false

bool IsStateInCountry(string stateCode, string countryCode)

Console.WriteLine(GeographyWizard.IsStateInCountry("CA", "US")); // true
Console.WriteLine(GeographyWizard.IsStateInCountry("AB", "US")); // false

string TranslateCountry(string input)

Console.WriteLine(GeographyWizard.TranslateCountry("USA")); // US
Console.WriteLine(GeographyWizard.TranslateCountry("Britain")); // UK
Console.WriteLine(GeographyWizard.TranslateCountry("Mars")); // null

string TranslateState(string input)

Console.WriteLine(GeographyWizard.TranslateState("Alberta")); // AB
Console.WriteLine(GeographyWizard.TranslateState("Masachusets")); // MA
Console.WriteLine(GeographyWizard.TranslateState("Mars")); // null

Last edited Aug 28, 2010 at 2:48 PM by weiwong, version 9

Comments

No comments yet.