Skip to content

Instantly share code, notes, and snippets.

@philipashlock
Created January 24, 2012 22:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philipashlock/1673092 to your computer and use it in GitHub Desktop.
Save philipashlock/1673092 to your computer and use it in GitHub Desktop.
Functions to get both legislators and district boundaries from teh OpenStates API
<?php
$state_legislators = $this->state_legislators($data['latitude'], $data['longitude']);
$state_chambers = $this->process_legislators($state_legislators);
function state_legislators($lat, $long) {
$url = "http://openstates.org/api/v1/legislators/geo/?long=" . $long . "&lat=" . $lat . "&fields=state,chamber,district,full_name,url,photo_url&apikey=" . $this->config->item('sunlight_api_key');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$state_legislators=curl_exec($ch);
curl_close($ch);
$state_legislators = json_decode($state_legislators, true);
return $state_legislators;
}
function state_boundaries($state, $chamber) {
$url = "http://openstates.org/api/v1/districts/" . $state . "/" . $chamber . "/?fields=name,boundary_id&apikey=" . $this->config->item('sunlight_api_key');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$state_boundaries=curl_exec($ch);
curl_close($ch);
$state_boundaries = json_decode($state_boundaries, true);
$state_boundaries = $this->process_boundaries($state_boundaries);
return $state_boundaries;
}
function state_boundary_shape($boundary_id) {
$url = "http://openstates.org/api/v1/districts/boundary/" . $boundary_id . "/?apikey=" . $this->config->item('sunlight_api_key');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$shape_data=curl_exec($ch);
curl_close($ch);
$geojson = json_decode($shape_data, true);
$boundary_shape['coordinates'] = $geojson['shape'];
$boundary_shape = json_encode($boundary_shape);
$shape['shape'] = $boundary_shape;
$shape['shape_center_lat'] = $geojson['region']['center_lat'];
$shape['shape_center_long'] = $geojson['region']['center_lon'];
return $shape;
}
function process_boundaries($boundary_array) {
// Clean up data model
foreach($boundary_array as $boundarydata){
$district = $boundarydata['name'];
$boundary_id = $boundarydata['boundary_id'];
$boundaries[$district]['boundary_id'] = $boundary_id;
}
return $boundaries;
}
function process_legislators($representatives) {
// Get our current state
$current_state = $representatives[0]['state'];
// Clean up data model
foreach($representatives as $repdata){
$rep = array(
'full_name' => $repdata['full_name']
);
// there are some missing fields on some entries, check for that
if(isset($repdata['photo_url'])){
$rep['photo_url'] = $repdata['photo_url'];
}
if(isset($repdata['url'])){
$rep['url'] = $repdata['url'];
}
$chamber = $repdata['chamber'];
$district = $repdata['district'];
$chambers[$chamber][$district]['reps'][] = $rep;
}
// Get the boundary_ids for this state
$boundary_ids['upper'] = $this->state_boundaries($current_state, 'upper');
$boundary_ids['lower'] = $this->state_boundaries($current_state, 'lower');
// Get shapes for each of the boundary ids we care about
while($districts = current($chambers)) {
$this_chamber = key($chambers);
if (!isset($current_chamber)) $current_chamber = '';
// reset current district in case district ids are reused across chambers
$current_district = '';
if ($current_chamber !== $this_chamber){
while($district = current($districts)) {
$this_district = key($districts);
if (!isset($current_district)) $current_district = '';
if ($current_district !== $this_district) {
// get shape for this boundary id
$boundary_id = $boundary_ids["$this_chamber"][$this_district]['boundary_id'];
$shape = $this->state_boundary_shape($boundary_id);
$chambers[$this_chamber][$this_district]['shape'] = $shape['shape'];
$chambers[$this_chamber][$this_district]['centerpoint_latlong'] = $shape['shape_center_lat'] . ',' . $shape['shape_center_long'];
}
$current_district = $this_district;
next($districts);
}
}
$current_chamber = $this_chamber;
next($chambers);
}
return $chambers;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment