-
Notifications
You must be signed in to change notification settings - Fork 0
/
geojson.php
58 lines (51 loc) · 1.39 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
<?php
/**
* Leaflet Map Plugin
* Version: 1.0.5
* Author: amaral <[email protected]>
* Website: http://lrio.com
* Copyright © 2023 Amaral All Rights Reserved
* License: GNU/GPL http://www.gnu.org/licenses/gpl-2.0.html
* Reference: https://github.com/jakecoll/mysql-to-geojson/blob/master/mysql-to-geojson.php
*/
// Include the PDO library
require_once '/www/rivers/includes/pdo.php';
// Define the SQL query
$sql = "SELECT * FROM rivers.idaho
WHERE basin LIKE '%clear%'
AND lat != 0
ORDER BY name";
// Execute the SQL query
$result = $pdo->query($sql);
if (!$result) {
echo 'An SQL error occurred.' . PHP_EOL;
exit;
}
// Initialize the GeoJSON array
$geojson = [
'type' => 'FeatureCollection',
'features' => []
];
// Loop through the query results and build the GeoJSON array
while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
$properties = $row;
unset($properties['lat']);
unset($properties['lng']);
$feature = [
'type' => 'Feature',
'geometry' => [
'type' => 'Point',
'coordinates' => [
$row['lng'],
$row['lat']
]
],
'properties' => $properties
];
$geojson['features'][] = $feature;
}
// Output the GeoJSON data
header('Content-type: application/json');
echo json_encode($geojson, JSON_PRETTY_PRINT);
// Close the PDO connection
$pdo = null;