Check in Distance Finder as found in ancient SVN repo

This commit is contained in:
Klaus-Uwe Mitterer 2016-05-24 00:51:25 +02:00
commit a2a61052fb
21 changed files with 157309 additions and 0 deletions

27
README Normal file
View file

@ -0,0 +1,27 @@
= Distance Finder =
PHP script allowing users to find the air distance between cities. Uses information from Google if data not found in local database.
== License ==
Distance Finder by Klaus-Uwe Mitterer is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/.
Permissions beyond the scope of this license may be available at info@klaus-uwe.me.
== Installation ==
Change the following settings in bootstrap.php:
DB_HOST = The host on which your MySQL database is running. Usually localhost.
DB_NAME = The name of your MySQL database.
DB_USER = The user name for your MySQL database.
DB_PASSWORD = The corresponding password.
CSVFILE = The file you want to import to the database. Has to be in the following format: City name, Country code, State code, Population, Latitude, Longitude. Can be an empty file. Make sure this file exists in the same folder as the install.php
Then go to http://yoursite.com/[subdirectory]/install.php and follow the instructions. Depending on the size of the CSV file you are importing into the database, this might take a while.
When the installation has finished, you might want to remove or rename install.php.
== Troubleshooting ==
If you keep getting empty files when trying to download the distance list, make sure that the web server has permissions to write in the folder (e.g. chown www-data:www-data)

0
authors-transform.txt Normal file
View file

22
bootstrap.php Normal file
View file

