Skip to content

Instantly share code, notes, and snippets.

@wboykinm
Created October 7, 2013 15:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wboykinm/6870093 to your computer and use it in GitHub Desktop.
Save wboykinm/6870093 to your computer and use it in GitHub Desktop.
Converting the DigitalGreen Farmbook API feed to GeoJSON
<?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