Last active
May 19, 2022 19:29
-
-
Save phayes/2c800491f216344b8f4dd02c2ab1b5eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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