{"id":483,"date":"2015-09-11T19:58:35","date_gmt":"2015-09-11T19:58:35","guid":{"rendered":"http:\/\/arlduc.org\/senseandscale\/?p=483"},"modified":"2015-09-24T16:56:06","modified_gmt":"2015-09-24T16:56:06","slug":"tutorial-open-data-queries-using-soda-and-soql","status":"publish","type":"post","link":"https:\/\/arlduc.org\/senseandscale\/?p=483","title":{"rendered":"Data Vis Tutorial: Open data queries using SODA and SoQL."},"content":{"rendered":"<h1>Background<\/h1>\n<p>&nbsp;<\/p>\n<h3>What is Socrata?<\/h3>\n<p>Socrata is a Seattle-based company, originally founded as Blist in 2007, that has engineered the platform for a number of open government databases, including that of New York, Chicago, Baltimore, the White House, and a number of federal agencies. Socrata <a href=\"https:\/\/opendata.socrata.com\/\" target=\"_blank\">catalogs all its open datasets here<\/a>.<strong><br \/>\n<\/strong>It also hosts <a href=\"http:\/\/List of Socrata community (hackathon) sites\" target=\"_blank\">community hackathon sites here<\/a>.<br \/>\n<strong>SODA =\u00a0Socrata Open Data API<br \/>\nSoQL =\u00a0Socrata Query Language<\/strong><\/p>\n<h3>Why are we learning Socrata tools?<\/h3>\n<p>Socrata platforms are the gateway to a number of government\u00a0datasets you may want to use. I also like that the platforms are well-structured, relatively well-documented, and offer an SQL-like query language that can be a good preview to using\u00a0MySQL and other database packages.<\/p>\n<h3>Do I always have to use SoQL to get Socrata data?<\/h3>\n<p>Not at all! Sometimes a\u00a0dataset is small enough that you can download it through the Socrata web UI. But in the case that you are dealing with massive datasets like the NYC 311 callbase, you will need to be able to use SoQL to get exactly what you want (unless you want to wait for hours and days to download massive datasets).<\/p>\n<h1><\/h1>\n<h1>Getting Started: NYC Open Data<\/h1>\n<p>Please peruse\u00a0the\u00a0<a href=\"http:\/\/dev.socrata.com\/docs\/endpoints.html\" target=\"_blank\">Socrata Open Data API Docs<\/a>\u00a0to understand the following\u00a0<a href=\"https:\/\/nycopendata.socrata.com\/\" target=\"_blank\">NYC Open Data<\/a>\u00a0queries. I wanted to query this month&#8217;s\u00a0<a href=\"http:\/\/dev.socrata.com\/foundry\/#\/data.cityofnewyork.us\/erm2-nwe9\" target=\"_blank\">311<\/a> noise complaints in in my neighborhood (zip code 11231), so I obtained the dataset&#8217;s <a href=\"http:\/\/dev.socrata.com\/docs\/endpoints.html\" target=\"_blank\">API endpoint<\/a>, then I\u00a0<a href=\"http:\/\/dev.socrata.com\/docs\/formats\/index.html\" target=\"_blank\">modified the format extension<\/a> to output CSVs instead of JSON, and finally I added <a href=\"http:\/\/dev.socrata.com\/docs\/filtering.html\" target=\"_blank\">filter<\/a> and <a href=\"http:\/\/dev.socrata.com\/docs\/queries.html\" target=\"_blank\">query<\/a> parameters to obtain\u00a0this month&#8217;s noise complaints in 11231.<\/p>\n<p>Try pasting\u00a0the following queries as URLs in your browser; for each query, a small a CSV will download\u00a0to your computer.<\/p>\n<ul>\n<li>First, try using a <a href=\"http:\/\/dev.socrata.com\/docs\/filtering.html\" target=\"_blank\">simple filter<\/a> to get some 311 calls in 11231. The default number of records is 1000, and the default start date is in 2010.<\/li>\n<\/ul>\n<pre data-wpview-marker=\"https%3A%2F%2Fdata.cityofnewyork.us%2Fresource%2Ferm2-nwe9.csv%3Fincident_zip%3D11231\">https:\/\/data.cityofnewyork.us\/resource\/erm2-nwe9.csv?incident_zip=11231<\/pre>\n<ul>\n<li>I want to start making more complex queries to obtain more recent records just from my neighborhood, so I changed the syntax to the <a href=\"http:\/\/dev.socrata.com\/docs\/queries.html\" target=\"_blank\">SoQL format<\/a>.<\/li>\n<\/ul>\n<pre data-wpview-marker=\"https%3A%2F%2Fdata.cityofnewyork.us%2Fresource%2Ferm2-nwe9.csv%3F%24where%3Dincident_zip%3D'11231'\">https:\/\/data.cityofnewyork.us\/resource\/erm2-nwe9.csv?$where=incident_zip='11231'<\/pre>\n<ul>\n<li data-wpview-marker=\"https%3A%2F%2Fdata.cityofnewyork.us%2Fresource%2Ferm2-nwe9.csv%3F%24where%3Dincident_zip%3D'11231'\">Then I formed a query to increase the output to 10,000\u00a0records:<\/li>\n<\/ul>\n<pre data-wpview-marker=\"https%3A%2F%2Fdata.cityofnewyork.us%2Fresource%2Ferm2-nwe9.csv%3F%24where%3Dincident_zip%3D'11231'%26%24limit%3D10000\">https:\/\/data.cityofnewyork.us\/resource\/erm2-nwe9.csv?$where=incident_zip='11231'&amp;$limit=10000<\/pre>\n<ul>\n<li>And a query for records created\u00a0on or after September 1 2015:<\/li>\n<\/ul>\n<pre>https:\/\/data.cityofnewyork.us\/resource\/erm2-nwe9.csv?$where=created_date &gt;='2015-09-01T00:00:00'<\/pre>\n<ul>\n<li>And a query for noise complaints only:<\/li>\n<\/ul>\n<pre data-wpview-marker=\"https%3A%2F%2Fdata.cityofnewyork.us%2Fresource%2Ferm2-nwe9.csv%3F%24where%3Dstarts_with(complaint_type%2C'Noise')\">https:\/\/data.cityofnewyork.us\/resource\/erm2-nwe9.csv?$where=starts_with(complaint_type,'Noise')<\/pre>\n<ul>\n<li>Finally, I combined all the previous queries to get exactly what I wanted.<\/li>\n<\/ul>\n<pre>https:\/\/data.cityofnewyork.us\/resource\/erm2-nwe9.csv?$where=starts_with(complaint_type,'Noise') AND created_date &gt;='2015-08-01T00:00:00' AND incident_zip='11231'<\/pre>\n<p>&nbsp;<\/p>\n<h1>Now Try It!<\/h1>\n<p>Expand the NYC Open Data exercise by looking at December 2014, the month of protests around Eric Garner\u2019s death. Form a query that outputs a\u00a0CSV with the following attributes:<\/p>\n<ul>\n<li>data source: NYC Open Data<\/li>\n<li>time period: December 2014<\/li>\n<li>CSV size: smaller than 1 MB<\/li>\n<\/ul>\n<h1>Start Visualizing<\/h1>\n<p>If you have some visualization experience, you can use the\u00a0<a href=\"http:\/\/dev.socrata.com\/consumers\/getting-started.html\" target=\"_blank\">Socrata examples<\/a>\u00a0to help you turn your new dataset into a visualization. This will be due in two weeks. If you aren&#8217;t there yet, give it a try. I&#8217;ll post a brief\u00a0tutorial on visualizing your data next week.<\/p>\n<h1>And a Handy Thing<\/h1>\n<p>You will probably be using Excel, Google Sheets, Numbers, or another spreadsheet tool to view your data. It can be very handy&#8211;especially when you&#8217;re annotating your vis or writing a report on it&#8211;to know how to put together basic formulas using spreadsheet functions. Since we all have access to Google via our NYU addresses, here are some basic how-tos on putting together a formula using Google Sheet functions:<\/p>\n<ul>\n<li>Add <a href=\"https:\/\/support.google.com\/docs\/answer\/46977?hl=en\">formulas to Google Sheets<\/a><\/li>\n<li><a href=\"https:\/\/support.google.com\/docs\/table\/25273?hl=en\" target=\"_blank\">Google Sheets function list<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Background &nbsp; What is Socrata? Socrata is a Seattle-based company, originally founded as Blist in 2007, that has engineered the platform for a number of open government databases, including that of New York, Chicago, Baltimore, the White House, and a number of federal agencies. Socrata catalogs all its open datasets here. It also hosts community [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7,8],"tags":[],"class_list":["post-483","post","type-post","status-publish","format-standard","hentry","category-nyu-datavis","category-tutorials"],"_links":{"self":[{"href":"https:\/\/arlduc.org\/senseandscale\/index.php?rest_route=\/wp\/v2\/posts\/483","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/arlduc.org\/senseandscale\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/arlduc.org\/senseandscale\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/arlduc.org\/senseandscale\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/arlduc.org\/senseandscale\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=483"}],"version-history":[{"count":5,"href":"https:\/\/arlduc.org\/senseandscale\/index.php?rest_route=\/wp\/v2\/posts\/483\/revisions"}],"predecessor-version":[{"id":564,"href":"https:\/\/arlduc.org\/senseandscale\/index.php?rest_route=\/wp\/v2\/posts\/483\/revisions\/564"}],"wp:attachment":[{"href":"https:\/\/arlduc.org\/senseandscale\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/arlduc.org\/senseandscale\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/arlduc.org\/senseandscale\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}