|
<?php |
|
# Fields to be removed (optional) |
|
$remove = ['fulcrum_id','created_by','updated_by','assigned_to']; |
|
# Fulcrum data share ID |
|
$shareID = 'your-share-id'; |
|
# Format- 'geojson' or 'csv' |
|
if (isset($_GET['format'])) { |
|
$format = $_GET['format']; |
|
} else { |
|
$format = 'geojson'; |
|
} |
|
# Fetch the GeoJSON |
|
$geojson = file_get_contents('https://web.fulcrumapp.com/shares/'.$shareID.'.geojson'); |
|
# Error on bad link |
|
if($geojson === false) { |
|
exit('Unable to access data share contents. Please verify <a href=\'https://web.fulcrumapp.com/shares/' . $shareID . '.geojson\'>https://web.fulcrumapp.com/shares/' . $shareID . '.geojson</a> is an active data share.'); |
|
} |
|
# Decode JSON into associative arrays |
|
$data = json_decode($geojson, TRUE); |
|
# Array to hold the properties |
|
$properties = array(); |
|
# Array to hold the csv rows |
|
$rows = array(); |
|
# Loop through GeoJSON FeatureCollection features |
|
foreach ($data['features'] as $featureKey => &$featureValue) { |
|
# Remove unwanted fields |
|
if (isset($remove)) { |
|
foreach ($remove as $key => $value) { |
|
unset($featureValue['properties'][$value]); |
|
} |
|
} |
|
# Array to hold csv header fields |
|
$fields = array(); |
|
# Loop through GeoJSON feature properties to get csv fields and values |
|
foreach ($featureValue['properties'] as $key => $value) { |
|
array_push($fields, $key); |
|
$properties[$key] = $value; |
|
} |
|
# Add properties to csv rows array |
|
array_push($rows, $properties); |
|
} |
|
if ($format === 'csv') { |
|
header('Content-type: text/csv'); |
|
header('Content-Disposition: attachment; filename=fulcrum.csv'); |
|
header('Pragma: no-cache'); |
|
header('Expires: 0'); |
|
$csv = fopen('php://output', 'w'); |
|
# Add header to csv |
|
fputcsv($csv, $fields); |
|
# Add data to Csv |
|
foreach ($rows as $row) { |
|
fputcsv($csv, $row); |
|
} |
|
} |
|
if ($format === 'geojson') { |
|
header('Content-type: application/json'); |
|
echo json_encode($data); |
|
} |
|
?> |