-
Notifications
You must be signed in to change notification settings - Fork 112
/
geojson.php
68 lines (56 loc) · 2.02 KB
/
geojson.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
<?php
// kudos to http://stackoverflow.com/a/18106727/1778785 for snippet of PHP to read Google spreadsheet as CSV
$googleSpreadsheetUrl = "https://docs.google.com/spreadsheet/pub?key=10eNXFh6mzFtii7B2PW90jmHtrQLJlRCrf3kkHU0HIH8&single=true&gid=0&output=csv";
$rowCount = 0;
$features = array();
$error = FALSE;
$output = array();
// attempt to set the socket timeout, if it fails then echo an error
if ( ! ini_set('default_socket_timeout', 15))
{
$output = array('error' => 'Unable to Change PHP Socket Timeout');
$error = TRUE;
} // end if, set socket timeout
// if the opening the CSV file handler does not fail
if ( !$error && (($handle = fopen($googleSpreadsheetUrl, "r")) !== FALSE) )
{
// while CSV has data, read up to 10000 rows
while (($csvRow = fgetcsv($handle, 10000, ",")) !== FALSE)
{
$rowCount++;
if ($rowCount == 1) { continue; } // skip the first/header row of the CSV
$features[] = array(
'type' => 'Feature',
'geometry' => array(
'type' => 'Point',
'coordinates' => array(
(float) $csvRow[1], // longitude, casted to type float
(float) $csvRow[0] // latitude, casted to type float
)
),
'properties' => array(
'title' => $csvRow[2],
'notes' => $csvRow[3],
'property3' => $csvRow[4]
)
);
} // end while, loop through CSV data
fclose($handle); // close the CSV file handler
$output = array(
'type' => 'FeatureCollection',
'features' => $features
);
} // end if , read file handler opened
// else, file didn't open for reading
else
{
$output = array('error' => 'Problem Reading Google CSV');
} // end else, file open fail
// convert the PHP output array to JSON "pretty" format
$jsonOutput = json_encode($output, JSON_PRETTY_PRINT);
// render JSON and no cache headers
header('Content-type: application/json; charset=utf-8');
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Access-Control-Allow-Origin: *');
print $jsonOutput;