Statistics
| Revision:

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

History | View | Annotate | Download (13.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 7122 2006-09-07 19:16:30Z jorpiell $
46
 * $Log$
47
 * Revision 1.11  2006-09-07 19:16:30  jorpiell
48
 * *** empty log message ***
49
 *
50
 * Revision 1.10  2006/09/07 18:11:06  luisw2
51
 * Adding Time to mapserver ...
52
 *
53
 * Revision 1.9  2006/09/07 16:34:28  luisw2
54
 * A?adido size
55
 *
56
 * Revision 1.8  2006/09/07 16:10:45  luisw2
57
 * a?adido flush() en el close;
58
 *
59
 * Revision 1.7  2006/09/07 12:59:44  jorpiell
60
 * Enganchada la interfaz gr?fica con el generador de ficheros
61
 *
62
 * Revision 1.6  2006/09/07 12:51:54  jorpiell
63
 * Enganchada la interfaz gr?fica con el generador de ficheros
64
 *
65
 * Revision 1.5  2006/09/07 12:47:39  jorpiell
66
 * A?adida la expression en el classMap
67
 *
68
 * Revision 1.4  2006/09/07 11:19:40  jvhigon
69
 * A?adido soporte para multiples class en una layer, y para layer sdentro de layer
70
 *
71
 * Revision 1.3  2006/09/01 06:59:00  luisw2
72
 * Headers mofification
73
 *
74
 */
75
import java.awt.Dimension;
76
import java.awt.Rectangle;
77
import java.awt.geom.Rectangle2D;
78
import java.io.File;
79
import java.io.FileNotFoundException;
80
import java.io.FileOutputStream;
81
import java.io.PrintStream;
82
import java.util.ArrayList;
83
import java.util.Iterator;
84

    
85
/**
86
 * Genera un .MAP
87
 * ... como este:
88
 MAP
89
        NAME test_postgis
90
        STATUS ON
91
        SIZE 400 300
92
        #SYMBOLSET ../etc/symbols.sym
93
        EXTENT 638610.4375 4222780 789330 4484662.5
94
        UNITS METERS
95
        SHAPEPATH "/home/shapes/"
96
        IMAGECOLOR 255 255 255
97
        #FONTSET ../etc/fonts.txt
98

99
        WEB
100
                #  IMAGEPATH "/ms4w/tmp/ms_tmp/"
101
                #  IMAGEURL "/ms_tmp/"
102
                 METADATA
103
                            "wms_title"     "test postgis"  ##required
104
                            "wms_onlineresource" "http://localhost/mapserver/mapserv"   ##required
105
                            "wms_srs"       "EPSG:42304 EPSG:42101 EPSG:4269 EPSG:4326 EPSG:23030"  ##recommended
106
                  END
107
        END
108

109
        PROJECTION
110
                "init=epsg:23030"   ##required
111
        END
112

113
        #
114
        # Start of layer definitions
115
        #
116

117
        LAYER
118
                  NAME "autopistas"
119
                  DATA "the_geom from autopistas"
120
                CONNECTIONTYPE POSTGIS
121
                CONNECTION "user=david password=chkdsk dbname=betel host=localhost port=5434"
122
                STATUS ON
123
                TYPE LINE
124
                METADATA
125
                            "wms_title"  "Autopistas GV"   ##required
126
                            "wms_extent" "638610.4375 4222780 789330 4484662.5"
127
                  END
128
                  PROJECTION
129
                            "init=epsg:23030"   ##recommended
130
                  END
131
                  CLASS
132
                    NAME "Autopistas"
133
                    STYLE
134
                            COLOR 200 255 0
135
                            OUTLINECOLOR 120 120 120
136
                    END    
137
              END
138
    END 
139

140
END # Map File    
141

142
 * @author david
143
 */
144

    
145
public abstract class MapServer {
146
        public static final String SHP_TYPE_LINE = "LINE";
147
        public static final String SHP_TYPE_POLYLINE = "POLYLINE";
148
        public static final String SHP_TYPE_POINT = "POINT";
149
        public static final String SHP_TYPE_POLYGON = "POLYGON"; 
150
        
151
        public static class RGB {
152
                int r,g,b;
153
                public RGB(int red,int green,int blue){
154
                        r=red;
155
                        g=green;
156
                        b=blue;                        
157
                }
158
                public String toString(){
159
                        return ""+r+" "+g+" "+b;
160
                }
161
        }
162
        
163
        public static class CRS extends MapServer {
164
                boolean init = false;
165
                String crs = "";
166
                public CRS(String crs, boolean init) {
167
                        this.crs = crs;
168
                        this.init =init;
169
                }
170
                
171
                public String toString() {
172
                        String str = "";
173
                        if (init)
174
                                str += "init=";
175
                        return str+crs; //LWS .toLowerCase();
176
                }
177
                
178
                public void toMap() {
179
                        toMapln("PROJECTION");
180
                        tabIn();
181
                        toMapln("\""+this+"\"");
182
                        tabOut();
183
                        toMapln("END");
184
                }
185
        }
186
        
187
        static PrintStream salida = null;
188
        static String tabstop = "";
189
        public Rectangle2D extent = null;
190
        public CRS crs = null;
191
        
192
        public void setExtent(double minx, double miny, double maxx, double maxy) {
193
                this.extent = new Rectangle2D.Double();
194
                this.extent.setFrameFromDiagonal(minx, miny, maxx, maxy);
195
        }
196

    
197
        public void setExtent(Rectangle2D extent) {
198
                this.extent = extent;
199
        }
200

    
201
        
202
        String extentToMapString(Rectangle2D ext) {
203
                if (ext != null)
204
                        return ext.getMinX()+" "+ext.getMinY()+" "+ext.getMaxX()+" "+ext.getMaxY();
205
                return "";
206
        }
207
        
208
        void projectionToMap(CRS prj, boolean init) {
209
                if (prj == null)
210
                        return;
211
                toMapln("PROJECTION");
212
                if (init == true)
213
                        prj.toString();
214
                tabIn();
215
                toMapln("\""+prj+"\"");
216
                tabOut();
217
                toMapln("END");
218
        }
219
        
220
        public String replicate(String str, int n) {
221
                int i;
222
                String ret = "";
223
                for(i=0;i<n;i++){
224
                        ret += str;
225
                }
226
                return ret;
227
        }
228
        
229
        public void tabIn() {
230
                tabstop += "   ";
231
        }
232
        
233
        public void tabOut() {
234
                tabstop = tabstop.substring(0,tabstop.length()-3);
235
        }
236
        
237
        void toMapln(String str){
238
                if (salida == null)
239
                        System.out.println(tabstop+str);
240
                else
241
                        salida.println(tabstop+str);        
242
        }
243
        
244
        public static class Metadata extends MapServer {
245
                public String prefix = "wms";
246
                public String title = null;
247
                
248
                public void setServiceAsWFS() {
249
                        prefix = "wfs";
250
                }
251
                
252
                public void setServiceAsWMS() {
253
                        prefix = "wms";
254
                }
255
                public void setServiceAsWCS() {
256
                        prefix = "wcs";
257
                }
258
//                
259
                void startMetadataToMap(){
260
                        toMapln("METADATA");
261
                        tabIn();
262
                        if (title != null)
263
                                toMapln("\""+prefix+"_title\" " +"\""+title+"\"");
264
                }
265
                
266
                void endMetadataToMap() {
267
                        tabOut();
268
                        toMapln("END");
269
                }
270
        }
271
        
272
        public static class MetadataWeb extends Metadata {
273
                //Atributos wms_xxxx WEB
274
                public String onlineresource = null;
275
                public String ows_schemas_location = null;
276
                public String timeFormat = null;
277
                
278
                void metadataToMap(){
279
                        startMetadataToMap();                
280
                        toMapln("\""+prefix+"_onlineresource\" \""+onlineresource+"\"");
281
                        toMapln("\""+prefix+"_srs\" \""+crs+"\"");
282
                        if (ows_schemas_location != null)
283
                                toMapln("\"ows_schemas_location\" \""+ows_schemas_location+"\"");
284
                        if (timeFormat != null)
285
                                toMapln("\""+prefix+"_timeformat\" \""+timeFormat+"\"");
286
                        endMetadataToMap();
287
                }
288
        }
289
        
290
        public static class MetadataLayer extends Metadata {
291
                //Atributos wms_xxxx LAYER        
292
                public String metalayertitle = null;
293
                public String gml_include_items = null;
294
                public String timeExtent = null;
295
                public String timeDefault = null;
296
                public String timeItem = null;
297
                void metadataToMap(){
298
                        startMetadataToMap();
299
                        if (extent != null)
300
                                toMapln("\""+prefix+"_extent\" \""+extentToMapString(extent)+"\"");
301
                        if (gml_include_items != null)
302
                                toMapln("\"gml_include_items\" \""+gml_include_items+"\"");
303
                        if (timeExtent != null)
304
                                toMapln("\""+prefix+"_timeextent\" \""+timeExtent+"\"");
305
                        if (timeDefault != null)
306
                                toMapln("\""+prefix+"_timedefault\" \""+timeDefault+"\"");
307
                        if (timeItem != null)
308
                                toMapln("\""+prefix+"_timeitem\" \""+timeItem+"\"");
309

    
310
                        endMetadataToMap();
311
                }
312
        }
313
        
314
        public static class StyleMap extends MapServer {
315
                public RGB styleColor = null;
316
                public RGB styleColorOutline = null;
317
                public int size = -1;
318
                public StyleMap(RGB line, RGB fill) {
319
                        styleColor = fill;
320
                        styleColorOutline = line;
321
                }
322
                void styleToMap(){
323
                        toMapln("STYLE ");
324
                        tabIn();
325
                        toMapln("COLOR "+styleColor);
326
                        toMapln("OUTLINECOLOR "+styleColorOutline);
327
                        if (size > -1)
328
                                toMapln("SIZE "+size);
329
                        tabOut();
330
                        toMapln("END");
331
                }
332
        }
333
        
334
        public static class WebMap extends MapServer{
335
                public MetadataWeb metadata = null;
336
                public String imagepath = null;
337
                public String imageurl = null;
338
                void webToMap(){
339
                        toMapln("WEB");
340
                        tabIn();
341
                        toMapln("IMAGEPATH \""+imagepath+"\"");
342
                        toMapln("IMAGEURL \""+imageurl+"\"");
343
                        metadata.metadataToMap();
344
                        tabOut();
345
                        toMapln("END");
346
                }
347
        }
348
        
349
        public static class MapClass extends MapServer {
350
                public String name;
351
                public StyleMap estilo = null;
352
                public String template = null;
353
                public String expression = null;
354
                public int size = -1;
355
                public MapClass(String n) {
356
                        name = n;
357
                }
358
                public void classToMap(){
359
                        toMapln("CLASS");
360
                        tabIn();
361
                        if (expression != null){
362
                                toMapln("EXPRESSION " + expression);
363
                        }
364
                        toMapln("NAME \""+name+"\"");
365
                        if (estilo != null)
366
                                estilo.styleToMap();
367
                        if (size > -1)
368
                                toMapln("SIZE "+size);
369
                        if (template != null)
370
                                toMapln("TEMPLATE "+template);
371
                        tabOut();
372
                        toMapln("END");
373
                }
374
        }
375
        
376
        public abstract static class MapLayer extends MapServer {
377
                public String name;
378
                public String title;
379
                public String status = "ON";
380
                public String type = null;
381
                public int transparency = -1;
382
                public ArrayList classList = null;
383
                public ArrayList layerList = null;
384
                public String data;
385
                public MetadataLayer metadata = null;
386
                public boolean dump = false;
387
                public CRS layercrs=null;
388
                public String tileIndex = null;
389
                public String tileItem = null;
390
                
391
                public void setDump(boolean d) {
392
                        dump = d;
393
                }
394
                
395
                void startToMap() {
396
                        toMapln("LAYER");
397
                        tabIn(); 
398
                        toMapln("NAME \""+name+"\"");
399
                        toMapln("STATUS "+status);
400
                        if (transparency > -1){
401
                                toMapln("TRANSPARENCY " + transparency);
402
                        }
403
                        if (tileIndex != null)
404
                                toMapln("TILEINDEX \""+tileIndex+"\"");
405
                        if (tileItem != null)
406
                                toMapln("TILEITEM \""+tileItem+"\"");
407
                        if (type != null)
408
                                toMapln("TYPE "+type);
409
                        if (dump)
410
                                toMapln("DUMP TRUE # required");
411
                }
412
                
413
                void endToMap() {
414
                        tabOut();
415
                        toMapln("END # Layer");
416
                }
417
                
418
                public void addClass(MapClass c) {
419
                        if (classList == null)
420
                                classList = new ArrayList();
421
                        classList.add(c);
422
                }
423
                
424
                public void classListToMap() {
425
                        if (classList != null) {
426
                                Iterator iter = classList.iterator();
427
                                while (iter.hasNext()) {
428
                                        MapClass c = (MapClass) iter.next();
429
                                        c.classToMap();
430
                                }
431
                        }
432
                                
433
                }
434
                
435
                public void addLayer(MapLayer c) {
436
                        if (layerList == null)
437
                                layerList = new ArrayList();
438
                        layerList.add(c);
439
                }
440
                
441
                public void layerListToMap() {
442
                        if (layerList != null) {
443
                                Iterator iter = layerList.iterator();
444
                                tabIn(); 
445
                                while (iter.hasNext()) {
446
                                        MapLayer l = (MapLayer) iter.next();
447
                                        l.layerToMap();
448
                                }
449
                                tabOut();
450
                        }
451
                                
452
                }
453
                
454
                public abstract void layerToMap();
455
        }
456
        
457
        public static class ShpLayer extends MapLayer {                
458
                public void layerToMap() {
459
                        startToMap();
460
                        toMapln("DATA \""+data+"\"");
461
                        metadata.metadataToMap();
462
                        if (layercrs != null) layercrs.toMap();
463
                        classListToMap();
464
                        endToMap();
465
                }
466
        }
467
        
468
        public static class PostgisLayer extends MapLayer {
469
                public String user;
470
                public String pass;
471
                public String host;
472
                public String dbname;
473
                public String port;
474
                
475
                public String tabla;
476
                public String data;
477
                
478
                public void setConnParams(String user, String pass, String host,
479
                                String dbname, String port) {
480
                        this.user = user;
481
                        this.pass = pass;
482
                        this.host = host;
483
                        this.dbname = dbname;
484
                        this.port = port;
485
                }
486
                
487
                public void layerToMap() {
488
                        startToMap();
489
                        toMapln("DATA \""+data+"\"");
490
                        toMapln("CONNECTIONTYPE POSTGIS");
491
                        toMapln("CONNECTION \"user="+user+" password="+pass+" dbname="+dbname+" host="+host+" port="+port+"\"");
492
                        metadata.metadataToMap();
493
                        if (layercrs != null) layercrs.toMap();
494
                        classListToMap();
495
                        endToMap();
496
                }
497
        }
498
        
499
        public static class RasterLayer extends MapLayer {                
500
                public void layerToMap() {
501
                        startToMap();
502
                        toMapln("DATA \""+data+"\"");
503
                        metadata.metadataToMap();
504
                        if (layercrs != null) layercrs.toMap();
505
                        classListToMap();
506
                        endToMap();
507
                }
508
        }
509
        
510
        public static class ConfigFile extends MapServer {
511
                /**
512
                 * Atributos wms_xxxx comunes a WEB y LAYER.
513
                 * @author david
514
                 */
515
                
516
                public String mapFileName = null;
517
                public String fName = null;
518
                public String mapName = null;
519
                public String mapStatus = null;
520
                public String mapUnits= null;
521
                public String mapShapePath; 
522
                public Dimension mapSize = null;
523
                public CRS mapcrs= null;
524
                public WebMap www = null;
525
                public String symbolset = null;
526
                public String fontset = null;
527
                public RGB imageColor = null;
528
                
529
                public ArrayList layers = new ArrayList(); 
530
                
531
                public ConfigFile() {
532
                }
533
                
534
                public void generate() {
535
                        generate(null);
536
                }
537
                
538
                public void generate(String donde) {
539
                        openMapFile(donde);
540
                        toMapln("MAP");
541
                        tabIn();
542
                        toMapln("NAME "+mapName);
543
                        if (mapSize != null)
544
                                toMapln("SIZE "+mapSize.width+" "+mapSize.height);
545
                        toMapln("EXTENT "+extentToMapString(extent));
546
                        toMapln("STATUS "+mapStatus);
547
                        toMapln("UNITS "+mapUnits);
548
                        toMapln("SHAPEPATH \""+mapShapePath+"\"");
549
                        if (symbolset != null)
550
                                toMapln("SYMBOLSET "+symbolset);
551
                        if (symbolset != null)
552
                                toMapln("FONTSET "+symbolset);
553
                        if (imageColor != null)
554
                                toMapln("IMAGECOLOR \""+imageColor+"\"");
555
                        www.webToMap();
556
                        if (mapcrs != null)
557
                                projectionToMap(mapcrs,true);
558
                        Iterator iter = layers.iterator();
559
                        while(iter.hasNext()) {
560
                                MapLayer l = (MapLayer) iter.next();
561
                                l.layerToMap();
562
                        }
563
                        tabOut();
564
                        toMapln("END # Map File");
565
                        closeMapFile();
566
                }
567
                
568
                void openMapFile(String donde) {
569
                        setMapFileName(donde);
570
                        if (mapFileName == null)
571
                                return;
572
                        File wmsmap = new File( mapFileName );
573
                        try{
574
                                //salida = new PrintStream(new BufferedOutputStream(new FileOutputStream(wmsmap)));
575
                                salida = new PrintStream(new FileOutputStream(wmsmap));        
576
                        } catch(FileNotFoundException e){
577
                                System.out.println("Fichero no encontrado.");
578
                        }
579
                        System.out.println(mapFileName+" abierto.");            
580
                        
581
                }
582
                
583
                void closeMapFile() {
584
                        if (salida != null) {
585
                                salida.flush();
586
                                salida.close();
587
                        }
588
                }
589
                
590
                public String getMapFileName() {
591
                        return mapFileName;
592
                }
593
                
594
                public void setMapFileName(String mapFileName) {
595
                        this.mapFileName = mapFileName;
596
                }
597
        }
598
}