Is there a library for normalizing postal codes? - normalization

Users of my application are asked for their country and postal code. I want to use the postal codes as a key in data store. Because the postal codes are entered by the user they can have been entered in different forms. For example a Canadian user might have entered:
A1A 1A1
a1a 1a1
A1A1A1
a1A 1A1
etc...
Now I can make a normalization function for Canadian postal codes that chooses one canonical form (for example, all uppercase without spaces) and convert the postal codes to the canonical form before saving it as a DB key.
But, that only covers Canada. I don't want to reinvent the wheel. Is there a library or API out there that can normalize (and possibly validate) postal codes for all (or many) countries?
Thanks!

It's pretty easy to do with Regular Expressions and string functions built into your language.
The CountryInfo table from Geonames contains Postal Code Regex and Format values for all countries with postal codes:
http://download.geonames.org/export/dump/countryInfo.txt
Don't forget that H0H 0H0 is a valid postal code for Santa Claus.

The important thing to remember with Postal Codes is that not all countries have them, and as addresses get added, updated or removed, Post Codes can get re-coded.
With that in mind, if you want to ensure the data you're capturing is accurate, the best solution is to usually use a paid for third party library or web service to validate this. There are a number of companies that provide services to validate the entire address, and not just the post code. These will vary in global coverage and cost, depending on your needs.
I work for a company which provides address validation for most countries world wide through a consistent interface, either a web service or API you can integrate with.
You can try out some examples here: http://www.qas.co.uk/knowledge-centre/product-information/address-postcode-finder.htm

Related

Given an address, how can I get the congressional district?

Is there a library or service that returns the US federal congressional district given a US address?
You can do this on govtrack.us. I am not sure if there is a code framework for this, but you could probably write one from this information.
Full disclosure - I work for a company that also provides congressional district data - smartystreets.com
Keep in mind that this information changes often and I have been looking for the last 4.5 years and haven't yet pinpointed an actual, absolutely reliable source for this data. All of the companies I have looked at, including mine, say that their source is the US Postal Service but no one in the US Postal Service has been willing to tell me how frequently the data is updated within their system and what their source might be. So, just be aware.
I'm one of the creators of Geocodio and we had this problem ourselves. So we made it part of our geolocation tools!
You can use the Geocodio API or spreadsheet upload to look up Congressional districts, Representative/Senator contact information, etc. Docs: https://www.geocod.io/docs/#congressional-districts
You can use the Sunlight Foundation Congress API, which is free to use. Go to: http://tryit.sunlightfoundation.com/congress You can determine congressional district by zipcode (not completely reliable since zipcodes can traverse multiple districts) or by latitude/longitude. Just use Google geocoding to retrieve lat/lng from your address, and then you can look up the congressional district.
The whoismyrepresentative.com/api site can translate a zipcode into a congressional district. You can do a call with the url below, replacing 10038 with your zipcode.
http://whoismyrepresentative.com/getall_mems.php?zip=10038&output=json
Hope that helps!
**Also note that the sunlightfoundation site returns a 404 error and govtrack.us does not have an api for searching for congressional districts.

Geolocation and getting a city from an input address (Rails)

