From 4cfc26a7c5e22c8a58da2ed06a99a8328cac0a70 Mon Sep 17 00:00:00 2001 From: Waldo Jaquith Date: Fri, 14 Feb 2020 21:49:48 -0500 Subject: [PATCH] First attempt at gathering district boundaries Toward openva/richmondsunlight.com#349. --- cron/district_boundaries.php | 66 ++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 cron/district_boundaries.php diff --git a/cron/district_boundaries.php b/cron/district_boundaries.php new file mode 100644 index 0000000..67904e5 --- /dev/null +++ b/cron/district_boundaries.php @@ -0,0 +1,66 @@ +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(); + +}