Skip to content

Instantly share code, notes, and snippets.

@phayes
Last active May 19, 2022 19:29
Show Gist options
  • Save phayes/2c800491f216344b8f4dd02c2ab1b5eb to your computer and use it in GitHub Desktop.
Save phayes/2c800491f216344b8f4dd02c2ab1b5eb to your computer and use it in GitHub Desktop.
<?php
$url = "BCBT_Dec_2021.csv";
$csv = file_get_contents($url);
$csv = explode("\n", $csv);
$csv = array_filter($csv, "strlen");
$csv = array_map('str_getcsv', $csv);
$keys = array_shift($csv);
foreach ($csv as $i=>$row) {
$csv[$i] = array_pad($row, count($keys), "");
}
foreach ($csv as $i=>$row) {
$csv[$i] = array_combine($keys, $row);
}
$deciduous = ['dogwood', 'oak', 'arbutus', 'juniper', 'cottonwood', 'birch', 'maple', 'alder', 'willow', 'hawthorn', 'cascara', 'apple', 'cherry', 'aspen'];
// We now have a good CSV, process into results
$features = [];
foreach ($csv as $i=>$row) {
if ($row['longitude'] && $row['latitude'] && $row['latitude'] != 'hidden') {
$row['common_name'] = trim($row['common_name']);
$row['common_name'] = trim($row['common_name'], '*');
$row['common_name'] = trim($row['common_name'], '^');
$common_lower = strtolower($row['common_name']);
$tree_type = 'coniferous';
foreach ($deciduous as $type) {
if (str_contains($common_lower, $type)) {
$tree_type = 'deciduous';
}
}
$row['tree_type'] = $tree_type;
$features[] = [
'type' => 'Feature',
'geometry' => [
'type' => 'Point',
'coordinates' => [$row['longitude'], $row['latitude']],
],
'properties' => $row
];
}
}
$results = [
"type" => "FeatureCollection",
"features" => $features
];
// Print the results
$output = dirname(__FILE__) . '/big_trees.json';
$encoded = json_encode($results, JSON_INVALID_UTF8_IGNORE, JSON_PRETTY_PRINT);
file_put_contents($output, $encoded);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment