-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First attempt at gathering district boundaries
Toward openva/richmondsunlight.com#349.
- Loading branch information
Showing
1 changed file
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
/* | ||
* Get every current district | ||
*/ | ||
$sql = $dbh->query('SELECT | ||
id, | ||
chamber, | ||
number | ||
FROM | ||
districts | ||
WHERE | ||
date_ended IS NULL'); | ||
|
||
/* | ||
* Prepare our boundary-insertion SQL statement | ||
*/ | ||
$update_stmt = $dbh->prepare('UPDATE districts | ||
SET boundaries = :boundaries | ||
WHERE id = :district_id'); | ||
|
||
/* | ||
* Iterate through districts | ||
*/ | ||
while ($district = $stmt->fetch()) | ||
{ | ||
|
||
if ($district['chamber'] == 'house') | ||
{ | ||
$c = 'l'; | ||
} | ||
else | ||
{ | ||
$c = 'u'; | ||
} | ||
|
||
$url = 'https://data.openstates.org/boundaries/2018/ocd-division/country:us/state:va/sld' . $c . ':' . $district['number'] . '.json'; | ||
$json = get_content($url); | ||
|
||
/* | ||
* If this is valid JSON. | ||
*/ | ||
if ($json != FALSE) | ||
{ | ||
|
||
$district_data = json_decode($json); | ||
|
||
/* | ||
* Swap lat/lon to X/Y. | ||
*/ | ||
foreach ($district_data->shape->coordinates[0][0] as &$pair) | ||
{ | ||
$tmp[0] = $pair[1]; | ||
$tmp[1] = $pair[0]; | ||
$pair = $tmp; | ||
} | ||
} | ||
|
||
/* | ||
* Save the boundary data. | ||
*/ | ||
$update_stmt->bindParam(':boundaries', $district_data); | ||
$update_stmt->bindParam(':district_id', $district['id']); | ||
$result = $update_stmt->execute(); | ||
|
||
} |