forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
map2img.c
348 lines (285 loc) · 10.8 KB
/
map2img.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
/******************************************************************************
* $Id$
*
* Project: MapServer
* Purpose: Commandline .map rendering utility, mostly for testing.
* Author: Steve Lime and the MapServer team.
*
******************************************************************************
* Copyright (c) 1996-2005 Regents of the University of Minnesota.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies of this Software or works derived from this Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include <stdbool.h>
#include "mapserver.h"
#include "maptime.h"
#include "limits.h"
int main(int argc, char *argv[])
{
int i,j,k;
mapObj *map=NULL;
imageObj *image = NULL;
configObj* config = NULL;
char **layers=NULL;
int num_layers=0;
int layer_found=0;
char *outfile=NULL; /* no -o sends image to STDOUT */
int iterations = 1;
int draws = 0;
/* ---- output version info and exit --- */
if(argc > 1 && strcmp(argv[1], "-v") == 0) {
printf("%s\n", msGetVersion());
exit(0);
}
/* ---- check the number of arguments, return syntax if not correct ---- */
if( argc < 3 ) {
fprintf(stdout, "\nPurpose: convert a mapfile to an image\n\n");
fprintf(stdout,
"Syntax: map2img -m mapfile [-o image] [-e minx miny maxx maxy] [-s sizex sizey]\n"
" [-l \"layer1 [layers2...]\"] [-i format]\n"
" [-all_debug n] [-map_debug n] [-layer_debug n] [-p n] [-c n] [-d layername datavalue]\n"
" [-conf filename]\n");
fprintf(stdout," -m mapfile: Map file to operate on - required\n" );
fprintf(stdout," -i format: Override the IMAGETYPE value to pick output format\n" );
fprintf(stdout," -o image: output filename (stdout if not provided)\n");
fprintf(stdout," -e minx miny maxx maxy: extents to render\n");
fprintf(stdout," -s sizex sizey: output image size\n");
fprintf(stdout," -l layers: layers / groups to enable - make sure they are quoted and space separated if more than one listed\n" );
fprintf(stdout," -all_debug n: Set debug level for map and all layers\n" );
fprintf(stdout," -map_debug n: Set map debug level\n" );
fprintf(stdout," -layer_debug layer_name n: Set layer debug level\n" );
fprintf(stdout," -c n: draw map n number of times\n" );
fprintf(stdout," -p n: pause for n seconds after reading the map\n" );
fprintf(stdout," -d layername datavalue: change DATA value for layer\n" );
fprintf(stdout," -conf filename: filename of the MapServer configuration file.\n" );
exit(0);
}
if ( msSetup() != MS_SUCCESS ) {
msWriteError(stderr);
exit(1);
}
bool some_debug_requested = FALSE;
const char* config_filename = NULL;
for(i=1; i<argc; i++) {
if (strcmp(argv[i],"-c") == 0) { /* user specified number of draws */
iterations = atoi(argv[i+1]);
if( iterations < 0 || iterations > INT_MAX - 1 ) {
printf("Invalid number of iterations");
return 1;
}
printf("We will draw %d times...\n", iterations);
continue;
}
if(i < argc-1 && strcmp(argv[i], "-all_debug") == 0) { /* global debug */
int debug_level = atoi(argv[++i]);
some_debug_requested = TRUE;
msSetGlobalDebugLevel(debug_level);
continue;
}
if(i < argc-1 && (strcmp(argv[i], "-map_debug") == 0 ||
strcmp(argv[i], "-layer_debug") == 0)) {
some_debug_requested = TRUE;
continue;
}
if(i < argc-1 && strcmp(argv[i], "-conf") == 0) {
config_filename = argv[i+1];
++i;
continue;
}
}
if( some_debug_requested ) {
/* Send output to stderr by default */
if (msGetErrorFile() == NULL)
msSetErrorFile("stderr", NULL);
}
config = msLoadConfig(config_filename);
for(draws=0; draws<iterations; draws++) {
struct mstimeval requeststarttime, requestendtime;
if(msGetGlobalDebugLevel() >= MS_DEBUGLEVEL_TUNING)
msGettimeofday(&requeststarttime, NULL);
/* Use PROJ_LIB env vars if set */
msProjLibInitFromEnv();
/* Use MS_ERRORFILE and MS_DEBUGLEVEL env vars if set */
if ( msDebugInitFromEnv() != MS_SUCCESS ) {
msWriteError(stderr);
msCleanup();
msFreeConfig(config);
exit(1);
}
for(i=1; i<argc; i++) { /* Step though the user arguments, 1st to find map file */
if(strcmp(argv[i],"-m") == 0) {
map = msLoadMap(argv[i+1], NULL, config);
if(!map) {
msWriteError(stderr);
msCleanup();
msFreeConfig(config);
exit(1);
}
msApplyDefaultSubstitutions(map);
}
}
if(!map) {
fprintf(stderr, "Mapfile (-m) option not specified.\n");
msCleanup();
msFreeConfig(config);
exit(1);
}
for(i=1; i<argc; i++) { /* Step though the user arguments */
if(strcmp(argv[i],"-m") == 0) { /* skip it */
i+=1;
}
if(strcmp(argv[i],"-p") == 0) {
int pause_length = atoi(argv[i+1]);
time_t start_time = time(NULL);
printf( "Start pause of %d seconds.\n", pause_length );
while( time(NULL) < start_time + pause_length ) {}
printf( "Done pause.\n" );
i+=1;
}
if(strcmp(argv[i],"-o") == 0) { /* load the output image filename */
outfile = argv[i+1];
i+=1;
}
if(strcmp(argv[i],"-i") == 0) {
outputFormatObj *format;
format = msSelectOutputFormat( map, argv[i+1] );
if( format == NULL )
printf( "No such OUTPUTFORMAT as %s.\n", argv[i+1] );
else {
msFree( (char *) map->imagetype );
map->imagetype = msStrdup( argv[i+1] );
msApplyOutputFormat( &(map->outputformat), format, MS_NOOVERRIDE);
}
i+=1;
}
if(strcmp(argv[i],"-d") == 0) { /* swap layer data */
for(j=0; j<map->numlayers; j++) {
if(strcmp(GET_LAYER(map, j)->name, argv[i+1]) == 0) {
free(GET_LAYER(map, j)->data);
GET_LAYER(map, j)->data = msStrdup(argv[i+2]);
break;
}
}
i+=2;
}
if(i < argc-1 && strcmp(argv[i], "-all_debug") == 0) { /* global debug */
int debug_level = atoi(argv[++i]);
/* msSetGlobalDebugLevel() already called. Just need to force debug
* level in map and all layers
*/
map->debug = debug_level;
for(j=0; j<map->numlayers; j++) {
GET_LAYER(map, j)->debug = debug_level;
}
}
if(i < argc-1 && strcmp(argv[i], "-map_debug") == 0) { /* debug */
map->debug = atoi(argv[++i]);
}
if(i < argc-1 && strcmp(argv[i], "-layer_debug") == 0) { /* debug */
const char *layer_name = argv[++i];
int debug_level = atoi(argv[++i]);
int got_layer = 0;
for(j=0; j<map->numlayers; j++) {
if(strcmp(GET_LAYER(map, j)->name,layer_name) == 0 ) {
GET_LAYER(map, j)->debug = debug_level;
got_layer = 1;
}
}
if( !got_layer )
fprintf( stderr,
" Did not find layer '%s' from -layer_debug switch.\n",
layer_name );
}
if(strcmp(argv[i],"-e") == 0) { /* change extent */
if( argc <= i+4 ) {
fprintf( stderr,
"Argument -e needs 4 space separated numbers as argument.\n" );
msCleanup();
msFreeConfig(config);
exit(1);
}
map->extent.minx = atof(argv[i+1]);
map->extent.miny = atof(argv[i+2]);
map->extent.maxx = atof(argv[i+3]);
map->extent.maxy = atof(argv[i+4]);
i+=4;
}
if (strcmp(argv[i], "-s") == 0) {
msMapSetSize(map, atoi(argv[i+1]), atoi(argv[i+2]));
i+=2;
}
if(strcmp(argv[i],"-l") == 0) { /* load layer list */
layers = msStringSplit(argv[i+1], ' ', &(num_layers));
for(j=0; j<num_layers; j++) { /* loop over -l */
layer_found=0;
for(k=0; k<map->numlayers; k++) {
if((GET_LAYER(map, k)->name && strcasecmp(GET_LAYER(map, k)->name, layers[j]) == 0) || (GET_LAYER(map, k)->group && strcasecmp(GET_LAYER(map, k)->group, layers[j]) == 0)) {
layer_found = 1;
break;
}
}
if (layer_found==0) {
fprintf(stderr, "Layer (-l) \"%s\" not found\n", layers[j]);
msCleanup();
msFreeConfig(config);
exit(1);
}
}
for(j=0; j<map->numlayers; j++) {
if(GET_LAYER(map, j)->status == MS_DEFAULT)
continue;
else {
GET_LAYER(map, j)->status = MS_OFF;
for(k=0; k<num_layers; k++) {
if((GET_LAYER(map, j)->name && strcasecmp(GET_LAYER(map, j)->name, layers[k]) == 0) ||
(GET_LAYER(map, j)->group && strcasecmp(GET_LAYER(map, j)->group, layers[k]) == 0)) {
GET_LAYER(map, j)->status = MS_ON;
break;
}
}
}
}
msFreeCharArray(layers, num_layers);
i+=1;
}
}
image = msDrawMap(map, MS_FALSE);
if(!image) {
msWriteError(stderr);
msFreeMap(map);
msCleanup();
msFreeConfig(config);
exit(1);
}
if( msSaveImage(map, image, outfile) != MS_SUCCESS ) {
msWriteError(stderr);
}
msFreeImage(image);
msFreeMap(map);
if(msGetGlobalDebugLevel() >= MS_DEBUGLEVEL_TUNING) {
msGettimeofday(&requestendtime, NULL);
msDebug("map2img total time: %.3fs\n",
(requestendtime.tv_sec+requestendtime.tv_usec/1.0e6)-
(requeststarttime.tv_sec+requeststarttime.tv_usec/1.0e6) );
}
} /* for(draws=0; draws<iterations; draws++) { */
msCleanup();
msFreeConfig(config);
return(0);
} /* ---- END Main Routine ---- */