Skip to content

Instantly share code, notes, and snippets.

@wboykinm
Last active December 23, 2015 16:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wboykinm/6666168 to your computer and use it in GitHub Desktop.
Save wboykinm/6666168 to your computer and use it in GitHub Desktop.
<?php
header('Content-type: text/plain');
# Grab URL
$ch = curl_init($_GET['url']);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$urlin = curl_exec($ch);
curl_close($ch);
$xmlloaded = simplexml_load_string($urlin);
# Build GeoJSON feature collection array
$geojson = array(
'type' => 'FeatureCollection',
'features' => array()
);
# Loop through rows to build feature arrays
foreach ($xmlloaded->xpath("//iati-activity") as $activity):
foreach ($activity->location as $site):
$feature = array(
'type' => 'Feature',
'geometry' => array(
'type' => 'Point',
'coordinates' => array(
(float)$site->coordinates['longitude'],
(float)$site->coordinates['latitude']
)
),
'properties' => array(
'title' => (string)$activity->title,
'description' => (string)$activity->description,
'sector' => (string)$activity->sector,
'collaboration' => (string)$activity->{'collaboration-type'},
'gazetteer-entry' => (string)$site->{'gazetteer-entry'},
'admin1' => (string)$site->administrative['adm1']
)
);
array_push($geojson['features'], $feature);
endforeach;
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