-
Notifications
You must be signed in to change notification settings - Fork 88
/
geolocalize.php
53 lines (45 loc) · 1.37 KB
/
geolocalize.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
<?php
require 'vendor/autoload.php';
/**
* Use Openstreetmap API to extract lng & lat for each belgian city.
*
* @author Jean-Francois Monfort <[email protected]>
*/
$file = 'zipcodes_num_fr.csv';
$new_file = 'zipcode-belgium.csv';
$new_file_json = 'zipcode-belgium.json';
$export = [];
if (($handle = fopen($file, 'r')) !== false) {
$fp = fopen($new_file, 'w');
$geocoder = new \Geocoder\Geocoder();
$adapter = new \Geocoder\HttpAdapter\CurlHttpAdapter();
$chain = new \Geocoder\Provider\ChainProvider([
new \Geocoder\Provider\OpenStreetMapProvider($adapter, 'be'),
]);
$geocoder->registerProvider($chain);
while (($data = fgetcsv($handle, 1000, ',')) !== false) {
if ($data[2] == '') {
continue;
}
try {
$addr = $data[0].' '.$data[1].' Belgium';
$geocode = $geocoder->geocode($addr);
$row = [
'zip' => $data[0],
'city' => mb_convert_case($data[1], MB_CASE_TITLE, 'UTF-8'),
'lng' => $geocode->getLongitude(),
'lat' => $geocode->getLatitude(),
];
$export[] = $row;
fputcsv($fp, $row);
} catch (\Exception $e) {
echo $e->getMessage();
}
sleep(1);
}
fclose($fp);
fclose($handle);
$fp = fopen($new_file_json, 'w');
fwrite($fp, json_encode($export));
fclose($fp);
}