Created
October 7, 2013 15:45
-
-
Save wboykinm/6870093 to your computer and use it in GitHub Desktop.
Converting the DigitalGreen Farmbook API feed to GeoJSON
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 | |
$dgfeed = file_get_contents('http://www.digitalgreen.org/bmgf/feed/'); | |
$dgfeed = str_replace('feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-us"', 'feed xmlns:georss="http://www.georss.org/georss" xmlns="http://www.w3.org/2005/Atom" xml:lang="en-us"', $dgfeed); | |
$xml = simplexml_load_string($dgfeed); | |
# Define the georss namespace | |
#$site = $village->children('http://www.georss.org/georss') | |
# Build GeoJSON feature collection array | |
$geojson = array( | |
'type' => 'FeatureCollection', | |
'features' => array() | |
); | |
# Loop through rows to build feature arrays | |
foreach ($xml->entry as $village): | |
$site = $village->children('http://www.georss.org/georss'); | |
#Weed out the sites with no geometry | |
if ($site->point != 'None None') { | |
$coords = (string) $site->point; | |
$coordsarray = explode(' ', $coords, 2); | |
# Build features | |
$feature = array( | |
'type' => 'Feature', | |
'geometry' => array( | |
'type' => 'Point', | |
'coordinates' => | |
# return x first, then y - need to flip the input order | |
array_map('floatval', array_reverse($coordsarray)) | |
), | |
'properties' => array( | |
'name' => (string) $village->title, | |
'url' => (string) $village->link['href'], | |
'id' => (string) $village->id | |
) | |
); | |
array_push($geojson['features'], $feature); | |
} else { | |
continue; | |
} | |
endforeach; | |
# Push GeoJSON Output | |
header('Content-type: application/json'); | |
echo json_encode($geojson); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment