Home
Home

Accessing Chicago, Cook and Illinois Open Data via PHP

Sep 30 11

Accessing Chicago, Cook and Illinois Open Data via PHP

Paul Weinstein

In Accessing the CTA’s API with PHP I outlined a class file I created1
for querying the Chicago Transit Authority’s three web-based application programming interfaces (APIs). However, that isn’t the only open data development I’ve been working on recently, I’ve also been working on a class file for accessing the City of Chicago’s Open Data Portal.

The City of Chicago’s Open Data Portal is driven by a web application developed by Socrata. Socrata’s platform provides a number of web-based API methods for retrieving published datasets2. class.windy.php provides the definition for a PHP object that wraps around Socrata’s API providing PHP written apps access in turn to the city’s open data.

Installation and Instantiation
The first step is to download the class.windy.php file from GitHub and save it in a location that the PHP application can access.

The next step is to include the file using the include function in the PHP application itself:

// Load our class file and create our object.
include_once( ‘class.windy.php’ );

Once the class file has been loaded, the next step is to instantiate the class:

// Create an object to get city data
$chicago = new windy( ‘city’ );

Note that initialization of of the new object requires providing the keyword ‘city’. Since the City of Chicago, Cook County and the State of Illinois all currently use Socrata’s platform for sharing government data, class.windy.php supports accessing all three data portals – more on accessing county and state data later.

Extra parameters can be provided during the construction of the object for additional customization. For example, by default, this class will request data in JSON form. But, Socrata does support other data formats3 so the PHP developer can have a bit of a choice as to how to handle and process the raw data, if desired.

Chicago Data
Let’s say for example that one is looking for information about Chicago’s roughly 200 neighborhoods. More specificity information about the rough boundaries around Chicago neighborhoods. Using the getViews4 method that class.windy.php provides, one can query for any dataset where the description might include the keywords ‘neighborhood boundaries’ and are tagged as KML:

// Let’s find any views that describe themselves as about
// Chicago’s neighborhood boundaries and are tagged with
// KML data
$views = $chicago->getViews(
”, ”,
‘neighborhood boundaries’, ‘kml’,
”, ‘false’, ”, ”
);
echo “Here are the views with a description including
‘neighborhood boundaries’ and are tagged as KML:\n”;
foreach ( $views as $view ) {
echo “View ID: “.$view->id. ” is named ”
.$view->name. ” and is described as a ”
.$view->description. “\n\n”;
}

Depending on what datasets are available the output of this query may include the follow result:

View ID: buma-fjbv is named Boundaries – Neighborhoods – KML and is described as a KML file of neighborhood boundaries in Chicago. To view or use these files, special GIS software, such as Google Earth, is required.

Perhaps, using Google Maps, one wishes to create an interactive map of Chicago neighborhoods? Or better yet a closer look at the boundaries of a specific area in the city? Retrieval of the KML file can be done via the getFileByViewID function:

