Skip to content

Commit

Permalink
using openstreetmap instead of gmaps
Browse files Browse the repository at this point in the history
  • Loading branch information
jief committed Apr 5, 2015
1 parent f81c542 commit e7b7e03
Show file tree
Hide file tree
Showing 6 changed files with 2,808 additions and 19,119 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.lock
vendor/
zipcodes_num_fr.csv
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ zipcode-belgium
===============

* List of belgian cities with zip code & longitude / latitude (csv & json format)
* PHP script to extract lng & lat from Google Maps
* PHP script to extract lng & lat from Openstreetmap

Based on http://www.bpost.be/site/fr/residential/customerservice/search/postal_codes.html

* Liste des communes belges avec code postal & longitude / latitude (format csv & json)
* Script PHP pour extraire les lng et lat depuis Google Maps
* Script PHP pour extraire les lng et lat depuis Openstreetmap

La liste des commnues provient de http://www.bpost.be/site/fr/residential/customerservice/search/postal_codes.html
La liste des communes provient de http://www.bpost.be/site/fr/residential/customerservice/search/postal_codes.html
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"willdurand/geocoder": "~2.8"
}
}
63 changes: 32 additions & 31 deletions geolocalize.php
Original file line number Diff line number Diff line change
@@ -1,48 +1,49 @@
<?php
require 'vendor/autoload.php';


/**
* Use Google Maps API to extract lng & lat for each belgian city
*
* Use Openstreetmap API to extract lng & lat for each belgian city
*
* @author Jean-Francois Monfort <[email protected]>
*/

$file = "zipcodes_num_fr.csv";
$new_file = "zipcodes_lonlat.csv";
$new_file_json = "zipcodes_lonlat.json";
$gmaps = "http://maps.googleapis.com/maps/api/geocode/json?";
$new_file = "zipcode-belgium.csv";
$new_file_json = "zipcode-belgium.json";
$export = array();

if (($handle = fopen($file, "r")) !== FALSE) {
$fp = fopen($new_file, 'w');

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$q = $data[0] . ' ' . $data[1]. ' belgium';
$url = $gmaps . '&address=' . urlencode($q) . '&sensor=false';
$geocoder = new \Geocoder\Geocoder();
$adapter = new \Geocoder\HttpAdapter\CurlHttpAdapter();
$chain = new \Geocoder\Provider\ChainProvider(array(
new \Geocoder\Provider\OpenStreetMapProvider($adapter, 'be'),
));
$geocoder->registerProvider($chain);

sleep(1);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$output = json_decode(curl_exec($ch));
$info = curl_getinfo($ch);
curl_close($ch);

if($info['http_code'] == '200') {
if($output->status == 'OK') {
$row = array(
"zip" => $data[0],
"city" => mb_convert_case($data[1], MB_CASE_TITLE, "UTF-8"),
"lng" => $output->results[0]->geometry->location->lng,
"lat" => $output->results[0]->geometry->location->lat,
);
$export[] = $row;
fputcsv($fp, $row);
} else {
print_r($output);
}
} else {
print_r($info);
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if($data[2] == "") {
continue;
}

try {
$addr = $data[0] . ' ' . $data[1]. ' Belgium';
$geocode = $geocoder->geocode($addr);

$row = array(
"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);
Expand Down
Loading

0 comments on commit e7b7e03

Please sign in to comment.