Skip to content

Instantly share code, notes, and snippets.

@substack substack/area.js
Last active Aug 29, 2015

Embed
What would you like to do?
oakland street trees per square kilometer by neighborhood
// using the data from:
// https://github.com/substack/Oakland_2006_Tree_Survey/blob/master/trees.geojson
// and https://github.com/substack/Oakland_2006_Tree_Survey/tree/master/by-gene-neighborhood (use this as -d)
var fs = require('fs');
var path = require('path');
var select = require('json-select');
var through = require('through');
var minimist = require('minimist');
var areaOf = require('geojson-area').geometry;
var argv = minimist(process.argv.slice(2));
var stringify = require('JSONStream').stringify;
var pending = 0;
var str = stringify();
str.pipe(process.stdout);
argv._.forEach(function (file) {
++ pending;
var keys = [ 'features', true, {
name: [ 'properties', /^name$/i ],
type: [ 'geometry', 'type' ],
geometry: true
} ];
fs.createReadStream(file)
.pipe(select(keys))
.pipe(through(write, end))
;
function write (row) {
if (row.type !== 'Polygon') return;
var file = path.resolve(argv.d, fileOf(row.name + '.geojson'));
try { var trees = JSON.parse(fs.readFileSync(file)).features }
catch (e) { return }
if (!trees.length) return;
var sqm = areaOf(row.geometry) / 1000 / 1000;
str.write({ name: row.name, trees: trees.length, sqKm: sqm });
}
function end () {
if (--pending === 0) str.end();
}
});
function fileOf (name) { return name.replace(/[ \/]+/g, '_') }
var rows = require('./density.json');
var sorted = rows.map(mapper).sort(cmp);
console.log(JSON.stringify(sorted, null, 2));
function mapper (row) {
row.treesPerSqKm = row.trees / row.sqKm;
return row;
}
function cmp (a, b) { return a.treesPerSqKm > b.treesPerSqKm ? -1 : 1 }
[
{
"name": "Bartlett",
"trees": 262,
"sqKm": 0.14296818903288358,
"treesPerSqKm": 1832.575496495506
},
{
"name": "Fairview Park",
"trees": 959,
"sqKm": 0.6985385580251199,
"treesPerSqKm": 1372.8662347734194
},
{
"name": "Shafter",
"trees": 999,
"sqKm": 0.7601180640464826,
"treesPerSqKm": 1314.2695158194651
},
{
"name": "Bushrod",
"trees": 1351,
"sqKm": 1.1625866464772208,
"treesPerSqKm": 1162.0639236599652
},
{
"name": "Lakeshore",
"trees": 757,
"sqKm": 0.6950845840839913,
"treesPerSqKm": 1089.076088484401
},
{
"name": "Rockridge",
"trees": 1130,
"sqKm": 1.0761769349791979,
"treesPerSqKm": 1050.0132118347644
},
{
"name": "Trestle Glen",
"trees": 1005,
"sqKm": 0.961450870080978,
"treesPerSqKm": 1045.2952212892108
},
{
"name": "Golden Gate",
"trees": 386,
"sqKm": 0.37295260444116807,
"treesPerSqKm": 1034.9840580370317
},
{
"name": "Peralta/ Hacienda",
"trees": 264,
"sqKm": 0.27287166248073513,
"treesPerSqKm": 967.4877838171948
},
{
"name": "Adams Point",
"trees": 684,
"sqKm": 0.7453264331090608,
"treesPerSqKm": 917.7186929313064
},
{
"name": "Upper Dimond",
"trees": 701,
"sqKm": 0.7788723160383029,
"treesPerSqKm": 900.0191502062922
},
{
"name": "Oak Center",
"trees": 706,
"sqKm": 0.7887947019285846,
"treesPerSqKm": 895.0364375848957
},
{
"name": "Temescal",
"trees": 875,
"sqKm": 1.0003263699870952,
"treesPerSqKm": 874.7145194335804
},
{
"name": "Upper Laurel",
"trees": 874,
"sqKm": 1.0061289566877836,
"treesPerSqKm": 868.6759228928692
},
{
"name": "Paradise Park",
"trees": 385,
"sqKm": 0.45114418840971887,
"treesPerSqKm": 853.3857021568275
},
{
"name": "Waverly",
"trees": 132,
"sqKm": 0.15703108188083256,
"treesPerSqKm": 840.5979148775903
},
{
"name": "Glenview",
"trees": 1151,
"sqKm": 1.4194965151950565,
"treesPerSqKm": 810.8508810547086
},
{
"name": "Bella Vista",
"trees": 168,
"sqKm": 0.21062624285190423,
"treesPerSqKm": 797.6214061707608
},
{
"name": "Grand Lake",
"trees": 465,
"sqKm": 0.5935702418610064,
"treesPerSqKm": 783.395068024463
},
{
"name": "Santa Fe",
"trees": 507,
"sqKm": 0.658603910896749,
"treesPerSqKm": 769.8101872940194
},
{
"name": "Jefferson",
"trees": 432,
"sqKm": 0.5954927545567655,
"treesPerSqKm": 725.4496325846052
},
{
"name": "Chinatown",
"trees": 304,
"sqKm": 0.41930560249096566,
"treesPerSqKm": 725.0081997331528
},
{
"name": "Patten",
"trees": 215,
"sqKm": 0.3009708867468916,
"treesPerSqKm": 714.3548079479501
},
{
"name": "Ivy Hill",
"trees": 305,
"sqKm": 0.4283020511635969,
"treesPerSqKm": 712.1142641539682
},
{
"name": "School",
"trees": 185,
"sqKm": 0.2614722040935061,
"treesPerSqKm": 707.5321854625948
},
{
"name": "Clinton",
"trees": 563,
"sqKm": 0.8210599903791794,
"treesPerSqKm": 685.6989825311023
},
{
"name": "Highland Terrace",
"trees": 337,
"sqKm": 0.4923983898099549,
"treesPerSqKm": 684.4051625149868
},
{
"name": "Harrington",
"trees": 286,
"sqKm": 0.42929144502396177,
"treesPerSqKm": 666.2140681234315
},
{
"name": "Tuxedo",
"trees": 215,
"sqKm": 0.3254068775947962,
"treesPerSqKm": 660.7112965440231
},
{
"name": "Hoover/ Foster",
"trees": 483,
"sqKm": 0.7345668616825564,
"treesPerSqKm": 657.5303422940536
},
{
"name": "Webster",
"trees": 463,
"sqKm": 0.7151414691262992,
"treesPerSqKm": 647.4243488713571
},
{
"name": "Allendale",
"trees": 417,
"sqKm": 0.647232349432024,
"treesPerSqKm": 644.2817642936676
},
{
"name": "Gaskill",
"trees": 194,
"sqKm": 0.3085035204916877,
"treesPerSqKm": 628.8420945433818
},
{
"name": "Frick",
"trees": 504,
"sqKm": 0.8040065476451281,
"treesPerSqKm": 626.8605665913745
},
{
"name": "McClymonds",
"trees": 492,
"sqKm": 0.8016901913563934,
"treesPerSqKm": 613.7034047623519
},
{
"name": "East Peralta",
"trees": 284,
"sqKm": 0.4702987097203057,
"treesPerSqKm": 603.8715270320419
},
{
"name": "St. Elizabeth",
"trees": 382,
"sqKm": 0.6449804762228923,
"treesPerSqKm": 592.2659895646026
},
{
"name": "Cox",
"trees": 348,
"sqKm": 0.5891885578664547,
"treesPerSqKm": 590.6428347151941
},
{
"name": "Hawthorne",
"trees": 247,
"sqKm": 0.41915979643274637,
"treesPerSqKm": 589.2740718506166
},
{
"name": "Dimond",
"trees": 179,
"sqKm": 0.3140622219896179,
"treesPerSqKm": 569.9507532807218
},
{
"name": "Laurel",
"trees": 178,
"sqKm": 0.3138053183784523,
"treesPerSqKm": 567.2306668344296
},
{
"name": "Rancho San Antonio",
"trees": 412,
"sqKm": 0.7318581379458488,
"treesPerSqKm": 562.9506302360534
},
{
"name": "Sausal Creek",
"trees": 301,
"sqKm": 0.535489704859889,
"treesPerSqKm": 562.1023098450729
},
{
"name": "Ralph Bunche",
"trees": 284,
"sqKm": 0.517657653329695,
"treesPerSqKm": 548.6251351124544
},
{
"name": "Cleveland Heights",
"trees": 631,
"sqKm": 1.1812858807771507,
"treesPerSqKm": 534.1636688189943
},
{
"name": "Mosswood",
"trees": 280,
"sqKm": 0.5379018835835093,
"treesPerSqKm": 520.5410290342104
},
{
"name": "Iveywood",
"trees": 270,
"sqKm": 0.5247693016040254,
"treesPerSqKm": 514.5118039007046
},
{
"name": "Longfellow",
"trees": 564,
"sqKm": 1.1298949496015136,
"treesPerSqKm": 499.16144876911704
},
{
"name": "Pill Hill",
"trees": 290,
"sqKm": 0.5908578447048948,
"treesPerSqKm": 490.8117961010421
},
{
"name": "Eastmont",
"trees": 247,
"sqKm": 0.5286949228584965,
"treesPerSqKm": 467.18814446816384
},
{
"name": "Oak Tree",
"trees": 127,
"sqKm": 0.27685004224394855,
"treesPerSqKm": 458.73209543559676
},
{
"name": "Las Palmas",
"trees": 223,
"sqKm": 0.4920046494425921,
"treesPerSqKm": 453.2477492898571
},
{
"name": "San Pablo Gateway",
"trees": 158,
"sqKm": 0.3492241438667,
"treesPerSqKm": 452.43149070560577
},
{
"name": "Castlemont",
"trees": 312,
"sqKm": 0.6952970341273828,
"treesPerSqKm": 448.72908222824327
},
{
"name": "Clawson",
"trees": 511,
"sqKm": 1.2075331413618333,
"treesPerSqKm": 423.1767911758544
},
{
"name": "Foothill Square",
"trees": 177,
"sqKm": 0.4314917546916866,
"treesPerSqKm": 410.2048256437059
},
{
"name": "Upper Rockridge",
"trees": 1197,
"sqKm": 2.9283017729514125,
"treesPerSqKm": 408.76934578827684
},
{
"name": "Durant Manor",
"trees": 173,
"sqKm": 0.4416717227501957,
"treesPerSqKm": 391.6936292021727
},
{
"name": "Arroyo Viejo",
"trees": 320,
"sqKm": 0.8568956058606533,
"treesPerSqKm": 373.4410560765996
},
{
"name": "Redwood Heights",
"trees": 768,
"sqKm": 2.09168946551016,
"treesPerSqKm": 367.167312674057
},
{
"name": "Fruitvale Station",
"trees": 158,
"sqKm": 0.43736094884472976,
"treesPerSqKm": 361.25767610791553
},
{
"name": "Brookfield Village",
"trees": 366,
"sqKm": 1.0388439249674766,
"treesPerSqKm": 352.31471369624506
},
{
"name": "Woodland",
"trees": 181,
"sqKm": 0.5343653196715634,
"treesPerSqKm": 338.7195862771332
},
{
"name": "Seminary",
"trees": 244,
"sqKm": 0.7227623230032427,
"treesPerSqKm": 337.5936905317978
},
{
"name": "Fremont",
"trees": 240,
"sqKm": 0.7278507753287576,
"treesPerSqKm": 329.7379190007679
},
{
"name": "Highland",
"trees": 224,
"sqKm": 0.7352588447313071,
"treesPerSqKm": 304.6546146369154
},
{
"name": "Merritt",
"trees": 106,
"sqKm": 0.3564641006488812,
"treesPerSqKm": 297.36514787055796
},
{
"name": "Lincoln Highlands",
"trees": 315,
"sqKm": 1.0757995569725276,
"treesPerSqKm": 292.805474735889
},
{
"name": "Millsmont",
"trees": 344,
"sqKm": 1.1785096394494174,
"treesPerSqKm": 291.8940910493628
},
{
"name": "Oakmore",
"trees": 489,
"sqKm": 1.6813457132199996,
"treesPerSqKm": 290.8384612130127
},
{
"name": "Northgate",
"trees": 78,
"sqKm": 0.27461815834482717,
"treesPerSqKm": 284.030744616889
},
{
"name": "Civic Center",
"trees": 62,
"sqKm": 0.23391793383635154,
"treesPerSqKm": 265.05022074696956
},
{
"name": "Coliseum",
"trees": 171,
"sqKm": 0.6520661494929403,
"treesPerSqKm": 262.24333241799627
},
{
"name": "South Prescott",
"trees": 130,
"sqKm": 0.5209482046128223,
"treesPerSqKm": 249.5449621457439
},
{
"name": "North Stonehurst",
"trees": 199,
"sqKm": 0.8010804852412374,
"treesPerSqKm": 248.41448976262745
},
{
"name": "Elmhurst Park",
"trees": 188,
"sqKm": 0.8580782901404256,
"treesPerSqKm": 219.09422736849987
},
{
"name": "Melrose",
"trees": 92,
"sqKm": 0.42047941298073527,
"treesPerSqKm": 218.79787014498874
},
{
"name": "Fitchburg",
"trees": 158,
"sqKm": 0.745419828422104,
"treesPerSqKm": 211.96109088545788
},
{
"name": "Eastmont Hills",
"trees": 244,
"sqKm": 1.2503608003101259,
"treesPerSqKm": 195.14367368161325
},
{
"name": "Prescott",
"trees": 859,
"sqKm": 5.670006520887386,
"treesPerSqKm": 151.49894393164857
},
{
"name": "Toler Heights",
"trees": 134,
"sqKm": 1.0621429710251897,
"treesPerSqKm": 126.16004027279116
},
{
"name": "Columbia Gardens",
"trees": 39,
"sqKm": 0.4310369490996901,
"treesPerSqKm": 90.47948228442962
},
{
"name": "South Stonehurst",
"trees": 22,
"sqKm": 0.2761645350495664,
"treesPerSqKm": 79.66265471434053
},
{
"name": "Sequoyah",
"trees": 554,
"sqKm": 7.206191927811075,
"treesPerSqKm": 76.87832985157264
},
{
"name": "Downtown",
"trees": 45,
"sqKm": 0.6813974009472524,
"treesPerSqKm": 66.04075674113628
},
{
"name": "Montclair",
"trees": 104,
"sqKm": 2.3203424425691286,
"treesPerSqKm": 44.8209704274724
},
{
"name": "Sobrante Park",
"trees": 26,
"sqKm": 0.6813101121159435,
"treesPerSqKm": 38.16177029759891
},
{
"name": "South Kennedy Tract",
"trees": 17,
"sqKm": 1.1210202922636034,
"treesPerSqKm": 15.164756710757665
},
{
"name": "Crestmont",
"trees": 11,
"sqKm": 0.745369545012502,
"treesPerSqKm": 14.757780316628711
},
{
"name": "Chabot Park",
"trees": 71,
"sqKm": 5.633784537684648,
"treesPerSqKm": 12.602540889712357
},
{
"name": "Caballo Hills",
"trees": 7,
"sqKm": 3.5551891401496563,
"treesPerSqKm": 1.9689529091285798
},
{
"name": "Shepherd Canyon",
"trees": 1,
"sqKm": 1.2873517314645304,
"treesPerSqKm": 0.7767884841094436
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.