-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeocode.php
34 lines (25 loc) · 964 Bytes
/
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
<?php
// Receive the data from the client
$input = file_get_contents('php://input');
$data = json_decode($input, true)['data'];
$accessToken = 'pk.eyJ1Ijoib2dib25kYWdsb3J5IiwiYSI6ImNsaGZlajZqZzA3eGQzbnBmc3Z1dXNhNHoifQ.5jg6108wmHZYjgvBoN-NoA';
$geocodedData = [];
// Geocode the data
foreach ($data as $d) {
$citizenship_stable = $d['citizenship_stable'];
$url = "https://api.mapbox.com/geocoding/v5/mapbox.places/$citizenship_stable.json?access_token=$accessToken";
$json = file_get_contents($url);
$geocodingData = json_decode($json, true);
$d['stable_longitude'] = $geocodingData['features'][0]['center'][0];
$d['stable_latitude'] = $geocodingData['features'][0]['center'][1];
$geocodedData[] = $d;
}
// Save the geocoded data to a CSV file
$fp = fopen('geocodedData.csv', 'w');
foreach ($geocodedData as $fields) {
fputcsv($fp, $fields);
}
fclose($fp);
// Send the geocoded data back to the client
echo json_encode($geocodedData);
?>