Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / extensions / extPublishMapserver / src / org / gvsig / publish / mapserver / conf / MapFile.java @ 16281

History | View | Annotate | Download (26.5 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004-2006 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *   Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Iba?ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *   +34 963862235
28
 *   gvsig@gva.es
29
 *   www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41

    
42
package org.gvsig.publish.mapserver.conf;
43

    
44
/* CVS MESSAGES:
45
 *
46
 * $Id$
47
 * $Log$
48
 * Revision 1.5.2.1  2006/11/15 04:11:03  jjdelcerro
49
 * *** empty log message ***
50
 *
51
 * Revision 1.5  2006/11/13 08:25:04  dagilgon
52
 * a?adidas "etiquetas" CLASS (completada) y JOIN
53
 *
54
 * Revision 1.4  2006/10/18 07:56:56  dagilgon
55
 * a?adidos los outputformats
56
 *
57
 * Revision 1.3  2006/10/17 11:51:09  dagilgon
58
 * outputformats
59
 *
60
 * Revision 1.2  2006/10/16 11:40:36  dagilgon
61
 * arreglado path de mapserver
62
 *
63
 * Revision 1.1  2006/10/05 13:22:23  jvhigon
64
 * Debido al refactor del nobre en la libreria de maapserver y geoserver. Mapserver --> MapserverConfig
65
 *
66
 * Revision 1.25  2006/09/19 07:34:00  dagilgon
67
 * Corregida salida duplicada del campo crs en matadataweb
68
 *
69
 * Revision 1.24  2006/09/19 00:30:31  luisw2
70
 * Soporte para WCS en mapserver. Pendiente de verificar que va
71
 *
72
 * Revision 1.23  2006/09/18 08:41:22  dagilgon
73
 * A?adida clase leyend y label
74
 *
75
 * Revision 1.22  2006/09/14 12:06:16  jorpiell
76
 * A?adido el constructor con dos colores para el stylemap
77
 *
78
 * Revision 1.21  2006/09/13 12:08:31  luisw2
79
 * Adding WCS to mapserver ...
80
 *
81
 * Revision 1.19  2006/09/08 18:03:18  jorpiell
82
 * El EPSG se escribe en min?sculas y los times se escriben en orden
83
 *
84
 * Revision 1.18  2006/09/08 17:58:01  jorpiell
85
 * El EPSG se escribe en min?sculas y los times se escriben en orden
86
 *
87
 * Revision 1.17  2006/09/08 12:50:20  jorpiell
88
 * Se tiene en cuanta el par?metro TIME
89
 *
90
 * Revision 1.16  2006/09/08 10:50:05  jorpiell
91
 * A?adida la opci?n de generar una capa POSTGIS
92
 *
93
 * Revision 1.15  2006/09/08 10:14:04  luisw2
94
 * IRSLayer Added
95
 *
96
 * Revision 1.14  2006/09/08 07:12:03  luisw2
97
 * Minor corrections for raster; abstract added
98
 *
99
 * Revision 1.13  2006/09/07 19:25:01  luisw2
100
 * Correcciones en metadata
101
 *
102
 * Revision 1.12  2006/09/07 19:20:51  jorpiell
103
 * Classpath modificado
104
 *
105
 * Revision 1.11  2006/09/07 19:16:30  jorpiell
106
 * *** empty log message ***
107
 *
108
 * Revision 1.10  2006/09/07 18:11:06  luisw2
109
 * Adding Time to mapserver ...
110
 *
111
 * Revision 1.9  2006/09/07 16:34:28  luisw2
112
 * A?adido size
113
 *
114
 * Revision 1.8  2006/09/07 16:10:45  luisw2
115
 * a?adido flush() en el close;
116
 *
117
 * Revision 1.7  2006/09/07 12:59:44  jorpiell
118
 * Enganchada la interfaz gr?fica con el generador de ficheros
119
 *
120
 * Revision 1.6  2006/09/07 12:51:54  jorpiell
121
 * Enganchada la interfaz gr?fica con el generador de ficheros
122
 *
123
 * Revision 1.5  2006/09/07 12:47:39  jorpiell
124
 * A?adida la expression en el classMap
125
 *
126
 * Revision 1.4  2006/09/07 11:19:40  jvhigon
127
 * A?adido soporte para multiples class en una layer, y para layer sdentro de layer
128
 *
129
 * Revision 1.3  2006/09/01 06:59:00  luisw2
130
 * Headers mofification
131
 *
132
 */
133

    
134
import java.awt.Dimension;
135
import java.awt.Point;
136
import java.awt.geom.Rectangle2D;
137
import java.io.File;
138
import java.io.FileNotFoundException;
139
import java.io.FileOutputStream;
140
import java.io.IOException;
141
import java.io.OutputStreamWriter;
142
import java.io.UnsupportedEncodingException;
143
import java.io.Writer;
144
import java.util.ArrayList;
145
import java.util.Iterator;
146

    
147

    
148
/**
149
 * Allows to write a .map Mapserver configuration file.
150
 *
151
 * @author David Gilsanz
152
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
153
 * @author jvhigon
154
 */
155

    
156
public abstract class MapFile {
157
        public static final String SHP_TYPE_LINE = "LINE";
158
        public static final String SHP_TYPE_POLYLINE = "POLYLINE";
159
        public static final String SHP_TYPE_POINT = "POINT";
160
        public static final String SHP_TYPE_POLYGON = "POLYGON"; 
161

    
162
        public static class RGB {
163
                int r,g,b;
164
                public RGB(int red,int green,int blue){
165
                        r=red;
166
                        g=green;
167
                        b=blue;                        
168
                }
169
                public String toString(){
170
                        return ""+r+" "+g+" "+b;
171
                }
172
        }
173

    
174
        public static class CRS extends MapFile {
175
                boolean init = false;
176
                String crs = "";
177
                public CRS(String crs, boolean init) {
178
                        this.crs = crs;
179
                        this.init =init;
180
                }
181

    
182
                public String toString() {
183
                        String str = "";
184
                        if (init){
185
                                str += "init=";
186
                                return str+crs.toLowerCase();
187
                        }
188
                        return crs; //LWS .toLowerCase();
189
                }
190

    
191
                public void toMap() {
192
                        toMapln("PROJECTION");
193
                        tabIn();
194
                        toMapln("\""+this+"\"");
195
                        tabOut();
196
                        toMapln("END");
197
                }
198

    
199
                /**
200
                 * @return Returns the crs.
201
                 */
202
                public String getCrs() {
203
                        return crs;
204
                }
205
        }
206

    
207
        public static class OutputFormat extends MapFile {
208
                /*
209
                OUTPUTFORMAT
210
                        NAME GEOTIFF_INT16
211
                        DRIVER "GDAL/GTiff"
212
                        IMAGEMODE INT16
213
                        EXTENSION "tif"
214
                END        
215
                 */
216
                public String name = null;
217
                public String driver = null;
218
                public String imageMode = null;
219
                public String extension = null;
220

    
221
                /**
222
                 * Creates a new OutputFormat.
223
                 * @param name
224
                 * @param driver
225
                 * @param imageMode
226
                 * @param extension
227
                 */
228
                public OutputFormat(String name, String driver, String imageMode, String extension) {
229
                        this.name = name;
230
                        this.driver = driver;
231
                        this.imageMode = imageMode;
232
                        this.extension = extension;
233
                }
234

    
235
                public void toMap() {
236
                        toMapln("OUTPUTFORMAT");
237
                        tabIn();
238
                        toMapln("NAME "+name+"");
239
                        toMapln("DRIVER \""+driver+"\"");
240
                        toMapln("IMAGEMODE "+imageMode+"");
241
                        toMapln("EXTENSION \""+extension+"\"");
242
                        tabOut();
243
                        toMapln("END");
244
                }
245
        }
246
        public static class Label extends MapFile{
247

    
248
                public String angle = null;
249
                public String antialias = null;
250
                public RGB backgroundcolor = null;
251
                public RGB backgroundshadowcolor = null;
252
                public Point backgroundshadowsize = null;
253
                public String buffer = null;
254
                public RGB color = null;
255
                public String encoding = null;
256
                public String force = null;
257
                public String maxsize = null;
258
                public String mindistance = null;
259
                public String minfeaturesize = null;
260
                public String minsize = null;
261
                public Point offset = null;
262
                public RGB outlinecolor = null;
263
                public String partials = null;
264
                public String position = null;
265
                public RGB shadowcolor = null;
266
                public Point shadowsize = null;
267
                public int size = -1;
268
                public String type = null;
269
                public String wrap = null;
270

    
271
                public void tomap(){
272
                        toMapln("LABEL");
273
                        tabIn();
274
                        if(angle != null)
275
                                toMapln("ANGLE "+angle.toUpperCase()+"");
276
                        if(antialias != null)
277
                                toMapln("ANTIALIAS \""+antialias+"\"");
278
                        if(backgroundcolor != null)
279
                                toMapln("BACKGROUNDCOLOR "+backgroundcolor.toString()+"");
280
                        if(backgroundshadowcolor != null)
281
                                toMapln("BACKGROUNDSHADOWCOLOR "+backgroundshadowcolor.toString()+"");
282
                        if(backgroundshadowsize != null)
283
                                toMapln("BACKGROUNDSHADOWSIZE "+backgroundshadowsize.toString()+"");
284
                        if(buffer != null)
285
                                toMapln("BUFFER \""+buffer+"\"");
286
                        if(color != null)
287
                                toMapln("COLOR "+color.toString()+"");
288
                        if(encoding != null)
289
                                toMapln("ENCODING \""+encoding+"\"");
290
                        if(force != null)
291
                                toMapln("FORCE \""+force+"\"");
292
                        if(maxsize != null)
293
                                toMapln("MAXSIZE "+maxsize+"");
294
                        if(mindistance != null)
295
                                toMapln("MINDISTANCE "+mindistance+"");
296
                        if(minfeaturesize != null)
297
                                toMapln("MINFEATURESIZE "+minfeaturesize+"" +"");
298
                        if(minsize != null)
299
                                toMapln("MINSIZE "+minsize+"");
300
                        if(offset != null)
301
                                toMapln("OFFSET "+offset.toString()+"");
302
                        if(outlinecolor != null)
303
                                toMapln("OUTLINECOLOR "+outlinecolor.toString()+"");
304
                        if(partials != null)
305
                                toMapln("PARTIALS \""+partials+"\"");
306
                        if(position != null)
307
                                toMapln("POSITION \""+position+"\"");
308
                        if(shadowcolor!= null)
309
                                toMapln("SHADOWCOLOR "+shadowcolor.toString()+"");
310
                        if(shadowsize != null)
311
                                toMapln("SHADOWSIZE "+shadowsize.toString()+"");                        
312
                        if(type != null)
313
                                toMapln("TYPE "+type);
314
                        if(wrap != null)
315
                                toMapln("WRAP \""+wrap+"\"");
316
                        if(size > -1)
317
                                toMapln("SIZE "+ size);
318
                        tabOut();
319
                        toMapln("END");
320
                }
321
        }
322

    
323
        public static class Legend extends MapFile{
324
                public RGB imagecolor = null;
325
                public RGB outlinecolor = null;
326
                public String interlace = null;
327
                public Label legendlabel = null;
328
                public String position = null;
329
                public Point keysize = null;
330
                public Point keyspacing = null;
331
                public String postlabelcache = null;
332
                public String status = null;
333
                public String template = null;
334
                public String transparent = null;
335

    
336
                public void toMap(){
337
                        toMapln("LEGEND");
338
                        tabIn();
339
                        if(imagecolor != null)
340
                                toMapln("IMAGECOLOR "+imagecolor.toString());
341
                        if(outlinecolor != null)
342
                                toMapln("OUTLINECOLOR "+outlinecolor.toString());
343
                        if(interlace != null){
344
                                toMapln("INTERLACE \""+interlace.toUpperCase()+"\"");
345
                        }
346
                        if(legendlabel != null){
347
                                legendlabel.tomap();
348
                        }
349
                        if(position != null)
350
                                toMapln("POSITION \""+position+"\"");
351
                        if(keysize != null)
352
                                toMapln("KEYSIZE "+keysize.toString());
353
                        if(keyspacing != null)
354
                                toMapln("KEYSPACING "+keyspacing.toString());
355
                        if(postlabelcache != null)
356
                                toMapln("POSTLABELCACHE \""+postlabelcache+"\"");
357
                        if(status != null)
358
                                toMapln("STATUS \""+status.toUpperCase()+"\"");
359
                        if(template!=null){
360
                                toMapln("TEMPLATE \""+template+"\"");
361
                        }
362
                        if(transparent != null){
363
                                toMapln("TRANSPARENT \""+transparent.toUpperCase()+"\"");
364
                        }
365
                        tabOut();
366
                        toMapln("END");
367
                }
368
        }
369

    
370
        public static class Metadata extends MapFile {
371
                //WMS
372
                public String wms_title = null;
373
                public String wms_name = null;
374
                public String wms_abstract = null;
375
                public String wms_onlineresource = null;
376
                public String wms_extent = null;                
377
                public String wms_encoding=null;
378
                public String wms_srs=null;
379
                //GML
380
                public String gml_include_items =null;
381
                //WFS
382
                public String wfs_title = null;
383
                public String wfs_name = null;
384
                public String wfs_abstract = null;
385
                public String wfs_onlineresource = null;
386
                public String wfs_extent = null;                
387
                public String wfs_encoding=null;
388
                public String wfs_featureid = null;
389
                public String wfs_srs=null;
390
                //WCS
391
                public String wcs_label = null;
392
                public String wcs_name = null;
393
                public String wcs_description = null;
394
                public String wcs_onlineresource = null;
395
                public String wcs_extent = null;                
396
                public String wcs_encoding=null;
397
                public String wcs_srs=null;
398
                public String wcs_rangeset_axes=null;
399
                
400
                void startMetadataToMap(){
401
                        toMapln("METADATA");
402
                        tabIn();
403

    
404
                }
405
                void endMetadataToMap() {
406
                        tabOut();
407
                        toMapln("END");
408
                }
409
                void metadataToMap(){
410
                        startMetadataToMap();
411
                        //WMS
412
                        if (wms_encoding != null)
413
                                toMapln("\"wms_encoding\"" + " \"" + wms_encoding + "\"");
414
                        if (wms_title != null)
415
                                toMapln("\"wms_title\"" + " \"" + wms_title + "\"");
416
                        if (wms_abstract != null)
417
                                toMapln("\"wms_abstract\"" + " \"" + wms_abstract + "\"");
418
                        if (wms_extent != null)
419
                                toMapln("\"wms_extent\"" + " \"" + wms_extent + "\"");
420
                        if (wms_srs != null)
421
                                toMapln("\"wms_srs\"" + " \"" + wms_srs + "\"");
422
                        //GML
423
                        if (gml_include_items != null)
424
                                toMapln("\"gml_include_items\"" + " \"" + gml_include_items + "\"");
425
                        //WFS
426
                        if (wfs_encoding != null)
427
                                toMapln("\"wfs_encoding\"" + " \"" + wfs_encoding + "\"");
428
                        if (wfs_title != null)
429
                                toMapln("\"wfs_title\"" + " \"" + wfs_title + "\"");
430
                        if (wfs_abstract != null)
431
                                toMapln("\"wfs_abstract\"" + " \"" + wfs_abstract + "\"");
432
                        if (wfs_extent != null)
433
                                toMapln("\"wfs_extent\"" + " \"" + wfs_extent + "\"");
434
                        if (wfs_featureid != null)
435
                                toMapln("\"wfs_featureid\"" + " \"" + wfs_featureid + "\"");
436
                        if (wfs_srs != null)
437
                                toMapln("\"wfs_srs\"" + " \"" + wfs_srs + "\"");
438

    
439
                        //WCS
440
                        if (wcs_encoding != null)
441
                                toMapln("\"wcs_encoding\"" + " \"" + wcs_encoding + "\"");
442
                        if (wcs_label != null)
443
                                toMapln("\"wcs_label\"" + " \"" + wcs_label + "\"");
444
                        if (wcs_description != null)
445
                                toMapln("\"wcs_description\"" + " \"" + wcs_description + "\"");
446
                        if (wcs_extent != null)
447
                                toMapln("\"wcs_extent\"" + " \"" + wcs_extent + "\"");
448
                        if (wcs_srs != null)
449
                                toMapln("\"wcs_srs\"" + " \"" + wcs_srs + "\"");
450
                        if (wcs_rangeset_axes != null)
451
                                toMapln("\"wcs_rangeset_axes\"" + " \"" + wcs_rangeset_axes + "\"");
452

    
453
        
454
                        endMetadataToMap();
455
                }
456

    
457
        }
458

    
459

    
460

    
461
        public static class StyleMap extends MapFile {
462
                public RGB styleColor = null;
463
                public RGB styleColorOutline = null;
464
                public int size = -1;
465
                public int width = -1;
466
                public String symbol = null;
467

    
468
//                public StyleMap(RGB line, RGB fill) {
469
//                styleColor = fill;
470
//                styleColorOutline = line;
471
//                }
472
//                public StyleMap(Color color, Color outLineColor) {
473
//                RGB rgbStyleColor = null;
474
//                RGB rgbStyleColorOutline = null;
475
//                try{
476
//                rgbStyleColor = (new RGB(color.getRed(),
477
//                color.getGreen(),
478
//                color.getBlue()));
479
//                }catch(NullPointerException e){
480
//                //Line color is empty
481
//                }
482
//                try{
483
//                rgbStyleColorOutline = (new RGB(outLineColor.getRed(),
484
//                outLineColor.getGreen(),
485
//                outLineColor.getBlue()));
486
//                }catch(NullPointerException e){
487
//                //Fill color is empty
488
//                }
489
//                styleColor = rgbStyleColor;
490
//                styleColorOutline = rgbStyleColorOutline;
491
//                }                
492
                void styleToMap(){
493
                        toMapln("STYLE ");
494
                        tabIn();
495
                        if (styleColor != null){
496
                                toMapln("COLOR "+styleColor);
497
                        }
498
                        if (styleColorOutline != null){
499
                                toMapln("OUTLINECOLOR "+styleColorOutline);
500
                        }
501
                        if (size > -1)
502
                                toMapln("SIZE "+size);
503
                        if (width > -1)        toMapln("WIDTH "+width);
504
                        if (symbol != null){
505
                                toMapln("SYMBOL '"+symbol+"'");
506
                        }
507
                        tabOut();
508
                        toMapln("END");
509
                }
510
        }
511

    
512
        public static class WebMap extends MapFile{
513
                public Metadata metadata = null;                
514
                public String imagepath = null;
515
                public String imageurl = null;
516
                public String log = null;
517

    
518
                void webToMap(){
519
                        toMapln("WEB");
520
                        tabIn();
521
                        if (log != null)
522
                                toMapln("LOG \""+log+"\"");
523
                        if (imagepath != null){
524
                                toMapln("IMAGEPATH \""+imagepath+"\"");
525
                        }
526
                        if (imageurl!=null){
527
                                toMapln("IMAGEURL \""+imageurl+"\"");
528
                        }
529
                        metadata.metadataToMap();
530
                        tabOut();
531
                        toMapln("END");
532
                }
533
        }
534

    
535

    
536
        public static class JoinMap extends MapFile{
537

    
538
                public String connection=null;
539
                public String connectiontype=null;
540
                public String from=null;
541
                public String name=null;
542
                public String table=null;
543
                public String template=null;
544
                public String to=null;
545
                public String type=null;
546

    
547
                void tomap(){
548
                        toMapln("JOIN");
549
                        tabIn();
550
                        if (connection != null)
551
                                toMapln("CONNECTION \"" + connection+"\"");
552
                        if (connectiontype != null)
553
                                toMapln("CONNECTIONTYPE \"" + connectiontype+"\"");
554
                        if (from != null)
555
                                toMapln("FROM \"" + from+"\"");
556
                        if (name != null)
557
                                toMapln("NAME \"" + name+"\"");
558
                        if (table != null)
559
                                toMapln("TABLE \"" + table+"\"");
560
                        if (to != null)
561
                                toMapln("TO \"" + to+"\"");
562
                        if (type != null)
563
                                toMapln("TYPE \"" + type+"\"");
564
                        tabOut();                
565
                        toMapln("END");
566

    
567
                }
568
        }
569

    
570
        public static class MapClass extends MapFile {
571
                public RGB backgroundcolor=null;
572
                public RGB color=null;
573
                public String debug=null;
574
                public String expression=null;
575
                public JoinMap join=null;
576
                public String keyimage=null;
577
                public Label etiqueta = null;
578
                public String maxscale =null;
579
                public String maxsize=null;
580
                public String minscale=null;
581
                public String minsize=null;
582
                public String name;
583
                public RGB outlinecolor=null;
584
                public int size=-1;
585
                public StyleMap estilo = null;
586
                public String symbol=null;
587
                public String template = null;
588
                public String text=null;
589

    
590
                public void classToMap(){
591
                        toMapln("CLASS");
592
                        tabIn();
593
                        //if maxscale and minescale are -1, there aren't scales
594
//                        if (maxscale.equals("-1") && minscale.equals("-1")){
595
//                                maxscale = null;
596
//                                minscale = null;
597
//                        }
598
                        
599
                        if (backgroundcolor != null)
600
                                toMapln("BACKGROUND \"" + backgroundcolor.toString()+"\"");
601
                        if (color != null)
602
                                toMapln("COLOR " + color.toString());
603
                        if (debug != null)
604
                                toMapln("DEBUG \"" + debug+"\"");                        
605
                        if (join != null)
606
                                join.tomap();
607
                        if (keyimage != null)
608
                                toMapln("KEYIMAGE \"" + keyimage+"\"");
609
                        if (maxscale != null)
610
                                toMapln("MAXSCALE \"" + maxscale+"\"");
611
                        if (maxsize != null)
612
                                toMapln("MAXSIZE \"" + maxsize+"\"");
613
                        if (minscale != null)
614
                                toMapln("MINSCALE \"" + minscale+"\"");
615
                        if (minsize != null)
616
                                toMapln("MINSIZE \"" + minsize+"\"");
617
                        if (outlinecolor != null)
618
                                toMapln("OUTLINECOLOR \"" + outlinecolor.toString()+"\"");
619
                        if (size > -1)
620
                                toMapln("SIZE \""+size+"\"");
621
                        if (estilo != null)
622
                                estilo.styleToMap();
623
                        if (symbol != null)
624
                                toMapln("SYMBOL \"" + symbol+"\"");        
625
                        if (template != null)
626
                                toMapln("TEMPLATE \"" + template+"\"");                        
627
                        if (text != null)
628
                                toMapln("TEXT \"" + text+"\"");                        
629
                        if (expression != null)
630
                                //toMapln("EXPRESSION \"" + expression+"\"");
631
                                toMapln("EXPRESSION " + expression);
632
                        if (name !=null)
633
                                toMapln("NAME \""+name+"\"");
634
                        if (template != null)
635
                                toMapln("TEMPLATE \""+template+"\"");
636
                        if (etiqueta != null)
637
                                etiqueta.tomap();
638
                        tabOut();
639
                        toMapln("END");
640
                }
641
        }
642

    
643
        public abstract static class MapLayer extends MapFile
644
        {
645
                public String name = null;
646
                public String status = null;
647
                public String type = null;
648
                public int transparency = -1;
649
                public ArrayList classList = null;
650
                //public ArrayList layerList = null;
651
                public String data = null;
652
                public Metadata metadata = null;
653
                public boolean dump = false;
654
                public CRS layercrs=null;
655
                public String tileIndex = null;
656
                public String tileItem = null;
657
                public String sizeunits = null;
658
                public String classitem = null;
659
                public String maxscale = null;
660
                public String minscale = null;
661
                public String labelangleitem = null;
662
                public String labelitem = null;
663
                public String labelsizeitem = null;
664

    
665

    
666
                public MapLayer() {
667
                        super();
668
                        metadata=new Metadata();
669
                }
670

    
671
                public void setDump(boolean d) {
672
                        dump = d;
673
                }
674

    
675
                void startToMap() {
676
                        toMapln("LAYER");
677
                        tabIn(); 
678
                        toMapln("NAME \""+name+"\"");
679
                        if (maxscale !=null)
680
                                toMapln("MAXSCALE "+maxscale);
681
                        if (minscale !=null)
682
                                toMapln("MINSCALE "+minscale);
683
                        if (status !=null)
684
                                toMapln("STATUS "+status);                        
685
                        if (transparency > -1)
686
                                toMapln("TRANSPARENCY " + transparency);
687
                        if (tileIndex != null)
688
                                toMapln("TILEINDEX \""+tileIndex+"\"");
689
                        if (tileItem != null)
690
                                toMapln("TILEITEM \""+tileItem+"\"");
691
                        if (type != null)
692
                                toMapln("TYPE "+type);
693
                        if (dump){
694
                                toMapln("DUMP TRUE");
695
                                toMapln("TEMPLATE \".\"");
696
                        }
697
                        if (data != null)
698
                                toMapln("DATA \""+data+"\"");
699
                        if (sizeunits != null)
700
                                toMapln("SIZEUNITS "+sizeunits);
701
                        if (classitem != null)
702
                                toMapln("CLASSITEM \""+classitem+"\"");
703
                        if (labelangleitem != null)
704
                                toMapln("LABELANGLEITEM \""+labelangleitem+"\"");
705
                        if (labelsizeitem != null)
706
                                toMapln("LABELSIZEITEM \""+labelsizeitem+"\"");
707
                        if (labelitem != null)
708
                                toMapln("LABELITEM \""+labelitem+"\"");
709
                        
710
                }
711

    
712
                void endToMap() {
713
                        tabOut();
714
                        toMapln("END # Layer");
715
                }
716

    
717
                public void addClass(MapClass c) {
718
                        if (classList == null)
719
                                classList = new ArrayList();
720
                        classList.add(c);
721
                }
722

    
723
                public void classListToMap() {
724
                        if (classList != null) {
725
                                Iterator iter = classList.iterator();
726
                                while (iter.hasNext()) {
727
                                        MapClass c = (MapClass) iter.next();
728
                                        c.classToMap();
729
                                }
730
                        }
731

    
732
                }
733

    
734
//                public void addLayer(MapLayer c) {
735
//                if (layerList == null)
736
//                layerList = new ArrayList();
737
//                layerList.add(c);
738
//                }
739

    
740
//                public void layerListToMap() {
741
//                if (layerList != null) {
742
//                Iterator iter = layerList.iterator();
743
//                tabIn(); 
744
//                while (iter.hasNext()) {
745
//                MapLayer l = (MapLayer) iter.next();
746
//                l.layerToMap();
747
//                }
748
//                tabOut();
749
//                }
750
//                }
751

    
752
                public abstract void layerToMap();
753

    
754
                public String getName() {
755
                        return name;
756
                }
757

    
758
                public void setName(String name) {
759
                        this.name = name;
760
                        //metadata.name = name;
761
                        //setTitle(name);
762
                }
763

    
764
//                public String getTitle() {
765
//                return title;
766
//                }
767

    
768
//                public void setTitle(String title) {
769
//                this.title = title;
770
//                metadata.title = title;
771
//                }
772

    
773
                public void setExtent(double minx, double miny, double maxx, double maxy) {
774
                        super.setExtent(minx, miny, maxx, maxy);
775
                        metadata.setExtent(extent);
776
                }
777

    
778
                public void setExtent(Rectangle2D extent) {
779
                        super.setExtent(extent);
780
                        metadata.setExtent(extent);
781
                }
782

    
783
        }
784

    
785
        public static class ShpLayer extends MapLayer {                
786
                public void layerToMap() {
787
                        startToMap();                        
788
                        if (layercrs != null) layercrs.toMap();
789
                        classListToMap();
790
                        metadata.metadataToMap();
791
                        endToMap();
792
                }
793
        }
794

    
795
        public static class PostgisLayer extends MapLayer {
796
                public String user;
797
                public String pass;
798
                public String host;
799
                public String dbname;
800
                public String port;
801

    
802
                public String tabla;
803
                public String data;
804

    
805
                public void setConnParams(String user, String pass, String host,
806
                                String dbname, String port) {
807
                        this.user = user;
808
                        this.pass = pass;
809
                        this.host = host;
810
                        this.dbname = dbname;
811
                        this.port = port;
812
                }
813

    
814
                public void layerToMap() {
815
                        startToMap();
816
                        toMapln("DATA \""+data+"\"");
817
                        toMapln("CONNECTIONTYPE POSTGIS");
818
                        StringBuffer connection = new StringBuffer();
819
                        connection.append("CONNECTION \"user="+user);
820
                        if (pass != null){
821
                                connection.append(" password="+pass);
822
                        }
823
                        connection.append(" dbname="+dbname+" host="+host+" port="+port+"\"");
824
                        toMapln(connection.toString());
825
                        metadata.metadataToMap();
826
                        if (layercrs != null) layercrs.toMap();
827
                        classListToMap();
828
                        endToMap();
829
                }
830
        }
831

    
832
        public static class RasterLayer extends MapLayer {                
833
                public void layerToMap() {
834
                        startToMap();
835
                        metadata.metadataToMap();
836
                        if (layercrs != null) layercrs.toMap();
837
                        classListToMap();
838
                        endToMap();
839
                }
840
        }
841

    
842
        public static class ConfigFile extends MapFile {
843
                /**
844
                 * Atributos wms_xxxx comunes a WEB y LAYER.
845
                 * @author david
846
                 */
847

    
848
                public String mapFileName = null;
849
                public String fName = null;
850
                public String mapName = null;
851
                public String mapStatus = null;
852
                public String mapUnits= null;
853
                public String mapShapePath; 
854
                public Dimension mapSize = null;
855
                public CRS projection= null;
856
                public WebMap web = null;
857
                public String symbolset = null;
858
                public String fontset = null;
859
                public RGB imageColor = null;
860
                public Legend legend = null;
861

    
862
                private ArrayList outputFormats = null;
863
                private ArrayList layers = null; 
864

    
865
                public ConfigFile() {
866
                        web = new MapFile.WebMap();
867
                        web.metadata=new MapFile.Metadata();
868
                        layers = new ArrayList();
869
                        outputFormats = new ArrayList();
870
                }
871

    
872
                public void generate() {
873
                        generate(null);
874
                }
875

    
876
                public void generate(String donde) {
877
                        openMapFile(donde);
878
                        toMapln("MAP");
879
                        tabIn();
880
                        toMapln("NAME "+mapName);
881
                        if (mapSize != null)
882
                                toMapln("SIZE "+mapSize.width+" "+mapSize.height);
883
                        if (extent != null)
884
                                toMapln("EXTENT "+extentToMapString());
885
                        if (mapStatus != null)
886
                                toMapln("STATUS "+mapStatus);
887
                        if (mapUnits != null)
888
                                toMapln("UNITS "+mapUnits);
889
                        if (mapShapePath != null)
890
                                toMapln("SHAPEPATH \""+mapShapePath+"\"");
891

    
892
                        // OUTPUTFORMATS
893
                        {
894
                                Iterator iter = outputFormats.iterator();
895
                                while(iter.hasNext()) {
896
                                        OutputFormat o = (OutputFormat) iter.next();
897
                                        o.toMap();
898
                                }
899
                        }
900

    
901
                        if (symbolset != null)
902
                                toMapln("SYMBOLSET "+symbolset);
903
                        if (fontset != null)
904
                                toMapln("FONTSET "+fontset);
905
                        if (imageColor != null)
906
                                toMapln("IMAGECOLOR \""+imageColor+"\"");
907
                        if(legend!=null){
908
                                legend.toMap();
909
                        }
910
                        web.webToMap();
911
                        if (projection != null)
912
                                projectionToMap(projection,true);
913
                        // LAYERS
914
                        {
915
                                Iterator iter = layers.iterator();
916
                                while(iter.hasNext()) {
917
                                        MapLayer l = (MapLayer) iter.next();
918
                                        l.layerToMap();
919
                                }
920
                        }
921
                        tabOut();
922
                        toMapln("END # Map File");
923
                        closeMapFile();
924
                }
925

    
926
                void openMapFile(String donde) {
927
                        setMapFileName(donde);
928
                        if (mapFileName == null)
929
                                return;
930
                        File wmsmap = new File( mapFileName );
931
                        if (wmsmap.exists());
932
                        wmsmap.delete();
933
                        try{
934
                                //salida = new PrintStream(new BufferedOutputStream(new FileOutputStream(wmsmap)));
935
                                FileOutputStream fileoutput = new FileOutputStream(wmsmap);
936
                                //salida = new PrintStream(fileoutput);        
937
                                try {
938
                                        salida = new OutputStreamWriter(fileoutput, "UTF-8");
939
                                } catch (UnsupportedEncodingException e) {
940
                                        
941
                                        salida = new OutputStreamWriter(fileoutput);
942
                                }
943
                        } catch(FileNotFoundException e){
944
                                //TODO: change this!! by PublishException
945
                                System.err.println("Fichero no encontrado.");
946
                        }                                 
947

    
948
                }
949

    
950
                void closeMapFile() {
951
                        if (salida != null) {
952
                                
953
                                try {
954
                                        salida.flush();
955
                                        salida.close();
956
                                } catch (IOException e) {
957
                                        //TODO: quit this
958
                                        e.printStackTrace();                                        
959
                                }
960
                                
961
                        }
962
                }
963

    
964
                public String getMapFileName() {
965
                        return mapFileName;
966
                }
967

    
968
                public void setMapFileName(String mapFileName) {
969
                        this.mapFileName = mapFileName;
970
                }
971

    
972
                public void add(MapLayer l) {
973
                        layers.add(l);
974
                }
975
                public int layersCnt() {
976
                        return layers.size();
977
                }
978
                public MapLayer getLayer(int i) {
979
                        return (MapLayer) layers.get(i);
980
                }
981
                public void add(OutputFormat of) {
982
                        outputFormats.add(of);
983
                }
984
        }
985

    
986
        //static PrintStream salida = null;
987
        static Writer salida = null;
988
        static String tabstop = "";
989
        public Rectangle2D extent = null;
990
        public CRS crs = null;
991

    
992
        /**
993
         * 
994
         * @param minx
995
         * @param miny
996
         * @param maxx
997
         * @param maxy
998
         */
999
        public void setExtent(double minx, double miny, double maxx, double maxy) {
1000
                extent = new Rectangle2D.Double();
1001
                extent.setFrameFromDiagonal(minx, miny, maxx, maxy);
1002
        }
1003

    
1004
        public void setExtent(Rectangle2D extent) {
1005
                this.extent = extent;
1006
        }
1007

    
1008

    
1009
        String extentToMapString(Rectangle2D ext) {
1010
                if (ext != null)
1011
                        return ext.getMinX()+" "+ext.getMinY()+" "+ext.getMaxX()+" "+ext.getMaxY();
1012
                return "";
1013
        }
1014
        String extentToMapString() {
1015
                if (extent != null)
1016
                        return extentToMapString(extent);
1017
                return "";
1018
        }
1019

    
1020
        void projectionToMap(CRS prj, boolean init) {
1021
                if (prj == null)
1022
                        return;
1023
                toMapln("PROJECTION");
1024
                if (init == true)
1025
                        prj.toString();
1026
                tabIn();
1027
                toMapln("\""+prj+"\"");
1028
                tabOut();
1029
                toMapln("END");
1030
        }
1031

    
1032
        public String replicate(String str, int n) {
1033
                int i;
1034
                String ret = "";
1035
                for(i=0;i<n;i++){
1036
                        ret += str;
1037
                }
1038
                return ret;
1039
        }
1040

    
1041
        public void tabIn() {
1042
                tabstop += "   ";
1043
        }
1044

    
1045
        public void tabOut() {
1046
                tabstop = tabstop.substring(0,tabstop.length()-3);
1047
        }
1048

    
1049
        void toMapln(String str){
1050
                if (salida == null)
1051
                        System.out.println(tabstop+str);
1052
                else{
1053
                        //salida.println(tabstop+str);
1054
                        try {
1055
                                salida.write(tabstop+str+ "\n");
1056
                        } catch (IOException e) {
1057
                                // TODO quit this
1058
                                e.printStackTrace();
1059
                        }
1060
                }
1061
        }
1062
}