Skip to content

Instantly share code, notes, and snippets.

@bmcbride
Last active March 15, 2018 16:24
Show Gist options
  • Save bmcbride/95ebec7bf7d9bc47955b to your computer and use it in GitHub Desktop.
Save bmcbride/95ebec7bf7d9bc47955b to your computer and use it in GitHub Desktop.
A simple proxy script for filtering out data fields and masking the URL of Fulcrum data shares. Specify ?format=csv to return CSV instead of default GeoJSON.
<?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);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment