Statistics
| Revision:

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

History | View | Annotate | Download (15.2 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 7137 2006-09-08 10:50:05Z jorpiell $
46
 * $Log$
47
 * Revision 1.16  2006-09-08 10:50:05  jorpiell
48
 * A?adida la opci?n de generar una capa POSTGIS
49
 *
50
 * Revision 1.15  2006/09/08 10:14:04  luisw2
51
 * IRSLayer Added
52
 *
53
 * Revision 1.14  2006/09/08 07:12:03  luisw2
54
 * Minor corrections for raster; abstract added
55
 *
56
 * Revision 1.13  2006/09/07 19:25:01  luisw2
57
 * Correcciones en metadata
58
 *
59
 * Revision 1.12  2006/09/07 19:20:51  jorpiell
60
 * Classpath modificado
61
 *
62
 * Revision 1.11  2006/09/07 19:16:30  jorpiell
63
 * *** empty log message ***
64
 *
65
 * Revision 1.10  2006/09/07 18:11:06  luisw2
66
 * Adding Time to mapserver ...
67
 *
68
 * Revision 1.9  2006/09/07 16:34:28  luisw2
69
 * A?adido size
70
 *
71
 * Revision 1.8  2006/09/07 16:10:45  luisw2
72
 * a?adido flush() en el close;
73
 *
74
 * Revision 1.7  2006/09/07 12:59:44  jorpiell
75
 * Enganchada la interfaz gr?fica con el generador de ficheros
76
 *
77
 * Revision 1.6  2006/09/07 12:51:54  jorpiell
78
 * Enganchada la interfaz gr?fica con el generador de ficheros
79
 *
80
 * Revision 1.5  2006/09/07 12:47:39  jorpiell
81
 * A?adida la expression en el classMap
82
 *
83
 * Revision 1.4  2006/09/07 11:19:40  jvhigon
84
 * A?adido soporte para multiples class en una layer, y para layer sdentro de layer
85
 *
86
 * Revision 1.3  2006/09/01 06:59:00  luisw2
87
 * Headers mofification
88
 *
89
 */
90
import java.awt.Dimension;
91
import java.awt.Rectangle;
92
import java.awt.geom.Rectangle2D;
93
import java.io.File;
94
import java.io.FileNotFoundException;
95
import java.io.FileOutputStream;
96
import java.io.PrintStream;
97
import java.util.ArrayList;
98
import java.util.Iterator;
99

    
100
import org.gvsig.remoteservices.conf.IRSLayer;
101

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

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

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

130
        PROJECTION
131
                "init=epsg:23030"   ##required
132
        END
133

134
        #
135
        # Start of layer definitions
136
        #
137

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

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

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

    
222
        public void setExtent(Rectangle2D extent) {
223
                this.extent = extent;
224
        }
225

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

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

    
489
                public String getName() {
490
                        return name;
491
                }
492

    
493
                public void setName(String name) {
494
                        this.name = name;
495
                }
496

    
497
                public String getTitle() {
498
                        return title;
499
                }
500

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