-
Notifications
You must be signed in to change notification settings - Fork 8
/
postcode2.php
95 lines (72 loc) · 3.22 KB
/
postcode2.php
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
require_once("xmlparse.php");
require_once("phpcoord-2.3.php");
require_once("electoraldistrict.php");
$db_name = 'pstcdrz_postcode';
$db_username = 'pstcdrz_postcod';
$db_password = 'UnleashedX';
mysql_connect('localhost', $db_username, $db_password);
mysql_select_db($db_name) or die(mysql_error());
if ($_GET['postcode']) {
$postcode = mysql_real_escape_string($_GET['postcode']);
if ($postcode != strtoupper($postcode)) {
header ('HTTP/1.1 301 Moved Permanently');
if (strlen($_GET['format']) > 0) {
header("Location: /postcode/".strtoupper($postcode).".".$_GET['format']);
} else {
header("Location: /postcode/".strtoupper($postcode));
}
}
$result = mysql_query("SELECT * FROM postcodes WHERE REPLACE(postcode,' ','') = '$postcode'");
} else {
$lat = mysql_real_escape_string($_GET['lat']);
$lng = mysql_real_escape_string($_GET['lng']);
$result = mysql_query("SELECT *, ( 3959 * acos( cos( radians($lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($lng) ) + sin( radians($lat) ) * sin( radians( lat ) ) ) ) AS distance FROM postcodes ORDER BY distance LIMIT 0,1");
$_GET['format'] = str_replace(".", "", $_GET['format']);
}
$single = TRUE;
$num_rows = mysql_num_rows($result);
if ($num_rows == 0) {
header("HTTP/1.0 404 Not Found");
include("404.php");
} else {
$row = mysql_fetch_array($result);
$lat = $row['lat'];
$lng = $row['lng'];
$ll2w = new LatLng($lat, $lng);
$ll2w->WGS84ToOSGB36();
$os2w = $ll2w->toOSRef();
$eastingnorthing = $os2w->toSplitString();
$easting = $eastingnorthing['easting'];
$northing = $eastingnorthing['northing'];
$geohash = file_get_contents("http://geohash.org?q=".$lat.",".$lng."&format=url");
if ($row['county'] != "00") {
$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'];
$district = electoralDistrict($easting, $northing);
}
print_r($district);
$district = get_xml("http://statistics.data.gov.uk/doc/local-authority/". $row['county'] . $row['district'] .".rdf");
$districttitle = $district['rdf:RDF']['rdf:Description'][0]['skos:prefLabel']['value'];
$districtcode = $district['rdf:RDF']['rdf:Description'][0]['skos:notation']['value'];
$ward = get_xml("http://statistics.data.gov.uk/doc/electoral-ward/". $row['county'] . $row['district'] . $row['ward'] .".rdf");
$wardtitle = $ward['rdf:RDF']['rdf:Description'][0]['rdfs:label'][1]['value'];
if ($_GET['format'] == "xml" || $_SERVER['HTTP_ACCEPT'] == "application/xml") {
header ("content-type: application/xml");
include("xml.php");
} elseif ($_GET['format'] == "json" || $_SERVER['HTTP_ACCEPT'] == "application/json") {
header('Content-type: application/json');
include("json.php");
} elseif ($_GET['format'] == "rdf" || $_SERVER['HTTP_ACCEPT'] == "application/rdf+xml") {
header ("Content-type: application/rdf+xml");
include("rdf.php");
} elseif ($_GET['format'] == "csv" || $_SERVER['HTTP_ACCEPT'] == "text/csv") {
header("Content-type: application/octet-stream");
include("csv.php");
} elseif (strlen($_GET['format']) == 0) {
header ("content-type: text/html");
include("result.php");
}
}
?>