Skip to content

Instantly share code, notes, and snippets.

@underbluewaters
Last active August 29, 2015 13:56
Show Gist options
  • Save underbluewaters/9005346 to your computer and use it in GitHub Desktop.
Save underbluewaters/9005346 to your computer and use it in GitHub Desktop.
PHP geojson service psuedocode
<?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