Rev 1221 | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1190 | werner | 1 | /** |
1191 | werner | 2 | The `Grid` class encapsulates a floating point grid (with double precision). The class provides high-performance functions |
3 | for element-wise computations, but also methods for Javascript access. You can {{#crossLink "Grid/load:method"}}{{/crossLink}} |
||
4 | grids from disk (and {{#crossLink "Grid/save:method"}}{{/crossLink}} to disk), or from iLand by using methods such as |
||
5 | {{#crossLink "Globals/grid:method"}}Globals.grid{{/crossLink}}, or one of its disturbance submodules (e.g., |
||
6 | {{#crossLink "Barkbeetle/grid:method"}}Barkbeetle.grid{{/crossLink}}, {{#crossLink "Wind/grid:method"}}Wind.grid{{/crossLink}}). The extent and |
||
7 | cell size depend on the respective functions, but usually cover the whole landscape. |
||
1190 | werner | 8 | |
1191 | werner | 9 | |
10 | Use {{#crossLink "Grid/apply:method"}}{{/crossLink}} for updating a single grid, and {{#crossLink "Grid/combine:method"}}{{/crossLink}} for |
||
11 | calculating grid values based on combining multiple grid sources. |
||
12 | |||
13 | Javacsript based access to grid values is possible via {{#crossLink "Grid/value:method"}}{{/crossLink}} and {{#crossLink "Grid/setValue:method"}}{{/crossLink}} methods. |
||
14 | |||
1190 | werner | 15 | Memory management |
16 | ----------------- |
||
1191 | werner | 17 | Methods such as {{#crossLink "Globals/grid:method"}}Globals.grid{{/crossLink}} return a copy of the data present in iLand, and calls to |
18 | {{#crossLink "Grid/apply:method"}}{{/crossLink}} or {{#crossLink "Grid/combine:method"}}{{/crossLink}} __alter__ the underlying data; you can use the |
||
19 | {{#crossLink "Grid/copy:method"}}{{/crossLink}} method create a duplicate grid (if the underlying data should not change). Memory of grids is |
||
20 | freed automatically by the Javascript engine (garbage collection) when they are no longer referenced. |
||
1190 | werner | 21 | |
22 | Examples |
||
23 | -------- |
||
1191 | werner | 24 | // memory management |
25 | var g=Globals.grid('height'); |
||
26 | g.apply('height*2'); // modify 'g' |
||
27 | var h=g.copy(); |
||
28 | h.apply('x*2'); // modify copy of 'g' |
||
29 | |||
1190 | werner | 30 | @module iLand |
31 | @class Grid |
||
32 | */ |
||
33 | Grid = { |
||
34 | |||
35 | /** |
||
36 | gets or sets the name of the grid (that are used for calculations) |
||
37 | |||
38 | @property name |
||
39 | @type string |
||
40 | */ |
||
41 | |||
42 | |||
43 | /** |
||
44 | the height (y-dimension) of the grid (number of pixels) |
||
45 | |||
46 | See also: {{#crossLink "Grid/cellsize:property"}}{{/crossLink}}, {{#crossLink "Grid/width:property"}}{{/crossLink}} |
||
47 | |||
48 | @property height |
||
49 | @type integer |
||
50 | @readOnly |
||
51 | */ |
||
52 | |||
53 | /** |
||
54 | the width (x-dimension) of the grid (number of pixels) |
||
55 | |||
56 | See also: {{#crossLink "Grid/cellsize:property"}}{{/crossLink}}, {{#crossLink "Grid/height:property"}}{{/crossLink}} |
||
57 | |||
58 | @property width |
||
59 | @type integer |
||
60 | @readOnly |
||
61 | */ |
||
62 | /** |
||
63 | the number of pixels of the grid. |
||
64 | |||
65 | See also: {{#crossLink "Grid/cellsize:property"}}{{/crossLink}}, {{#crossLink "Grid/height:property"}}{{/crossLink}}, {{#crossLink "Grid/width:property"}}{{/crossLink}} |
||
66 | |||
67 | @property count |
||
68 | @type integer |
||
69 | @readOnly |
||
70 | */ |
||
71 | /** |
||
72 | the cell size of the grid in meters. |
||
73 | |||
74 | See also: {{#crossLink "Grid/count:property"}}{{/crossLink}}, {{#crossLink "Grid/height:property"}}{{/crossLink}}, {{#crossLink "Grid/width:property"}}{{/crossLink}} |
||
75 | |||
76 | @property cellsize |
||
77 | @type integer |
||
78 | @readOnly |
||
79 | */ |
||
80 | /** |
||
81 | is `true` if the grid contains data. |
||
82 | |||
83 | @property isValid |
||
84 | @type boolean |
||
85 | @readOnly |
||
86 | */ |
||
87 | |||
88 | // methods |
||
89 | |||
90 | /// create a copy of the current grid and return a new script object |
||
91 | //QJSValue copy(); |
||
92 | /// fill the grid with 0-values |
||
93 | //void clear(); |
||
94 | |||
95 | /** |
||
1194 | werner | 96 | Create a copy of the current grid and return a new grid object. The `name` of the copied grid is _x_. |
1190 | werner | 97 | |
98 | @method copy |
||
99 | @return {grid} a copy of the grid |
||
100 | @Example |
||
101 | var a = Globals.grid('height'); |
||
102 | var ac = a.copy(); |
||
1191 | werner | 103 | ac.name = 'h2'; // change the name |
1190 | werner | 104 | */ |
105 | |||
106 | /** |
||
1194 | werner | 107 | Fill the grid with 0-values. |
1190 | werner | 108 | |
109 | @method clear |
||
110 | */ |
||
111 | /** |
||
1194 | werner | 112 | Retrieve some key parameters of the grid as a string. |
1190 | werner | 113 | |
114 | @method info |
||
115 | @return {string} the information string |
||
116 | @Example |
||
117 | var g = Globals.grid('height'); |
||
118 | console.log(g.info()); |
||
119 | //prints: grid-dimensions: 1820/1020 (cellsize: 10, N cells: 1856400), grid-name='height' |
||
120 | */ |
||
121 | |||
122 | /** |
||
1194 | werner | 123 | Save to a file `file_name` as ESRI ASCII raster file. |
1190 | werner | 124 | |
125 | |||
126 | See also: {{#crossLink "Grid/load:method"}}{{/crossLink}} |
||
127 | |||
128 | @method save |
||
129 | @param {string} file_name destination file name (relative to project folder) |
||
130 | */ |
||
131 | |||
132 | /** |
||
1194 | werner | 133 | Load from a file `file_name` (ESRI ASCII raster grid). The `name` property is set to the base file name (without path and extension). |
1190 | werner | 134 | |
135 | See also: {{#crossLink "Grid/save:method"}}{{/crossLink}} |
||
136 | |||
137 | @method load |
||
138 | @param {string} file_name source file name (relative to project folder) |
||
139 | @return { boolean } true on success. |
||
140 | */ |
||
141 | |||
142 | /** |
||
1194 | werner | 143 | Apply a function on the values of the grid, thus modifiying the grid (see the copy() function). |
1191 | werner | 144 | The function is given as a string representing an [Expression](http://iland.boku.ac.at/Expression) and is evaluated for each cell of the grid. |
1190 | werner | 145 | In the expression, the current value of the grid cell can be accessed using the {{#crossLink "Grid/name:property"}}{{/crossLink}} property. |
146 | |||
147 | See also: {{#crossLink "Grid/copy:method"}}{{/crossLink}}, {{#crossLink "Grid/combine:method"}}{{/crossLink}} |
||
148 | |||
149 | @method apply |
||
150 | @param {string} expression expression that is applied to each cell of the grid |
||
151 | @Example |
||
152 | var g = Globals.grid('height'); // name is 'height' |
||
153 | g.apply('x*x'); // error, invalid variable |
||
154 | g.apply('min(height, 30'); // ok |
||
155 | g.apply('if(height<=4, 0, height)'); // ok |
||
1191 | werner | 156 | var h = g.copy(); |
157 | h.apply('x^2'); // use copy() if the grid should not change (note: copies are named 'x') |
||
1190 | werner | 158 | */ |
159 | |||
160 | /** |
||
1194 | werner | 161 | Combine multiple grids, and set the value of the internal grid to the result of `expression` for each cell. The function expects |
1190 | werner | 162 | an object that includes named source grids. The `expression` is an [iLand Expression](http://iland.boku.ac.at/Expression), |
1191 | werner | 163 | and you can refer to the grids in `grid_objects` with the respective name of the grid. Note that the function |
1190 | werner | 164 | alters the data of the grid. |
165 | |||
166 | |||
167 | All grids must have the same dimensions, and the grid iteself can be accessed by adding the grid to `grid_objects`. |
||
168 | |||
169 | |||
170 | See also: {{#crossLink "Grid/copy:method"}}{{/crossLink}}, {{#crossLink "Grid/apply:method"}}{{/crossLink}} |
||
171 | |||
172 | @method combine |
||
173 | @param {string} expression expression that is applied to each cell of the grid |
||
174 | @param {object} grid_objects object including the source grids |
||
175 | @Example |
||
176 | var g = Globals.grid('height'); // name of g is 'height' |
||
177 | var j = Globals.grid('count'); |
||
1191 | werner | 178 | var k = g.copy(); |
179 | k.apply('if(height>30,1,0)'); |
||
1190 | werner | 180 | // update 'k' by combining g,j, and k |
181 | k.combine('height*count * filter', { height: g, filter: k, count: j } ); |
||
182 | */ |
||
183 | |||
184 | /** |
||
1194 | werner | 185 | Apply the expression `expression` on all pixels of the grid and return the sum of the values |
1190 | werner | 186 | |
187 | See also: {{#crossLink "Grid/apply:method"}}{{/crossLink}} |
||
188 | |||
189 | @method sum |
||
190 | @param {string} expresion expression to evaluate |
||
191 | @return { double } sum of `expression` over all cells |
||
192 | @Example |
||
193 | var g = Globals.grid('height'); |
||
194 | var mean_h = g.sum('height') / g.count; |
||
195 | g.apply('height/' + mean_h); // scale the height grid |
||
196 | */ |
||
197 | |||
198 | /** |
||
1194 | werner | 199 | Access individual cell values of the grid at the given position. If the grid is empty, or the the |
1190 | werner | 200 | given position is invalid, -1 is returned. |
201 | |||
202 | See also: {{#crossLink "Grid/setValue:method"}}{{/crossLink}} |
||
203 | |||
204 | @method value |
||
205 | @param {integer} x index in x direction (between 0 and grid.width-1) |
||
206 | @param {integer} y index in y direction (between 0 and grid.height-1) |
||
207 | @return { double } value of the grid at position (`x`, `y`) |
||
208 | |||
209 | */ |
||
210 | /** |
||
1194 | werner | 211 | Set the value at position (`x`, `y`) to `value`. Note that using the {{#crossLink "Grid/value:method"}}{{/crossLink}} and |
1191 | werner | 212 | {{#crossLink "Grid/setValue:method"}}{{/crossLink}} methods is much slower than using functions such as {{#crossLink "Grid/apply:method"}}{{/crossLink}}. |
1190 | werner | 213 | |
1191 | werner | 214 | |
1190 | werner | 215 | See also: {{#crossLink "Grid/value:method"}}{{/crossLink}} |
216 | |||
217 | @method setValue |
||
218 | @param {integer} x index in x direction (between 0 and grid.width-1) |
||
219 | @param {integer} y index in y direction (between 0 and grid.height-1) |
||
220 | @param {double} value value to set |
||
221 | @Example |
||
1191 | werner | 222 | // using javascript access functions can be 100x times slower: |
223 | var g = Globals.grid('height'); |
||
224 | var ela=Globals.msec; // for measuring time, see also the 'msec' doc |
||
225 | |||
226 | for (var i=0;i<g.width;++i) { |
||
227 | for (var j=0;j<g.height;++j) { |
||
228 | g.setValue(i,j, g.value(i,j)*2); |
||
229 | } |
||
230 | } |
||
231 | console.log("javascript: " + (Globals.msec - ela) + "ms"); // 1650ms |
||
232 | ela = Globals.msec; |
||
233 | g.apply('height*2'); |
||
234 | console.log("apply(): " + (Globals.msec - ela) + "ms"); // 17ms |
||
235 | |||
1190 | werner | 236 | */ |
237 | } |