Last active
August 29, 2015 13:56
-
-
Save underbluewaters/9005346 to your computer and use it in GitHub Desktop.
PHP geojson service psuedocode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
# Connect to MySQL database | |
$conn = new PDO('mysql:host=localhost;dbname=mydatabase','myusername','mypassword'); | |
# Build SQL SELECT statement | |
$sql = 'SELECT * FROM horrible_stuff_happening_in_syria'; | |
# Try query or error | |
$rs = $conn->query($sql); | |
if (!$rs) { | |
echo 'An SQL error occured.\n'; | |
exit; | |
} | |
# start the geojson data structure | |
echo ' | |
{ "type": "FeatureCollection", | |
"features": [ | |
'; | |
# for each record, add a feature to the geojson | |
while ($row = $rs->fetch(PDO::FETCH_ASSOC)) { | |
echo <<<EOT | |
{ | |
"type": "Feature", | |
"geometry": { | |
"type": "Point", | |
"coordinates": [$row['lon'], $row['lat']] | |
}, | |
"properties": { | |
"sourceUrl": "$row['url']", | |
"otherThing": "$row['otherThing']" | |
} | |
}, | |
EOT; | |
} | |
# close the array and feature collection. You might have a trailing comma in there. | |
# Not sure if that will break things | |
echo "]}"; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment