forked from OSGeo/grass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.c
863 lines (710 loc) · 18.8 KB
/
map.c
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
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
#include <grass/config.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <unistd.h>
#ifdef HAVE_PTHREAD_H
#include <pthread.h>
#endif
#include <grass/gis.h>
#include <grass/raster.h>
#include <grass/raster.h>
#include <grass/btree.h>
#include <grass/glocale.h>
#include <grass/calc.h>
#include "mapcalc.h"
#include "globals.h"
#include "globals2.h"
/****************************************************************************/
static void prepare_region_from_maps(expression **, int, int);
int columns;
struct Cell_head current_region2;
void setup_region(void)
{
G_get_window(¤t_region2);
rows = Rast_window_rows();
columns = Rast_window_cols();
calc_init(columns);
depths = 1;
}
/****************************************************************************/
struct sub_cache
{
int row;
char *valid;
void **buf;
};
struct row_cache
{
int fd;
int nrows;
struct sub_cache *sub[3];
};
struct map
{
const char *name;
const char *mapset;
int have_cats;
int have_colors;
int use_rowio;
int min_row, max_row;
int fd;
struct Categories cats;
struct Colors colors;
BTREE btree;
struct row_cache cache;
#ifdef HAVE_PTHREAD_H
pthread_mutex_t mutex;
#endif
};
/****************************************************************************/
static struct map *maps;
static int num_maps;
static int max_maps;
static int masking;
static int min_row = INT_MAX;
static int max_row = -INT_MAX;
static int min_col = INT_MAX;
static int max_col = -INT_MAX;
static int max_rows_in_memory = 8;
#ifdef HAVE_PTHREAD_H
static pthread_mutex_t cats_mutex;
static pthread_mutex_t mask_mutex;
#endif
/****************************************************************************/
static void read_row(int fd, void *buf, int row, int res_type)
{
#ifdef HAVE_PTHREAD_H
if (masking)
pthread_mutex_lock(&mask_mutex);
#endif
Rast_get_row(fd, buf, row, res_type);
#ifdef HAVE_PTHREAD_H
if (masking)
pthread_mutex_unlock(&mask_mutex);
#endif
}
static void cache_sub_init(struct row_cache *cache, int data_type)
{
struct sub_cache *sub = G_malloc(sizeof(struct sub_cache));
int i;
sub->row = -cache->nrows;
sub->valid = G_calloc(cache->nrows, 1);
sub->buf = G_malloc(cache->nrows * sizeof(void *));
for (i = 0; i < cache->nrows; i++)
sub->buf[i] = Rast_allocate_buf(data_type);
cache->sub[data_type] = sub;
}
static void cache_setup(struct row_cache *cache, int fd, int nrows)
{
cache->fd = fd;
cache->nrows = nrows;
cache->sub[CELL_TYPE] = NULL;
cache->sub[FCELL_TYPE] = NULL;
cache->sub[DCELL_TYPE] = NULL;
}
static void cache_release(struct row_cache *cache)
{
int t;
for (t = 0; t < 3; t++) {
struct sub_cache *sub = cache->sub[t];
int i;
if (!sub)
continue;
for (i = 0; i < cache->nrows; i++)
G_free(sub->buf[i]);
G_free(sub->buf);
G_free(sub->valid);
G_free(sub);
}
}
static void *cache_get_raw(struct row_cache *cache, int row, int data_type)
{
struct sub_cache *sub;
void **tmp;
char *vtmp;
int i, j;
int newrow;
if (!cache->sub[data_type])
cache_sub_init(cache, data_type);
sub = cache->sub[data_type];
i = row - sub->row;
if (i >= 0 && i < cache->nrows) {
if (!sub->valid[i]) {
read_row(cache->fd, sub->buf[i], row, data_type);
sub->valid[i] = 1;
}
return sub->buf[i];
}
if (i <= -cache->nrows || i >= cache->nrows * 2 - 1) {
memset(sub->valid, 0, cache->nrows);
sub->row = row;
read_row(cache->fd, sub->buf[0], row, data_type);
sub->valid[0] = 1;
return sub->buf[0];
}
tmp = G_alloca(cache->nrows * sizeof(void *));
memcpy(tmp, sub->buf, cache->nrows * sizeof(void *));
vtmp = G_alloca(cache->nrows);
memcpy(vtmp, sub->valid, cache->nrows);
i = (i < 0)
? 0
: cache->nrows - 1;
newrow = row - i;
for (j = 0; j < cache->nrows; j++) {
int r = newrow + j;
int k = r - sub->row;
int l = (k + cache->nrows) % cache->nrows;
sub->buf[j] = tmp[l];
sub->valid[j] = k >= 0 && k < cache->nrows && vtmp[l];
}
sub->row = newrow;
G_freea(tmp);
G_freea(vtmp);
read_row(cache->fd, sub->buf[i], row, data_type);
sub->valid[i] = 1;
return sub->buf[i];
}
static void cache_get(struct row_cache *cache, void *buf, int row, int res_type)
{
void *p = cache_get_raw(cache, row, res_type);
memcpy(buf, p, columns * Rast_cell_size(res_type));
}
/****************************************************************************/
static int compare_ints(const void *a, const void *b)
{
return *(const int *)a - *(const int *)b;
}
static void init_colors(struct map *m)
{
if (Rast_read_colors((char *)m->name, (char *)m->mapset, &m->colors) < 0)
G_fatal_error(_("Unable to read color file for raster map <%s@%s>"),
m->name, m->mapset);
m->have_colors = 1;
}
static void init_cats(struct map *m)
{
if (Rast_read_cats((char *)m->name, (char *)m->mapset, &m->cats) < 0)
G_fatal_error(_("Unable to read category file of raster map <%s@%s>"),
m->name, m->mapset);
if (!btree_create(&m->btree, compare_ints, 1))
G_fatal_error(_("Unable to create btree for raster map <%s@%s>"),
m->name, m->mapset);
m->have_cats = 1;
}
static void translate_from_colors(struct map *m, DCELL *rast, CELL *cell,
int ncols, int mod)
{
unsigned char *red = G_alloca(columns);
unsigned char *grn = G_alloca(columns);
unsigned char *blu = G_alloca(columns);
unsigned char *set = G_alloca(columns);
int i;
Rast_lookup_d_colors(rast, red, grn, blu, set, ncols, &m->colors);
switch (mod) {
case 'r':
for (i = 0; i < ncols; i++)
cell[i] = red[i];
break;
case 'g':
for (i = 0; i < ncols; i++)
cell[i] = grn[i];
break;
case 'b':
for (i = 0; i < ncols; i++)
cell[i] = blu[i];
break;
case '#': /* grey (backwards compatible) */
/* old weightings: R=0.177, G=0.813, B=0.011 */
for (i = 0; i < ncols; i++)
cell[i] =
(181 * red[i] + 833 * grn[i] + 11 * blu[i] + 512) / 1024;
break;
case 'y': /* grey (NTSC) */
/* NTSC weightings: R=0.299, G=0.587, B=0.114 */
for (i = 0; i < ncols; i++)
cell[i] =
(306 * red[i] + 601 * grn[i] + 117 * blu[i] + 512) / 1024;
break;
case 'i': /* grey (equal weight) */
for (i = 0; i < ncols; i++)
cell[i] = (red[i] + grn[i] + blu[i]) / 3;
break;
case 'M':
case '@':
default:
G_fatal_error(_("Invalid map modifier: '%c'"), mod);
break;
}
G_freea(red);
G_freea(grn);
G_freea(blu);
G_freea(set);
}
/* convert cell values to double based on the values in the
* category file.
*
* This requires performing sscanf() of the category label
* and only do it it for new categories. Must maintain
* some kind of maps of already scaned values.
*
* This maps is a hybrid tree, where the data in each node
* of the tree is an array of, for example, 64 values, and
* the key of the tree is the category represented by the
* first index of the data
*
* To speed things up a little, use shifts instead of divide or multiply
* to compute the key and the index
*
* This uses the BTREE library to manage the tree itself
* btree structure must already be initialized
* pcats structure must already contain category labels
*/
#define SHIFT 6
#define NCATS (1<<SHIFT)
static void translate_from_cats(struct map *m, CELL * cell, DCELL * xcell,
int ncols)
{
struct Categories *pcats;
BTREE *btree;
int i, idx;
CELL cat, key;
double vbuf[1 << SHIFT];
double *values;
void *ptr;
char *label;
#ifdef HAVE_PTHREAD_H
pthread_mutex_lock(&cats_mutex);
#endif
btree = &m->btree;
pcats = &m->cats;
for (; ncols-- > 0; cell++, xcell++) {
cat = *cell;
if (IS_NULL_C(cell)) {
SET_NULL_D(xcell);
continue;
}
/* compute key as cat/NCATS * NCATS, adjusting down for negatives
* and idx so that key+idx == cat
*/
if (cat < 0)
key = -(((-cat - 1) >> SHIFT) << SHIFT) - NCATS;
else
key = (cat >> SHIFT) << SHIFT;
idx = cat - key;
/* If key not already in the tree, sscanf() all cats for this key
* and put them into the tree
*/
if (!btree_find(btree, &key, &ptr)) {
values = vbuf;
for (i = 0; i < NCATS; i++) {
CELL cat = i + key;
if ((label = Rast_get_c_cat(&cat, pcats)) == NULL
|| sscanf(label, "%lf", values) != 1)
SET_NULL_D(values);
values++;
}
values = vbuf;
btree_update(btree, &key, sizeof(key), vbuf, sizeof(vbuf));
}
else
values = ptr;
/* and finally lookup the translated value */
if (IS_NULL_D(&values[idx]))
SET_NULL_D(xcell);
else
*xcell = values[idx];
}
#ifdef HAVE_PTHREAD_H
pthread_mutex_unlock(&cats_mutex);
#endif
}
static void setup_map(struct map *m)
{
int nrows = m->max_row - m->min_row + 1;
#ifdef HAVE_PTHREAD_H
pthread_mutex_init(&m->mutex, NULL);
#endif
if (nrows > 1 && nrows <= max_rows_in_memory) {
cache_setup(&m->cache, m->fd, nrows);
m->use_rowio = 1;
}
else
m->use_rowio = 0;
}
static void read_map(struct map *m, void *buf, int res_type, int row, int col)
{
CELL *ibuf = buf;
FCELL *fbuf = buf;
DCELL *dbuf = buf;
if (row < 0 || row >= rows) {
int i;
switch (res_type) {
case CELL_TYPE:
for (i = 0; i < columns; i++)
SET_NULL_C(&ibuf[i]);
break;
case FCELL_TYPE:
for (i = 0; i < columns; i++)
SET_NULL_F(&fbuf[i]);
break;
case DCELL_TYPE:
for (i = 0; i < columns; i++)
SET_NULL_D(&dbuf[i]);
break;
default:
G_fatal_error(_("Unknown type: %d"), res_type);
break;
}
return;
}
if (m->use_rowio)
cache_get(&m->cache, buf, row, res_type);
else
read_row(m->fd, buf, row, res_type);
if (col)
column_shift(buf, res_type, col);
}
static void close_map(struct map *m)
{
if (m->fd < 0)
return;
Rast_close(m->fd);
#ifdef HAVE_PTHREAD_H
pthread_mutex_destroy(&m->mutex);
#endif
if (m->have_cats) {
btree_free(&m->btree);
Rast_free_cats(&m->cats);
m->have_cats = 0;
}
if (m->have_colors) {
Rast_free_colors(&m->colors);
m->have_colors = 0;
}
if (m->use_rowio) {
cache_release(&m->cache);
m->use_rowio = 0;
}
}
/****************************************************************************/
int map_type(const char *name, int mod)
{
const char *mapset;
char *tmpname;
int result;
switch (mod) {
case 'M':
tmpname = G_store((char *)name);
mapset = G_find_raster2(tmpname, "");
result = mapset ? Rast_map_type(tmpname, mapset) : -1;
G_free(tmpname);
return result;
case '@':
return DCELL_TYPE;
case 'r':
case 'g':
case 'b':
case '#':
case 'y':
case 'i':
return CELL_TYPE;
default:
G_fatal_error(_("Invalid map modifier: '%c'"), mod);
return -1;
}
}
int open_map(const char *name, int mod, int row, int col)
{
int i;
const char *mapset;
int use_cats = 0;
int use_colors = 0;
struct map *m;
if (row < min_row)
min_row = row;
if (row > max_row)
max_row = row;
if (col < min_col)
min_col = col;
if (col > max_col)
max_col = col;
mapset = G_find_raster2(name, "");
if (!mapset)
G_fatal_error(_("Raster map <%s> not found"), name);
switch (mod) {
case 'M':
break;
case '@':
use_cats = 1;
break;
case 'r':
case 'g':
case 'b':
case '#':
case 'y':
case 'i':
use_colors = 1;
break;
default:
G_fatal_error(_("Invalid map modifier: '%c'"), mod);
break;
}
for (i = 0; i < num_maps; i++) {
m = &maps[i];
if (strcmp(m->name, name) != 0 || strcmp(m->mapset, mapset) != 0)
continue;
if (row < m->min_row)
m->min_row = row;
if (row > m->max_row)
m->max_row = row;
if (use_cats && !m->have_cats)
init_cats(m);
if (use_colors && !m->have_colors)
init_colors(m);
return i;
}
if (num_maps >= max_maps) {
max_maps += 10;
maps = G_realloc(maps, max_maps * sizeof(struct map));
}
m = &maps[num_maps];
m->name = name;
m->mapset = mapset;
m->have_cats = 0;
m->have_colors = 0;
m->use_rowio = 0;
m->min_row = row;
m->max_row = row;
m->fd = -1;
if (use_cats)
init_cats(m);
if (use_colors)
init_colors(m);
m->fd = Rast_open_old(name, mapset);
return num_maps++;
}
void setup_maps(void)
{
int i;
#ifdef HAVE_PTHREAD_H
pthread_mutex_init(&cats_mutex, NULL);
pthread_mutex_init(&mask_mutex, NULL);
masking = Rast_maskfd() >= 0;
#endif
for (i = 0; i < num_maps; i++)
setup_map(&maps[i]);
}
void get_map_row(int idx, int mod, int depth, int row, int col, void *buf,
int res_type)
{
CELL *ibuf;
DCELL *fbuf;
struct map *m = &maps[idx];
#ifdef HAVE_PTHREAD_H
pthread_mutex_lock(&m->mutex);
#endif
switch (mod) {
case 'M':
read_map(m, buf, res_type, row, col);
break;
case '@':
ibuf = G_alloca(columns * sizeof(CELL));
read_map(m, ibuf, CELL_TYPE, row, col);
translate_from_cats(m, ibuf, buf, columns);
G_freea(ibuf);
break;
case 'r':
case 'g':
case 'b':
case '#':
case 'y':
case 'i':
fbuf = G_alloca(columns * sizeof(DCELL));
read_map(m, fbuf, DCELL_TYPE, row, col);
translate_from_colors(m, fbuf, buf, columns, mod);
G_freea(fbuf);
break;
default:
G_fatal_error(_("Invalid map modifier: '%c'"), mod);
break;
}
#ifdef HAVE_PTHREAD_H
pthread_mutex_unlock(&m->mutex);
#endif
}
void close_maps(void)
{
int i;
for (i = 0; i < num_maps; i++)
close_map(&maps[i]);
num_maps = 0;
#ifdef HAVE_PTHREAD_H
pthread_mutex_destroy(&cats_mutex);
pthread_mutex_destroy(&mask_mutex);
#endif
}
void list_maps(FILE *fp, const char *sep)
{
int i;
for (i = 0; i < num_maps; i++) {
const struct map *m = &maps[i];
fprintf(fp, "%s%s@%s", i ? sep : "", m->name, m->mapset);
}
}
/****************************************************************************/
int check_output_map(const char *name)
{
return !!G_find_raster2(name, G_mapset());
}
int open_output_map(const char *name, int res_type)
{
return Rast_open_new((char *)name, res_type);
}
void put_map_row(int fd, void *buf, int res_type)
{
Rast_put_row(fd, buf, res_type);
}
void close_output_map(int fd)
{
Rast_close(fd);
}
void unopen_output_map(int fd)
{
Rast_unopen(fd);
}
/****************************************************************************/
void copy_cats(const char *dst, int idx)
{
const struct map *m = &maps[idx];
struct Categories cats;
if (Rast_read_cats((char *)m->name, (char *)m->mapset, &cats) < 0)
return;
Rast_write_cats((char *)dst, &cats);
Rast_free_cats(&cats);
}
void copy_colors(const char *dst, int idx)
{
const struct map *m = &maps[idx];
struct Colors colr;
if (Rast_read_colors((char *)m->name, (char *)m->mapset, &colr) <= 0)
return;
Rast_write_colors((char *)dst, G_mapset(), &colr);
Rast_free_colors(&colr);
}
void copy_history(const char *dst, int idx)
{
const struct map *m = &maps[idx];
struct History hist;
if (Rast_read_history((char *)m->name, (char *)m->mapset, &hist) < 0)
return;
Rast_write_history((char *)dst, &hist);
}
void create_history(const char *dst, expression * e)
{
int RECORD_LEN = 80;
int WIDTH = RECORD_LEN - 12;
struct History hist;
char *expr = format_expression(e);
char *p = expr;
int len = strlen(expr);
int i;
Rast_short_history(dst, "raster", &hist);
for (i = 0; ; i++) {
char buf[RECORD_LEN];
int n;
if (!len)
break;
if (len > WIDTH) {
for (n = WIDTH; n > 0 && p[n] != ' '; n--) ;
if (n <= 0)
n = WIDTH;
else
n++;
}
else
n = len;
memcpy(buf, p, n);
buf[n] = '\0';
Rast_append_history(&hist, buf);
p += n;
len -= n;
}
if (seeded) {
char buf[RECORD_LEN];
sprintf(buf, "random seed = %ld", seed_value);
Rast_append_history(&hist, buf);
}
Rast_write_history(dst, &hist);
G_free(expr);
}
/****************************************************************************/
void prepare_region_from_maps_union(expression **map_list, int num_maps) {
prepare_region_from_maps(map_list, 1, num_maps);
}
void prepare_region_from_maps_intersect(expression **map_list, int num_maps) {
prepare_region_from_maps(map_list, 2, num_maps);
}
void prepare_region_from_maps(expression **map_list, int type, int num_maps) {
/* \brief Setup the computational region from all raster maps
*
* This function computes the disjoint union or intersection extent
* that is covered by all raster maps and sets the smallest
* resolution that it can find.
*/
int first = 0;
struct Cell_head window, temp_window;
int i;
const char *mapset;
for (i = 0; i < num_maps; i++) {
expression *e = map_list[i];
char rast_name[GNAME_MAX];
strcpy(rast_name, e->data.map.name);
mapset = G_find_raster2(rast_name, "");
if (!mapset)
G_fatal_error(_("Raster map <%s> not found"), rast_name);
G_debug(1, "Setting region from raster map: %s@%s", rast_name, mapset);
Rast_get_cellhd(rast_name, mapset, &temp_window);
if (!first) {
window = temp_window;
first = 1;
}
else {
if (type == 1) {
/* Union: find the largest extent */
window.north = (window.north > temp_window.north) ?
window.north : temp_window.north;
window.south = (window.south < temp_window.south) ?
window.south : temp_window.south;
window.east = (window.east > temp_window.east) ?
window.east : temp_window.east;
window.west = (window.west < temp_window.west) ?
window.west : temp_window.west;
}
else {
/* Intersect: Find the smallest extent */
window.north = (window.north < temp_window.north) ?
window.north : temp_window.north;
window.south = (window.south > temp_window.south) ?
window.south : temp_window.south;
window.east = (window.east < temp_window.east) ?
window.east : temp_window.east;
window.west = (window.west > temp_window.west) ?
window.west : temp_window.west;
}
/* Find the smallest resolution */
window.ns_res = (window.ns_res < temp_window.ns_res) ?
window.ns_res : temp_window.ns_res;
window.ew_res = (window.ew_res < temp_window.ew_res) ?
window.ew_res : temp_window.ew_res;
}
}
/* Set the region only if a map was found in the expression */
if (first == 1) {
G_adjust_Cell_head3(&window, 0, 0, 0);
G_debug(1, "Region was set to n %g s %g e %g w %g ns_res %g ew_res %g", window.north,
window.south, window.east, window.west, window.ns_res, window.ew_res);
G_put_window(&window);
}
}
/****************************************************************************/