This repository was archived by the owner on Dec 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcreate_mapping.php
More file actions
357 lines (328 loc) · 15 KB
/
Copy pathcreate_mapping.php
File metadata and controls
357 lines (328 loc) · 15 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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/usr/bin/php
<?php
ini_set('memory_limit', '-1');
class CreateMapping {
public $placesuggestfile;
public $fullSuccess;
public $partialSuccess;
public $failure;
public $counts;
public $mapping;
public function __construct() {
//Tab seperated source data
$this->placesuggestfile = "source_data.tsv";
//Mysql database containing geonames data
$this->aux = mysql_connect('server:port', 'user', 'password');
mysql_select_db('geoname', $this->aux);
mysql_set_charset('utf8');
//files and counters for output tracking
$this->fullSuccess = fopen("match.txt", "w");
$this->partialSuccess = fopen("multiple_match.txt", "w");
$this->failure = fopen("failure.txt", "w");
$this->mapping = fopen("mapping.tsv", "w");
$this->counts = array();
$this->counts["total"] = 0;
$this->counts["success"] = 0;
$this->counts["multiple"] = 0;
$this->counts["failure"] = 0;
$this->counts["missingCity"] = 0;
}
public function printMapLine($data, $terms) {
$string = trim($terms[0]) . ", " . trim($terms[1]) . ", " . trim($terms[2]) . "\t" . trim($data[2]) . "\n";
fwrite($this->mapping, $string);
}
public function printOutputLine($handle, $data, $distance) {
$string = "$distance - {$data[0]} {$data[1]} - {$data[2]}\n";
fwrite($handle, $string);
}
public function addCustomMappings() {
$map = array();
//$map["new york, new york, united states"] = 5125771;
foreach ($map as $city=>$id) {
$string = "$city\t$id";
fwrite($this->mapping, $string);
}
}
public function parseDataFile() {
//$fields[0] == name
//$fields[1] == region
//$fields[2] == country name
//$fields[3] == country code
//$fields[4] == latitude
//$fields[5] == longitude
//$fields[6] == population - not used
$importHandle = fopen($this->placesuggestfile, "r");
if ($importHandle) {
while (( $buffer = fgets($importHandle, 4096)) !== false) {
$fields = str_getcsv($buffer, "\t", "\0");
if (count($fields) != 7) {
print_r($fields);
print "FIELD FAILURE\n";
$locationMatch = null;
} else {
$locationMatch = $this->findLocationMatch(
$fields[0], $fields[1], $fields[2],
$fields[3], $fields[4], $fields[5], $fields[0]
);
if (is_null($locationMatch) && $fields[0] != "") {
$locationMatch = $this->findLocationMatch(
$fields[0] . "%", $fields[1], $fields[2],
$fields[3], $fields[4], $fields[5], $fields[0]
);
}
if (is_null($locationMatch) && $fields[0] != "" && strlen($fields[0]) > 4) {
if (strlen($fields[0]) < 10) {
$len = round(strlen($fields[0]) * .5, 0);
} else if (strlen($fields[0]) > 30) {
$len = round(strlen($fields[0]) * .2, 0);
} else {
$len = round(strlen($fields[0]) * .3, 0);
}
$locationMatch = $this->findLocationMatch(
substr($fields[0], 0, $len) . "%", $fields[1], $fields[2],
$fields[3], $fields[4], $fields[5], $fields[0]
);
}
}
if (is_null($locationMatch)) {
if ($fields[0] == "" || is_null($fields)) {
$this->counts["missingCity"] += 1;
}
$string = implode(" ", $fields);
fwrite($this->failure, $string . "\n");
$this->counts["total"] += 1;
$this->counts["failure"] += 1;
$locationMatch = $this->findNearestInCountryLocation(
$fields[3], $fields[4], $fields[5], array($fields[0], $fields[1], $fields[2])
);
//get nearest in country location instead
}
if (!is_null($locationMatch)) {
foreach ($locationMatch as $dist=>$values) {
if ($dist < 2000000000 || count($locationMatch) == 1) {
print "FOUND < 20km:\t{$values[0]} $dist for $buffer";
break;
} else {
print "FOUND > 20km:\t{$values[0]} $dist for $buffer";
}
}
} else {
print "FAILED:\t$buffer";
}
}
}
}
public function findNearestInCountryLocation($countryCode, $latitude, $longitude, $placeArray) {
if ($placeArray[0]) {
$query = sprintf(
"SELECT geonameid, name, latitude, longitude, population FROM geoname WHERE " .
"country = '%s' AND fclass='P' and latitude >= %f and latitude <= %f " .
"and longitude >= %f and longitude <= %f",
mysql_real_escape_string(strtolower($countryCode)), $latitude - .4,
$latitude + .4, $longitude - .4, $longitude + .4
);
print $query . "\n";
$result = mysql_query($query);
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
$results = array();
while ($item = mysql_fetch_array($result)) {
$results[] = $item;
}
if (count($result)) {
return $this->rankDistance($results, $latitude, $longitude, $countryCode, $placeArray);
}
}
return null;
}
public function findNonUsLocationMatch($city, $region, $country, $countryCode,
$latitude, $longitude, $original_city) {
if (is_null($city) || $city == "") {
$city = $region;
}
$query = sprintf(
"SELECT geonameid, name, latitude, longitude, population FROM geoname WHERE name like '%s' " .
"AND country = '%s' AND fclass='P' ORDER BY population DESC",
mysql_real_escape_string(strtolower($city)), mysql_real_escape_string(strtolower($countryCode))
);
$result = mysql_query($query);
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
$results = array();
while ($item = mysql_fetch_array($result)) {
$results[] = $item;
}
if (count($results)) {
return $this->rankDistance($results, $latitude, $longitude, $countryCode,
array($original_city, $region, $country));
} else {
return $this->findNonUsLocationMatchUsingAlt($city, $region, $country, $countryCode,
$latitude, $longitude, $original_city);
}
}
public function findNonUsLocationMatchUsingAlt($city, $region, $country, $countryCode,
$latitude, $longitude, $original_city) {
if (is_null($city) || $city == "") {
$city = $region;
}
$query = sprintf(
"SELECT geonameid, name, latitude, longitude, population FROM geoname WHERE alternatenames like '%%%s%%' " .
"AND country = '%s' AND fclass='P' ORDER BY population DESC",
mysql_real_escape_string(strtolower($city)), mysql_real_escape_string(strtolower($countryCode))
);
print "Trying alternatenames query: $query\n";
$result = mysql_query($query);
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
$results = array();
while ($item = mysql_fetch_array($result)) {
$results[] = $item;
}
if (count($results)) {
return $this->rankDistance($results, $latitude, $longitude, $countryCode,
array($original_city, $region, $country));
} else {
return null;
}
}
public function findLocationMatch($city, $region, $country, $countryCode, $latitude, $longitude, $original_city) {
//Puerto rican override;
if ($countryCode == "us" && $region == "puerto rico") {
$region = "";
$countryCode = "pr";
$country = "puerto rico";
}
//Now Puerto Rico will not fire for the us and should map
if ($countryCode != "us") {
$places = $this->findNonUSLocationMatch($city, $region, $country, $countryCode,
$latitude, $longitude, $original_city);
return $places;
} else {
$stateQuery = sprintf(
"SELECT code FROM admin1Codes WHERE name = '%s'",
mysql_real_escape_string($region)
);
$result = mysql_query($stateQuery);
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
$stateRes = array();
while ($item = mysql_fetch_array($result)) {
$stateRes[] = $item;
}
if (count($stateRes) ) {
$stateArray = explode(".", $stateRes[0][0]);
$state = $stateArray[1];
$query = sprintf(
"SELECT geonameid, name, latitude, longitude, population FROM geoname WHERE name like '%s' " .
"AND admin1 = '%s' AND country = '%s' and fclass='P'",
mysql_real_escape_string($city),
mysql_real_escape_string($state),
mysql_real_escape_string($countryCode)
);
$result = mysql_query($query);
if (!$result) {
$message = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
$results = array();
while ($item = mysql_fetch_array($result)) {
$results[] = $item;
}
if (count($results)) {
return $this->rankDistance($results, $latitude, $longitude, $countryCode,
array($original_city, $region, $country));
} else {
return null;
}
} else {
return null;
}
}
}
//More complex distance formula - used
function distance($lat1, $lon1, $lat2, $lon2) {
$theta = (double)$lon1 - (double)$lon2;
$dist = sin(deg2rad((double)$lat1)) * sin(deg2rad((double)$lat2)) + cos(deg2rad((double)$lat1))
* cos(deg2rad((double)$lat2)) * cos(deg2rad((double)$theta));
$dist = acos($dist);
$dist = rad2deg($dist);
$miles = $dist * 60 * 1.1515;
return ($miles * 1.609344);
}
//Simple distance formula for distace between 2 lat/lon pairs - not used
public function calculateDistance($newLat, $newLon, $oldLat, $oldLon) {
$lat1 = (float)$newLat/57.2958;
$lat2 = (float)$oldLat/57.2958;
$lon1 = (float)$newLon/57.2958;
$lon2 = (float)$oldLon/57.2958;
$distance = round(acos(sin($lat1) * sin($lat2) + cos($lat1) * cos($lat2) * cos($lon2 - $lon1)));
return $distance;
}
public function rankDistance($results, $latitude, $longitude, $countryCode, $terms) {
$places = array();
if (count($results) == 1) {
$distance = $this->distance(
$results[0]['latitude'], $results[0]['longitude'],
$latitude, $longitude
);
$this->printOutputLine(
$this->fullSuccess, array($results[0]['name'], $countryCode,
$results[0]['geonameid']), $distance
);
$this->printMapLine(array($results[0]['name'], $countryCode, $results[0]['geonameid']), $terms);
$this->counts["total"] += 1;
$this->counts["success"] += 1;
$places[$distance] = array($results[0]['name'], $countryCode, $results[0]['geonameid']);
} else {
foreach ($results as $result) {
$distance = $this->distance($result['latitude'], $result['longitude'], $latitude, $longitude);
//To 'index' by population, divide by the population (or 1)
// we then have to multiply by 10000000 since php indexes arrays on ints so we need real numbers
$mod_distance = $distance / (1 + $result['population']) * 100000000;
//print "distance: $distance, $mod_distance, {$result['name']} {$result['population']}\n";
$places[$mod_distance] = array($result['name'], $countryCode, $result['geonameid'], $distance);
}
ksort($places, SORT_NUMERIC);
foreach ($places as $distance=>$place) {
// If we found a place closer than 20km (20 * 100000000) call it a match
if ($place[3]< 2000000000) {
$this->printOutputLine($this->fullSuccess, $place, $distance);
$this->printMapLine(array($place[0], $place[1], $place[2]), $terms);
$this->counts["success"] += 1;
$this->counts["total"] += 1;
} else {
$this->printOutputLine($this->partialSuccess, $place, $distance);
$this->counts["multiple"] += 1;
$this->counts["total"] += 1;
}
break;
}
}
return $places;
}
}
$test = new CreateMapping();
$test->parseDataFile();
$test->addCustomMappings();
$percentage = round(($test->counts["success"] + $test->counts["multiple"]) / $test->counts["total"], 4);
$percentageNoCity = round(
($test->counts["success"] + $test->counts["multiple"])
/ ($test->counts["total"] - $test->counts["missingCity"]), 4
);
print "\nTotal {$test->counts["total"]} : Success {$test->counts["success"]} : " .
"Multiple {$test->counts["multiple"]} : Failure {$test->counts["failure"]} : " .
"Missing City {$test->counts["missingCity"]} : ".
"% {$percentage} : % No City {$percentageNoCity}\n\n";