The app I'm building needs to be able to match up users to events based on the city/town they're in. I'm still relatively new to Rails and completely new to Geolocation and using locations in an app. I'd figured on a design where users have one or many cities, and events would have one city which I'd hoped to extract without specifically asking the user for it, by getting it from the event address entered.
Mostly to provide some outside checking to help get the address entered correctly and consistently, but also to show a map, I installed this jquery address picker (https://github.com/sgruhier/jquery-addresspicker). Unfortunately the data returned by Google doesn't include a city but a "locality" or an "administrative area" that doesn't correlate reliably to city names. The localities being returned are more like what we in my home town would call "suburbs". What I need to procure is a city so I can allow users to search all events in their city rather than just the ones in their suburb.
Can anyone offer advice on how I could go about doing this? Many thanks.
Edit: Should maybe add that I'm wanting to do geocoding client-side so I don't run into problems with Google Maps limits or have to pay for geocoding etc.
There are some gems that provide you with that and may others geo related features, like calculating distances.
Here are the 2 most famous: https://github.com/alexreisner/geocoder and https://github.com/imajes/geokit
In the future I highly recommend you to head to https://www.ruby-toolbox.com/ to see what is available as a gem already and see what is the most popular at the moment.
For raw address info, use Google Maps API Reverse Geocoding which accepts lat/lon inputs and returns street address components. Modern browsers support location awareness (geolocation), with user permission, and will give you a lat/lon that "tends to be close" to where the browser is. That will probably get you a correct city/town in most cases.
The maps API is part of Google's broad suite of API tools -- there are gems that handle any Google API (well, most of them), or check out Google Maps for Rails, which will at the very least give you a good head start on how to use the API.
But if you're looking to validate postal code, this method will come up short, since the location awareness will vary in accuracy depending on browser, device (more accurate for mobile), the connection, population density, network coverage, and so on. Also, calling the
If you can get GPS-accurate lat/lon then it will be much more accurate ... except in some cases like in large cities, a single building will have its own postal code, so a few feet one way or the other might matter.

setting up a countries table for Rails i18n

In preparation to internationalize our Rails app, I'm finding we need to create a countries table and model that would contain (among other things) the default locale for each country. I found a 4 year old resource http://snippets.dzone.com/posts/show/1727 that has most of what I want, but not locale. Does everybody have to piece this part together by hand or am I overlooking some easier way to map country to a default locale.
P.S. We already look up a users country using GeoIpCountry which works nicely, but that table only provides country name and 2-letter abbreviation. I also want to use country to assign the users default locale, currency, and 3-letter abbreviation (needed by a third party integration)
The best way to identify user's locale is to look up the HTTP request's Accept-Language setting.
e.g., mine says: Accept-Language: fr-BE
Which gives you my locale, my language (fr: french) and my country (BE: Belgium).
Also, remember that locale identification based on country is wrong in many countries, such as mine: Many websites identify my country (Belgium) correctly and assume a default locale: nl-BE because 60% of belgian people speak Dutch (nl). Hence these sites have it wrong for 40% of belgian visitors (including me).
Their site is a bad experience for me because it displays in a language which is not mine and the first thing I must do is look for their tiny language popup menu.
Had they simply looked up my Accept-Language, they would have shown me their site in my language right away.
have a look at: https://github.com/mm1/country-list

Resolve city code from user input

There are a lot of geocode services out there (like http://geo-autocomplete.googlecode.com/svn/trunk/demo/ui.demo.html for example) where user can write a location (or part of it) and it will be resolved to real existing location.
There is also a lot of info provided with the search result (like country ISO code, coordinates, etc..), but none of the services seem to provide country and city code.
What I mean is a code used to phone to certain are. For example Country code for Germany will be 49 and for city of Dusseldorf will be 211.
Is there any service, where I can get this info from the user input. Or is there any way to combine the two. For example I get city name from google geocode service and the try it on some city codes database. If yes, can anyone please provide me with links.
You can use for instance Yahoo's GeoPlanet. For more elaboration on services to solve your problem you might want to check out this answer on gis.stackexchange.com, which seems to cover the kind of service you need.

All Countries States Cities and Zipcodes in a Application

Is there a way getting all countries, states,cities zipcodes/ in one single database.
I have been looking all over.
I discovered geonames.org, which has I guess all of the content. But there is no way to fetch that data directly.
I am using rails. If there any helpers or Plugins, it'd be great. It would be awesome if there is a complete resource in any parsable format which I can use to get this database in place.
Please recommend if you any thanks
I do have the iso country code list but no States for some countries.
Thanks in advance
Thanks Guys,
I got all the the countries and states of all of them, but no zip codes.
http://27.org/isocountrylist/iso_country_list.sql
This contains all countries, and there iso-3166 codes ( Siberia code needs to be changed from "CS" to "RS")
And here is the FIPS file for countries with states
http://www.maxmind.com/app/fips_include
For zip code validation I am using
http://www.geonames.org webservices
There is a geonames ruby gem as well, which makes it really simple to do zipcode validations
Hope that helps anybody who needs this information, worth keeping it handy for web projects.
Another Link ( thanks #laise ): http://dev.maxmind.com/static/csv/codes/maxmind/region.csv
The Carmen plugin is almost certainly what you're looking for.
This will take a little legwork and data munging, but it's not too bad.
Countries and country codes are easy to come by. Sounds like you already have them.
Not all countries are divided into states. You may have to simply address these one by one-- there may be some combined reference, but not that I know of.
U.S. cities are available, and look for things like FIPS codes. There are commercial data providers that will provide you clean, well-organized data, but they are charging for it.
The post office has some databases with all the zip codes. Note that these charge periodically, and a single zip code doesn't imply a single single (far from it!). And it may not be necessary to actually have a database of all of them, as there are zip verification/correction services available for free.
For loading the data, check out a recent railscast on the subject.
I was looking for the same and found this resource. I hope somebody serves well as me.
http://dev.mysql.com/doc/world-setup/en/index.html
For those using the FIPS 10-4/FIPS 5-2 standard, I have been maintaining the NGA GEC (formerly FIPS 10-4/5-2) Excel spreadsheet (original format) for 1st level administrative divisions of countries. The spreadsheet is currently updated to Update 15 (March 2014):
http://www.opengeocode.org/archive.php

Resources