Skip to content

Instantly share code, notes, and snippets.

@mbleigh
Created April 5, 2011 03:55
Show Gist options
  • Save mbleigh/902990 to your computer and use it in GitHub Desktop.
Save mbleigh/902990 to your computer and use it in GitHub Desktop.
The model and generation code to pull the 100 biggest US cities out as geographical models for a Rails app.
# Data culled from Wikipedia. Could be cleaner, but this is nice and simple.
major_cities = [["New York", "New York"], ["Los Angeles", "California"], ["Chicago", "Illinois"], ["Houston", "Texas"], ["Philadelphia", "Pennsylvania"], ["Phoenix", "Arizona"], ["San Antonio", "Texas"], ["San Diego", "California"], ["Dallas", "Texas"], ["San Jose", "California"], ["Jacksonville", "Florida"], ["Indianapolis", "Indiana"], ["San Francisco", "California"], ["Austin", "Texas"], ["Columbus", "Ohio"], ["Fort Worth", "Texas"], ["Charlotte", "North Carolina"], ["Detroit", "Michigan"], ["El Paso", "Texas"], ["Memphis", "Tennessee"], ["Baltimore", "Maryland"], ["Boston", "Massachusetts"], ["Seattle", "Washington"], ["Washington", "District of Columbia"], ["Nashville", "Tennessee"], ["Denver", "Colorado"], ["Louisville", "Kentucky"], ["Milwaukee", "Wisconsin"], ["Portland", "Oregon"], ["Las Vegas", "Nevada"], ["Oklahoma City", "Oklahoma"], ["Albuquerque", "New Mexico"], ["Tucson", "Arizona"], ["Fresno", "California"], ["Sacramento", "California"], ["Long Beach", "California"], ["Kansas City", "Missouri"], ["Mesa", "Arizona"], ["Virginia Beach", "Virginia"], ["Atlanta", "Georgia"], ["Colorado Springs", "Colorado"], ["Omaha", "Nebraska"], ["Raleigh", "North Carolina"], ["Miami", "Florida"], ["Cleveland", "Ohio"], ["Tulsa", "Oklahoma"], ["Oakland", "California"], ["Minneapolis", "Minnesota"], ["Wichita", "Kansas"], ["Arlington", "Texas"], ["Bakersfield", "California"], ["New Orleans", "Louisiana"], ["Honolulu", "Hawaii"], ["Anaheim", "California"], ["Tampa", "Florida"], ["Aurora", "Colorado"], ["Santa Ana", "California"], ["St Louis", "Missouri"], ["Pittsburgh", "Pennsylvania"], ["Corpus Christi", "Texas"], ["Riverside", "California"], ["Cincinnati", "Ohio"], ["Lexington", "Kentucky"], ["Anchorage", "Alaska"], ["Stockton", "California"], ["Toledo", "Ohio"], ["St Paul", "Minnesota"], ["Newark", "New Jersey"], ["Greensboro", "North Carolina"], ["Buffalo", "New York"], ["Plano", "Texas"], ["Lincoln", "Nebraska"], ["Henderson", "Nevada"], ["Fort Wayne", "Indiana"], ["Jersey City", "New Jersey"], ["St Petersburg", "Florida"], ["Chula Vista", "California"], ["Norfolk", "Virginia"], ["Orlando", "Florida"], ["Chandler", "Arizona"], ["Laredo", "Texas"], ["Madison", "Wisconsin"], ["Winston-Salem", "North Carolina"], ["Lubbock", "Texas"], ["Baton Rouge", "Louisiana"], ["Durham", "North Carolina"], ["Garland", "Texas"], ["Glendale", "Arizona"], ["Reno", "Nevada"], ["Hialeah", "Florida"], ["Paradise", "Nevada"], ["Chesapeake", "Virginia"], ["Scottsdale", "Arizona"], ["North Las Vegas", "Nevada"], ["Irving", "Texas"], ["Fremont", "California"], ["Irvine", "California"], ["Birmingham", "Alabama"], ["Rochester", "New York"], ["San Bernardino", "California"]]
puts
major_cities.each do |city|
loc = city.join ', '
print "\n- Creating '#{loc}' market..."
if Market.generate(loc)
print "done."
else
print "FAILED!"
end
end
class Market
include MongoMapper::Document
key :city, String, :required => true
key :state, String, :required => true
key :location, Location, :required => true
key :country, String, :required => true
ensure_index [[:location,'2d']]
# Class Methods
def self.generate(city)
geo = Geokit::Geocoders::MultiGeocoder.geocode(city)
if geo.success
market = Market.find_by_city(geo.city) and return market
Market.create(
:city => geo.city,
:state => geo.state,
:country => geo.country,
:location => [geo.lng, geo.lat]
)
else
return nil
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment