-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpostcode.php
181 lines (144 loc) · 7.44 KB
/
postcode.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
<?php
require_once("db.php");
require_once("xmlparse.php");
require_once("phpcoord-2.3.php");
require_once("json.php");
require_once("geohash.php");
//require_once("electoraldistrict.php");
$link = mysql_connect($db_host, $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));
}
exit;
}
if (strstr($postcode, " ")) {
header ('HTTP/1.1 301 Moved Permanently');
if (strlen($_GET['format']) > 0) {
header("Location: /postcode/".str_replace(" ", "", $postcode).".".$_GET['format']);
} else {
header("Location: /postcode/".str_replace(" ", "", $postcode));
}
exit;
}
if (strlen($postcode) == 6) {
$postcode = substr($postcode, 0, 3) ." ". substr($postcode, 3, 3);
} elseif (strlen($postcode) == 5) {
$postcode = substr($postcode, 0, 2) ." ". substr($postcode, 2, 3);
}
$result = mysql_query("SELECT postcode, lat, lng, easting, northing, postcodes.county AS countygss, counties.county AS countyname, counties.snacid AS countysnac, postcodes.council AS councilgss, councils.snacid AS councilsnac, councils.council AS councilname, postcodes.ward AS wardgss, wards.ward as wardname, wards.snacid as wardsnac, postcodes.constituency AS constituencygss, constituencies.constituency AS constituencyname, constituencies.snacid AS constituencycode, postcodes.parish AS parishgss, parishes.snacid AS parishsnac, parishes.parish AS parishname FROM postcodes LEFT JOIN councils ON postcodes.council = councils.code LEFT JOIN wards ON postcodes.ward = wards.code LEFT JOIN counties ON postcodes.county = counties.code LEFT JOIN constituencies ON postcodes.constituency = constituencies.code LEFT JOIN parishes ON postcodes.parish = parishes.code WHERE postcodes.postcode = '$postcode' LIMIT 0,1");
} else {
$lat = mysql_real_escape_string($_GET['lat']);
$lng = mysql_real_escape_string($_GET['lng']);
$result = mysql_query("SELECT postcode, ( 3959 * acos( cos( radians($lat) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians($lng) ) + sin( radians($lat) ) * sin( radians( lat ) ) ) ) AS distance FROM postcodes WHERE ".($lat-0.1)." < lat AND ".($lat+0.1)." > lat AND ".($lng-0.1)." < lng AND ".($lng+0.1)." > lng ORDER BY distance LIMIT 0,1");
//$result = mysql_query("SELECT postcode, ( 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");
$row = mysql_fetch_array($result);
$query = "SELECT postcode, lat, lng, easting, northing, postcodes.county AS countygss, counties.county AS countyname, counties.snacid AS countysnac, postcodes.council AS councilgss, councils.snacid AS councilsnac, councils.council AS councilname, postcodes.ward AS wardgss, wards.ward as wardname, wards.snacid as wardsnac, postcodes.constituency AS constituencygss, constituencies.constituency AS constituencyname, constituencies.snacid AS constituencycode, postcodes.parish AS parishgss, parishes.snacid AS parishsnac, parishes.parish AS parishname FROM postcodes LEFT JOIN councils ON postcodes.council = councils.code LEFT JOIN wards ON postcodes.ward = wards.code LEFT JOIN counties ON postcodes.county = counties.code LEFT JOIN constituencies ON postcodes.constituency = constituencies.code LEFT JOIN parishes ON postcodes.parish = parishes.code WHERE postcodes.postcode = '$row[postcode]' LIMIT 0,1";
$result = mysql_query($query);
$_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");
exit;
}
$row = mysql_fetch_array($result);
$updatepostcode = $row['postcode'];
if (!strstr(" ", $row['postcode'])) {
$row['postcode'] = substr($row['postcode'], 0, 4) ." ". substr($row['postcode'], -3);
}
$lat = $row['lat'];
$lng = $row['lng'];
$easting = $row['easting'];
$northing = $row['northing'];
$hash = $geohash->encode($lat, $lng);
$geohash = "http://geohash.org/" + $hash
$mapit = null;
if (!strstr($row['countygss'], "99999999")) {
$countytitle = $row['countyname'];
$countycode = $row['countysnac'];
//XXX HACK
if (strlen($row['electoraldistrict']) == 0 && false) {
$mapit = json_decode(file_get_contents("http://mapit.mysociety.org/postcode/". urlencode($updatepostcode) .".json"));
foreach ($mapit->areas as $area) {
if ($area->type_name == "County council ward") {
$edistrict['uri'] = "http://data.ordnancesurvey.co.uk/doc/7". str_pad($area->codes->unit_id, 15, "0", STR_PAD_LEFT);
$edistrict['code'] = $area->codes->unit_id;
$edistrict['name'] = $area->name;
}
}
//mysql_query("UPDATE postcodes SET electoraldistrict = '$edistrict[code]' WHERE postcode = '$updatepostcode'");
}
}
$districttitle = $row['councilname'];
$districtcode = $row['councilsnac'];
$wardtitle = $row['wardname'];
// Fix for NI wards, who have the "GSS" code, not the Snac in URIs
if (strlen($row['wardgss']) == "6") {
$wardcode = $row['wardgss'];
$row['wardsnac'] = str_replace(" ", "_", $row['wardgss']);
} else {
$wardcode = $row['wardsnac'];
}
$constituencycode = $row['constituencycode'];
$constituencytitle = $row['constituencyname'];
if (strlen($constituencycode) == 3) {
$ngnote = "<em>(Irish National Grid)</em>";
$constituencyuri = "http://statistics.data.gov.uk/doc/parliamentary-constituency/". $constituencycode ;
} else {
$constituencyuri = "http://data.ordnancesurvey.co.uk/doc/7". str_pad($constituencycode, 15, "0", STR_PAD_LEFT);
}
//XXX HACK
if (strlen($row['parishgss'] == 0) && false) {
if (!$mapit && false) {
$mapit = json_decode(file_get_contents("http://mapit.mysociety.org/postcode/". urlencode($updatepostcode) .".json"));
}
foreach ($mapit->areas as $area) {
if ($area->type_name == "Civil Parish") {
$parishcode = $area->codes->ons;
$parishuri = "http://data.ordnancesurvey.co.uk/doc/7". str_pad($area->codes->unit_id, 15, "0", STR_PAD_LEFT);
$parishname = $area->name;
}
}
mysql_query("UPDATE postcodes SET parish = '$parishcode' WHERE postcode = '$updatepostcode'");
} else {
$parishname = $row['parishname'];
$parishcode = $row['parishsnac'];
}
// Santa Easter Egg
if ($row['postcode'] == "SAN TA1") {
$row['postcode'] = "SAN TA1";
$constituencytitle = "North Pole";
$constituencyuri = "http://www.northpole.com/";
$districttitle = "Reindeerland";
$row['countygss'] = "99999999";
$wardtitle = "Santa’s Grotto";
$easting = "N/A";
$northing = "N/A";
}
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") {
$callback = $_GET['callback'];
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");
}
mysql_close($link);