@ -0,0 +1,22 @@
<?php
define('DB_HOST', 'localhost');
define('DB_NAME', '');
define('DB_USER', '');
define('DB_PASSWORD', '');
define('CSVFILE', 'df_geocities.csv');
define('WEBPATH', rtrim(str_replace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']), '/') . '/');
define('DIRPATH', str_replace(basename(__FILE__), '', __FILE__));
define('DISTANCES_FILE', 'distances.txt');
define('LOCATIONS_FILE', 'towns.txt');
define('DB_ERRORS_FILE', 'db-errors.txt');
define('NEWLINE', "\n");
require_once 'db-functions.php';
require_once 'functions.php';
dbConnect();

11
city-data/README Normal file
View file

@ -0,0 +1,11 @@
Use one of the .csv files in here to initialize the database.
Files from GeoNames (http://download.geonames.org/export/dump/):
- df_geocities.csv
- geocities_more.csv
These files are licensed under a Creative Commons Attribution 3.0 License,
see http://creativecommons.org/licenses/by/3.0/
The Data is provided "as is" without warranty or any representation of accuracy, timeliness or completeness.

22909
city-data/df_geocities.csv Normal file

File diff suppressed because it is too large Load diff

0
city-data/empty.csv Normal file
View file

132865
city-data/geocities_more.csv Normal file

File diff suppressed because it is too large Load diff

72
db-functions.php Normal file
View file

@ -0,0 +1,72 @@
<?php
function dbConnect(){
mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or exit(mysql_error());
mysql_select_db(DB_NAME) or exit(mysql_error());
}
function dbLogError($query=''){
file_put_contents(DIRPATH.DB_ERRORS_FILE, @date('Y-m-d H:i:s ').mysql_error()." $query \n" , FILE_APPEND);
}
function dbQuery($query){
$result = mysql_query($query) or dbLogError($query);
return $result;
}
function dbEscape($string){
$string = mysql_real_escape_string($string);
return $string;
}
function dbSelect($query){
$rows = array();
$result = mysql_query($query) or dbLogError($query);
if(mysql_num_rows($result)){
while($row = mysql_fetch_object($result)){
$rows[] = $row;
}
}
return $rows;
}
function dbGetRow($query){
$result = mysql_query($query) or dbLogError($query);
if(mysql_num_rows($result)){
return mysql_fetch_object($result);
}else{
return null;
}
}
function dbGetInsertId(){
return mysql_insert_id();
}
function dbGetVal($query){
$result = mysql_query($query) or dbLogError($query);
if(mysql_num_rows($result)){
return mysql_result($result, 0, 0);
}
return '';
}
function dbInsert($table, $fields){
foreach($fields as $name => $value){
$fields[$name] = mysql_real_escape_string($value);
}
$query = sprintf("INSERT INTO `%s` (`%s`) VALUES ('%s')", $table, implode(array_keys($fields), '`,`'), implode($fields, "','"));
mysql_query($query) or dbLogError($query);
}
function dbUpdate($table, $fields, $where=''){
foreach($fields as $name => $value) {
$value = mysql_real_escape_string($value);
$fields[$name] = "`$name`='$value'";
}
$query = sprintf("UPDATE `%s` SET %s %s", $table, implode($fields, ','), $where);
mysql_query($query) or dbLogError($query);
}

0
distances.txt Normal file
View file

21
download.php Normal file
View file

@ -0,0 +1,21 @@
<?php
require_once 'bootstrap.php';
$content = file_get_contents(DIRPATH.DISTANCES_FILE);
$contentLength = strlen($content);
header('Expires: Mon, 23 Jul 1993 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename=' . DISTANCES_FILE);
header('Content-Length: ' . $contentLength);
echo $content;

38
functions.php Normal file
View file

@ -0,0 +1,38 @@
<?php
function formatAddress($address){
return ucwords(strtolower($address));
}
function getDbDistance($id1, $id2){
return (string) dbGetVal("select distance from df_distances where (id1='$id1' and id2='$id2') or (id1='$id2' and id2='$id1') limit 1");
}
function uploadFile($newname) {
$uploadErrorCodes = array(
1 => 'File exceeds upload_max_filesize',
2 => 'File exceeds MAX_FILE_SIZE',
3 => 'File only partially uploaded',
4 => 'No file uploaded',
6 => 'Missing temporary folder',
7 => 'Disk write failed',
);
if ($_FILES['upload']['name']=='') {
return;
}
if ($_FILES['upload']['error']) {
return $uploadErrorCodes[$_FILES['upload']['error']];
}
if(!move_uploaded_file($_FILES['upload']['tmp_name'], $newname)) {
return 'Server error. Could not move uploaded file.<br>';
}
}

27
get-location1.php Normal file
View file

@ -0,0 +1,27 @@
<?php
require_once 'bootstrap.php';
@unlink(DISTANCES_FILE);
$location = new stdClass;
$location->adr = trim($_REQUEST['address']);
$location->id = '';
$location->lat = '';
$location->lng = '';
$row = dbGetRow("select * from df_locations where address='".dbEscape($location->adr)."'");
if(!$row and $_REQUEST['geosrc']=='db'){
list($city, $countrycode) = explode(',', $location->adr);
$row = dbGetRow("select * from df_geocities where city='".dbEscape($city)."' and countrycode='".trim($countrycode)."' order by population desc limit 1 ");
}
if($row){
$location->id = $row->id;
$location->lat = $row->latitude;
$location->lng = $row->longitude;
}
echo json_encode($location);

61
get-locations.php Normal file
View file

@ -0,0 +1,61 @@
<?php
require_once 'bootstrap.php';
$locationId = $_GET['locationId'];
$file = file_get_contents(LOCATIONS_FILE);
$locations = array();
foreach(explode("\n", $file) as $address){
$address = trim($address);
if($address==''){
continue;
}
$location = new stdClass;
$location->adr = $address;
$location->lat = '';
$location->lng = '';
$location->dst = '';
$location->id = '';
$row = dbGetRow("select * from df_locations where address='".dbEscape($address)."' limit 1 ");
if($row){
$location->id = $row->id;
$location->lat = $row->latitude;
$location->lng = $row->longitude;
$location->dst = getDbDistance($locationId, $location->id);
}
if(!$row and $_REQUEST['geosrc']=='db'){
list($city, $countrycode) = explode(',', $address);
$row = dbGetRow("select * from df_geocities where city='".dbEscape($city)."' and countrycode='".trim($countrycode)."' order by population desc limit 1 ");
if($row){
$location->lat = $row->latitude;
$location->lng = $row->longitude;
dbInsert('df_locations', array(
'address' => formatAddress($address),
'latitude' => $location->lat,
'longitude' => $location->lng,
));
$location->id = dbGetInsertId();
}
}
$locations[] = $location;
}
echo json_encode($locations);

805
index.php Normal file
View file

@ -0,0 +1,805 @@
<?php
require_once 'bootstrap.php';
$error = '';
$address = '';
if ($_POST){
$error = uploadFile(DIRPATH.LOCATIONS_FILE);
$address = trim($_POST['address']);
if(!strlen($address)){
$error .= '<div>Address missing</div>';
}
if (!$error) {
header('location: map.php?geosrc='.$_POST['geosrc'].'&address='.urlencode($address));
exit;
}
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body>
<h4 style="color:red;"><?php echo $error; ?></h4>
<form method="post" enctype="multipart/form-data">
Enter address: <input type="text" name="address" value="<?php echo htmlspecialchars($address); ?>"> (example: London, UK)<br><br>
Geolocate addresses using
<input type="radio" name="geosrc" value="google" checked> Google
<input type="radio" name="geosrc" value="db"> Database
<br><br> Upload addresses file <input type="file" name="upload"> (optional)
<p><input type="submit" value="Submit"></p>
</form>
<hr>
<h3>Country codes (ISO 3166-1 alpha-2)</h3>
<div id="country-codes">
<table border="1">
<tr>
<th style="text-align:left;">Code</th>
<th style="text-align:left;">Country Name</th>
</tr>
<tr>
<td>AD</td>
<td>ANDORRA</td>
</tr><tr>
<td>AE</td>
<td>UNITED ARAB EMIRATES</td>
</tr><tr>
<td>AF</td>
<td>AFGHANISTAN</td>
</tr><tr>
<td>AG</td>
<td>ANTIGUA AND BARBUDA</td>
</tr><tr>
<td>AI</td>
<td>ANGUILLA</td>
</tr><tr>
<td>AL</td>
<td>ALBANIA</td>
</tr><tr>
<td>AM</td>
<td>ARMENIA</td>
</tr><tr>
<td>AO</td>
<td>ANGOLA</td>
</tr><tr>
<td>AQ</td>
<td>ANTARCTICA</td>
</tr><tr>
<td>AR</td>
<td>ARGENTINA</td>
</tr><tr>
<td>AS</td>
<td>AMERICAN SAMOA</td>
</tr><tr>
<td>AT</td>
<td>AUSTRIA</td>
</tr><tr>
<td>AU</td>
<td>AUSTRALIA</td>
</tr><tr>
<td>AW</td>
<td>ARUBA</td>
</tr><tr>
<td>AX</td>
<td>ÅLAND ISLANDS</td>
</tr><tr>
<td>AZ</td>
<td>AZERBAIJAN</td>
</tr><tr>
<td>BA</td>
<td>BOSNIA AND HERZEGOVINA</td>
</tr><tr>
<td>BB</td>
<td>BARBADOS</td>
</tr><tr>
<td>BD</td>
<td>BANGLADESH</td>
</tr><tr>
<td>BE</td>
<td>BELGIUM</td>
</tr><tr>
<td>BF</td>
<td>BURKINA FASO</td>
</tr><tr>
<td>BG</td>
<td>BULGARIA</td>
</tr><tr>
<td>BH</td>
<td>BAHRAIN</td>
</tr><tr>
<td>BI</td>
<td>BURUNDI</td>
</tr><tr>
<td>BJ</td>
<td>BENIN</td>
</tr><tr>
<td>BL</td>
<td>SAINT BARTHÉLEMY</td>
</tr><tr>
<td>BM</td>
<td>BERMUDA</td>
</tr><tr>
<td>BN</td>
<td>BRUNEI DARUSSALAM</td>
</tr><tr>
<td>BO</td>
<td>BOLIVIA, PLURINATIONAL STATE OF</td>
</tr><tr>
<td>BQ</td>
<td>BONAIRE, SINT EUSTATIUS AND SABA</td>
</tr><tr>
<td>BR</td>
<td>BRAZIL</td>
</tr><tr>
<td>BS</td>
<td>BAHAMAS</td>
</tr><tr>
<td>BT</td>
<td>BHUTAN</td>
</tr><tr>
<td>BV</td>
<td>BOUVET ISLAND</td>
</tr><tr>
<td>BW</td>
<td>BOTSWANA</td>
</tr><tr>
<td>BY</td>
<td>BELARUS</td>
</tr><tr>
<td>BZ</td>
<td>BELIZE</td>
</tr><tr>
<td>CA</td>
<td>CANADA</td>
</tr><tr>
<td>CC</td>
<td>COCOS (KEELING) ISLANDS</td>
</tr><tr>
<td>CD</td>
<td>CONGO, THE DEMOCRATIC REPUBLIC OF THE</td>
</tr><tr>
<td>CF</td>
<td>CENTRAL AFRICAN REPUBLIC</td>
</tr><tr>
<td>CG</td>
<td>CONGO</td>
</tr><tr>
<td>CH</td>
<td>SWITZERLAND</td>
</tr><tr>
<td>CI</td>
<td>CÔTE D'IVOIRE</td>
</tr><tr>
<td>CK</td>
<td>COOK ISLANDS</td>
</tr><tr>
<td>CL</td>
<td>CHILE</td>
</tr><tr>
<td>CM</td>
<td>CAMEROON</td>
</tr><tr>
<td>CN</td>
<td>CHINA</td>
</tr><tr>
<td>CO</td>
<td>COLOMBIA</td>
</tr><tr>
<td>CR</td>
<td>COSTA RICA</td>
</tr><tr>
<td>CU</td>
<td>CUBA</td>
</tr><tr>
<td>CV</td>
<td>CAPE VERDE</td>
</tr><tr>
<td>CW</td>
<td>CURAÇAO</td>
</tr><tr>
<td>CX</td>
<td>CHRISTMAS ISLAND</td>
</tr><tr>
<td>CY</td>
<td>CYPRUS</td>
</tr><tr>
<td>CZ</td>
<td>CZECH REPUBLIC</td>
</tr><tr>
<td>DE</td>
<td>GERMANY</td>
</tr><tr>
<td>DJ</td>
<td>DJIBOUTI</td>
</tr><tr>
<td>DK</td>
<td>DENMARK</td>
</tr><tr>
<td>DM</td>
<td>DOMINICA</td>
</tr><tr>
<td>DO</td>
<td>DOMINICAN REPUBLIC</td>
</tr><tr>
<td>DZ</td>
<td>ALGERIA</td>
</tr><tr>
<td>EC</td>
<td>ECUADOR</td>
</tr><tr>
<td>EE</td>
<td>ESTONIA</td>
</tr><tr>
<td>EG</td>
<td>EGYPT</td>
</tr><tr>
<td>EH</td>
<td>WESTERN SAHARA</td>
</tr><tr>
<td>ER</td>
<td>ERITREA</td>
</tr><tr>
<td>ES</td>
<td>SPAIN</td>
</tr><tr>
<td>ET</td>
<td>ETHIOPIA</td>
</tr><tr>
<td>FI</td>
<td>FINLAND</td>
</tr><tr>
<td>FJ</td>
<td>FIJI</td>
</tr><tr>
<td>FK</td>
<td>FALKLAND ISLANDS (MALVINAS)</td>
</tr><tr>
<td>FM</td>
<td>MICRONESIA, FEDERATED STATES OF</td>
</tr><tr>
<td>FO</td>
<td>FAROE ISLANDS</td>
</tr><tr>
<td>FR</td>
<td>FRANCE</td>
</tr><tr>
<td>GA</td>
<td>GABON</td>
</tr><tr>
<td>GB</td>
<td>UNITED KINGDOM</td>
</tr><tr>
<td>GD</td>
<td>GRENADA</td>
</tr><tr>
<td>GE</td>
<td>GEORGIA</td>
</tr><tr>
<td>GF</td>
<td>FRENCH GUIANA</td>
</tr><tr>
<td>GG</td>
<td>GUERNSEY</td>
</tr><tr>
<td>GH</td>
<td>GHANA</td>
</tr><tr>
<td>GI</td>
<td>GIBRALTAR</td>
</tr><tr>
<td>GL</td>
<td>GREENLAND</td>
</tr><tr>
<td>GM</td>
<td>GAMBIA</td>
</tr><tr>
<td>GN</td>
<td>GUINEA</td>
</tr><tr>
<td>GP</td>
<td>GUADELOUPE</td>
</tr><tr>
<td>GQ</td>
<td>EQUATORIAL GUINEA</td>
</tr><tr>
<td>GR</td>
<td>GREECE</td>
</tr><tr>
<td>GS</td>
<td>SOUTH GEORGIA AND THE SOUTH SANDWICH ISLANDS</td>
</tr><tr>
<td>GT</td>
<td>GUATEMALA</td>
</tr><tr>
<td>GU</td>
<td>GUAM</td>
</tr><tr>
<td>GW</td>
<td>GUINEA-BISSAU</td>
</tr><tr>
<td>GY</td>
<td>GUYANA</td>
</tr><tr>
<td>HK</td>
<td>HONG KONG</td>
</tr><tr>
<td>HM</td>
<td>HEARD ISLAND AND MCDONALD ISLANDS</td>
</tr><tr>
<td>HN</td>
<td>HONDURAS</td>
</tr><tr>
<td>HR</td>
<td>CROATIA</td>
</tr><tr>
<td>HT</td>
<td>HAITI</td>
</tr><tr>
<td>HU</td>
<td>HUNGARY</td>
</tr><tr>
<td>ID</td>
<td>INDONESIA</td>
</tr><tr>
<td>IE</td>
<td>IRELAND</td>
</tr><tr>
<td>IL</td>
<td>ISRAEL</td>
</tr><tr>
<td>IM</td>
<td>ISLE OF MAN</td>
</tr><tr>
<td>IN</td>
<td>INDIA</td>
</tr><tr>
<td>IO</td>
<td>BRITISH INDIAN OCEAN TERRITORY</td>
</tr><tr>
<td>IQ</td>
<td>IRAQ</td>
</tr><tr>
<td>IR</td>
<td>IRAN, ISLAMIC REPUBLIC OF</td>
</tr><tr>
<td>IS</td>
<td>ICELAND</td>
</tr><tr>
<td>IT</td>
<td>ITALY</td>
</tr><tr>
<td>JE</td>
<td>JERSEY</td>
</tr><tr>
<td>JM</td>
<td>JAMAICA</td>
</tr><tr>
<td>JO</td>
<td>JORDAN</td>
</tr><tr>
<td>JP</td>
<td>JAPAN</td>
</tr><tr>
<td>KE</td>
<td>KENYA</td>
</tr><tr>
<td>KG</td>
<td>KYRGYZSTAN</td>
</tr><tr>
<td>KH</td>
<td>CAMBODIA</td>
</tr><tr>
<td>KI</td>
<td>KIRIBATI</td>
</tr><tr>
<td>KM</td>
<td>COMOROS</td>
</tr><tr>
<td>KN</td>
<td>SAINT KITTS AND NEVIS</td>
</tr><tr>
<td>KP</td>
<td>KOREA, DEMOCRATIC PEOPLE'S REPUBLIC OF</td>
</tr><tr>
<td>KR</td>
<td>KOREA, REPUBLIC OF</td>
</tr><tr>
<td>KW</td>
<td>KUWAIT</td>
</tr><tr>
<td>KY</td>
<td>CAYMAN ISLANDS</td>
</tr><tr>
<td>KZ</td>
<td>KAZAKHSTAN</td>
</tr><tr>
<td>LA</td>
<td>LAO PEOPLE'S DEMOCRATIC REPUBLIC</td>
</tr><tr>
<td>LB</td>
<td>LEBANON</td>
</tr><tr>
<td>LC</td>
<td>SAINT LUCIA</td>
</tr><tr>
<td>LI</td>
<td>LIECHTENSTEIN</td>
</tr><tr>
<td>LK</td>
<td>SRI LANKA</td>
</tr><tr>
<td>LR</td>
<td>LIBERIA</td>
</tr><tr>
<td>LS</td>
<td>LESOTHO</td>
</tr><tr>
<td>LT</td>
<td>LITHUANIA</td>
</tr><tr>
<td>LU</td>
<td>LUXEMBOURG</td>
</tr><tr>
<td>LV</td>
<td>LATVIA</td>
</tr><tr>
<td>LY</td>
<td>LIBYA</td>
</tr><tr>
<td>MA</td>
<td>MOROCCO</td>
</tr><tr>
<td>MC</td>
<td>MONACO</td>
</tr><tr>
<td>MD</td>
<td>MOLDOVA, REPUBLIC OF</td>
</tr><tr>
<td>ME</td>
<td>MONTENEGRO</td>
</tr><tr>
<td>MF</td>
<td>SAINT MARTIN (FRENCH PART)</td>
</tr><tr>
<td>MG</td>
<td>MADAGASCAR</td>
</tr><tr>
<td>MH</td>
<td>MARSHALL ISLANDS</td>
</tr><tr>
<td>MK</td>
<td>MACEDONIA, THE FORMER YUGOSLAV REPUBLIC OF</td>
</tr><tr>
<td>ML</td>
<td>MALI</td>
</tr><tr>
<td>MM</td>
<td>MYANMAR</td>
</tr><tr>
<td>MN</td>
<td>MONGOLIA</td>
</tr><tr>
<td>MO</td>
<td>MACAO</td>
</tr><tr>
<td>MP</td>
<td>NORTHERN MARIANA ISLANDS</td>
</tr><tr>
<td>MQ</td>
<td>MARTINIQUE</td>
</tr><tr>
<td>MR</td>
<td>MAURITANIA</td>
</tr><tr>
<td>MS</td>
<td>MONTSERRAT</td>
</tr><tr>
<td>MT</td>
<td>MALTA</td>
</tr><tr>
<td>MU</td>
<td>MAURITIUS</td>
</tr><tr>
<td>MV</td>
<td>MALDIVES</td>
</tr><tr>
<td>MW</td>
<td>MALAWI</td>
</tr><tr>
<td>MX</td>
<td>MEXICO</td>
</tr><tr>
<td>MY</td>
<td>MALAYSIA</td>
</tr><tr>
<td>MZ</td>
<td>MOZAMBIQUE</td>
</tr><tr>
<td>NA</td>
<td>NAMIBIA</td>
</tr><tr>
<td>NC</td>
<td>NEW CALEDONIA</td>
</tr><tr>
<td>NE</td>
<td>NIGER</td>
</tr><tr>
<td>NF</td>
<td>NORFOLK ISLAND</td>
</tr><tr>
<td>NG</td>
<td>NIGERIA</td>
</tr><tr>
<td>NI</td>
<td>NICARAGUA</td>
</tr><tr>
<td>NL</td>
<td>NETHERLANDS</td>
</tr><tr>
<td>NO</td>
<td>NORWAY</td>
</tr><tr>
<td>NP</td>
<td>NEPAL</td>
</tr><tr>
<td>NR</td>
<td>NAURU</td>
</tr><tr>
<td>NU</td>
<td>NIUE</td>
</tr><tr>
<td>NZ</td>
<td>NEW ZEALAND</td>
</tr><tr>
<td>OM</td>
<td>OMAN</td>
</tr><tr>
<td>PA</td>
<td>PANAMA</td>
</tr><tr>
<td>PE</td>
<td>PERU</td>
</tr><tr>
<td>PF</td>
<td>FRENCH POLYNESIA</td>
</tr><tr>
<td>PG</td>
<td>PAPUA NEW GUINEA</td>
</tr><tr>
<td>PH</td>
<td>PHILIPPINES</td>
</tr><tr>
<td>PK</td>
<td>PAKISTAN</td>
</tr><tr>
<td>PL</td>
<td>POLAND</td>
</tr><tr>
<td>PM</td>
<td>SAINT PIERRE AND MIQUELON</td>
</tr><tr>
<td>PN</td>
<td>PITCAIRN</td>
</tr><tr>
<td>PR</td>
<td>PUERTO RICO</td>
</tr><tr>
<td>PS</td>
<td>PALESTINIAN TERRITORY, OCCUPIED</td>
</tr><tr>
<td>PT</td>
<td>PORTUGAL</td>
</tr><tr>
<td>PW</td>
<td>PALAU</td>
</tr><tr>
<td>PY</td>
<td>PARAGUAY</td>
</tr><tr>
<td>QA</td>
<td>QATAR</td>
</tr><tr>
<td>RE</td>
<td>RÉUNION</td>
</tr><tr>
<td>RO</td>
<td>ROMANIA</td>
</tr><tr>
<td>RS</td>
<td>SERBIA</td>
</tr><tr>
<td>RU</td>
<td>RUSSIAN FEDERATION</td>
</tr><tr>
<td>RW</td>
<td>RWANDA</td>
</tr><tr>
<td>SA</td>
<td>SAUDI ARABIA</td>
</tr><tr>
<td>SB</td>
<td>SOLOMON ISLANDS</td>
</tr><tr>
<td>SC</td>
<td>SEYCHELLES</td>
</tr><tr>
<td>SD</td>
<td>SUDAN</td>
</tr><tr>
<td>SE</td>
<td>SWEDEN</td>
</tr><tr>
<td>SG</td>
<td>SINGAPORE</td>
</tr><tr>
<td>SH</td>
<td>SAINT HELENA, ASCENSION AND TRISTAN DA CUNHA</td>
</tr><tr>
<td>SI</td>
<td>SLOVENIA</td>
</tr><tr>
<td>SJ</td>
<td>SVALBARD AND JAN MAYEN</td>
</tr><tr>
<td>SK</td>
<td>SLOVAKIA</td>
</tr><tr>
<td>SL</td>
<td>SIERRA LEONE</td>
</tr><tr>
<td>SM</td>
<td>SAN MARINO</td>
</tr><tr>
<td>SN</td>
<td>SENEGAL</td>
</tr><tr>
<td>SO</td>
<td>SOMALIA</td>
</tr><tr>
<td>SR</td>
<td>SURINAME</td>
</tr><tr>
<td>SS</td>
<td>SOUTH SUDAN</td>
</tr><tr>
<td>ST</td>
<td>SAO TOME AND PRINCIPE</td>
</tr><tr>
<td>SV</td>
<td>EL SALVADOR</td>
</tr><tr>
<td>SX</td>
<td>SINT MAARTEN (DUTCH PART)</td>
</tr><tr>
<td>SY</td>
<td>SYRIAN ARAB REPUBLIC</td>
</tr><tr>
<td>SZ</td>
<td>SWAZILAND</td>
</tr><tr>
<td>TC</td>
<td>TURKS AND CAICOS ISLANDS</td>
</tr><tr>
<td>TD</td>
<td>CHAD</td>
</tr><tr>
<td>TF</td>
<td>FRENCH SOUTHERN TERRITORIES</td>
</tr><tr>
<td>TG</td>
<td>TOGO</td>
</tr><tr>
<td>TH</td>
<td>THAILAND</td>
</tr><tr>
<td>TJ</td>
<td>TAJIKISTAN</td>
</tr><tr>
<td>TK</td>
<td>TOKELAU</td>
</tr><tr>
<td>TL</td>
<td>TIMOR-LESTE</td>
</tr><tr>
<td>TM</td>
<td>TURKMENISTAN</td>
</tr><tr>
<td>TN</td>
<td>TUNISIA</td>
</tr><tr>
<td>TO</td>
<td>TONGA</td>
</tr><tr>
<td>TR</td>
<td>TURKEY</td>
</tr><tr>
<td>TT</td>
<td>TRINIDAD AND TOBAGO</td>
</tr><tr>
<td>TV</td>
<td>TUVALU</td>
</tr><tr>
<td>TW</td>
<td>TAIWAN, PROVINCE OF CHINA</td>
</tr><tr>
<td>TZ</td>
<td>TANZANIA, UNITED REPUBLIC OF</td>
</tr><tr>
<td>UA</td>
<td>UKRAINE</td>
</tr><tr>
<td>UG</td>
<td>UGANDA</td>
</tr><tr>
<td>UM</td>
<td>UNITED STATES MINOR OUTLYING ISLANDS</td>
</tr><tr>
<td>US</td>
<td>UNITED STATES</td>
</tr><tr>
<td>UY</td>
<td>URUGUAY</td>
</tr><tr>
<td>UZ</td>
<td>UZBEKISTAN</td>
</tr><tr>
<td>VA</td>
<td>HOLY SEE (VATICAN CITY STATE)</td>
</tr><tr>
<td>VC</td>
<td>SAINT VINCENT AND THE GRENADINES</td>
</tr><tr>
<td>VE</td>
<td>VENEZUELA, BOLIVARIAN REPUBLIC OF</td>
</tr><tr>
<td>VG</td>
<td>VIRGIN ISLANDS, BRITISH</td>
</tr><tr>
<td>VI</td>
<td>VIRGIN ISLANDS, U.S.</td>
</tr><tr>
<td>VN</td>
<td>VIET NAM</td>
</tr><tr>
<td>VU</td>
<td>VANUATU</td>
</tr><tr>
<td>WF</td>
<td>WALLIS AND FUTUNA</td>
</tr><tr>
<td>WS</td>
<td>SAMOA</td>
</tr><tr>
<td>YE</td>
<td>YEMEN</td>
</tr><tr>
<td>YT</td>
<td>MAYOTTE</td>
</tr><tr>
<td>ZA</td>
<td>SOUTH AFRICA</td>
</tr><tr>
<td>ZM</td>
<td>ZAMBIA</td>
</tr><tr>
<td>ZW</td>
<td>ZIMBABWE</td>
</tr>
</table>
</div>
</body>
</html>

89
install.php Normal file
View file

@ -0,0 +1,89 @@
<center>
<h3>Installation</h3>
<?php if(!$_POST) { ?>
<table>
<tr>
<td>
<ul>
<li>Create table df_distances</li>
<li>Create table df_locations</li>
<li>Create table df_geocities</li>
<li>Insert data into df_geocities (might take a while...)</li>
</ul>
</td>
</tr>
</table>
<form method="post"><input type="submit" name="btn-submit" value="Install"></form>
<?php exit; } ?>
<?php
require_once 'bootstrap.php';
set_time_limit(0);
$query = "
CREATE TABLE `df_distances` (
`id1` int(11) NOT NULL,
`id2` int(11) NOT NULL,
`distance` varchar(50) character set latin1 NOT NULL,
UNIQUE KEY `city1` (`id1`,`id2`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
";
dbQuery($query);
$query = "
CREATE TABLE `df_locations` (
`id` int(11) NOT NULL auto_increment,
`address` varchar(250) character set latin1 NOT NULL,
`latitude` varchar(50) character set latin1 NOT NULL,
`longitude` varchar(50) character set latin1 NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `address` (`address`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
";
dbQuery($query);
$query = "
CREATE TABLE `df_geocities` (
`city` varchar(50) character set latin1 NOT NULL,
`countrycode` varchar(2) character set latin1 NOT NULL,
`statecode` varchar(50) character set latin1 NOT NULL,
`population` int(11) NOT NULL,
`latitude` varchar(50) character set latin1 NOT NULL,
`longitude` varchar(50) character set latin1 NOT NULL,
KEY `city` (`city`),
KEY `countrycode` (`countrycode`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
";
dbQuery($query);
if(1){
$file = file_get_contents(DIRPATH.CSVFILE);
$columns = array('city', 'countrycode', 'statecode', 'population', 'latitude', 'longitude');
foreach(explode("\n", $file) as $i => $line){
if(!trim($line))continue;
$insert = array_combine($columns, explode(',', $line));
dbInsert('df_geocities', $insert);
echo 'row '.$i.' inserted<br>';
}
}
?>
<h2>Install complete.</h2>
<a href="index.php">Go to panel</a>
</center>

237
javascript.js Normal file
View file

@ -0,0 +1,237 @@
var map = null;
var geocoder = null;
var nbAddressesProcessed = 0;
var requestDelay = 500;
var maxSliceSize = 20;
var slice = [];
var distanceNA = 'N/A';
var isStopped = false;
var statusDiv = null;
var markers = [];
var polylines = [];
var LOCATIONS = {
index: -1,
elements: [],
processNext : function(noIncrement){
if(typeof noIncrement=='undefined'){
this.index++;
}
if(this.elements.length && this.index < this.elements.length){
this.processCallback(this);
}else{
this.endCallback(this);
}
},
set: function(property,value){
this.elements[this.index][property] = value;
},
get : function(){
return this.elements[this.index];
}
};
function initGoogle(){
var elem = document.getElementById('map');
var options = {
zoom: 2,
center: new google.maps.LatLng(51.500152,-0.126236),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(elem, options);
geocoder = new google.maps.Geocoder();
statusDiv = document.getElementById('search-status').getElementsByTagName('div')[0];
}
function geolocateLocation1(callback){
if(LOCATION1.id){
LOCATION1.position = new google.maps.LatLng(LOCATION1.lat, LOCATION1.lng);
callback();
return;
}
geocoder.geocode({'address': LOCATION1.adr}, function(results, status){
if(status != google.maps.GeocoderStatus.OK){
alert('The address ['+LOCATION1.adr+'] has caused an error.\nGoogle Geocoder Status: '+status);
return;
}
var position = results[0].geometry.location;
LOCATION1.lat = position.lat();
LOCATION1.lng = position.lng();
jQuery.post('save-location1.php', {location: LOCATION1}, function(locationId){
if(locationId){
LOCATION1.id = locationId;
LOCATION1.position = position;
callback();
}else{
alert('The address ['+LOCATION1.adr+'] has caused an error.\nThe application could not save the location. '+locationId);
}
});
});
}
function geolocateLocationsAll(){
jQuery.get('get-locations.php?geosrc='+geosrc+'&locationId='+LOCATION1.id, function(json){
jQuery('#btnStop').show();
LOCATIONS.processCallback = geolocateLocationN;
LOCATIONS.endCallback = geolocateLocationsEnd;
LOCATIONS.elements = eval(json);
LOCATIONS.processNext();
});
}
function saveLocationOnServer(location, delay) {
nbAddressesProcessed++;
showDistance(location);
slice.push(location);
if(slice.length>=maxSliceSize){
saveSlice();
setTimeout(clearMarkers, 500);
setTimeout(function(){ LOCATIONS.processNext(); }, 1000);
}else{
setTimeout(function(){ LOCATIONS.processNext(); }, delay);
}
}
function geolocateLocationN(){
if(isStopped){
doAbort();
return;
}
var location = LOCATIONS.get();
if(location.dst){
saveLocationOnServer(location, 0);
return;
}
if(location.id){
var position = new google.maps.LatLng(location.lat, location.lng);
location.dst = google.maps.geometry.spherical.computeDistanceBetween(LOCATION1.position, position);
saveLocationOnServer(location, 0);
return;
}
geocoder.geocode({'address': location.adr}, function(results, status){
if(status == google.maps.GeocoderStatus.OVER_QUERY_LIMIT){
setTimeout(function(){
LOCATIONS.processNext(false);
}, 5000);
return;
}
if(status != google.maps.GeocoderStatus.OK){
$('#map').css('opacity','0.3');
$('#options').show(666);
$('#options h3 span').html(location.adr);
$('#options input').val(location.adr);
jQuery('#btnStop').hide();
return;
}
var position = results[0].geometry.location;
location.lat = position.lat();
location.lng = position.lng();
location.dst = google.maps.geometry.spherical.computeDistanceBetween(LOCATION1.position, position);
saveLocationOnServer(location, requestDelay);
});
}
function geolocateLocationsEnd(){
setTimeout(saveSlice, 1000);
jQuery('#search-complete').show();
jQuery('#btnStop').hide();
}
function showDistance(location){
var pos1 = LOCATION1.position;
var pos2 = new google.maps.LatLng(location.lat, location.lng);
var marker2 = new google.maps.Marker({position:pos2});
var polyline = new google.maps.Polyline({path: [pos1,pos2], strokeColor: "#FF0000", strokeOpacity: 1.0,strokeWeight: 1});
marker2.setMap(map);
polyline.setMap(map);
showStatus(location.adr, location.dst);
if(!markers.length){
var marker1 = new google.maps.Marker({position:pos1});
marker1.setMap(map);
markers.push(marker1);
}
markers.push(marker2);
polylines.push(polyline);
}
function saveSlice(){
if(slice.length){
jQuery.post('save-locations.php?locationId='+LOCATION1.id, {locations:slice});
slice = [];
}
}
function doResume() {
unhideMap();
setTimeout(function(){
LOCATIONS.set('adr', $('#options input').val());
LOCATIONS.processNext(false);
},1000);
}
function doSkip() {
nbAddressesProcessed++;
unhideMap();
showStatus($('#options input').val(), distanceNA);
LOCATIONS.set('dst', distanceNA);
slice.push(LOCATIONS.get());
LOCATIONS.processNext();
}
function doAbort() {
unhideMap();
showStatus($('#options input').val(), distanceNA);
geolocateLocationsEnd();
}
function showStatus(address, distance) {
if(distance!=distanceNA){
distance = Math.ceil(distance/1000);
}
var str = 'Addresses processed: '+nbAddressesProcessed+' / '+LOCATIONS.elements.length;
str += '<br>Last: '+distance+' km to '+address;
statusDiv.innerHTML = str;
}
function unhideMap() {
$('#map').css('opacity','1');
$('#options').hide(500);
jQuery('#btnStop').show();
}
function clearMarkers() {
for (var i = 0; i < markers.length; i++) {
markers[i].setMap(null);
}
for (var i = 0; i < polylines.length; i++) {
polylines[i].setMap(null);
}
polylines = [];
markers = [];
}

51
map.php Normal file
View file

@ -0,0 +1,51 @@
<?php require_once 'bootstrap.php'; ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="//maps.google.com/maps/api/js?sensor=false&libraries=geometry"></script>
<script type="text/javascript" src="javascript.js"></script>
<link href="styles.css" type="text/css" rel="stylesheet"/>
<script type="text/javascript">
var geosrc = '<?php echo $_REQUEST['geosrc']; ?>';
var LOCATION1 = <?php require 'get-location1.php'; ?>;
$(document).ready(function(){
initGoogle();
geolocateLocation1(function(){
geolocateLocationsAll();
});
});
</script>
</head>
<body>
<div id="search-complete">
<div>
<h3>Search complete</h3>
<span>Download <a href="download.php"><?php echo DISTANCES_FILE; ?></a>.</span>
<span>Start a <a href="<?php echo WEBPATH; ?>">new search</a>.</span>
</div>
</div>
<div id="options">
<div>
<h3>Unable to geolocate <span></span>. Your options are:</h3>
<table>
<tr><td>1) Stop the search while saving all results found so far</td><td><button onclick="doAbort()">Stop</button></td></tr>
<tr><td>2) Skip this address and save the distance as N/A</td><td><button onclick="doSkip()">Skip</button></td></tr>
<tr><td>3) Correct the address manually and resume the search</td><td><form onsubmit="doResume();return false;"><button type="submit">Resume</button><input name="correction"></form></td></tr>
</table>
</div>
</div>
<h3 id="search-status">
<div></div>
<button onclick="window.isStopped=true" id="btnStop">Stop this search</button>
</h3>
<div id="map"></div>
</body>
</html>

20
save-location1.php Normal file
View file

@ -0,0 +1,20 @@
<?php
require_once 'bootstrap.php';
$id = dbGetVal("select id from df_locations where address='".dbEscape($_POST['location']['adr'])."'");
if($id){
echo $id;
exit;
}
dbInsert('df_locations', array(
'address' => formatAddress($_POST['location']['adr']),
'latitude' => $_POST['location']['lat'],
'longitude' => $_POST['location']['lng'],
));
echo dbGetInsertId();

36
save-locations.php Normal file
View file

@ -0,0 +1,36 @@
<?php
require_once 'bootstrap.php';
$locationId = $_GET['locationId'];
$distances = array();
foreach($_POST['locations'] as $location) {
$distances[] = $location['dst'];
if(!$location['id']){
dbInsert('df_locations', array(
'address' => formatAddress($location['adr']),
'latitude' => $location['lat'],
'longitude' => $location['lng'],
));
$location['id'] = dbGetInsertId();
}
$dst = (string) getDbDistance($locationId, $location['id']);
if ($location['id'] and !$dst){
dbInsert('df_distances', array(
'id1' => $locationId,
'id2' => $location['id'],
'distance' => $location['dst'],
));
}
}
if(sizeof($distances)){
$text = implode($distances, NEWLINE) . NEWLINE;
file_put_contents(DISTANCES_FILE, $text, FILE_APPEND);
}

15
styles.css Normal file
View file

@ -0,0 +1,15 @@
a { color:blue; }
#map { width:1000px; height:680px; float:left; }
#options { display:none; width:1000px; font-family:sans-serif; }
#options h3 { color:orange; }
#options div { padding:15px; border:double 5px #bbb; }
#options span { font-style:italic; color:#a5bfdd; }
#options button { width:80px; margin-left:10px; }
#options input { width:220px; margin-left:5px; padding-left:2px; }
#country-codes { width:300px; height:300px; overflow:auto; float:right; }
#search-complete { display:none; width:1000px; font-family:sans-serif; }
#search-complete h3 { color:green; }
#search-complete div { padding:15px; border:double 5px #bbb; }
#search-complete span { margin-right:10px; }
#btnStop { display:none; }

3
towns.txt Normal file
View file

@ -0,0 +1,3 @@
Thörl, AT
Rovinj, HR
Melbourne, AU