#FUCKYEAHMAPS

mappingViolence

Over the past few weeks I’ve been using my knowledge as a developer to map the data the people over at deadspin are compiling. Their goal is to create a community powered database of all of the police to civilian shootings in the United States (you can contribute to the database here). This data does not include incidents which did not appear in the media, and incidents in which the civilian was shot at, but not hit.

This invaluable data is not available directly from any government agency. Since there is no nationally standardized way by which police officers logged when and why they use their weapons, there is no way to fully track this data. At the moment, the best we have is this valuable community powered data set. In the hopes to visualize this data and show just how often officers discharge their weapons on civilians, I am creating this map (this project is currently in progress, so expect to see all kinds of weird things on the page).

**For the techies following this blog: below, you’ll find some technical info on how the map was created.

theMap

The map was created using Leafletjs, “an open-source JavaScript library for mobile-friendly interactive maps.” Leaflet allows you to create quick Js maps with a lot of add-ons and flexibility. You can get unique tiles for your map at Mapbox and Stamen Design. In addition to customizing the look of the map, you can add useful functionality like popup with individualized information for each plotted point. Leafletjs has a great set of out-of-the-box tutorials, as well as links to many other add-on tutorials.

theDatabase

On the back end of the map is a Rails application with a Mongo database. This was my first time using Mongodb, and it proved challenging. You can install Mongodb using HomeBrew. Once Mongodb is installed in your machine, you can use a number of different gems to get Mongodb going in your Rails app. I chose to use the MongoMapper gem. The gem itself has decent, but not great, documentation for querying. Mongo is a schema-free database, meaning no need to run migrations. Unlike MySQL and traditional DBs, Mongodb stores data in a hash like object with key value pairs. This allows for a lot of flexibility in tacking on new data dynamically. For example, for this particular set of data, geo information (such as lat long values) is being tacked on after the DB creation. Since Mongodb is schemaless, it allows you to quickly add more “columns” without having to re-run migrations and compromise the other data in your DB.

theData

The data was derived from this spreadsheet. It was saved as a csv, parsed using the Rails csv tools and saved to the DB.

CSV.foreach("data.csv", :headers => true).each do |shooting|
 Policeshootings.create( shooting.to_h )
end

Since the goal was to plot the data onto a map, I needed lat long values. These were obtained by using the Google Geocoding API and HTTParty gem. You can see a code snippet of that call below.

def self.add_locations
  Policeshootings.where(:city.ne => nil, :lat => nil).each do |incident|
  @path = (URI.encode("https://maps.googleapis.com/maps/api/geocode/json?address=#{incident.city.gsub!(" ", "-")||incident.city}&components=country:US|administrative_area:#{incident.state[0..1]}"))

  #since the API has some rate limits, it helps to give it a fraction of second between each call.
  sleep 0.2

  response = HTTParty.get(@path)
  incident.set(:lat =>
    response['results']
            [0]
            ['geometry']
            ['location']
            ['lat'] )
  incident.set(:lng =>
    response['results']
            [0]
            ['geometry']
            ['location']
            ['lng'] )
 end
end

The **in-progress** map can be found here.

Advertisements

2 thoughts on “#FUCKYEAHMAPS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s