Repository navigation
Expand file tree
/
Copy pathdata.html
More file actions
822 lines (571 loc) · 49.1 KB
/
Copy pathdata.html
File metadata and controls
822 lines (571 loc) · 49.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
<!DOCTYPE html>
<html>
<head>
<title>data.coffee</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, target-densitydpi=160dpi, initial-scale=1.0; maximum-scale=1.0; user-scalable=0;">
<link rel="stylesheet" media="all" href="docco.css" />
</head>
<body>
<div id="container">
<div id="background"></div>
<ul class="sections">
<li id="title">
<div class="annotation">
<h1>data.coffee</h1>
</div>
</li>
<li id="section-1">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-1">¶</a>
</div>
<p>DataSet is an addon for managing data arrays.</p>
<p>DataSets do not need to be the same size as the patch sets. Rather they have
methods for sampling and creating new datasets of any width/height.
They also cam set patch variables resampled to the patch width/height.
Several utilities are provided for creating datasets from Image and
XmlHTTPRequest inputs, and for drawing into the patch drawing layer.</p>
</div>
</li>
<li id="section-2">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-2">¶</a>
</div>
<p>Two specific subclasses are provided for Asc GIS elevation data,
and an image-as-data class.</p>
</div>
<div class="content"><div class='highlight'><pre>
u = ABM.Util
ABM.DataSet = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DataSet</span></span></pre></div></div>
</li>
<li id="section-3">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-3">¶</a>
</div>
<p>Static members:</p>
</div>
</li>
<li id="section-4">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-4">¶</a>
</div>
<p>Create a new dataset using function f on each patch.
If f is string, f set to fcn returning p[f]</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-property">@patchDataSet</span>: <span class="hljs-function"><span class="hljs-params">(f)</span> -></span> <span class="hljs-keyword">new</span> PatchDataSet f</pre></div></div>
</li>
<li id="section-5">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-5">¶</a>
</div>
<p>Create a new dataset from an image file name.
Note that datasets can be built in two steps:
An empty constructor which then can be completed by an
async array, Image or XHR response.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-property">@importImageDataSet</span>: <span class="hljs-function"><span class="hljs-params">(name, f, format=u.pixelByte(<span class="hljs-number">0</span>), arrayType=Uint8ClampedArray, rowsPerSlice)</span> -></span>
ds = <span class="hljs-keyword">new</span> ImageDataSet(<span class="hljs-literal">null</span>, format, arrayType, rowsPerSlice) <span class="hljs-comment"># empty dataset</span>
u.importImage name, <span class="hljs-function"><span class="hljs-params">(img)</span> -></span> <span class="hljs-comment"># => not needed</span>
ds.parse img
f(ds) <span class="hljs-keyword">if</span> f?
ds <span class="hljs-comment"># async: ds will be empty until import finishes</span></pre></div></div>
</li>
<li id="section-6">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-6">¶</a>
</div>
<p>Create a new dataset from an Asc GIS file.
The parse method converts a string into a dataset. Async</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-property">@importAscDataSet</span>: <span class="hljs-function"><span class="hljs-params">(name, f)</span> -></span>
ds = <span class="hljs-keyword">new</span> AscDataSet() <span class="hljs-comment"># empty dataset</span>
u.xhrLoadFile name, <span class="hljs-string">"GET"</span>, <span class="hljs-string">"text"</span>, <span class="hljs-function"><span class="hljs-params">(response)</span> -></span> <span class="hljs-comment"># -> OK, closure</span>
ds.parse response <span class="hljs-comment"># complete the empty dataset</span>
f(ds) <span class="hljs-keyword">if</span> f?
ds <span class="hljs-comment"># async: ds will be empty until import finishes</span></pre></div></div>
</li>
<li id="section-7">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-7">¶</a>
</div>
<p>2D Dataset: width/height and an array with length = width*height</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">constructor</span>: <span class="hljs-function"><span class="hljs-params">(width=<span class="hljs-number">0</span>, height=<span class="hljs-number">0</span>, data=[], <span class="hljs-property">@model</span>)</span> -></span>
<span class="hljs-property">@setDefaults</span>()
<span class="hljs-property">@reset</span> width, height, data</pre></div></div>
</li>
<li id="section-8">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-8">¶</a>
</div>
<p>Reset a dataset to have new width, height and data. Allows creating
an empty dataset and having it filled by another function.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">reset</span>: <span class="hljs-function"><span class="hljs-params">(<span class="hljs-property">@width</span>, <span class="hljs-property">@height</span>, <span class="hljs-property">@data</span>)</span> -></span>
<span class="hljs-keyword">if</span> <span class="hljs-property">@data</span>.length <span class="hljs-keyword">isnt</span> <span class="hljs-property">@width</span>*<span class="hljs-property">@height</span>
u.error <span class="hljs-string">"""DataSet: data array length error:
data.length: <span class="hljs-subst">#{<span class="hljs-property">@data</span>.length}</span> width: <span class="hljs-subst">#{<span class="hljs-property">@width</span>}</span> height: <span class="hljs-subst">#{<span class="hljs-property">@height</span>}</span>
"""</span>
@</pre></div></div>
</li>
<li id="section-9">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-9">¶</a>
</div>
<p>Check that x,y are valid coords (floats), from top-left of dataset.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">checkXY</span>: <span class="hljs-function"><span class="hljs-params">(x,y)</span> -></span>
u.error <span class="hljs-string">"x,y out of range: <span class="hljs-subst">#{x}</span>,<span class="hljs-subst">#{y}</span>"</span> <span class="hljs-keyword">unless</span> (<span class="hljs-number">0</span><=x<=<span class="hljs-property">@width</span>-<span class="hljs-number">1</span> <span class="hljs-keyword">and</span> <span class="hljs-number">0</span><=y<=<span class="hljs-property">@height</span>-<span class="hljs-number">1</span>)</pre></div></div>
</li>
<li id="section-10">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-10">¶</a>
</div>
<p>Set dataset transform parameters.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">setDefaults</span>: <span class="hljs-function">-></span>
<span class="hljs-property">@useNearest</span>=<span class="hljs-literal">false</span>
<span class="hljs-property">@crop</span>=<span class="hljs-literal">false</span>
<span class="hljs-property">@normalizeImage</span>=<span class="hljs-literal">true</span>
<span class="hljs-property">@alpha</span>=<span class="hljs-number">255</span>
<span class="hljs-property">@gray</span>=<span class="hljs-literal">true</span>
<span class="hljs-attribute">setSampler</span>: <span class="hljs-function"><span class="hljs-params">(<span class="hljs-property">@useNearest</span>)</span> -></span>
<span class="hljs-attribute">setConvolveCrop</span>: <span class="hljs-function"><span class="hljs-params">(<span class="hljs-property">@crop</span>)</span> -></span>
<span class="hljs-attribute">setImageNormalize</span>: <span class="hljs-function"><span class="hljs-params">(<span class="hljs-property">@normalizeImage</span>)</span> -></span>
<span class="hljs-attribute">setImageAlpha</span>: <span class="hljs-function"><span class="hljs-params">(<span class="hljs-property">@alpha</span>)</span> -></span> <span class="hljs-comment"># pixel alpha: [0,255]</span>
<span class="hljs-attribute">setImageGray</span>: <span class="hljs-function"><span class="hljs-params">(<span class="hljs-property">@gray</span>)</span> -></span></pre></div></div>
</li>
<li id="section-11">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-11">¶</a>
</div>
<p>Set the model to use by default for things like <code>toPatchVar()</code>,
<code>toDrawing()</code>, and <code>toPatchColors()</code>.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">setModel</span>: <span class="hljs-function"><span class="hljs-params">(<span class="hljs-property">@model</span>)</span> -></span></pre></div></div>
</li>
<li id="section-12">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-12">¶</a>
</div>
<p>Sample the dataset.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">sample</span>: <span class="hljs-function"><span class="hljs-params">(x,y)</span> -></span> <span class="hljs-keyword">if</span> <span class="hljs-property">@useNearest</span> <span class="hljs-keyword">then</span> <span class="hljs-property">@nearest</span> x,y <span class="hljs-keyword">else</span> <span class="hljs-property">@bilinear</span> x,y</pre></div></div>
</li>
<li id="section-13">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-13">¶</a>
</div>
<p>Sample dataset using nearest neighbor. x,y floats in range</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">nearest</span>: <span class="hljs-function"><span class="hljs-params">(x,y)</span> -></span> <span class="hljs-property">@getXY</span> Math.round(x), Math.round(y)</pre></div></div>
</li>
<li id="section-14">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-14">¶</a>
</div>
<p>Sample dataset using bilinear (2D) interpolation. x,y floats in range</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">bilinear</span>: <span class="hljs-function"><span class="hljs-params">(x,y)</span> -></span> <span class="hljs-comment"># http://en.wikipedia.org/wiki/Bilinear_interpolation</span>
<span class="hljs-property">@checkXY</span> x,y
x0=Math.floor x; y0=Math.floor y; i=<span class="hljs-property">@toIndex</span> x0,y0; w=<span class="hljs-property">@width</span></pre></div></div>
</li>
<li id="section-15">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-15">¶</a>
</div>
<p>Edge case: If x is width-1, x0 is width-1, x is 0, dx is 1</p>
</div>
<div class="content"><div class='highlight'><pre> x=x-x0; y=y-y0; dx=<span class="hljs-number">1</span>-x; dy=<span class="hljs-number">1</span>-y</pre></div></div>
</li>
<li id="section-16">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-16">¶</a>
</div>
<p>Edge case: fij is 0 if beyond data array; undefined -> 0
if fij wrap on x, x is 0, dx is 1; return f00<em>dy+f01</em>y, linear on y</p>
</div>
<div class="content"><div class='highlight'><pre> f00=<span class="hljs-property">@data</span>[i]; f01=<span class="hljs-property">@data</span>[i+w] ? <span class="hljs-number">0</span>; f10=<span class="hljs-property">@data</span>[++i] ? <span class="hljs-number">0</span>; f11=<span class="hljs-property">@data</span>[i+w] ? <span class="hljs-number">0</span>
f00*dx*dy + f10*x*dy + f01*dx*y + f11*x*y</pre></div></div>
</li>
<li id="section-17">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-17">¶</a>
</div>
<p>Convert x,y ints to index into data array</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">toIndex</span>: <span class="hljs-function"><span class="hljs-params">(x,y)</span> -></span> x + y*<span class="hljs-property">@width</span></pre></div></div>
</li>
<li id="section-18">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-18">¶</a>
</div>
<p>Convert int index into data array into x,y ints</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">toXY</span>: <span class="hljs-function"><span class="hljs-params">(i)</span> -></span> [i % <span class="hljs-property">@width</span>, Math.floor i/<span class="hljs-property">@width</span>]</pre></div></div>
</li>
<li id="section-19">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-19">¶</a>
</div>
<p>Get data at x,y int offset into data array</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">getXY</span>: <span class="hljs-function"><span class="hljs-params">(x,y)</span> -></span> <span class="hljs-property">@checkXY</span> x,y; <span class="hljs-property">@data</span>[<span class="hljs-property">@toIndex</span> x,y]</pre></div></div>
</li>
<li id="section-20">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-20">¶</a>
</div>
<p>Set the data and int x,y coord of data array</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">setXY</span>: <span class="hljs-function"><span class="hljs-params">(x,y,num)</span> -></span> <span class="hljs-property">@checkXY</span> x,y; <span class="hljs-property">@data</span>[<span class="hljs-property">@toIndex</span> x,y] = num</pre></div></div>
</li>
<li id="section-21">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-21">¶</a>
</div>
<p>Return a printable string for dataset.
If p < 0, print data, otherwise use toFixed to truncate to p precision</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">toString</span>: <span class="hljs-function"><span class="hljs-params">(p=<span class="hljs-number">2</span>, sep=<span class="hljs-string">", "</span>)</span>-></span>
s = <span class="hljs-string">"width: <span class="hljs-subst">#{<span class="hljs-property">@width</span>}</span> height: <span class="hljs-subst">#{<span class="hljs-property">@height</span>}</span> data:"</span>
data = <span class="hljs-keyword">if</span> p<<span class="hljs-number">0</span> <span class="hljs-keyword">then</span> <span class="hljs-property">@data</span> <span class="hljs-keyword">else</span> u.aToFixed <span class="hljs-property">@data</span>, p
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> [<span class="hljs-number">0.</span>..<span class="hljs-property">@height</span>]
s += <span class="hljs-string">"\n"</span> + <span class="hljs-string">"<span class="hljs-subst">#{i}</span>: <span class="hljs-subst">#{data.slice i*<span class="hljs-property">@width</span>, (i+<span class="hljs-number">1</span>)*<span class="hljs-property">@width</span>}</span>"</span>
s.replace <span class="hljs-regexp">/,/g</span>, sep</pre></div></div>
</li>
<li id="section-22">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-22">¶</a>
</div>
<p>Convert dataset into an image.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">toImage</span>: <span class="hljs-function">-></span> <span class="hljs-property">@toContext</span>().canvas
<span class="hljs-attribute">toContext</span>: <span class="hljs-function">-></span>
ctx = u.createCtx <span class="hljs-property">@width</span>, <span class="hljs-property">@height</span>
idata = ctx.getImageData(<span class="hljs-number">0</span>, <span class="hljs-number">0</span>, <span class="hljs-property">@width</span>, <span class="hljs-property">@height</span>); ta = idata.data
<span class="hljs-keyword">if</span> <span class="hljs-property">@normalizeImage</span>
data = <span class="hljs-keyword">if</span> <span class="hljs-property">@gray</span> <span class="hljs-keyword">then</span> u.normalize8 <span class="hljs-property">@data</span> <span class="hljs-keyword">else</span> u.normalizeInt <span class="hljs-property">@data</span>, <span class="hljs-number">0</span>, Math.pow(<span class="hljs-number">2</span>,<span class="hljs-number">24</span>)-<span class="hljs-number">1</span>
<span class="hljs-keyword">else</span>
data = (Math.round d <span class="hljs-keyword">for</span> d <span class="hljs-keyword">in</span> <span class="hljs-property">@data</span>)
<span class="hljs-keyword">for</span> num, i <span class="hljs-keyword">in</span> data
j=<span class="hljs-number">4</span>*i
<span class="hljs-keyword">if</span> <span class="hljs-property">@gray</span>
ta[j] = ta[j+<span class="hljs-number">1</span>] = ta[j+<span class="hljs-number">2</span>] = Math.floor num
ta[j+<span class="hljs-number">3</span>] = <span class="hljs-property">@alpha</span>
<span class="hljs-keyword">else</span>
ta[j]=(num>><span class="hljs-number">16</span>)&<span class="hljs-number">0xff</span>; ta[j+<span class="hljs-number">1</span>]=(num>><span class="hljs-number">8</span>)&<span class="hljs-number">0xff</span>; ta[j+<span class="hljs-number">2</span>]=num&<span class="hljs-number">0xff</span>
ta[j+<span class="hljs-number">3</span>] = <span class="hljs-keyword">if</span> <span class="hljs-property">@normalizeImage</span> <span class="hljs-keyword">then</span> <span class="hljs-property">@alpha</span> <span class="hljs-keyword">else</span> ta[j+<span class="hljs-number">3</span>]=(num>><span class="hljs-number">24</span>)&<span class="hljs-number">0xff</span>
ctx.putImageData idata, <span class="hljs-number">0</span>, <span class="hljs-number">0</span>
ctx</pre></div></div>
</li>
<li id="section-23">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-23">¶</a>
</div>
<p>Get a data url representing this dataset as an image. Useful for
debugging: console.log the data url to the console and open it
in a new browser tab for viewing.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">toDataUrl</span>: <span class="hljs-function"><span class="hljs-params">()</span> -></span> u.ctxToDataUrl <span class="hljs-property">@toContext</span>()</pre></div></div>
</li>
<li id="section-24">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-24">¶</a>
</div>
<p>Show dataset as image in patch drawing layer or patch colors, return image</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">toDrawing</span>: <span class="hljs-function"><span class="hljs-params">(model = <span class="hljs-property">@model</span>)</span> -></span> model.patches.installDrawing(img=<span class="hljs-property">@toImage</span>()); img
<span class="hljs-attribute">toPatchColors</span>: <span class="hljs-function"><span class="hljs-params">(model = <span class="hljs-property">@model</span>)</span> -></span> model.patches.installColors(img=<span class="hljs-property">@toImage</span>()); img</pre></div></div>
</li>
<li id="section-25">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-25">¶</a>
</div>
<p>Resample dataset to patch width/height and set named patch variable.
Note this “insets” the dataset so the variable is sampled the center of the patch.
The dataset can be sampled directly to its edges .. i.e. in turtle coords.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">toPatchVar</span>: <span class="hljs-function"><span class="hljs-params">(name, model = <span class="hljs-property">@model</span>)</span> -></span>
<span class="hljs-keyword">if</span> (ps=model.patches).length <span class="hljs-keyword">is</span> <span class="hljs-property">@data</span>.length
<span class="hljs-keyword">then</span> p[name] = <span class="hljs-property">@data</span>[i] <span class="hljs-keyword">for</span> p,i <span class="hljs-keyword">in</span> ps
<span class="hljs-keyword">else</span> p[name] = <span class="hljs-property">@patchSample</span> p.x, p.y, model <span class="hljs-keyword">for</span> p <span class="hljs-keyword">in</span> ps
<span class="hljs-literal">null</span></pre></div></div>
</li>
<li id="section-26">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-26">¶</a>
</div>
<p>Sample via transformed coords.
x,y is in topleft-bottomright box: [tlx,tly,tlx+w,tly-h]</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">coordSample</span>: <span class="hljs-function"><span class="hljs-params">(x, y, tlx, tly, w, h)</span> -></span> <span class="hljs-comment">#brx, bry) -></span>
xs=(x-tlx)*(<span class="hljs-property">@width</span>-<span class="hljs-number">1</span>)/w; ys=(tly-y)*(<span class="hljs-property">@height</span>-<span class="hljs-number">1</span>)/h <span class="hljs-comment"># convert to sample space</span>
<span class="hljs-property">@sample</span> xs,ys
<span class="hljs-attribute">patchSample</span>: <span class="hljs-function"><span class="hljs-params">(px, py, model = <span class="hljs-property">@model</span>)</span> -></span>
w=model.world
<span class="hljs-property">@coordSample</span> px, py, w.minXcor, w.maxYcor, w.numX, w.numY</pre></div></div>
</li>
<li id="section-27">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-27">¶</a>
</div>
<p>Normalize a dataset to linear interpolation: [min,max] -> [lo, hi], float</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">normalize</span>: <span class="hljs-function"><span class="hljs-params">(lo, hi)</span> -></span> <span class="hljs-keyword">new</span> DataSet <span class="hljs-property">@width</span>, <span class="hljs-property">@height</span>, u.normalize(<span class="hljs-property">@data</span>, lo, hi), <span class="hljs-property">@model</span></pre></div></div>
</li>
<li id="section-28">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-28">¶</a>
</div>
<p>Normalize a dataset to clamped Uint8 bytes [0-255]</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">normalize8</span>: <span class="hljs-function">-></span> <span class="hljs-keyword">new</span> DataSet <span class="hljs-property">@width</span>, <span class="hljs-property">@height</span>, u.normalize8(<span class="hljs-property">@data</span>), <span class="hljs-property">@model</span></pre></div></div>
</li>
<li id="section-29">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-29">¶</a>
</div>
<p>Resample dataset to new width, height</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">resample</span>: <span class="hljs-function"><span class="hljs-params">(width, height)</span> -></span>
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> DataSet width,height,<span class="hljs-property">@data</span>,<span class="hljs-property">@model</span> <span class="hljs-keyword">if</span> width <span class="hljs-keyword">is</span> <span class="hljs-property">@width</span> <span class="hljs-keyword">and</span> height <span class="hljs-keyword">is</span> <span class="hljs-property">@height</span>
data = []; xScale = (<span class="hljs-property">@width</span>-<span class="hljs-number">1</span>)<span class="hljs-regexp">/(width-1); yScale = (@height-1)/</span>(height-<span class="hljs-number">1</span>)
<span class="hljs-keyword">for</span> y <span class="hljs-keyword">in</span> [<span class="hljs-number">0.</span>..height] <span class="hljs-keyword">by</span> <span class="hljs-number">1</span>
<span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> [<span class="hljs-number">0.</span>..width] <span class="hljs-keyword">by</span> <span class="hljs-number">1</span>
xs=x*xScale; ys=y*yScale
data.push <span class="hljs-property">@sample</span> xs,ys
<span class="hljs-keyword">new</span> DataSet width, height, data, <span class="hljs-property">@model</span></pre></div></div>
</li>
<li id="section-30">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-30">¶</a>
</div>
<p>Return neighbor values of the given x,y of the dataset.
Off-edge neighbors revert to nearest edge value.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">neighborhood</span>: <span class="hljs-function"><span class="hljs-params">(x,y,array=[])</span> -></span> <span class="hljs-comment"># return 3x3 neighborhood</span>
array.length = <span class="hljs-number">0</span> <span class="hljs-comment"># in case user supplied an array</span>
<span class="hljs-keyword">for</span> dy <span class="hljs-keyword">in</span> [-<span class="hljs-number">1.</span><span class="hljs-number">.1</span>]
<span class="hljs-keyword">for</span> dx <span class="hljs-keyword">in</span> [-<span class="hljs-number">1.</span><span class="hljs-number">.1</span>]
x0=u.clamp x+dx, <span class="hljs-number">0</span>, <span class="hljs-property">@width</span>-<span class="hljs-number">1</span>
y0=u.clamp y+dy, <span class="hljs-number">0</span>, <span class="hljs-property">@height</span>-<span class="hljs-number">1</span>
array.push <span class="hljs-property">@data</span>[<span class="hljs-property">@toIndex</span> x0, y0]
array</pre></div></div>
</li>
<li id="section-31">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-31">¶</a>
</div>
<p>Return a new dataset of same size convolved with the given kernel 3x3 matrix.
See <a href="http://goo.gl/ubFiji">Convolution article</a></p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">convolve</span>: <span class="hljs-function"><span class="hljs-params">(kernel,factor=<span class="hljs-number">1</span>)</span> -></span> <span class="hljs-comment"># Factory: return new convolved dataset</span>
array = []; n = []
<span class="hljs-keyword">if</span> <span class="hljs-property">@crop</span>
<span class="hljs-keyword">then</span> x0=y0=<span class="hljs-number">1</span>; h=<span class="hljs-property">@height</span>-<span class="hljs-number">1</span>; w=<span class="hljs-property">@width</span>-<span class="hljs-number">1</span>
<span class="hljs-keyword">else</span> x0=y0=<span class="hljs-number">0</span>; h=<span class="hljs-property">@height</span>; w=<span class="hljs-property">@width</span>
<span class="hljs-keyword">for</span> y <span class="hljs-keyword">in</span> [y0...h] <span class="hljs-keyword">by</span> <span class="hljs-number">1</span>
<span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> [x0...w] <span class="hljs-keyword">by</span> <span class="hljs-number">1</span>
<span class="hljs-property">@neighborhood</span> x,y,n
array.push u.aSum(u.aPairMul(kernel, n))*factor
<span class="hljs-keyword">new</span> DataSet w-x0, h-y0, array, <span class="hljs-property">@model</span></pre></div></div>
</li>
<li id="section-32">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-32">¶</a>
</div>
<p>A few common convolutions. dzdx/y are also called horiz/vert Sobel</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">dzdx</span>: <span class="hljs-function"><span class="hljs-params">(n=<span class="hljs-number">2</span>,factor=<span class="hljs-number">1</span>/<span class="hljs-number">8</span>)</span> -></span> <span class="hljs-property">@convolve</span>([-<span class="hljs-number">1</span>,<span class="hljs-number">0</span>,<span class="hljs-number">1</span>,-n,<span class="hljs-number">0</span>,n,-<span class="hljs-number">1</span>,<span class="hljs-number">0</span>,<span class="hljs-number">1</span>],factor)
<span class="hljs-attribute">dzdy</span>: <span class="hljs-function"><span class="hljs-params">(n=<span class="hljs-number">2</span>,factor=<span class="hljs-number">1</span>/<span class="hljs-number">8</span>)</span> -></span> <span class="hljs-property">@convolve</span>([<span class="hljs-number">1</span>,n,<span class="hljs-number">1</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,<span class="hljs-number">0</span>,-<span class="hljs-number">1</span>,-n,-<span class="hljs-number">1</span>],factor)
<span class="hljs-attribute">laplace8</span>: <span class="hljs-function">-></span> <span class="hljs-property">@convolve</span>([-<span class="hljs-number">1</span>,-<span class="hljs-number">1</span>,-<span class="hljs-number">1</span>,-<span class="hljs-number">1</span>,<span class="hljs-number">8</span>,-<span class="hljs-number">1</span>,-<span class="hljs-number">1</span>,-<span class="hljs-number">1</span>,-<span class="hljs-number">1</span>])
<span class="hljs-attribute">laplace4</span>: <span class="hljs-function">-></span> <span class="hljs-property">@convolve</span>([<span class="hljs-number">0</span>,-<span class="hljs-number">1</span>,<span class="hljs-number">0</span>,-<span class="hljs-number">1</span>,<span class="hljs-number">4</span>,-<span class="hljs-number">1</span>,<span class="hljs-number">0</span>,-<span class="hljs-number">1</span>,<span class="hljs-number">0</span>])
<span class="hljs-attribute">blur</span>: <span class="hljs-function"><span class="hljs-params">(factor=<span class="hljs-number">0.0625</span>)</span> -></span> <span class="hljs-property">@convolve</span>([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">4</span>,<span class="hljs-number">2</span>,<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">1</span>], factor) <span class="hljs-comment"># 1/16 = 0.0625</span>
<span class="hljs-attribute">edge</span>: <span class="hljs-function">-></span> <span class="hljs-property">@convolve</span>([<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,-<span class="hljs-number">7</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>,<span class="hljs-number">1</span>])</pre></div></div>
</li>
<li id="section-33">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-33">¶</a>
</div>
<p>Return filtered dataset by applying f to each dataset element</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">filter</span>: <span class="hljs-function"><span class="hljs-params">(f)</span> -></span> <span class="hljs-keyword">new</span> DataSet <span class="hljs-property">@width</span>, <span class="hljs-property">@height</span>, (f(d) <span class="hljs-keyword">for</span> d <span class="hljs-keyword">in</span> <span class="hljs-property">@data</span>), <span class="hljs-property">@model</span></pre></div></div>
</li>
<li id="section-34">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-34">¶</a>
</div>
<p>Create two new convolved datasets, slope and aspect, common in
the use of an elevation data set. See Esri tutorials for
<a href="http://goo.gl/ZcOl08">slope</a> and <a href="http://goo.gl/KoI4y5">aspect</a>
It also returns the two derivitive DataSets, dzdx, dzdy for
those wanting to use the results of the two convolutions.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">slopeAndAspect</span>: <span class="hljs-function"><span class="hljs-params">(noNaNs=<span class="hljs-literal">true</span>, posAngle=<span class="hljs-literal">true</span>)</span> -></span>
dzdx = <span class="hljs-property">@dzdx</span>() <span class="hljs-comment"># sub left z from right</span>
dzdy = <span class="hljs-property">@dzdy</span>() <span class="hljs-comment"># sub bottom z from top</span>
aspect = []; slope = []; h = dzdx.height; w = dzdx.width
<span class="hljs-keyword">for</span> y <span class="hljs-keyword">in</span> [<span class="hljs-number">0.</span>..h] <span class="hljs-keyword">by</span> <span class="hljs-number">1</span>
<span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> [<span class="hljs-number">0.</span>..w] <span class="hljs-keyword">by</span> <span class="hljs-number">1</span>
gx = dzdx.getXY(x,y); gy = dzdy.getXY(x,y)
slope.push Math.atan(Math.sqrt(gx*gx + gy*gy)) <span class="hljs-comment">#/(@cellsize)) # radians</span>
<span class="hljs-keyword">while</span> noNaNs <span class="hljs-keyword">and</span> gx <span class="hljs-keyword">is</span> gy
gx += u.randomNormal <span class="hljs-number">0</span>,<span class="hljs-number">.0001</span>; gy += u.randomNormal <span class="hljs-number">0</span>,<span class="hljs-number">.0001</span></pre></div></div>
</li>
<li id="section-35">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-35">¶</a>
</div>
<p>radians in [-PI,PI], downhill</p>
</div>
<div class="content"><div class='highlight'><pre> rad = <span class="hljs-keyword">if</span> gx <span class="hljs-keyword">is</span> gy <span class="hljs-keyword">is</span> <span class="hljs-number">0</span> <span class="hljs-keyword">then</span> NaN <span class="hljs-keyword">else</span> Math.atan2 -gy,-gx</pre></div></div>
</li>
<li id="section-36">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-36">¶</a>
</div>
<p>positive radians in [0,2PI] if desired</p>
</div>
<div class="content"><div class='highlight'><pre> rad += <span class="hljs-number">2</span>*Math.PI <span class="hljs-keyword">if</span> posAngle <span class="hljs-keyword">and</span> rad < <span class="hljs-number">0</span>
aspect.push rad
slope = <span class="hljs-keyword">new</span> DataSet w, h, slope, <span class="hljs-property">@model</span>
aspect = <span class="hljs-keyword">new</span> DataSet w, h, aspect, <span class="hljs-property">@model</span>
u.aToObj [slope, aspect, dzdx, dzdy], [<span class="hljs-string">"slope"</span>, <span class="hljs-string">"aspect"</span>, <span class="hljs-string">"dzdx"</span>, <span class="hljs-string">"dzdy"</span>]</pre></div></div>
</li>
<li id="section-37">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-37">¶</a>
</div>
<p>Return a subset of the dataset. x,y,width,height integers</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">subset</span>: <span class="hljs-function"><span class="hljs-params">(x, y, width, height)</span> -></span>
u.error <span class="hljs-string">"subSet: params out of range"</span> <span class="hljs-keyword">if</span> x+width><span class="hljs-property">@width</span> <span class="hljs-keyword">or</span> y+height><span class="hljs-property">@height</span>
data = []
<span class="hljs-keyword">for</span> j <span class="hljs-keyword">in</span> [y...y+height] <span class="hljs-keyword">by</span> <span class="hljs-number">1</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> [x...x+width] <span class="hljs-keyword">by</span> <span class="hljs-number">1</span>
data.push <span class="hljs-property">@getXY</span> i,j
<span class="hljs-keyword">new</span> DataSet width, height, data, <span class="hljs-property">@model</span>
ABM.AscDataSet = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AscDataSet</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">DataSet</span></span></pre></div></div>
</li>
<li id="section-38">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-38">¶</a>
</div>
<p>An .asc GIS file a text file with a header:</p>
<pre><code>ncols <span class="hljs-number">195</span>
nrows <span class="hljs-number">195</span>
xllcorner -<span class="hljs-number">84.355652</span>
yllcorner <span class="hljs-number">39.177963</span>
cellsize <span class="hljs-number">0.000093</span>
NODATA_value -<span class="hljs-number">9999</span>
</code></pre><p>..followed by a ncols X nrows matrix of numbers</p>
</div>
</li>
<li id="section-39">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-39">¶</a>
</div>
<p>Constructor takes a string generally via an xhr request.
It can be “empty” .. i.e. needing a second parse() call
so that it can be used in an async file operation.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">constructor</span>: <span class="hljs-function"><span class="hljs-params">(<span class="hljs-property">@str</span>=<span class="hljs-string">""</span>, <span class="hljs-property">@model</span>)</span> -></span>
<span class="hljs-keyword">super</span>() <span class="hljs-comment"># start out as an empty dataset</span>
<span class="hljs-keyword">return</span> <span class="hljs-keyword">if</span> <span class="hljs-property">@str</span>.length <span class="hljs-keyword">is</span> <span class="hljs-number">0</span>
<span class="hljs-property">@parse</span> <span class="hljs-property">@str</span></pre></div></div>
</li>
<li id="section-40">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-40">¶</a>
</div>
<p>Complete an initial, empty dataset object who’s string
is read via an xhr request.</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">parse</span>: <span class="hljs-function"><span class="hljs-params">(<span class="hljs-property">@str</span>)</span> -></span>
textData = <span class="hljs-property">@str</span>.split <span class="hljs-string">"\n"</span>; <span class="hljs-property">@header</span> = {}; <span class="hljs-comment">#gisData.data = []</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> [<span class="hljs-number">0.</span><span class="hljs-number">.5</span>]
keyVal = textData[i].split <span class="hljs-regexp">/\s+/</span>
<span class="hljs-property">@header</span>[keyVal[<span class="hljs-number">0</span>].toLowerCase()] = parseFloat keyVal[<span class="hljs-number">1</span>]
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> [<span class="hljs-number">0.</span>..<span class="hljs-property">@header</span>.nrows] <span class="hljs-keyword">by</span> <span class="hljs-number">1</span>
nums = textData[<span class="hljs-number">6</span>+i].trim().split(<span class="hljs-string">" "</span>)
nums[i] = parseFloat nums[i] <span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> [<span class="hljs-number">0.</span>..nums.length]
<span class="hljs-property">@data</span> = <span class="hljs-property">@data</span>.concat nums
<span class="hljs-property">@reset</span> <span class="hljs-property">@header</span>.ncols, <span class="hljs-property">@header</span>.nrows, <span class="hljs-property">@data</span>
ABM.ImageDataSet = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ImageDataSet</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">DataSet</span></span></pre></div></div>
</li>
<li id="section-41">
<div class="annotation">
<div class="pilwrap ">
<a class="pilcrow" href="#section-41">¶</a>
</div>
<p>An image-as-data dataset. The parser takes an image
data Uint8Array, enumerates it in 4-byte segments converting
the segments into a data element for the given array type.
If rowsPerSlice is set, incrementally traverse image in image
slice of that height; used for huge images.
Defaults to gray scale and Uint8ClampedArray
img may be a canvas</p>
</div>
<div class="content"><div class='highlight'><pre> <span class="hljs-attribute">constructor</span>: <span class="hljs-function"><span class="hljs-params">(img, <span class="hljs-property">@f</span>=u.pixelByte(<span class="hljs-number">0</span>), <span class="hljs-property">@arrayType</span>=Uint8ClampedArray, <span class="hljs-property">@rowsPerSlice</span>, <span class="hljs-property">@model</span>)</span> -></span>
<span class="hljs-keyword">super</span>() <span class="hljs-comment"># start out as an empty dataset</span>
<span class="hljs-keyword">return</span> <span class="hljs-keyword">unless</span> img?
<span class="hljs-property">@parse</span> img
<span class="hljs-attribute">parse</span>: <span class="hljs-function"><span class="hljs-params">(img)</span> -></span>
<span class="hljs-property">@rowsPerSlice</span> <span class="hljs-keyword">or</span>= img.height
data = u.imageRowsToData img, <span class="hljs-property">@rowsPerSlice</span>, <span class="hljs-property">@f</span>, <span class="hljs-property">@arrayType</span>
<span class="hljs-property">@reset</span> img.width, img.height, data
ABM.PatchDataSet = <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PatchDataSet</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">DataSet</span></span>
<span class="hljs-attribute">constructor</span>: <span class="hljs-function"><span class="hljs-params">(f, arrayType=Array, <span class="hljs-property">@model</span>)</span> -></span>
data = <span class="hljs-keyword">new</span> arrayType (ps=<span class="hljs-property">@model</span>.patches).length
f = u.propFcn f <span class="hljs-keyword">if</span> u.isString f
data[i] = f(p) <span class="hljs-keyword">for</span> p,i <span class="hljs-keyword">in</span> ps
<span class="hljs-keyword">super</span> ps.numX, ps.numY, data
<span class="hljs-property">@useNearest</span> = <span class="hljs-literal">true</span>
<span class="hljs-attribute">toPatchVar</span>: <span class="hljs-function"><span class="hljs-params">(name)</span> -></span>
p[name] = <span class="hljs-property">@data</span>[i] <span class="hljs-keyword">for</span> p,i <span class="hljs-keyword">in</span> <span class="hljs-property">@model</span>.patches; <span class="hljs-literal">null</span></pre></div></div>
</li>
</ul>
</div>
</body>
</html>