An open source admin tool for fusion tables

Smart Chicago’s Connect Chicago campaign is part of the federal Broadband Technology Opportunities Program. Smart Chicago has helped administer millions of dollars of these funds in programs for its partner, the City of Chicago.

The Connect Chicago location tool tool is a Ruby on Rails app that powers the map and detail page section of our site.




Origins

As part of this project, we hired Chicago developer Derek Eder, who has also worked on other Smart Chicago Collaborative since July 2012. The Connect Chicago Locator originated from Derek’s open source Searchable Map Template. It is powered by the free Google Fusion Tables service that lets you take spreadsheets of data and turn them into maps and other visualizations. His template, which can run on any web server, connects to your Fusion Table, reads in the data, and displays a searchable, filterable map. This template is super-useful and has been used by organizations like the City of Chicago, Chicago Public Schools, and the Chicago Tribune to get map-based sets up and running quickly.

The issue with the Searchable Map Template in the context of this project was that once the map is published, the only way to update it is to directly change the underlying Fusion Table through Google’s interface.

At Connect Chicago, we cover more that 250 places where you can use a computer for free, and it’s important that people in each of these locations (librarians, computer lab managers, social services agency leads, and so on) be able to update their detail page easily.

With an admin tool, managers at each location could update its own detail page with images, tables, contact info and transit directions. As far as we can tell know, no one has made a tool like this for Fusion Tables before.

We also knew that this general idea– the ability for non-technical people to update a robust web map with updated information– was broadly useful to Chicagoans. All of the code for this tool, including the underlying admin module, is available in this package.

One nice feature that I wanted in there, based on my long-term love for Flickr (30,000+ Creative Commons photos posted over nearly a decade): all photos on the detail pages are pulled from Flickr using system-generated unique tags. For example, here’s a bunch of photos I took of the Merlo Library on Belmont and tagged with “pcc-merlo-157 “).

Here’s some technical details from Derek about how we got this done:

The fusion_tables gem

The piece of technology that made these advanced features possible was the fusion_tables gem by Simon Tokumine. This gem, or code library, acts as an interface to read and write data to Fusion Tables from a Ruby application. It sounds simple, but in fact, adds a huge amount of power and convenience for building websites that interact with Fusion Tables. With this gem, we were able to fetch a row based on a URL and populate a detail page with the information. 

Here’s a snippet of how it works (viewing the Connect Chicago Fusion Table alongside will help):

#initialize Fusion Tables API
FT = GData::Client::FusionTables.new      
FT.clientlogin(APP_CONFIG['google_account'], APP_CONFIG['google_password'])
FT.set_api_key(APP_CONFIG['google_api_key'])

@location = FT.execute("SELECT * FROM 
1xy_wp4-NhtKPecuDlhsS8MLO0z-g5ayY1OfBhAg WHERE slug = 
‘roosevelt-library-1101-w-taylor-street';").first

From there, we have a handle on this @location object, and I can access different attributes like:

@location[:organization_name] # returns ‘Roosevelt Library’
@location[:address] # returns ‘1101 W. Taylor St’

Ruby on Rails Active Model framework

Now that we had an easy way to dynamically read data from our Fusion Table, we needed to give our site administrators some way to edit their data. On any web form, you want to make sure that the information people are entering is correct and what you expect. Form validation is the way to do this. Because we were using Ruby on Rails 3, we were able to leverage their powerful Active Model framework to handle it.

Active Model allows you to extend the non-database functionality of Active Record (an Object Relational Mapper) to any object. Remember our @location object we got from our fusion_tables gem? That is just a simple list of attributes and values (otherwise known as a Hash). By using Active Model, however, we can treat it like an Active Record object and get our form validation for free!

Inside the locations_controller:

@location = Location.new(location_edit)
if @location.valid?
   # save our data
else
   # oops! didn’t validate, take them back to the edit page
end

And here’s our location model:

class Location
  include ActiveModel::Validations
  include ActiveModel::Conversion
  extend ActiveModel::Naming

  # this line tells ActiveModel to make sure that orgainzation_name and others aren’t empty when validation is called
  validates_presence_of :organization_name, :organization_type, :org_phone, :address, :city, :state, :zip_code

  def create_method( name, &block )
    self.class.send( :define_method, name, &block )
  end

  def create_attr( name )
    create_method( "#{name}=".to_sym ) { |val| 
      instance_variable_set( "@" + name, val)
    }

    create_method( name.to_sym ) { 
      instance_variable_get( "@" + name ) 
    }
  end

  def initialize(attributes = {})
    #read in a hash of attributes from Fusion Tables and set them as attributes of the model
    #for more, see http://railscasts.com/episodes/219-active-model
    attributes.each do |name, value|
      name = "#{name}"
      create_attr name
      send("#{name}=", value)
    end
  end

  def persisted?
    false
  end
end

Advancing the utility of a useful tool

From here on out, if you need a Searchable Map Template with an easy-to-use admin tool for distributed detail page authorship, use the Connect Chicago Locator Tool. We look forward to seeing what comes of this!

 

Two OpenStreetMap Events

One of the main objectives of Smart Chicago is to help build the infrastructure of civic innovation in Chicago. That’s why we publish so much open source code, host so many apps, and support so many events.

Since so much of  civic innovation relates to place, there’s nothing more infrastructural than the maps upon which we publish information. That’s why we’re stoked to support Ian Dees and the work that he & others are doing around OpenStreetMap.

OpenStreetMap is the wiki-style map of the world. Just like Wikipedia, you can add or change any part of the map, making it better for the community around you. In Chicago, the map is particularly detailed thanks to help from many dedicated mappers and data released by the city. The more people contribute, the better the map will get.

OpenStreetMap

April is a busy OpenStreetMap month in Chicago. They are putting together two exciting weekends of events that we’re supporting along with our partner, Knight-Mozilla OpenNews: the Map-a-Thon and Hack Weekends.

OpenStreetMap Map-a-Thon

Beginning mappers are invited to be a part of a national OpenStreetMap Map-a-thon by learning how to use our tools to improve the map in your area. You can add your favorite restaurant or comic book store, a local school or hospital. During the map-a-thon we’ll walk you through the process of finding your area, creating an account, and making your first edit. With that foundation, you can go on to make an impact by adding tons of information relevant to you and your community!

Attend the Map-a-thon April 20th and 21st at 1871 on the 12th floor of the Merchandise Mart, 222 Merchandise Mart Plaza from noon to 6pm. Participants will enjoy food and drinks thanks to Smart Chicago Collaborative.

For more information about the map-a-thon and to RSVP, please visit the Meetup page for the event.

OpenStreetMap Hack Weekend

If you know your way around a compiler, feel comfortable with JSON and XML, or know the difference between an ellipsoid and a geoid, then the Hack Weekend is for you. We’re looking for those with technical know-how to help make a difference in OpenStreetMap’s core software by writing patches and new software to help make mapping faster and easier. Special thanks to Knight-Mozilla OpenNews for their support and sponsorship.

The hack weekend will be held April 26th and 28th at 1871 from 9am to dinner time each day.

For more information about the hack weekend, please visit the OSM wiki page for the event.