-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrastmap.c
129 lines (101 loc) · 3.42 KB
/
rastmap.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
#include <fcntl.h>
#include "rastmap.h"
int allocate_buf(cell_map *map)
{
if(map->type == CELL_TYPE) {
map->buf = Rast_allocate_buf ( map->type );
map->cbuf = (CELL*)map->buf;
}
if(map->type == FCELL_TYPE) {
map->buf = Rast_allocate_buf ( map->type );
map->fbuf = (FCELL*)map->buf;
}
if(map->type == DCELL_TYPE) {
map->buf = Rast_allocate_buf ( map->type );
map->dbuf = (DCELL*)map->buf;
}
return 0;
}
int get_segment_number(int row, int col, seg_map *segment_info){
// xrow//srows * cols // scols + xcol//scols
return row/segment_info->srows * segment_info->ncols / segment_info->scols +
col/segment_info->scols;
}
int check_fd(cell_map *map)
{
map->fd = Rast_open_old ( map->name, map->mapset );
return 0;
}
int manage_segments(seg_map *seg)
{
seg->nseg = ( ( seg->nrows + seg->srows - 1 ) / seg->srows ) *
( ( seg->ncols + seg->scols - 1 ) / seg->scols );
/* calculate total number of segments */
if ( seg->maxmem > 0 )
seg->segments_in_memory = ( seg->maxmem * seg->nseg ) / 100;
/* maxmem = 0 */
else
seg->segments_in_memory = 4 * ( seg->nrows / seg->srows +
seg->ncols / seg->scols + 2 );
if ( seg->segments_in_memory == 0 )
seg->segments_in_memory = 1;
return 0;
}
int init_seg_map(cell_map *map, seg_map *seg)
{
int size;
if(map->type == CELL_TYPE)
size = sizeof(CELL);
if(map->type == FCELL_TYPE)
size = sizeof(FCELL);
if(map->type == DCELL_TYPE)
size = sizeof(DCELL);
map->temp_file = G_tempfile();
G_message("Create tempfile %s for map <%s>\n", map->temp_file, map->name);
map->temp_fd = creat ( map->temp_file, 0600 );
G_message("fd tempfile %d\n", map->temp_fd);
if ( segment_format ( map->temp_fd,
seg->nrows, seg->ncols,
seg->srows, seg->scols,
size) != 1 )
G_fatal_error ( "Unable to create temporary file" );
close ( map->temp_fd );
map->temp_fd = open ( map->temp_file, 2 );
if ( segment_init ( &map->seg, map->temp_fd, seg->segments_in_memory ) != 1 )
G_fatal_error ( "Unable to create temporary file" );
return 0;
}
int copy_segment(cell_map *map, short int direction){
int row;
void *buf = NULL;
struct Cell_head window;
/* The computation window */
G_get_window(&window);
if(map->type == CELL_TYPE)
buf = (CELL *) Rast_allocate_buf ( map->type );
if(map->type == FCELL_TYPE)
buf = (FCELL *) Rast_allocate_buf ( map->type );
if(map->type == DCELL_TYPE)
buf = (DCELL *) Rast_allocate_buf ( map->type );
/* Flush segments from memory to disk */
segment_flush( &map->seg);
/* Copy map to segment */
if(direction == 0) {
G_message("Copy map <%s> content to segment file", map->name);
for(row = 0; row < window.rows; row++) {
Rast_get_row(map->fd, buf, row, map->type);
segment_put_row(&map->seg, buf, row);
}
}
/* Copy segment to map */
if(direction == 1) {
G_message("Copy segment content to map <%s>", map->name);
for(row = 0; row < window.rows; row++) {
segment_get_row(&map->seg, buf, row);
Rast_put_row(map->fd, buf, map->type);
}
}
if(buf)
G_free(buf);
return 1;
}