Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 75 lines (66 sloc) 2.873 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
<?php
    /**
* Resolving UK postcodes using uk-postcodes.com
*
* Another basic example that demonstrates registering namespaces,
* loading RDF data from the web and then directly displaying
* literals from the graph on the page.
*
* @package EasyRdf
* @copyright Copyright (c) 2009-2013 Nicholas J Humfrey
* @license http://unlicense.org/
*/

    set_include_path(get_include_path() . PATH_SEPARATOR . '../lib/');
    require_once "EasyRdf.php";
    require_once "html_tag_helpers.php";

    EasyRdf_Namespace::set('postcode', 'http://data.ordnancesurvey.co.uk/ontology/postcode/');
    EasyRdf_Namespace::set('sr', 'http://data.ordnancesurvey.co.uk/ontology/spatialrelations/');
    EasyRdf_Namespace::set('eg', 'http://statistics.data.gov.uk/def/electoral-geography/');
    EasyRdf_Namespace::set('ag', 'http://statistics.data.gov.uk/def/administrative-geography/');
    EasyRdf_Namespace::set('osag', 'http://data.ordnancesurvey.co.uk/ontology/admingeo/');
?>
<html>
<head>
<title>EasyRdf UK Postcode Resolver</title>
<style type="text/css" media="all">
#map
{
border: 1px gray solid;
float: right;
margin: 0 0 20px 20px;
}
th { text-align: right }
td { padding: 5px; }
</style>
</head>
<body>
<h1>EasyRdf UK Postcode Resolver</h1>

<?= form_tag() ?>
<?= text_field_tag('postcode', 'W1A 1AA', array('size'=>10)) ?>
<?= submit_tag() ?>
<?= form_end_tag() ?>

<?php
    if (isset($_REQUEST['postcode'])) {
        $postcode = str_replace(' ', '', strtoupper($_REQUEST['postcode']));
        $docuri = "http://www.uk-postcodes.com/postcode/$postcode.rdf";
        $graph = EasyRdf_Graph::newAndLoad($docuri);


        // Get the first resource of type PostcodeUnit
        $res = $graph->get('postcode:PostcodeUnit', '^rdf:type');
        if ($res) {
            $ll = $res->get('geo:lat').','.$res->get('geo:long');
            print "<iframe id='map' width='500' height='250' frameborder='0' scrolling='no' src='http://maps.google.com/maps?f=q&amp;ll=$ll&amp;output=embed'></iframe>";
            print "<table id='facts'>\n";
            print "<tr><th>Easting:</th><td>" . $res->get('sr:easting') . "</td></tr>\n";
            print "<tr><th>Northing:</th><td>" . $res->get('sr:northing') . "</td></tr>\n";
            print "<tr><th>Longitude:</th><td>" . $res->get('geo:long') . "</td></tr>\n";
            print "<tr><th>Latitude:</th><td>" . $res->get('geo:lat') . "</td></tr>\n";
            print "<tr><th>Local Authority:</th><td>" . $res->get('ag:localAuthority')->label() . "</td></tr>\n";
            print "<tr><th>Electoral Ward:</th><td>" . $res->get('eg:ward')->label() . "</td></tr>\n";
            print "</table>\n";

            print "<div style='clear: both'></div>\n";
        }

        print $graph->dump();
    }
?>
</body>
</html>
Something went wrong with that request. Please try again.