Geoinformatica  0.90
grid.h
Go to the documentation of this file.
1 #ifndef RAL_GRID_H
2 #define RAL_GRID_H
3 
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <limits.h>
8 #include <math.h>
9 
31 #define RAL_MSG_BUF_SIZE 1024
32 
33 #define RAL_MALLOC(type) (type*)malloc(sizeof(type))
34 #define RAL_CALLOC(count, type) (type*)calloc((count), sizeof(type))
35 #define RAL_REALLOC(pointer, count, type) (type*)realloc((pointer), (count)*sizeof(type))
36 #define RAL_FREE(pointer) { if(pointer){ free(pointer); pointer = NULL; } }
37 
41 #define RAL_CHECK(test) { if (!(test)) { goto fail; } }
42 
46 #ifdef MSVC
47 #define RAL_CHECKM(test, msg) { if (!(test)) { \
48  ral_set_msg(msg); \
49  goto fail; } }
50 #else
51 #define RAL_CHECKM(test, msg) { if (!(test)) { \
52  char tmp[RAL_MSG_BUF_SIZE]; \
53  snprintf(tmp, RAL_MSG_BUF_SIZE-1, "%s: %s", __PRETTY_FUNCTION__, msg); \
54  ral_set_msg(tmp); \
55  goto fail; } }
56 #endif
57 
58 #ifdef RAL_HAVE_GDAL
59 
60 void CPL_DLL CPL_STDCALL ral_cpl_error(CPLErr eclass, int code, const char *msg);
61 #endif
62 
63 typedef char *string_handle;
64 
65 void RAL_CALL ral_set_msg(char *msg);
66 
67 string_handle RAL_CALL ral_msg(const char *format, ...);
68 
70 int RAL_CALL ral_has_msg();
71 
73 string_handle RAL_CALL ral_get_msg();
74 
75 #define AND &&
76 #define OR ||
77 
78 #define SQRT2 1.414213562
79 
80 #define RAL_EPSILON 1.0E-6
81 
82 #define EVEN(a) ((a) % 2 == 0)
83 #undef ODD
84 #define ODD(n) ((n) & 1)
85 
86 #undef max
87 #define max(x, y) ((x)>(y) ? (x) : (y))
88 #undef min
89 #define min(x, y) ((x)<(y) ? (x) : (y))
90 
91 #define swap(a, b, temp) \
92  {(temp) = (a);(a) = (b);(b) = (temp);}
93 
94 #define round(x) \
95  ((x)<0 ? ((long)((x)-0.5)) : ((long)((x)+0.5)))
96 
100 #define RAL_INTEGER_GRID 1
101 #define RAL_REAL_GRID 2
102 
104 typedef struct {
105  int i;
106  int j;
107 } ral_cell;
108 
109 typedef ral_cell *ral_cell_handle;
110 
111 #define RAL_SAME_CELL(a, b) \
112  ((a).i == (b).i AND (a).j == (b).j)
113 
114 #define RAL_DISTANCE_BETWEEN_CELLS(a, b) \
115  (sqrt(((double)((b).i)-(double)((a).i)) * \
116  ((double)((b).i)-(double)((a).i)) + \
117  ((double)((b).j)-(double)((a).j)) * \
118  ((double)((b).j)-(double)((a).j))))
119 
120 ral_cell_handle RAL_CALL ral_cell_create();
121 void RAL_CALL ral_cell_destroy(ral_cell **c);
122 
124 typedef struct {
125  ral_cell up_left;
126  ral_cell down_right;
127 } ral_window;
128 
129 #define RAL_WINDOW_HEIGHT(w) ((w).down_right.i-(w).up_left.i+1)
130 #define RAL_WINDOW_WIDTH(w) ((w).down_right.j-(w).up_left.j+1)
131 
132 #define RAL_N 1
133 #define RAL_NE 2
134 #define RAL_E 3
135 #define RAL_SE 4
136 #define RAL_S 5
137 #define RAL_SW 6
138 #define RAL_W 7
139 #define RAL_NW 8
140 
141 #define RAL_FLAT_AREA -1
142 #define RAL_PIT_CELL 0
143 
144 #define RAL_DIRECTIONS(dir) \
145  for ((dir) = 1; (dir) < 9; (dir)++)
146 
147 #define RAL_DISTANCE_UNIT(dir) \
148  (EVEN((dir)) ? SQRT2 : 1)
149 
151 ral_cell RAL_CALL ral_cell_move(ral_cell c, int dir);
152 
154 int RAL_CALL ral_cell_dir(ral_cell a, ral_cell b);
155 
156 #define RAL_NEXT_DIR(d) \
157  ((d) > 7 ? 1 : (d) + 1)
158 
159 #define RAL_PREV_DIR(d) \
160  ((d) > 1 ? (d) - 1 : 8)
161 
162 #define RAL_INV_DIR(d) \
163  ((d) > 4 ? (d) - 4 : (d) + 4)
164 
166 typedef struct ral_grid {
168  int datatype;
170  int M;
172  int N;
174  double cell_size;
181  void *data;
183  struct ral_grid *mask;
184 } ral_grid;
185 
186 typedef ral_grid *ral_grid_handle;
187 
191 ral_grid_handle RAL_CALL ral_grid_create(int datatype, int M, int N);
192 
197 
203 
204 void RAL_CALL ral_grid_destroy(ral_grid **gd);
205 
206 #ifdef RAL_HAVE_GDAL
207 
212 ral_grid_handle RAL_CALL ral_grid_create_using_GDAL(GDALDatasetH dataset, int band, ral_rectangle clip_region, double cell_size);
213 #endif
214 
217 int RAL_CALL ral_grid_write(ral_grid *gd, char *filename);
218 
219 int RAL_CALL ral_grid_get_height(ral_grid *gd);
220 int RAL_CALL ral_grid_get_width(ral_grid *gd);
221 int RAL_CALL ral_grid_get_datatype(ral_grid *gd);
222 
223 double RAL_CALL ral_grid_get_cell_size(ral_grid *gd);
224 ral_rectangle RAL_CALL ral_grid_get_world(ral_grid *gd);
225 
226 int RAL_CALL ral_grid_has_nodata_value(ral_grid *gd);
227 
231 int RAL_CALL ral_grid_get_integer_nodata_value(ral_grid *gd, RAL_INTEGER *nodata_value);
232 int RAL_CALL ral_grid_get_real_nodata_value(ral_grid *gd, RAL_REAL *nodata_value);
233 
236 int RAL_CALL ral_grid_set_integer_nodata_value(ral_grid *gd, RAL_INTEGER nodata_value);
237 int RAL_CALL ral_grid_set_real_nodata_value(ral_grid *gd, RAL_REAL nodata_value);
238 
239 void RAL_CALL ral_grid_remove_nodata_value(ral_grid *gd);
240 
241 ral_grid_handle RAL_CALL ral_grid_get_mask(ral_grid *gd);
242 void RAL_CALL ral_grid_set_mask(ral_grid *gd, ral_grid *mask);
243 void RAL_CALL ral_grid_clear_mask(ral_grid *gd);
244 
245 void RAL_CALL ral_grid_flip_horizontal(ral_grid *gd);
246 void RAL_CALL ral_grid_flip_vertical(ral_grid *gd);
247 
248 #define RAL_FOR(c, gd) \
249  for((c).i=0;(c).i<(gd)->M;(c).i++) for((c).j=0;(c).j<(gd)->N;(c).j++) \
250  if (!(gd)->mask OR (RAL_GRID_CELL_IN((gd)->mask, (c)) AND RAL_INTEGER_GRID_CELL((gd)->mask, (c))))
251 
252 #define RAL_GRID_INDEX(i, j, N) (j)+(N)*(i)
253 
254 #define RAL_INTEGER_GRID_AT(gd, i, j) \
255  (((RAL_INTEGER *)((gd)->data))[RAL_GRID_INDEX((i),(j),((gd)->N))])
256 
257 #define RAL_REAL_GRID_AT(gd, i, j) \
258  (((RAL_REAL *)((gd)->data))[RAL_GRID_INDEX((i),(j),((gd)->N))])
259 
260 #define RAL_INTEGER_GRID_CELL(gd, c) \
261  (((RAL_INTEGER *)((gd)->data))[RAL_GRID_INDEX(((c).i),((c).j),((gd)->N))])
262 
263 #define RAL_REAL_GRID_CELL(gd, c) \
264  (((RAL_REAL *)((gd)->data))[RAL_GRID_INDEX(((c).i),((c).j),((gd)->N))])
265 
266 #define RAL_GRID_CELL(gd, c) \
267  ((gd)->datatype == RAL_INTEGER_GRID ? RAL_INTEGER_GRID_CELL(gd, c) : RAL_REAL_GRID_CELL(gd, c))
268 
269 #define RAL_GRID_CELL_IN(gd, c) \
270  ((c).i >= 0 AND (c).j >= 0 AND (c).i < (gd)->M AND (c).j < (gd)->N)
271 
272 #define RAL_GRID_CELL_OUT(gd, c) \
273  ((c).i < 0 OR (c).j < 0 OR (c).i >= (gd)->M OR (c).j >= (gd)->N)
274 
275 #define RAL_INTEGER_GRID_NODATA_VALUE(gd) ((gd)->nodata_value ? *((RAL_INTEGER *)((gd)->nodata_value)) : 0)
276 
277 #define RAL_REAL_GRID_NODATA_VALUE(gd) ((gd)->nodata_value ? *((RAL_REAL *)((gd)->nodata_value)) : 0)
278 
279 #define RAL_GRID_NODATA_VALUE(gd) \
280  ((gd)->datatype == RAL_INTEGER_GRID ? RAL_INTEGER_GRID_NODATA_VALUE(gd) : RAL_REAL_GRID_NODATA_VALUE(gd))
281 
282 #define RAL_INTEGER_GRID_DATACELL(gd, c) \
283  ((gd)->nodata_value ? (RAL_INTEGER_GRID_CELL((gd), (c)) != RAL_INTEGER_GRID_NODATA_VALUE(gd)) : TRUE)
284 
285 #define RAL_REAL_GRID_DATACELL(gd, c) \
286  ((gd)->nodata_value ? (RAL_REAL_GRID_CELL((gd), (c)) != RAL_REAL_GRID_NODATA_VALUE(gd)) : TRUE)
287 
288 #define RAL_GRID_DATACELL(gd, c) \
289  ((gd)->nodata_value ? \
290  ((gd)->datatype == RAL_INTEGER_GRID ? \
291  (RAL_INTEGER_GRID_CELL((gd), (c)) != RAL_INTEGER_GRID_NODATA_VALUE(gd)) : \
292  (RAL_REAL_GRID_CELL((gd), (c)) != RAL_REAL_GRID_NODATA_VALUE(gd))) : TRUE)
293 
294 #define RAL_GRID_NODATACELL(gd, c) \
295  ((gd)->nodata_value ? \
296  ((gd)->datatype == RAL_INTEGER_GRID ? RAL_INTEGER_GRID_NODATACELL(gd, c) : \
297  RAL_REAL_GRID_NODATACELL(gd, c)) : FALSE)
298 
299 #define RAL_INTEGER_GRID_NODATACELL(gd, c) (!(RAL_INTEGER_GRID_DATACELL(gd, c)))
300 
301 #define RAL_REAL_GRID_NODATACELL(gd, c) (!(RAL_REAL_GRID_DATACELL(gd, c)))
302 
303 #define RAL_GRID_POINT_IN(gd, p) \
304  ((p).x >= (gd)->world.min.x AND (p).y >= (gd)->world.min.y AND \
305  (p).x <= (gd)->world.max.x AND (p).y <= (gd)->world.max.y)
306 
307 #define RAL_GRID_POINT_OUT(gd, p) \
308  ((p).x < (gd)->world.min.x OR (p).y < (gd)->world.min.y OR \
309  (p).x > (gd)->world.max.x OR (p).y > (gd)->world.max.y)
310 
312 #define RAL_INTEGER_GRID_SETNODATACELL(gd, c) \
313  (RAL_INTEGER_GRID_CELL((gd), (c)) = (gd)->nodata_value ? *((RAL_INTEGER *)((gd)->nodata_value)) : 0)
314 
316 #define RAL_REAL_GRID_SETNODATACELL(gd, c) \
317  (RAL_REAL_GRID_CELL((gd), (c)) = (gd)->nodata_value ? *((RAL_REAL *)((gd)->nodata_value)) : 0)
318 
320 int RAL_CALL ral_grid_coerce(ral_grid *gd, int data_type);
321 
323 int RAL_CALL ral_grid_overlayable(ral_grid *g1, ral_grid *g2);
324 
325 void RAL_CALL ral_grid_set_bounds_csnn(ral_grid *gd, double cell_size, double minX, double minY);
326 void RAL_CALL ral_grid_set_bounds_csnx(ral_grid *gd, double cell_size, double minX, double maxY);
327 void RAL_CALL ral_grid_set_bounds_csxn(ral_grid *gd, double cell_size, double maxX, double minY);
328 void RAL_CALL ral_grid_set_bounds_csxx(ral_grid *gd, double cell_size, double maxX, double maxY);
329 void RAL_CALL ral_grid_set_bounds_nxn(ral_grid *gd, double minX, double maxX, double minY);
330 void RAL_CALL ral_grid_set_bounds_nxx(ral_grid *gd, double minX, double maxX, double maxY);
331 void RAL_CALL ral_grid_set_bounds_nnx(ral_grid *gd, double minX, double minY, double maxY);
332 void RAL_CALL ral_grid_set_bounds_xnx(ral_grid *gd, double maxX, double minY, double maxY);
333 
334 void RAL_CALL ral_grid_copy_bounds(ral_grid *from, ral_grid *to);
335 
340 
343 
346 
347 int RAL_CALL ral_grid_get_real(ral_grid *gd, ral_cell c, RAL_REAL *x);
348 int RAL_CALL ral_grid_get_integer(ral_grid *gd, ral_cell c, RAL_INTEGER *x);
349 int RAL_CALL ral_grid_set_real(ral_grid *gd, ral_cell c, RAL_REAL x);
350 int RAL_CALL ral_grid_set_integer(ral_grid *gd, ral_cell c, RAL_INTEGER x);
351 int RAL_CALL ral_grid_set_nodata(ral_grid *gd, ral_cell c);
352 
353 int RAL_CALL ral_grid_set_all_integer(ral_grid *gd, RAL_INTEGER x);
354 int RAL_CALL ral_grid_set_all_real(ral_grid *gd, RAL_REAL x);
355 int RAL_CALL ral_grid_set_all_nodata(ral_grid *gd);
356 
357 ral_grid_handle RAL_CALL ral_grid_round(ral_grid *gd);
358 
359 typedef void *ral_handle;
360 
361 ral_handle RAL_CALL ral_grid_get_focal(ral_grid *gd, ral_cell c, int d);
362 void RAL_CALL ral_grid_set_focal(ral_grid *gd, ral_cell c, void *x, int *mask, int d);
363 
367 int RAL_CALL ral_integer_grid_focal_sum(ral_grid *grid, ral_cell cell, int *mask, int delta, int *sum);
368 
372 int RAL_CALL ral_real_grid_focal_sum(ral_grid *grid, ral_cell cell, int *mask, int delta, double *sum);
373 
377 int RAL_CALL ral_grid_focal_mean(ral_grid *grid, ral_cell cell, int *mask, int delta, double *mean);
378 
382 int RAL_CALL ral_grid_focal_variance(ral_grid *grid, ral_cell cell, int *mask, int delta, double *variance);
383 
387 int RAL_CALL ral_grid_focal_count(ral_grid *grid, ral_cell cell, int *mask, int delta);
388 
393 int RAL_CALL ral_grid_focal_count_of(ral_grid *grid, ral_cell cell, int *mask, int delta, RAL_INTEGER value);
394 
399 int RAL_CALL ral_integer_grid_focal_range(ral_grid *grid, ral_cell cell, int *mask, int delta, ral_integer_range *range);
400 
405 int RAL_CALL ral_real_grid_focal_range(ral_grid *grid, ral_cell cell, int *mask, int delta, ral_real_range *range);
406 
410 int RAL_CALL ral_grid_convolve(ral_grid *grid, ral_cell cell, double *kernel, int delta, double *g);
411 
412 ral_grid_handle RAL_CALL ral_grid_focal_sum_grid(ral_grid *grid, int *mask, int delta);
413 ral_grid_handle RAL_CALL ral_grid_focal_mean_grid(ral_grid *grid, int *mask, int delta);
414 ral_grid_handle RAL_CALL ral_grid_focal_variance_grid(ral_grid *grid, int *mask, int delta);
415 ral_grid_handle RAL_CALL ral_grid_focal_count_grid(ral_grid *grid, int *mask, int delta);
416 ral_grid_handle RAL_CALL ral_grid_focal_count_of_grid(ral_grid *grid, int *mask, int delta, RAL_INTEGER value);
417 
419 ral_grid_handle RAL_CALL ral_grid_spread(ral_grid *grid, double *mask, int delta);
421 ral_grid_handle RAL_CALL ral_grid_spread_random(ral_grid *grid, double *mask, int delta);
422 
423 ral_grid_handle RAL_CALL ral_grid_convolve_grid(ral_grid *grid, double *kernel, int delta);
424 
425 int RAL_CALL ral_integer_grid_get_value_range(ral_grid *gd, ral_integer_range *range);
426 int RAL_CALL ral_real_grid_get_value_range(ral_grid *gd, ral_real_range *range);
427 
429 int RAL_CALL ral_grid_data(ral_grid *gd);
430 
439 int RAL_CALL ral_grid_not(ral_grid *gd);
455 int RAL_CALL ral_grid_and_grid(ral_grid *gd1, ral_grid *gd2);
470 int RAL_CALL ral_grid_or_grid(ral_grid *gd1, ral_grid *gd2);
471 
472 int RAL_CALL ral_grid_add_real(ral_grid *gd, RAL_REAL x);
473 int RAL_CALL ral_grid_add_integer(ral_grid *gd, RAL_INTEGER x);
474 int RAL_CALL ral_grid_add_grid(ral_grid *gd1, ral_grid *gd2);
475 int RAL_CALL ral_grid_sub_grid(ral_grid *gd1, ral_grid *gd2);
476 int RAL_CALL ral_grid_mult_real(ral_grid *gd, RAL_REAL x);
477 int RAL_CALL ral_grid_mult_integer(ral_grid *gd, RAL_INTEGER x);
478 int RAL_CALL ral_grid_mult_grid(ral_grid *gd1, ral_grid *gd2);
479 int RAL_CALL ral_grid_div_real(ral_grid *gd, RAL_REAL x);
480 int RAL_CALL ral_grid_div_integer(ral_grid *gd, RAL_INTEGER x);
481 int RAL_CALL ral_real_div_grid(RAL_REAL x, ral_grid *gd);
482 int RAL_CALL ral_integer_div_grid(RAL_INTEGER x, ral_grid *gd);
483 int RAL_CALL ral_grid_div_grid(ral_grid *gd1, ral_grid *gd2);
484 int RAL_CALL ral_grid_modulus_integer(ral_grid *gd, RAL_INTEGER x);
485 int RAL_CALL ral_integer_modulus_grid(RAL_INTEGER x, ral_grid *gd);
486 int RAL_CALL ral_grid_modulus_grid(ral_grid *gd1, ral_grid *gd2);
487 int RAL_CALL ral_grid_power_real(ral_grid *gd, RAL_REAL x);
488 int RAL_CALL ral_real_power_grid(RAL_REAL x, ral_grid *gd);
489 int RAL_CALL ral_grid_power_grid(ral_grid *gd1, ral_grid *gd2);
490 
491 int RAL_CALL ral_grid_abs(ral_grid *gd);
492 int RAL_CALL ral_grid_acos(ral_grid *gd);
493 int RAL_CALL ral_grid_atan(ral_grid *gd);
494 int RAL_CALL ral_grid_atan2(ral_grid *gd1, ral_grid *gd2);
495 int RAL_CALL ral_grid_ceil(ral_grid *gd);
496 int RAL_CALL ral_grid_cos(ral_grid *gd);
497 int RAL_CALL ral_grid_cosh(ral_grid *gd);
498 int RAL_CALL ral_grid_exp(ral_grid *gd);
499 int RAL_CALL ral_grid_floor(ral_grid *gd);
500 int RAL_CALL ral_grid_log(ral_grid *gd);
501 int RAL_CALL ral_grid_log10(ral_grid *gd);
502 int RAL_CALL ral_grid_pow_real(ral_grid *gd, RAL_REAL b);
503 int RAL_CALL ral_grid_pow_integer(ral_grid *gd, RAL_INTEGER b);
504 int RAL_CALL ral_grid_sin(ral_grid *gd);
505 int RAL_CALL ral_grid_sinh(ral_grid *gd);
506 int RAL_CALL ral_grid_sqrt(ral_grid *gd);
507 int RAL_CALL ral_grid_tan(ral_grid *gd);
508 int RAL_CALL ral_grid_tanh(ral_grid *gd);
509 
510 int RAL_CALL ral_grid_lt_real(ral_grid *gd, RAL_REAL x);
511 int RAL_CALL ral_grid_gt_real(ral_grid *gd, RAL_REAL x);
512 int RAL_CALL ral_grid_le_real(ral_grid *gd, RAL_REAL x);
513 int RAL_CALL ral_grid_ge_real(ral_grid *gd, RAL_REAL x);
514 int RAL_CALL ral_grid_eq_real(ral_grid *gd, RAL_REAL x);
515 int RAL_CALL ral_grid_ne_real(ral_grid *gd, RAL_REAL x);
516 int RAL_CALL ral_grid_cmp_real(ral_grid *gd, RAL_REAL x);
517 
518 int RAL_CALL ral_grid_lt_integer(ral_grid *gd, RAL_INTEGER x);
519 int RAL_CALL ral_grid_gt_integer(ral_grid *gd, RAL_INTEGER x);
520 int RAL_CALL ral_grid_le_integer(ral_grid *gd, RAL_INTEGER x);
521 int RAL_CALL ral_grid_ge_integer(ral_grid *gd, RAL_INTEGER x);
522 int RAL_CALL ral_grid_eq_integer(ral_grid *gd, RAL_INTEGER x);
523 int RAL_CALL ral_grid_ne_integer(ral_grid *gd, RAL_INTEGER x);
524 int RAL_CALL ral_grid_cmp_integer(ral_grid *gd, RAL_INTEGER x);
525 
526 int RAL_CALL ral_grid_lt_grid(ral_grid *gd1, ral_grid *gd2);
527 int RAL_CALL ral_grid_gt_grid(ral_grid *gd1, ral_grid *gd2);
528 int RAL_CALL ral_grid_le_grid(ral_grid *gd1, ral_grid *gd2);
529 int RAL_CALL ral_grid_ge_grid(ral_grid *gd1, ral_grid *gd2);
530 int RAL_CALL ral_grid_eq_grid(ral_grid *gd1, ral_grid *gd2);
531 int RAL_CALL ral_grid_ne_grid(ral_grid *gd1, ral_grid *gd2);
532 int RAL_CALL ral_grid_cmp_grid(ral_grid *gd1, ral_grid *gd2);
533 
534 int RAL_CALL ral_grid_min_real(ral_grid *gd, RAL_REAL x);
535 int RAL_CALL ral_grid_max_real(ral_grid *gd, RAL_REAL x);
536 
537 int RAL_CALL ral_grid_min_integer(ral_grid *gd, RAL_INTEGER x);
538 int RAL_CALL ral_grid_max_integer(ral_grid *gd, RAL_INTEGER x);
539 
540 int RAL_CALL ral_grid_min_grid(ral_grid *gd1, ral_grid *gd2);
541 int RAL_CALL ral_grid_max_grid(ral_grid *gd1, ral_grid *gd2);
542 
544 void RAL_CALL ral_grid_random(ral_grid *gd);
545 
546 ral_grid_handle RAL_CALL ral_grid_cross(ral_grid *a, ral_grid *b);
547 
549 int RAL_CALL ral_grid_if_then_real(ral_grid *a, ral_grid *b, RAL_REAL c);
551 int RAL_CALL ral_grid_if_then_integer(ral_grid *a, ral_grid *b, RAL_INTEGER c);
553 int RAL_CALL ral_grid_if_then_else_real(ral_grid *a, ral_grid *b, RAL_REAL c, RAL_REAL d);
555 int RAL_CALL ral_grid_if_then_else_integer(ral_grid *a, ral_grid *b, RAL_INTEGER c, RAL_INTEGER d);
557 int RAL_CALL ral_grid_if_then_grid(ral_grid *a, ral_grid *b, ral_grid *c);
558 
560 int RAL_CALL ral_grid_zonal_if_then_real(ral_grid *a, ral_grid *b, RAL_INTEGER *k, RAL_REAL *v, int n);
562 int RAL_CALL ral_grid_zonal_if_then_integer(ral_grid *a, ral_grid *b, RAL_INTEGER *k, RAL_INTEGER *v, int n);
563 
565 int RAL_CALL ral_grid_apply_templ(ral_grid *gd, int *templ, int new_val);
566 
571 ral_grid_handle RAL_CALL ral_grid_ca_step(ral_grid *gd, void *k);
572 
574 int RAL_CALL ral_grid_map(ral_grid *gd, int *s, int *d, int n);
575 
579 int RAL_CALL ral_grid_map_integer_grid(ral_grid *gd, int *s_min, int *s_max, int *d, int n, int *deflt);
580 
584 int RAL_CALL ral_grid_map_real_grid(ral_grid *gd, double *s_min, double *s_max, double *d, int n, double *deflt);
585 
586 void RAL_CALL ral_integer_grid_reclassify(ral_grid *gd, ral_hash *h);
588 ral_grid_handle RAL_CALL ral_real_grid_reclassify(ral_grid *gd, RAL_REAL *x, RAL_INTEGER *y, int n);
589 
590 double RAL_CALL ral_grid_zonesize(ral_grid *gd, ral_cell c);
591 
594 
597 
598 ral_grid_handle RAL_CALL ral_grid_areas(ral_grid *gd, int k);
599 
600 int RAL_CALL ral_grid_connect(ral_grid *gd);
601 
602 int RAL_CALL ral_grid_number_of_areas(ral_grid *gd, int connectivity);
603 
609 ral_grid_handle RAL_CALL ral_grid_join(ral_grid *g1, ral_grid *g2);
610 
616 int RAL_CALL ral_grid_pick(ral_grid *dest, ral_grid *src);
617 
626 ral_grid_handle RAL_CALL ral_grid_transform(ral_grid *gd, double tr[], int M, int N, int pick, int value);
627 
628 
630 void RAL_CALL ral_real_grid_line(ral_grid *gd, ral_cell c1, ral_cell c2, RAL_REAL pen);
631 
633 void RAL_CALL ral_integer_grid_line(ral_grid *gd, ral_cell c1, ral_cell c2, RAL_INTEGER pen);
634 
636 void RAL_CALL ral_real_grid_filled_rect(ral_grid *gd, ral_cell c1, ral_cell c2, RAL_REAL pen);
637 
639 void RAL_CALL ral_integer_grid_filled_rect(ral_grid *gd, ral_cell c1, ral_cell c2, RAL_INTEGER pen);
640 
642 #define RAL_LINE(grid, cell1, cell2, pen, assignment) \
643  { \
644  ral_cell c; \
645  int di, dj, incr1, incr2, d, \
646  iend, jend, idirflag, jdirflag; \
647  cell1.i = max(min(cell1.i,grid->M-1),0); \
648  cell1.j = max(min(cell1.j,grid->N-1),0); \
649  cell2.i = max(min(cell2.i,grid->M-1),0); \
650  cell2.j = max(min(cell2.j,grid->N-1),0); \
651  di = abs(cell2.i-cell1.i); \
652  dj = abs(cell2.j-cell1.j); \
653  if (dj <= di) { \
654  d = 2*dj - di; \
655  incr1 = 2*dj; \
656  incr2 = 2 * (dj - di); \
657  if (cell1.i > cell2.i) { \
658  c.i = cell2.i; \
659  c.j = cell2.j; \
660  jdirflag = (-1); \
661  iend = cell1.i; \
662  } else { \
663  c.i = cell1.i; \
664  c.j = cell1.j; \
665  jdirflag = 1; \
666  iend = cell2.i; \
667  } \
668  assignment(grid, c, pen); \
669  if (((cell2.j - cell1.j) * jdirflag) > 0) { \
670  while (c.i < iend) { \
671  c.i++; \
672  if (d <0) { \
673  d+=incr1; \
674  } else { \
675  c.j++; \
676  d+=incr2; \
677  } \
678  assignment(grid, c, pen); \
679  } \
680  } else { \
681  while (c.i < iend) { \
682  c.i++; \
683  if (d <0) { \
684  d+=incr1; \
685  } else { \
686  c.j--; \
687  d+=incr2; \
688  } \
689  assignment(grid, c, pen); \
690  } \
691  } \
692  } else { \
693  d = 2*di - dj; \
694  incr1 = 2*di; \
695  incr2 = 2 * (di - dj); \
696  if (cell1.j > cell2.j) { \
697  c.j = cell2.j; \
698  c.i = cell2.i; \
699  jend = cell1.j; \
700  idirflag = (-1); \
701  } else { \
702  c.j = cell1.j; \
703  c.i = cell1.i; \
704  jend = cell2.j; \
705  idirflag = 1; \
706  } \
707  assignment(grid, c, pen); \
708  if (((cell2.i - cell1.i) * idirflag) > 0) { \
709  while (c.j < jend) { \
710  c.j++; \
711  if (d <0) { \
712  d+=incr1; \
713  } else { \
714  c.i++; \
715  d+=incr2; \
716  } \
717  assignment(grid, c, pen); \
718  } \
719  } else { \
720  while (c.j < jend) { \
721  c.j++; \
722  if (d <0) { \
723  d+=incr1; \
724  } else { \
725  c.i--; \
726  d+=incr2; \
727  } \
728  assignment(grid, c, pen); \
729  } \
730  } \
731  } \
732  }
733 
735 #define RAL_FILLED_CIRCLE(grid, cell, r, pen, assignment) \
736  { \
737  int a, b, di, dj, r2 = r*r; \
738  di = max(-r, -cell.i); \
739  a = r2 - di*di; \
740  while (1) { \
741  dj = max(-r, -cell.j); \
742  b = dj*dj; \
743  while (1) { \
744  ral_cell d; \
745  d.i = cell.i+di; \
746  d.j = cell.j+dj; \
747  if (d.j >= (grid)->N) break; \
748  if (b < a) assignment(grid, d, pen); \
749  dj++; \
750  if (dj > r) break; \
751  b += 2*dj - 1; \
752  } \
753  di++; \
754  if (di > r OR cell.i+di >= (grid)->M) break; \
755  a -= 2*di - 1; \
756  } \
757  }
758 
759 #define RAL_REAL_GRID_SET_CELL(grid, cell, value) RAL_REAL_GRID_CELL((grid), (cell)) = (value)
760 
761 #define RAL_INTEGER_GRID_SET_CELL(grid, cell, value) RAL_INTEGER_GRID_CELL((grid), (cell)) = (value)
762 
763 int RAL_CALL ral_grid_filled_polygon(ral_grid *gd, ral_geometry *g, RAL_INTEGER pen_integer, RAL_REAL pen_real);
764 
765 #ifdef RAL_HAVE_GDAL
766 
770 int RAL_CALL ral_grid_rasterize_feature(ral_grid *gd, OGRFeatureH f, int value_field, OGRFieldType ft, int render_override);
771 int RAL_CALL ral_grid_rasterize_layer(ral_grid *gd, OGRLayerH l, int value_field, int render_override);
772 #endif
773 
775 typedef struct {
776  ral_cell *cells;
777  RAL_INTEGER *values;
778  int size;
779  int max_size;
781 
783 
784 ral_cell_integer_values_handle RAL_CALL ral_cell_integer_values_create(int size);
785 void RAL_CALL ral_cell_integer_values_destroy(ral_cell_integer_values **data);
786 void RAL_CALL ral_add_cell_integer_value(ral_grid *gd, ral_cell d, ral_cell_integer_values *data);
787 
789 typedef struct {
790  ral_cell *cells;
791  RAL_REAL *values;
792  int size;
793  int max_size;
795 
797 
798 ral_cell_real_values_handle RAL_CALL ral_cell_real_values_create(int size);
799 void RAL_CALL ral_cell_real_values_destroy(ral_cell_real_values **data);
800 void RAL_CALL ral_add_cell_real_value(ral_grid *gd, ral_cell d, ral_cell_real_values *data);
801 
802 ral_cell_integer_values_handle RAL_CALL ral_integer_grid_get_line(ral_grid *gd, ral_cell c1, ral_cell c2);
803 ral_cell_real_values_handle RAL_CALL ral_real_grid_get_line(ral_grid *gd, ral_cell c1, ral_cell c2);
804 ral_cell_integer_values_handle RAL_CALL ral_integer_grid_get_rect(ral_grid *gd, ral_cell c1, ral_cell c2);
805 ral_cell_real_values_handle RAL_CALL ral_real_grid_get_rect(ral_grid *gd, ral_cell c1, ral_cell c2);
806 ral_cell_integer_values_handle RAL_CALL ral_integer_grid_get_circle(ral_grid *gd, ral_cell c, int r);
807 ral_cell_real_values_handle RAL_CALL ral_real_grid_get_circle(ral_grid *gd, ral_cell c, int r);
808 
813 void RAL_CALL ral_integer_grid_floodfill(ral_grid *gd, ral_grid *done, ral_cell c, RAL_INTEGER pen, int connectivity);
814 
819 void RAL_CALL ral_real_grid_floodfill(ral_grid *gd, ral_grid *done, ral_cell c, RAL_REAL pen, int connectivity);
820 
822 int RAL_CALL ral_integer_grid2list(ral_grid *gd, ral_cell **c, RAL_INTEGER **value, size_t *size);
823 
825 int RAL_CALL ral_real_grid2list(ral_grid *gd, ral_cell **c, RAL_REAL **value, size_t *size);
826 
828 void RAL_CALL ral_grid_histogram(ral_grid *gd, double *h, int *c, int n);
829 
832 
833 /*********** ZONAL FUNCTIONS ***********/
834 
835 ral_hash_handle RAL_CALL ral_grid_zonal_count(ral_grid *gd, ral_grid *zones);
836 ral_hash_handle RAL_CALL ral_grid_zonal_count_of(ral_grid *gd, ral_grid *zones, RAL_INTEGER value);
837 ral_hash_handle RAL_CALL ral_grid_zonal_sum(ral_grid *gd, ral_grid *zones);
838 ral_hash_handle RAL_CALL ral_grid_zonal_range(ral_grid *gd, ral_grid *zones);
839 ral_hash_handle RAL_CALL ral_grid_zonal_min(ral_grid *gd, ral_grid *zones);
840 ral_hash_handle RAL_CALL ral_grid_zonal_max(ral_grid *gd, ral_grid *zones);
842 ral_hash_handle RAL_CALL ral_grid_zonal_variance(ral_grid *gd, ral_grid *zones);
843 
844 int RAL_CALL ral_grid_zonal_contents(ral_grid *gd, ral_grid *zones, ral_hash ***table, ral_hash *index);
845 
852 int RAL_CALL ral_grid_grow_zones(ral_grid *zones, ral_grid *grow, int connectivity);
853 
860 int RAL_CALL ral_grid_neighbors(ral_grid *gd, ral_hash ***b, int **c, int *n);
861 
866 ral_grid_handle RAL_CALL ral_grid_bufferzone(ral_grid *gd, int z, double w);
867 
869 long RAL_CALL ral_grid_count(ral_grid *gd);
870 long RAL_CALL ral_grid_count_of(ral_grid *gd, RAL_INTEGER value);
871 double RAL_CALL ral_grid_sum(ral_grid *gd);
872 double RAL_CALL ral_grid_mean(ral_grid *gd);
873 double RAL_CALL ral_grid_variance(ral_grid *gd);
874 
879 
884 
888 
892 ral_grid_handle RAL_CALL ral_grid_nn(ral_grid *gd);
893 
902 int RAL_CALL ral_grid_zones(ral_grid *gd, ral_grid *z, double ***tot, int **c, int **k, int *n);
903 
906 
908 int RAL_CALL ral_grid_print(ral_grid *gd);
909 
910 int RAL_CALL ral_grid_save_ascii(ral_grid *gd, char *outfile);
911 
912 #endif
an XYZM point
Definition: vector.h:9
void RAL_CALL ral_grid_random(ral_grid *gd)
void RAL_CALL ral_real_grid_line(ral_grid *gd, ral_cell c1, ral_cell c2, RAL_REAL pen)
int RAL_CALL ral_grid_coerce(ral_grid *gd, int data_type)
int RAL_CALL ral_grid_apply_templ(ral_grid *gd, int *templ, int new_val)
ral_cell RAL_CALL ral_cell_move(ral_cell c, int dir)
ral_rectangle world
Definition: grid.h:177
int RAL_CALL ral_grid_or_grid(ral_grid *gd1, ral_grid *gd2)
a collection of cells from a real valued grid
Definition: grid.h:789
ral_grid_handle RAL_CALL ral_grid_distances(ral_grid *gd)
int RAL_CALL ral_grid_set_integer_nodata_value(ral_grid *gd, RAL_INTEGER nodata_value)
ral_cell RAL_CALL ral_grid_nearest_neighbor(ral_grid *gd, ral_cell c)
int datatype
Definition: grid.h:168
ral_grid_handle RAL_CALL ral_grid_create_like(ral_grid *gd, int datatype)
ral_hash_handle RAL_CALL ral_grid_zonal_mean(ral_grid *gd, ral_grid *zones)
int RAL_CALL ral_grid_grow_zones(ral_grid *zones, ral_grid *grow, int connectivity)
int RAL_CALL ral_grid_rasterize_feature(ral_grid *gd, OGRFeatureH f, int value_field, OGRFieldType ft, int render_override)
ral_grid_handle RAL_CALL ral_grid_bufferzone(ral_grid *gd, int z, double w)
ral_hash_handle RAL_CALL ral_grid_contents(ral_grid *gd)
void RAL_CALL ral_integer_grid_line(ral_grid *gd, ral_cell c1, ral_cell c2, RAL_INTEGER pen)
void RAL_CALL ral_integer_grid_filled_rect(ral_grid *gd, ral_cell c1, ral_cell c2, RAL_INTEGER pen)
int M
Definition: grid.h:170
rectangular grid of integer or real values for geospatial data
Definition: grid.h:166
ral_grid_handle RAL_CALL ral_grid_nn(ral_grid *gd)
ral_point RAL_CALL ral_grid_cell2point(ral_grid *gd, ral_cell c)
ral_grid_handle RAL_CALL ral_grid_create(int datatype, int M, int N)
void * data
Definition: grid.h:181
a collection of cells from an integer grid
Definition: grid.h:775
int RAL_CALL ral_grid_not(ral_grid *gd)
int RAL_CALL ral_grid_focal_variance(ral_grid *grid, ral_cell cell, int *mask, int delta, double *variance)
int RAL_CALL ral_grid_zones(ral_grid *gd, ral_grid *z, double ***tot, int **c, int **k, int *n)
ral_grid_handle RAL_CALL ral_grid_directions(ral_grid *gd)
void * nodata_value
Definition: grid.h:179
int RAL_CALL ral_integer_grid_focal_range(ral_grid *grid, ral_cell cell, int *mask, int delta, ral_integer_range *range)
int RAL_CALL ral_grid_pick(ral_grid *dest, ral_grid *src)
ral_grid_handle RAL_CALL ral_grid_spread(ral_grid *grid, double *mask, int delta)
void RAL_CALL ral_real_grid_floodfill(ral_grid *gd, ral_grid *done, ral_cell c, RAL_REAL pen, int connectivity)
an integer range
Definition: hash.h:14
void RAL_CALL ral_real_grid_filled_rect(ral_grid *gd, ral_cell c1, ral_cell c2, RAL_REAL pen)
int RAL_CALL ral_grid_map_integer_grid(ral_grid *gd, int *s_min, int *s_max, int *d, int n, int *deflt)
int RAL_CALL ral_grid_zonal_if_then_integer(ral_grid *a, ral_grid *b, RAL_INTEGER *k, RAL_INTEGER *v, int n)
void RAL_CALL ral_grid_histogram(ral_grid *gd, double *h, int *c, int n)
int RAL_CALL ral_real_grid_focal_range(ral_grid *grid, ral_cell cell, int *mask, int delta, ral_real_range *range)
ral_grid_handle RAL_CALL ral_grid_create_using_GDAL(GDALDatasetH dataset, int band, ral_rectangle clip_region, double cell_size)
a hash (index for hash items)
Definition: hash.h:67
int RAL_CALL ral_grid_if_then_else_integer(ral_grid *a, ral_grid *b, RAL_INTEGER c, RAL_INTEGER d)
ral_grid_handle RAL_CALL ral_grid_create_copy(ral_grid *gd, int datatype)
int RAL_CALL ral_integer_grid2list(ral_grid *gd, ral_cell **c, RAL_INTEGER **value, size_t *size)
int RAL_CALL ral_grid_zonal_if_then_real(ral_grid *a, ral_grid *b, RAL_INTEGER *k, RAL_REAL *v, int n)
an array of points, which represent a simple geometry
Definition: vector.h:97
int RAL_CALL ral_grid_and_grid(ral_grid *gd1, ral_grid *gd2)
ral_hash_handle RAL_CALL ral_grid_zonal_max(ral_grid *gd, ral_grid *zones)
ral_grid_handle RAL_CALL ral_grid_borders(ral_grid *gd)
ral_grid_handle RAL_CALL ral_real_grid_reclassify(ral_grid *gd, RAL_REAL *x, RAL_INTEGER *y, int n)
address of a cell of a grid
Definition: grid.h:104
ral_grid_handle RAL_CALL ral_grid_ca_step(ral_grid *gd, void *k)
ral_grid_handle RAL_CALL ral_grid_spread_random(ral_grid *grid, double *mask, int delta)
int RAL_CALL ral_cell_dir(ral_cell a, ral_cell b)
int RAL_CALL ral_grid_if_then_grid(ral_grid *a, ral_grid *b, ral_grid *c)
int RAL_CALL ral_grid_print(ral_grid *gd)
ral_grid_handle RAL_CALL ral_grid_dijkstra(ral_grid *w, ral_cell c)
int N
Definition: grid.h:172
int RAL_CALL ral_grid_map(ral_grid *gd, int *s, int *d, int n)
int RAL_CALL ral_grid_overlayable(ral_grid *g1, ral_grid *g2)
long RAL_CALL ral_grid_count(ral_grid *gd)
two points: SW and NE corners
Definition: vector.h:27
int RAL_CALL ral_grid_focal_mean(ral_grid *grid, ral_cell cell, int *mask, int delta, double *mean)
a real value range
Definition: hash.h:20
int RAL_CALL ral_real_grid2list(ral_grid *gd, ral_cell **c, RAL_REAL **value, size_t *size)
int RAL_CALL ral_grid_focal_count_of(ral_grid *grid, ral_cell cell, int *mask, int delta, RAL_INTEGER value)
int RAL_CALL ral_grid_neighbors(ral_grid *gd, ral_hash ***b, int **c, int *n)
ral_grid_handle RAL_CALL ral_grid_clip(ral_grid *gd, ral_window w)
window in a grid
Definition: grid.h:124
int RAL_CALL ral_grid_convolve(ral_grid *grid, ral_cell cell, double *kernel, int delta, double *g)
int RAL_CALL ral_grid_if_then_integer(ral_grid *a, ral_grid *b, RAL_INTEGER c)
struct ral_grid ral_grid
rectangular grid of integer or real values for geospatial data
int RAL_CALL ral_integer_grid_focal_sum(ral_grid *grid, ral_cell cell, int *mask, int delta, int *sum)
ral_cell RAL_CALL ral_grid_point2cell(ral_grid *gd, ral_point p)
double cell_size
Definition: grid.h:174
string_handle RAL_CALL ral_get_msg()
void RAL_CALL ral_integer_grid_floodfill(ral_grid *gd, ral_grid *done, ral_cell c, RAL_INTEGER pen, int connectivity)
int RAL_CALL ral_has_msg()
int RAL_CALL ral_grid_get_integer_nodata_value(ral_grid *gd, RAL_INTEGER *nodata_value)
ral_grid_handle RAL_CALL ral_grid_borders_recursive(ral_grid *gd)
ral_grid_handle RAL_CALL ral_grid_transform(ral_grid *gd, double tr[], int M, int N, int pick, int value)
ral_point RAL_CALL ral_grid_cell2point_upleft(ral_grid *gd, ral_cell c)
int RAL_CALL ral_real_grid_focal_sum(ral_grid *grid, ral_cell cell, int *mask, int delta, double *sum)
int RAL_CALL ral_grid_map_real_grid(ral_grid *gd, double *s_min, double *s_max, double *d, int n, double *deflt)
int RAL_CALL ral_grid_write(ral_grid *gd, char *filename)
void CPL_DLL CPL_STDCALL ral_cpl_error(CPLErr eclass, int code, const char *msg)
struct ral_grid * mask
Definition: grid.h:183
int RAL_CALL ral_grid_if_then_else_real(ral_grid *a, ral_grid *b, RAL_REAL c, RAL_REAL d)
int RAL_CALL ral_grid_focal_count(ral_grid *grid, ral_cell cell, int *mask, int delta)
int RAL_CALL ral_grid_data(ral_grid *gd)
int RAL_CALL ral_grid_if_then_real(ral_grid *a, ral_grid *b, RAL_REAL c)