forked from webERP-team/webERP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geocode.php
147 lines (116 loc) · 4.89 KB
/
geocode.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
<?php
// geocode.php
//$PageSecurity = 3;
$Title = _('Geocode Generate');
include ('includes/session.php');
include ('includes/header.php');
//include ('includes/SQL_CommonFunctions.inc');
$sql = "SELECT * FROM geocode_param WHERE 1";
$ErrMsg = _('An error occurred in retrieving the information');
$resultgeo = DB_query($sql, $ErrMsg);
$row = DB_fetch_array($resultgeo);
$api_key = $row['geocode_key'];
$center_long = $row['center_long'];
$center_lat = $row['center_lat'];
$map_height = $row['map_height'];
$map_width = $row['map_width'];
$map_host = $row['map_host'];
define("MAPS_HOST", $map_host);
define("KEY", $api_key);
echo '<p class="page_title_text"><img src="'.$RootPath.'/css/'.$Theme.'/images/maintenance.png" title="' . _('Geocode Setup') . '" alt="" />' . ' ' . _('Geocoding of Customers and Suppliers') . '</p>';
// select all the customer branches
$sql = "SELECT * FROM custbranch WHERE 1";
$ErrMsg = _('An error occurred in retrieving the information');
$result = DB_query($sql, $ErrMsg);
// select all the suppliers
$sql = "SELECT * FROM suppliers WHERE 1";
$ErrMsg = _('An error occurred in retrieving the information');
$result2 = DB_query($sql, $ErrMsg);
// Initialize delay in geocode speed
$delay = 0;
$base_url = "http://" . MAPS_HOST . "/maps/api/geocode/xml?address=";
// Iterate through the customer branch rows, geocoding each address
while ($row = DB_fetch_array($result)) {
$geocode_pending = true;
while ($geocode_pending) {
$address = urlencode($row["braddress1"] . "," . $row["braddress2"] . "," . $row["braddress3"] . "," . $row["braddress4"]);
$id = $row["branchcode"];
$debtorno =$row["debtorno"];
$request_url = $base_url . $address . ',&sensor=true';
echo '<br \>', _('Customer Code'), ': ', $id;
$xml = simplexml_load_string(utf8_encode(file_get_contents($request_url))) or die("url not loading");
// $xml = simplexml_load_file($request_url) or die("url not loading");
$status = $xml->status;
if (strcmp($status, "OK") == 0) {
// Successful geocode
$geocode_pending = false;
$coordinates = $xml->GeocodeResponse->result->geometry->location;
$coordinatesSplit = explode(",", $coordinates);
// Format: Longitude, Latitude, Altitude
$lat = $xml->result->geometry->location->lat;
$lng = $xml->result->geometry->location->lng;
$query = sprintf("UPDATE custbranch " .
" SET lat = '%s', lng = '%s' " .
" WHERE branchcode = '%s' " .
" AND debtorno = '%s' LIMIT 1;",
($lat),
($lng),
($id),
($debtorno));
$update_result = DB_query($query);
if ($update_result==1) {
echo '<br />'. 'Address: ' . $address . ' updated to geocode.';
echo '<br />'. 'Received status ' . $status . '<br />';
}
} else {
// failure to geocode
$geocode_pending = false;
echo '<br />' . 'Address: ' . $address . _('failed to geocode.');
echo 'Received status ' . $status . '<br />';
}
usleep($delay);
}
}
// Iterate through the Supplier rows, geocoding each address
while ($row2 = DB_fetch_array($result2)) {
$geocode_pending = true;
while ($geocode_pending) {
$address = $row2["address1"] . ",+" . $row2["address2"] . ",+" . $row2["address3"] . ",+" . $row2["address4"];
$address = urlencode($row2["address1"] . "," . $row2["address2"] . "," . $row2["address3"] . "," . $row2["address4"]);
$id = $row2["supplierid"];
$request_url = $base_url . $address . ',&sensor=true';
echo '<p>' . _('Supplier Code: ') . $id;
$xml = simplexml_load_string(utf8_encode(file_get_contents($request_url))) or die("url not loading");
// $xml = simplexml_load_file($request_url) or die("url not loading");
$status = $xml->status;
if (strcmp($status, "OK") == 0) {
// Successful geocode
$geocode_pending = false;
$coordinates = $xml->GeocodeResponse->result->geometry->location;
$coordinatesSplit = explode(",", $coordinates);
// Format: Longitude, Latitude, Altitude
$lat = $xml->result->geometry->location->lat;
$lng = $xml->result->geometry->location->lng;
$query = sprintf("UPDATE suppliers " .
" SET lat = '%s', lng = '%s' " .
" WHERE supplierid = '%s' LIMIT 1;",
($lat),
($lng),
($id));
$update_result = DB_query($query);
if ($update_result==1) {
echo '<br />' . 'Address: ' . $address . ' updated to geocode.';
echo '<br />' . 'Received status ' . $status . '<br />';
}
} else {
// failure to geocode
$geocode_pending = false;
echo '<br />' . 'Address: ' . $address . ' failed to geocode.';
echo '<br />' . 'Received status ' . $status . '<br />';
}
usleep($delay);
}
}
echo '<br /><div class="centre"><a href="' . $RootPath . '/GeocodeSetup.php">' . _('Go back to Geocode Setup') . '</a></div>';
include ('includes/footer.php');
?>