Recently, I started working on a new project where people will need to enter city/state, as well as university details. The reasons are unimportant here except to say that entering this into a database using an input field is problematic if you need to use the database later in a programmatic fashion. It only takes one person accidentally entering their city as “New Yrok” for your database calls to start screwing up. The best thing you can do is give people a choice, and force them to use it.
I debated a while on how to do this, and even found other people looking for the same thing. Following up on that, I downloaded the citynames GIS database from the National Weather Service and started working. The result was a full suite of US city names– over 41,000 records!
I created my first Rails plugin to incorporate this data into an existing Rails application. The plugin is hosted on Github as geoinfo. This is as much an attempt to learn about creating plugins/gems as it is an attempt to provide a useful library for someone. Those of you who are looking to use it, check things out first.
Basically, the important part is in lib/db. In that file are some YAML files holding the actual database of 41.5k cities and US states and outlying areas as well as Canadian provinces and territories. In lib/db/migrate are the migration files for geoinfo_cities and geoinfo_states database tables. I chose this naming convention to avoid clashes with other tables. Thus, if you want to use this data without the plugin, grabbing the lib/db folder is really all you need to do.
In the future, I hope to create custom methods for generating smart AJAXy dropdowns so that people can choose a state and have a city dropdown populated from that. I’d also like to include postal codes and more countries, because others may find that useful. We’ll see how much I can add– given that hunting down the data is the hard part.
Details, updates, changes, etc. will mostly be housed on Github. Feel free to fork and update/modify and send pull requests. If you have data to add (i.e. other countries or zipcode info), I’d love to collaborate.