Statistics
| Revision:

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

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

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

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

112
        PROJECTION
113
                "init=epsg:23030"   ##required
114
        END
115

116
        #
117
        # Start of layer definitions
118
        #
119

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

143
END # Map File    
144

145
 * @author david
146
 */
147

    
148
public abstract class MapServer {
149
        public static final String SHP_TYPE_LINE = "LINE";
150
        public static final String SHP_TYPE_POLYLINE = "POLYLINE";
151
        public static final String SHP_TYPE_POINT = "POINT";
152
        public static final String SHP_TYPE_POLYGON = "POLYGON"; 
153
        
154
        public static class RGB {
155
                int r,g,b;
156
                public RGB(int red,int green,int blue){
157
                        r=red;
158
                        g=green;
159
                        b=blue;                        
160
                }
161
                public String toString(){
162
                        return ""+r+" "+g+" "+b;
163
                }
164
        }
165
        
166
        public static class CRS extends MapServer {
167
                boolean init = false;
168
                String crs = "";
169
                public CRS(String crs, boolean init) {
170
                        this.crs = crs;
171
                        this.init =init;
172
                }
173
                
174
                public String toString() {
175
                        String str = "";
176
                        if (init)
177
                                str += "init=";
178
                        return str+crs; //LWS .toLowerCase();
179
                }
180
                
181
                public void toMap() {
182
                        toMapln("PROJECTION");
183
                        tabIn();
184
                        toMapln("\""+this+"\"");
185
                        tabOut();
186
                        toMapln("END");
187
                }
188

    
189
                /**
190
                 * @return Returns the crs.
191
                 */
192
                public String getCrs() {
193
                        return crs;
194
                }
195
        }
196
        
197
        static PrintStream salida = null;
198
        static String tabstop = "";
199
        public Rectangle2D extent = null;
200
        public CRS crs = null;
201
        
202
        public void setExtent(double minx, double miny, double maxx, double maxy) {
203
                this.extent = new Rectangle2D.Double();
204
                this.extent.setFrameFromDiagonal(minx, miny, maxx, maxy);
205
        }
206

    
207
        public void setExtent(Rectangle2D extent) {
208
                this.extent = extent;
209
        }
210

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

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