// With our foreknowledge of datasets the file with
// view id buma-fjbv looks interesting, let’s get
// that file
if ( $view->id == ‘buma-fjbv’ ) {
$file = $chicago->getFileByViewID(
$view->blobId, $view->id
);
// Since KML is an XML notation for
// expressing geographic annotation,
// let’s use SimpleXML to parse this data
// Note: SimpleXML requires the libxml
// PHP extension
$xml = simplexml_load_string( $file );

The above chunk of code nested in the previous example code’s foreach loop compared the $view->id value of each dataset looking for a match to the specific KML file of Chicago neighborhoods. Once found, the getFileByViewID method, given the file id, found in $view->blobID and the known view id sends a query for and retrieval of the file from data portal.

Once the file has been loaded, the KML file, a type of XML format, can be parsed using the simplexml_load_string function which returns a SimpleXML object and can be traversed as needed:

// Ok, now let’s find out what the
// boundaries are for Albany Park
foreach (
$xml->Document->Folder->Placemark
as $hood
) {
if ( preg_match(
‘/Albany Park/’,
$hood->description
)) {
echo “Here are
Albany Park’s
boundaries: ”
.$hood->
MultiGeometry->
Polygon->
outerBoundaryIs->
LinearRing->
coordinates. “\n”;
}
}

Thus the output of our search for the boundaries of Chicago’s Albany Park neighborhood results in the the following geographic data points:

Here are Albany Park’s boundaries:
-87.724211,41.975689,0.000000
-87.724081,41.975576,0.000000 -87.723951,41.975578,0.000000
-87.723773,41.975580,0.000000 -87.723544,41.975583,0.000000
-87.723355,41.975585,0.000000 -87.722786,41.975592,0.000000
-87.721267,41.975605,0.000000 -87.720888,41.975608,0.000000
-87.720859,41.974088,0.000000 -87.720825,41.974085,0.000000
-87.720636,41.974088,0.000000 -87.720561,41.974079,0.000000
-87.720501,41.974061,0.000000 -87.720370,41.974010,0.000000
-87.720264,41.973981,0.000000 -87.720135,41.973962,0.000000
-87.719949,41.973890,0.000000 -87.719723,41.973819,0.000000
-87.719616,41.973775,0.000000 -87.719252,41.973625,0.000000
-87.719147,41.973578,0.000000 -87.719074,41.973535,0.000000
-87.718948,41.973461,0.000000 -87.718826,41.973396,0.000000
-87.718714,41.973346,0.000000 -87.718388,41.973226,0.000000
-87.718366,41.973218,0.000000 -87.718291,41.973195,0.000000
-87.718069,41.973097,0.000000 -87.717935,41.973023,0.000000
-87.717737,41.972915,0.000000 -87.717479,41.972781,0.000000
-87.717195,41.972637,0.000000 -87.717132,41.972604,0.000000
-87.716961,41.972517,0.000000 -87.716548,41.972280,0.000000
-87.716203,41.972086,0.000000 -87.715963,41.971978,0.000000
-87.715918,41.971968,0.000000 -87.715808,41.971941,0.000000
-87.715666,41.971921,0.000000 -87.715501,41.971920,0.000000
-87.715076,41.971932,0.000000 -87.714697,41.971930,0.000000
-87.714681,41.971930,0.000000 -87.714619,41.971930,0.000000
-87.714614,41.971930,0.000000 -87.714547,41.971937,0.000000
-87.714479,41.971942,0.000000 -87.714435,41.971949,0.000000
-87.714385,41.971952,0.000000 -87.714305,41.971954,0.000000
-87.714227,41.971960,0.000000 -87.714178,41.971965,0.000000
-87.714026,41.971969,0.000000 -87.713652,41.972033,0.000000
-87.713466,41.972078,0.000000 -87.713254,41.972129,0.000000
-87.712834,41.972277,0.000000 …

Cook and Illinois Data Portals

As previously mentioned the Cook County and the State of Illinois also currently use Socrata’s platform for sharing government data. For the moment that makes supporting city, county and state open data with one class file a trivial manner:

// Create an object to get county data
$cook = new windy( ‘county’ );
// Let’s find any views that describe the boundaries of the
// county forest preserves
$views = $cook->getViews(
”, ”, ”, ‘county park boundaries’, ”, ‘false’, ”, ”
);
echo “Here are the views with a description including
‘county park boundaries’:\n”;
foreach ( $views as $view ) {
echo “View ID: “.$view->id. ” is named ”
.$view->name. ” and is described as a ”
.$view->description. “\n\n”;
}

In Review
class.widy.php is a single PHP class file that primarily provides access to City of Chicago’s data portal. The class implements functions for accessing all API methods and by default returns an object that a PHP developer can use to incorporate information about the City of Chicago into their PHP based application.

Since Cook County and the State of Illinois have also adopted Socrata’s data platform, support for these additional data portals using Socrata’s Open Data Platform has been included as a secondary feature of the class file.


1 And which can be found on GtHub

2 Known as SODA, a standards-based, RESTful application programming interface.

3 Such as XML, RDF, XLS and XLSX (Execl), CSV, TXT and PDF.

4 getViews is one of a collection of methods Socrata refers to as ViewsService. This collection of API calls provide for the retrieval and manipulation of datasets and metadata about datasets.