Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extPublish / src / org / gvsig / remoteservices / conf / mapserver / MapServer.java @ 7136

History | View | Annotate | Download (14.9 KB)

1
package org.gvsig.remoteservices.conf.mapserver;
2

    
3
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
4
 *
5
 * Copyright (C) 2004-2006 IVER T.I. and Generalitat Valenciana.
6
 *
7
 * This program is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU General Public License
9
 * as published by the Free Software Foundation; either version 2
10
 * of the License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU General Public License
18
 * along with this program; if not, write to the Free Software
19
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
20
 *
21
 * For more information, contact:
22
 *
23
 *   Generalitat Valenciana
24
 *   Conselleria d'Infraestructures i Transport
25
 *   Av. Blasco Ib??ez, 50
26
 *   46010 VALENCIA
27
 *   SPAIN
28
 *
29
 *   +34 963862235
30
 *   gvsig@gva.es
31
 *   www.gvsig.gva.es
32
 *
33
 *    or
34
 *
35
 *   IVER T.I. S.A
36
 *   Salamanca 50
37
 *   46005 Valencia
38
 *   Spain
39
 *
40
 *   +34 963163400
41
 *   dac@iver.es
42
 */
43
/* CVS MESSAGES:
44
 *
45
 * $Id: MapServer.java 7136 2006-09-08 10:14:04Z luisw2 $
46
 * $Log$
47
 * Revision 1.15  2006-09-08 10:14:04  luisw2
48
 * IRSLayer Added
49
 *
50
 * Revision 1.14  2006/09/08 07:12:03  luisw2
51
 * Minor corrections for raster; abstract added
52
 *
53
 * Revision 1.13  2006/09/07 19:25:01  luisw2
54
 * Correcciones en metadata
55
 *
56
 * Revision 1.12  2006/09/07 19:20:51  jorpiell
57
 * Classpath modificado
58
 *
59
 * Revision 1.11  2006/09/07 19:16:30  jorpiell
60
 * *** empty log message ***
61
 *
62
 * Revision 1.10  2006/09/07 18:11:06  luisw2
63
 * Adding Time to mapserver ...
64
 *
65
 * Revision 1.9  2006/09/07 16:34:28  luisw2
66
 * A?adido size
67
 *
68
 * Revision 1.8  2006/09/07 16:10:45  luisw2
69
 * a?adido flush() en el close;
70
 *
71
 * Revision 1.7  2006/09/07 12:59:44  jorpiell
72
 * Enganchada la interfaz gr?fica con el generador de ficheros
73
 *
74
 * Revision 1.6  2006/09/07 12:51:54  jorpiell
75
 * Enganchada la interfaz gr?fica con el generador de ficheros
76
 *
77
 * Revision 1.5  2006/09/07 12:47:39  jorpiell
78
 * A?adida la expression en el classMap
79
 *
80
 * Revision 1.4  2006/09/07 11:19:40  jvhigon
81
 * A?adido soporte para multiples class en una layer, y para layer sdentro de layer
82
 *
83
 * Revision 1.3  2006/09/01 06:59:00  luisw2
84
 * Headers mofification
85
 *
86
 */
87
import java.awt.Dimension;
88
import java.awt.Rectangle;
89
import java.awt.geom.Rectangle2D;
90
import java.io.File;
91
import java.io.FileNotFoundException;
92
import java.io.FileOutputStream;
93
import java.io.PrintStream;
94
import java.util.ArrayList;
95
import java.util.Iterator;
96

    
97
import org.gvsig.remoteservices.conf.IRSLayer;
98

    
99
/**
100
 * Allows to write a .map Mapserver configuration file.
101
 *
102
 * @author David Gilsanz
103
 * @author "Luis W. Sevilla" <sevilla_lui@gva.es>
104
 */
