Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added experimental county electoral district lookup

  • Loading branch information...
commit 4e996692cf244a073708f206690ccd178bc40c9d 1 parent ffecf55
@pezholio authored
Showing with 68 additions and 0 deletions.
  1. +64 −0 electoraldistrict.php
  2. +3 −0  postcode.php
  3. +1 −0  result.php
View
64 electoraldistrict.php
@@ -0,0 +1,64 @@
+<?php
+function myWithin($myPolygon,$point) {
+$counter = 0;
+// get rid of unnecessary stuff
+$myPolygon = str_replace("MULTIPOLYGON","",$myPolygon);
+$myPolygon = str_replace("(","",$myPolygon);
+$myPolygon = str_replace(")","",$myPolygon);
+$point = str_replace("POINT","",$point);
+$point = str_replace("(","",$point);
+$point = str_replace(")","",$point);
+// make an array of points of the polygon
+$polygon = explode(",",$myPolygon);
+// get the x and y coordinate of the point
+$p = explode(" ",$point);
+$px = $p[0];
+$py = $p[1];
+// number of points in the polygon
+$n = count($polygon);
+$poly1 = $polygon[0];
+for ($i=1; $i <= $n; $i++) {
+$poly1XY = explode(" ",$poly1);
+$poly1x = $poly1XY[0];
+$poly1y = $poly1XY[1];
+$poly2 = $polygon[$i % $n];
+$poly2XY = explode(" ",$poly2);
+$poly2x = $poly2XY[0];
+$poly2y = $poly2XY[1];
+if ($py > min($poly1y,$poly2y)) {
+if ($py <= max($poly1y,$poly2y)) {
+if ($px <= max($poly1x,$poly2x)) {
+if ($poly1y != $poly2y) {
+$xinters = ($py-$poly1y)*($poly2x-$poly1x)/($poly2y-$poly1y)+$poly1x;
+if ($poly1x == $poly2x || $px <= $xinters) {
+$counter++;
+}
+}
+}
+}
+}
+$poly1 = $poly2;
+} // end of While each polygon
+if ($counter % 2 == 0) {
+return FALSE; // outside
+} else {
+return TRUE; // inside
+}
+}
+
+function electoralDistrict($easting, $northing) {
+
+$result = mysql_query("SELECT *, AsText(ogc_geom) AS myPolygon FROM `county_electoral_division_region` WHERE Contains(`ogc_geom`, GeomFromText('Point($easting $northing)'))");
+
+while ($row = mysql_fetch_array($result)) {
+if (myWithin($row['myPolygon'], $easting." ".$northing) === TRUE) {
+$district['uri'] = "http://data.ordnancesurvey.co.uk/doc/7". str_pad($row['UNIT_ID'], 15, "0", STR_PAD_LEFT);
+$district['code'] = "7". str_pad($row['UNIT_ID'], 15, "0", STR_PAD_LEFT);;
+$district['name'] = str_replace(" ED", "", $row['NAME']);
+}
+}
+
+return $district;
+
+}
+?>
View
3  postcode.php
@@ -1,6 +1,7 @@
<?php
require_once("xmlparse.php");
require_once("phpcoord-2.3.php");
+require_once("electoraldistrict.php");
$db_name = '';
$db_username = '';
@@ -60,6 +61,8 @@
$county = get_xml("http://statistics.data.gov.uk/doc/local-authority/". $row['county'] .".rdf");
$countytitle = $county['rdf:RDF']['rdf:Description'][0]['skos:prefLabel']['value'];
$countycode = $county['rdf:RDF']['rdf:Description'][0]['skos:notation']['value'];
+
+$edistrict = electoralDistrict($easting, $northing);
}
$district = get_xml("http://statistics.data.gov.uk/doc/local-authority/". $row['county'] . $row['district'] .".rdf");
View
1  result.php
@@ -29,6 +29,7 @@
if ($row['county'] != "00") {
?>
<p><strong>County</strong> <a href="<?php echo "http://statistics.data.gov.uk/doc/local-authority/". $row['county']; ?>"><?php echo $countytitle; ?></a></p>
+<p><strong>County Electoral District</strong> <a href="<?php echo $edistrict['uri']; ?>"><?php echo $edistrict['name']; ?></a> <em>(Experimental)</em> <!-- <a href="http://www.uk-postcodes.com/boundary.php?easting=<?php echo $easting; ?>&northing=<?php echo $northing; ?>&code=<?php echo $edistrict['code']; ?>"><img src="http://www.uk-postcodes.com/map.png" alt="View on map" border="0" /></a> --></p>
<?php } ?>
<p><strong>District</strong> <a href="<?php echo "http://statistics.data.gov.uk/doc/local-authority/". $row['county'] . $row['district']; ?>"><?php echo $districttitle; ?></a></p>
<p><strong>Ward</strong> <a href="<?php echo "http://statistics.data.gov.uk/doc/electoral-ward/". $row['county'] . $row['district'] . $row['ward'];?>" class="locality"><?php echo $wardtitle; ?></a></p>
Please sign in to comment.
Something went wrong with that request. Please try again.