Skip to content

Commit

Permalink
Added experimental county electoral district lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
pezholio committed Apr 14, 2010
1 parent ffecf55 commit 4e99669
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
64 changes: 64 additions & 0 deletions electoraldistrict.php
Original file line number Diff line number Diff line change
@@ -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;

}
?>
3 changes: 3 additions & 0 deletions postcode.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
require_once("xmlparse.php");
require_once("phpcoord-2.3.php");
require_once("electoraldistrict.php");

$db_name = '';
$db_username = '';
Expand Down Expand Up @@ -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");
Expand Down
1 change: 1 addition & 0 deletions result.php
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down

0 comments on commit 4e99669

Please sign in to comment.