105

    
106
/* MAP
107
        NAME test_postgis
108
        STATUS ON
109
        SIZE 400 300
110
        #SYMBOLSET ../etc/symbols.sym
111
        EXTENT 638610.4375 4222780 789330 4484662.5
112
        UNITS METERS
113
        SHAPEPATH "/home/shapes/"
114
        IMAGECOLOR 255 255 255
115
        #FONTSET ../etc/fonts.txt
116

117
        WEB
118
                #  IMAGEPATH "/ms4w/tmp/ms_tmp/"
119
                #  IMAGEURL "/ms_tmp/"
120
                 METADATA
121
                            "wms_title"     "test postgis"  ##required
122
                            "wms_onlineresource" "http://localhost/mapserver/mapserv"   ##required
123
                            "wms_srs"       "EPSG:42304 EPSG:42101 EPSG:4269 EPSG:4326 EPSG:23030"  ##recommended
124
                  END
125
        END
126

127
        PROJECTION
128
                "init=epsg:23030"   ##required
129
        END
130

131
        #
132
        # Start of layer definitions
133
        #
134

135
        LAYER
136
                  NAME "autopistas"
137
                  DATA "the_geom from autopistas"
138
                CONNECTIONTYPE POSTGIS
139
                CONNECTION "user=david password=pwd dbname=betel host=localhost port=5434"
140
                STATUS ON
141
                TYPE LINE
142
                METADATA
143
                            "wms_title"  "Autopistas GV"   ##required
144
                            "wms_extent" "638610.4375 4222780 789330 4484662.5"
145
                  END
146
                  PROJECTION
147
                            "init=epsg:23030"   ##recommended
148
                  END
149
                  CLASS
150
                    NAME "Autopistas"
151
                    STYLE
152
                            COLOR 200 255 0
153
                            OUTLINECOLOR 120 120 120
154
                    END    
155
              END
156
    END 
157

158
END # Map File    
159
*/
160
public abstract class MapServer {
161
        public static final String SHP_TYPE_LINE = "LINE";
162
        public static final String SHP_TYPE_POLYLINE = "POLYLINE";
163
        public static final String SHP_TYPE_POINT = "POINT";
164
        public static final String SHP_TYPE_POLYGON = "POLYGON"; 
165
        
166
        public static class RGB {
167
                int r,g,b;
168
                public RGB(int red,int green,int blue){
169
                        r=red;
170
                        g=green;
171
                        b=blue;                        
172
                }
173
                public String toString(){
174
                        return ""+r+" "+g+" "+b;
175
                }
176
        }
177
        
178
        public static class CRS extends MapServer {
179
                boolean init = false;
180
                String crs = "";
181
                public CRS(String crs, boolean init) {
182
                        this.crs = crs;
183
                        this.init =init;
184
                }
185
                
186
                public String toString() {
187
                        String str = "";
188
                        if (init)
189
                                str += "init=";
190
                        return str+crs; //LWS .toLowerCase();
191
                }
192
                
193
                public void toMap() {
194
                        toMapln("PROJECTION");
195
                        tabIn();
196
                        toMapln("\""+this+"\"");
197
                        tabOut();
198
                        toMapln("END");
199
                }
200

    
201
                /**
202
                 * @return Returns the crs.
203
                 */
204
                public String getCrs() {
205
                        return crs;
206
                }
207
        }
208
        
209
        static PrintStream salida = null;
210
        static String tabstop = "";
211
        public Rectangle2D extent = null;
212
        public CRS crs = null;
213
        
214
        public void setExtent(double minx, double miny, double maxx, double maxy) {
215
                this.extent = new Rectangle2D.Double();
216
                this.extent.setFrameFromDiagonal(minx, miny, maxx, maxy);
217
        }
218

    
219
        public void setExtent(Rectangle2D extent) {
220
                this.extent = extent;
221
        }
222

    
223
        
224
        String extentToMapString(Rectangle2D ext) {
225
                if (ext != null)
226
                        return ext.getMinX()+" "+ext.getMinY()+" "+ext.getMaxX()+" "+ext.getMaxY();
227
                return "";
228
        }
229
        
230
        void projectionToMap(CRS prj, boolean init) {
231
                if (prj == null)
232
                        return;
233
                toMapln("PROJECTION");
234
                if (init == true)
235
                        prj.toString();
236
                tabIn();
237
                toMapln("\""+prj+"\"");
238
                tabOut();
239
                toMapln("END");
240
        }
241
        
242
        public String replicate(String str, int n) {
243
                int i;
244
                String ret = "";
245
                for(i=0;i<n;i++){
246
                        ret += str;
247
                }
248
                return ret;
249
        }
250
        
251
        public void tabIn() {
252
                tabstop += "   ";
253
        }
254
        
255
        public void tabOut() {
256
                tabstop = tabstop.substring(0,tabstop.length()-3);
257
        }
258
        
259
        void toMapln(String str){
260
                if (salida == null)
261
                        System.out.println(tabstop+str);
262
                else
263
                        salida.println(tabstop+str);        
264
        }
265
        
266
        public static class Metadata extends MapServer {
267
                public String prefix = "wms";
268
                public String title = null;
269
                public String abstrac = null;
270
                
271
                public void setServiceAsWFS() {
272
                        prefix = "wfs";
273
                }
274
                
275
                public void setServiceAsWMS() {
276
                        prefix = "wms";
277
                }
278
                public void setServiceAsWCS() {
279
                        prefix = "wcs";
280
                }
281
//                
282
                void startMetadataToMap(){
283
                        toMapln("METADATA");
284
                        tabIn();
285
                        if (title != null)
286
                                toMapln("\""+prefix+"_title\" " +"\""+title+"\"");
287
                        if (abstrac != null)
288
                                toMapln("\""+prefix+"_astract\" " +"\""+abstrac+"\"");
289
                        if (crs != null)
290
                                toMapln("\""+prefix+"_srs\" \""+crs+"\"");
291
                        if (extent != null)
292
                                toMapln("\""+prefix+"_extent\" \""+extentToMapString(extent)+"\"");
293
                }
294
                
295
                void endMetadataToMap() {
296
                        tabOut();
297
                        toMapln("END");
298
                }
299
        }
300
        
301
        public static class MetadataWeb extends Metadata {
302
                //Atributos wms_xxxx WEB
303
                public String onlineresource = null;
304
                public String ows_schemas_location = null;
305
                public String timeFormat = null;
306
                
307
                void metadataToMap(){
308
                        startMetadataToMap();                
309
                        toMapln("\""+prefix+"_onlineresource\" \""+onlineresource+"\"");
310
                        if (crs != null)
311
                                toMapln("\""+prefix+"_srs\" \""+crs+"\"");
312
                        if (ows_schemas_location != null)
313
                                toMapln("\"ows_schemas_location\" \""+ows_schemas_location+"\"");
314
                        if (timeFormat != null)
315
                                toMapln("\""+prefix+"_timeformat\" \""+timeFormat+"\"");
316
                        endMetadataToMap();
317
                }
318
        }
319
        
320
        public static class MetadataLayer extends Metadata {
321
                //Atributos wms_xxxx LAYER        
322
                public String gml_include_items = null;
323
                public String timeExtent = null;
324
                public String timeDefault = null;
325
                public String timeItem = null;
326
                void metadataToMap(){
327
                        startMetadataToMap();
328
                        if (gml_include_items != null)
329
                                toMapln("\"gml_include_items\" \""+gml_include_items+"\"");
330
                        if (timeExtent != null)
331
                                toMapln("\""+prefix+"_timeextent\" \""+timeExtent+"\"");
332
                        if (timeDefault != null)
333
                                toMapln("\""+prefix+"_timedefault\" \""+timeDefault+"\"");
334
                        if (timeItem != null)
335
                                toMapln("\""+prefix+"_timeitem\" \""+timeItem+"\"");
336

    
337
                        endMetadataToMap();
338
                }
339
        }
340
        
341
        public static class StyleMap extends MapServer {
342
                public RGB styleColor = null;
343
                public RGB styleColorOutline = null;
344
                public int size = -1;
345
                public StyleMap(RGB line, RGB fill) {
346
                        styleColor = fill;
347
                        styleColorOutline = line;
348
                }
349
                void styleToMap(){
350
                        toMapln("STYLE ");
351
                        tabIn();
352
                        toMapln("COLOR "+styleColor);
353
                        toMapln("OUTLINECOLOR "+styleColorOutline);
354
                        if (size > -1)
355
                                toMapln("SIZE "+size);
356
                        tabOut();
357
                        toMapln("END");
358
                }
359
        }
360
        
361
        public static class WebMap extends MapServer{
362
                public MetadataWeb metadata = null;
363
                public String imagepath = null;
364
                public String imageurl = null;
365
                void webToMap(){
366
                        toMapln("WEB");
367
                        tabIn();
368
                        toMapln("IMAGEPATH \""+imagepath+"\"");
369
                        toMapln("IMAGEURL \""+imageurl+"\"");
370
                        metadata.metadataToMap();
371
                        tabOut();
372
                        toMapln("END");
373
                }
374
        }
375
        
376
        public static class MapClass extends MapServer {
377
                public String name;
378
                public StyleMap estilo = null;
379
                public String template = null;
380
                public String expression = null;
381
                public int size = -1;
382
                public MapClass(String n) {
383
                        name = n;
384
                }
385
                public void classToMap(){
386
                        toMapln("CLASS");
387
                        tabIn();
388
                        if (expression != null){
389
                                toMapln("EXPRESSION " + expression);
390
                        }
391
                        toMapln("NAME \""+name+"\"");
392
                        if (estilo != null)
393
                                estilo.styleToMap();
394
                        if (size > -1)
395
                                toMapln("SIZE "+size);
396
                        if (template != null)
397
                                toMapln("TEMPLATE "+template);
398
                        tabOut();
399
                        toMapln("END");
400
                }
401
        }
402
        
403
        public abstract static class MapLayer extends MapServer
404
                implements IRSLayer {
405
                public String name;
406
                public String title=null;
407
                public String status = "ON";
408
                public String type = null;
409
                public int transparency = -1;
410
                public ArrayList classList = null;
411
                public ArrayList layerList = null;
412
                public String data;
413
                public MetadataLayer metadata = null;
414
                public boolean dump = false;
415
                public CRS layercrs=null;
416
                public String tileIndex = null;
417
                public String tileItem = null;
418
                
419
                public void setDump(boolean d) {
420
                        dump = d;
421
                }
422
                
423
                void startToMap() {
424
                        toMapln("LAYER");
425
                        tabIn(); 
426
                        toMapln("NAME \""+name+"\"");
427
                        if (title != null)
428
                                toMapln("TITLE \""+title+"\"");
429
                        toMapln("STATUS "+status);
430
                        if (transparency > -1)
431
                                toMapln("TRANSPARENCY " + transparency);
432
                        if (tileIndex != null)
433
                                toMapln("TILEINDEX \""+tileIndex+"\"");
434
                        if (tileItem != null)
435
                                toMapln("TILEITEM \""+tileItem+"\"");
436
                        if (type != null)
437
                                toMapln("TYPE "+type);
438
                        if (dump)
439
                                toMapln("DUMP TRUE # required");
440
                        if (data != null)
441
                                toMapln("DATA \""+data+"\"");
442
                }
443
                
444
                void endToMap() {
445
                        tabOut();
446
                        toMapln("END # Layer");
447
                }
448
                
449
                public void addClass(MapClass c) {
450
                        if (classList == null)
451
                                classList = new ArrayList();
452
                        classList.add(c);
453
                }
454
                
455
                public void classListToMap() {
456
                        if (classList != null) {
457
                                Iterator iter = classList.iterator();
458
                                while (iter.hasNext()) {
459
                                        MapClass c = (MapClass) iter.next();
460
                                        c.classToMap();
461
                                }
462
                        }
463
                                
464
                }
465
                
466
                public void addLayer(MapLayer c) {
467
                        if (layerList == null)
468
                                layerList = new ArrayList();
469
                        layerList.add(c);
470
                }
471
                
472
                public void layerListToMap() {
473
                        if (layerList != null) {
474
                                Iterator iter = layerList.iterator();
475
                                tabIn(); 
476
                                while (iter.hasNext()) {
477
                                        MapLayer l = (MapLayer) iter.next();
478
                                        l.layerToMap();
479
                                }
480
                                tabOut();
481
                        }
482
                }
483
                
484
                public abstract void layerToMap();
485

    
486
                public String getName() {
487
                        return name;
488
                }
489

    
490
                public void setName(String name) {
491
                        this.name = name;
492
                }
493

    
494
                public String getTitle() {
495
                        return title;
496
                }
497

    
498
                public void setTitle(String title) {
499
                        this.title = title;
500
                }
501
        }
502
        
503
        public static class ShpLayer extends MapLayer {                
504
                public void layerToMap() {
505
                        startToMap();
506
                        metadata.metadataToMap();
507
                        if (layercrs != null) layercrs.toMap();
508
                        classListToMap();
509
                        endToMap();
510
                }
511
        }
512
        
513
        public static class PostgisLayer extends MapLayer {
514
                public String user;
515
                public String pass;
516
                public String host;
517
                public String dbname;
518
                public String port;
519
                
520
                public String tabla;
521
                public String data;
522
                
523
                public void setConnParams(String user, String pass, String host,
524
                                String dbname, String port) {
525
                        this.user = user;
526
                        this.pass = pass;
527
                        this.host = host;
528
                        this.dbname = dbname;
529
                        this.port = port;
530
                }
531
                
532
                public void layerToMap() {
533
                        startToMap();
534
                        toMapln("DATA \""+data+"\"");
535
                        toMapln("CONNECTIONTYPE POSTGIS");
536
                        toMapln("CONNECTION \"user="+user+" password="+pass+" dbname="+dbname+" host="+host+" port="+port+"\"");
537
                        metadata.metadataToMap();
538
                        if (layercrs != null) layercrs.toMap();
539
                        classListToMap();
540
                        endToMap();
541
                }
542
        }
543
        
544
        public static class RasterLayer extends MapLayer {                
545
                public void layerToMap() {
546
                        startToMap();
547
                        metadata.metadataToMap();
548
                        if (layercrs != null) layercrs.toMap();
549
                        classListToMap();
550
                        endToMap();
551
                }
552
        }
553
        
554
        public static class ConfigFile extends MapServer {
555
                /**
556
                 * Atributos wms_xxxx comunes a WEB y LAYER.
557
                 * @author david
558
                 */
559
                
560
                public String mapFileName = null;
561
                public String fName = null;
562
                public String mapName = null;
563
                public String mapStatus = null;
564
                public String mapUnits= null;
565
                public String mapShapePath; 
566
                public Dimension mapSize = null;
567
                public CRS mapcrs= null;
568
                public WebMap www = null;
569
                public String symbolset = null;
570
                public String fontset = null;
571
                public RGB imageColor = null;
572
                
573
                public ArrayList layers = new ArrayList(); 
574
                
575
                public ConfigFile() {
576
                }
577
                
578
                public void generate() {
579
                        generate(null);
580
                }
581
                
582
                public void generate(String donde) {
583
                        openMapFile(donde);
584
                        toMapln("MAP");
585
                        tabIn();
586
                        toMapln("NAME "+mapName);
587
                        if (mapSize != null)
588
                                toMapln("SIZE "+mapSize.width+" "+mapSize.height);
589
                        toMapln("EXTENT "+extentToMapString(extent));
590
                        toMapln("STATUS "+mapStatus);
591
                        toMapln("UNITS "+mapUnits);
592
                        toMapln("SHAPEPATH \""+mapShapePath+"\"");
593
                        if (symbolset != null)
594
                                toMapln("SYMBOLSET "+symbolset);
595
                        if (symbolset != null)
596
                                toMapln("FONTSET "+symbolset);
597
                        if (imageColor != null)
598
                                toMapln("IMAGECOLOR \""+imageColor+"\"");
599
                        www.webToMap();
600
                        if (mapcrs != null)
601
                                projectionToMap(mapcrs,true);
602
                        Iterator iter = layers.iterator();
603
                        while(iter.hasNext()) {
604
                                MapLayer l = (MapLayer) iter.next();
605
                                l.layerToMap();
606
                        }
607
                        tabOut();
608
                        toMapln("END # Map File");
609
                        closeMapFile();
610
                }
611
                
612
                void openMapFile(String donde) {
613
                        setMapFileName(donde);
614
                        if (mapFileName == null)
615
                                return;
616
                        File wmsmap = new File( mapFileName );
617
                        if (wmsmap.exists());
618
                                wmsmap.delete();
619
                        try{
620
                                //salida = new PrintStream(new BufferedOutputStream(new FileOutputStream(wmsmap)));
621
                                salida = new PrintStream(new FileOutputStream(wmsmap));        
622
                        } catch(FileNotFoundException e){
623
                                System.out.println("Fichero no encontrado.");
624
                        }
625
                        System.out.println(mapFileName+" abierto.");            
626
                        
627
                }
628
                
629
                void closeMapFile() {
630
                        if (salida != null) {
631
                                salida.flush();
632
                                salida.close();
633
                        }
634
                }
635
                
636
                public String getMapFileName() {
637
                        return mapFileName;
638
                }
639
                
640
                public void setMapFileName(String mapFileName) {
641
                        this.mapFileName = mapFileName;
642
                }
643
        }
644
}