Statistics
| Revision:

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

History | View | Annotate | Download (11.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 7096 2006-09-07 11:19:40Z jvhigon $
46
 * $Log$
47
 * Revision 1.4  2006-09-07 11:19:40  jvhigon
48
 * Añadido soporte para multiples class en una layer, y para layer sdentro de layer
49
 *
50
 * Revision 1.3  2006/09/01 06:59:00  luisw2
51
 * Headers mofification
52
 *
53
 */
54
import java.awt.Dimension;
55
import java.awt.Rectangle;
56
import java.io.File;
57
import java.io.FileNotFoundException;
58
import java.io.FileOutputStream;
59
import java.io.PrintStream;
60
import java.util.ArrayList;
61
import java.util.Iterator;
62

    
63

    
64
/**
65
 * Genera un .MAP
66
 * ... como este:
67
 MAP
68
        NAME test_postgis
69
        STATUS ON
70
        SIZE 400 300
71
        #SYMBOLSET ../etc/symbols.sym
72
        EXTENT 638610.4375 4222780 789330 4484662.5
73
        UNITS METERS
74
        SHAPEPATH "/home/shapes/"
75
        IMAGECOLOR 255 255 255
76
        #FONTSET ../etc/fonts.txt
77

78
        WEB
79
                #  IMAGEPATH "/ms4w/tmp/ms_tmp/"
80
                #  IMAGEURL "/ms_tmp/"
81
                 METADATA
82
                            "wms_title"     "test postgis"  ##required
83
                            "wms_onlineresource" "http://localhost/mapserver/mapserv"   ##required
84
                            "wms_srs"       "EPSG:42304 EPSG:42101 EPSG:4269 EPSG:4326 EPSG:23030"  ##recommended
85
                  END
86
        END
87

88
        PROJECTION
89
                "init=epsg:23030"   ##required
90
        END
91

92
        #
93
        # Start of layer definitions
94
        #
95

96
        LAYER
97
                  NAME "autopistas"
98
                  DATA "the_geom from autopistas"
99
                CONNECTIONTYPE POSTGIS
100
                CONNECTION "user=david password=chkdsk dbname=betel host=localhost port=5434"
101
                STATUS ON
102
                TYPE LINE
103
                METADATA
104
                            "wms_title"  "Autopistas GV"   ##required
105
                            "wms_extent" "638610.4375 4222780 789330 4484662.5"
106
                  END
107
                  PROJECTION
108
                            "init=epsg:23030"   ##recommended
109
                  END
110
                  CLASS
111
                    NAME "Autopistas"
112
                    STYLE
113
                            COLOR 200 255 0
114
                            OUTLINECOLOR 120 120 120
115
                    END    
116
              END
117
    END 
118

119
END # Map File    
120

121
 * @author david
122
 */
123

    
124
public abstract class MapServer {
125
        public static class RGB {
126
                int r,g,b;
127
                public RGB(int red,int green,int blue){
128
                        r=red;
129
                        g=green;
130
                        b=blue;                        
131
                }
132
                public String toString(){
133
                        return ""+r+" "+g+" "+b;
134
                }
135
        }
136
        
137
        public static class CRS extends MapServer {
138
                boolean init = false;
139
                String crs = "";
140
                public CRS(String crs, boolean init) {
141
                        this.crs = crs;
142
                        this.init =init;
143
                }
144
                
145
                public String toString() {
146
                        String str = "";
147
                        if (init)
148
                                str += "init=";
149
                        return str+crs.toLowerCase();
150
                }
151
                
152
                public void toMap() {
153
                        toMapln("PROJECTION");
154
                        tabIn();
155
                        toMapln("\""+this+"\"");
156
                        tabOut();
157
                        toMapln("END");
158
                }
159
        }
160
        
161
        static PrintStream salida = null;
162
        static String tabstop = "";
163
        public Rectangle extent = null;
164
        public CRS crs = null;
165
        
166
        public void setExtent(double minx, double miny, double maxx, double maxy) {
167
                extent = new Rectangle();
168
                extent.setFrameFromDiagonal(minx, miny, maxx, maxy);
169
        }
170
        
171
        String extentToMapString(Rectangle ext) {
172
                if (ext != null)
173
                        return ext.getMinX()+" "+ext.getMinY()+" "+ext.getMaxX()+" "+ext.getMaxY();
174
                return "";
175
        }
176
        
177
        void projectionToMap(CRS prj, boolean init) {
178
                if (prj == null)
179
                        return;
180
                toMapln("PROJECTION");
181
                if (init == true)
182
                        prj.toString();
183
                tabIn();
184
                toMapln("\""+prj+"\"");
185
                tabOut();
186
                toMapln("END");
187
        }
188
        
189
        public String replicate(String str, int n) {
190
                int i;
191
                String ret = "";
192
                for(i=0;i<n;i++){
193
                        ret += str;
194
                }
195
                return ret;
196
        }
197
        
198
        public void tabIn() {
199
                tabstop += "   ";
200
        }
201
        
202
        public void tabOut() {
203
                tabstop = tabstop.substring(0,tabstop.length()-3);
204
        }
205
        
206
        void toMapln(String str){
207
                if (salida == null)
208
                        System.out.println(tabstop+str);
209
                else
210
                        salida.println(tabstop+str);        
211
        }
212
        
213
        public static class Metadata extends MapServer {
214
                public String prefix = "wms";
215
                public String title = null;
216
                
217
                public void setServiceAsWFS() {
218
                        prefix = "wfs";
219
                }
220
                
221
                public void setServiceAsWMS() {
222
                        prefix = "wms";
223
                }
224
                public void setServiceAsWCS() {
225
                        prefix = "wcs";
226
                }
227
//                
228
                void startMetadataToMap(){
229
                        toMapln("METADATA");
230
                        tabIn();
231
                        if (title != null)
232
                                toMapln("\""+prefix+"_title\" " +"\""+title+"\"");
233
                }
234
                
235
                void endMetadataToMap() {
236
                        tabOut();
237
                        toMapln("END");
238
                }
239
        }
240
        
241
        public static class MetadataWeb extends Metadata {
242
                //Atributos wms_xxxx WEB
243
                public String onlineresource = null;
244
                public String ows_schemas_location = null;
245
                void metadataToMap(){
246
                        startMetadataToMap();                
247
                        toMapln("\""+prefix+"_onlineresource\" \""+onlineresource+"\"");
248
                        toMapln("\""+prefix+"_srs\" \""+crs+"\"");
249
                        if (ows_schemas_location != null)
250
                                toMapln("\"ows_schemas_location\" \""+ows_schemas_location+"\"");
251
                        endMetadataToMap();
252
                }
253
        }
254
        
255
        public static class MetadataLayer extends Metadata {
256
                //Atributos wms_xxxx LAYER        
257
                public String metalayertitle = null;
258
                public String gml_include_items = null;
259
                void metadataToMap(){
260
                        startMetadataToMap();
261
                        if (extent != null)
262
                                toMapln("\""+prefix+"_extent\" \""+extentToMapString(extent)+"\"");
263
                        if (gml_include_items != null)
264
                                toMapln("\"gml_include_items\" \""+gml_include_items+"\"");
265
                        endMetadataToMap();
266
                }
267
        }
268
        
269
        public static class StyleMap extends MapServer {
270
                public RGB styleColor = null;
271
                public RGB styleColorOutline = null;
272
                public StyleMap(RGB line, RGB fill) {
273
                        styleColor = fill;
274
                        styleColorOutline = line;
275
                }
276
                void styleToMap(){
277
                        toMapln("STYLE ");
278
                        tabIn();
279
                        toMapln("COLOR "+styleColor);
280
                        toMapln("OUTLINECOLOR "+styleColorOutline);
281
                        tabOut();
282
                        toMapln("END");
283
                }
284
        }
285
        
286
        public static class WebMap extends MapServer{
287
                public MetadataWeb metadata = null;
288
                public String imagepath = null;
289
                public String imageurl = null;
290
                void webToMap(){
291
                        toMapln("WEB");
292
                        tabIn();
293
                        toMapln("IMAGEPATH \""+imagepath+"\"");
294
                        toMapln("IMAGEURL \""+imageurl+"\"");
295
                        metadata.metadataToMap();
296
                        tabOut();
297
                        toMapln("END");
298
                }
299
        }
300
        
301
        public static class MapClass extends MapServer {
302
                public String name;
303
                public StyleMap estilo = null;
304
                public String template = null;
305
                public MapClass(String n) {
306
                        name = n;
307
                }
308
                public void classToMap(){
309
                        toMapln("CLASS");
310
                        tabIn();
311
                        toMapln("NAME \""+name+"\"");
312
                        if (estilo != null)
313
                                estilo.styleToMap();
314
                        if (template != null)
315
                                toMapln("TEMPLATE "+template);
316
                        tabOut();
317
                        toMapln("END");
318
                }
319
        }
320
        
321
        public abstract static class MapLayer extends MapServer {
322
                public String name;
323
                public String title;
324
                public String status = "ON";
325
                public String type = null;
326
                public ArrayList classList = null;
327
                public ArrayList layerList = null;
328
                public String data;
329
                public MetadataLayer metadata = null;
330
                public boolean dump = false;
331
                public CRS layercrs=null;
332
                
333
                public void setDump(boolean d) {
334
                        dump = d;
335
                }
336
                
337
                void startToMap() {
338
                        toMapln("LAYER");
339
                        tabIn(); 
340
                        toMapln("NAME \""+name+"\"");
341
                        toMapln("STATUS "+status);
342
                        if (type != null)
343
                                toMapln("TYPE "+type);
344
                        if (dump)
345
                                toMapln("DUMP TRUE # required");
346
                }
347
                
348
                void endToMap() {
349
                        tabOut();
350
                        toMapln("END # Layer");
351
                }
352
                
353
                public void addClass(MapClass c) {
354
                        if (classList == null)
355
                                classList = new ArrayList();
356
                        classList.add(c);
357
                }
358
                
359
                public void classListToMap() {
360
                        if (classList != null) {
361
                                Iterator iter = classList.iterator();
362
                                while (iter.hasNext()) {
363
                                        MapClass c = (MapClass) iter.next();
364
                                        c.classToMap();
365
                                }
366
                        }
367
                                
368
                }
369
                
370
                public void addLayer(MapLayer c) {
371
                        if (layerList == null)
372
                                layerList = new ArrayList();
373
                        layerList.add(c);
374
                }
375
                
376
                public void layerListToMap() {
377
                        if (layerList != null) {
378
                                Iterator iter = layerList.iterator();
379
                                tabIn(); 
380
                                while (iter.hasNext()) {
381
                                        MapLayer l = (MapLayer) iter.next();
382
                                        l.layerToMap();
383
                                }
384
                                tabOut();
385
                        }
386
                                
387
                }
388
                
389
                public abstract void layerToMap();
390
        }
391
        
392
        public static class ShpLayer extends MapLayer {                
393
                public void layerToMap() {
394
                        startToMap();
395
                        toMapln("DATA \""+data+"\"");
396
                        metadata.metadataToMap();
397
                        if (layercrs != null) layercrs.toMap();
398
                        classListToMap();
399
                        endToMap();
400
                }
401
        }
402
        
403
        public static class PostgisLayer extends MapLayer {
404
                public String user;
405
                public String pass;
406
                public String host;
407
                public String dbname;
408
                public String port;
409
                
410
                public String tabla;
411
                public String data;
412
                
413
                public void setConnParams(String user, String pass, String host,
414
                                String dbname, String port) {
415
                        this.user = user;
416
                        this.pass = pass;
417
                        this.host = host;
418
                        this.dbname = dbname;
419
                        this.port = port;
420
                }
421
                
422
                public void layerToMap() {
423
                        startToMap();
424
                        toMapln("DATA \""+data+"\"");
425
                        toMapln("CONNECTIONTYPE POSTGIS");
426
                        toMapln("CONNECTION \"user="+user+" password="+pass+" dbname="+dbname+" host="+host+" port="+port+"\"");
427
                        metadata.metadataToMap();
428
                        if (layercrs != null) layercrs.toMap();
429
                        classListToMap();
430
                        endToMap();
431
                }
432
        }
433
        
434
        public static class RasterLayer extends MapLayer {                
435
                public void layerToMap() {
436
                        startToMap();
437
                        toMapln("DATA \""+data+"\"");
438
                        metadata.metadataToMap();
439
                        if (layercrs != null) layercrs.toMap();
440
                        classListToMap();
441
                        endToMap();
442
                }
443
        }
444
        
445
        public static class ConfigFile extends MapServer {
446
                /**
447
                 * Atributos wms_xxxx comunes a WEB y LAYER.
448
                 * @author david
449
                 */
450
                
451
                public String mapFileName = null;
452
                public String fName = null;
453
                public String mapName = null;
454
                public String mapStatus = null;
455
                public String mapUnits= null;
456
                public String mapShapePath; 
457
                public Dimension mapSize = null;
458
                public CRS mapcrs= null;
459
                public WebMap www = null;
460
                public String symbolset = null;
461
                public String fontset = null;
462
                public RGB imageColor = null;
463
                
464
                public ArrayList layers = new ArrayList(); 
465
                
466
                public ConfigFile() {
467
                }
468
                
469
                public void generate() {
470
                        generate(null);
471
                }
472
                
473
                public void generate(String donde) {
474
                        openMapFile(donde);
475
                        toMapln("MAP");
476
                        tabIn();
477
                        toMapln("NAME "+mapName);
478
                        if (mapSize != null)
479
                                toMapln("SIZE "+mapSize.width+" "+mapSize.height);
480
                        toMapln("EXTENT "+extentToMapString(extent));
481
                        toMapln("STATUS "+mapStatus);
482
                        toMapln("UNITS "+mapUnits);
483
                        toMapln("SHAPEPATH \""+mapShapePath+"\"");
484
                        if (symbolset != null)
485
                                toMapln("SYMBOLSET "+symbolset);
486
                        if (symbolset != null)
487
                                toMapln("FONTSET "+symbolset);
488
                        if (imageColor != null)
489
                                toMapln("IMAGECOLOR \""+imageColor);
490
                        www.webToMap();
491
                        if (mapcrs != null)
492
                                projectionToMap(mapcrs,true);
493
                        Iterator iter = layers.iterator();
494
                        while(iter.hasNext()) {
495
                                MapLayer l = (MapLayer) iter.next();
496
                                l.layerToMap();
497
                        }
498
                        tabOut();
499
                        toMapln("END # Map File");
500
                        closeMapFile();
501
                }
502
                
503
                void openMapFile(String donde) {
504
                        setMapFileName(donde);
505
                        if (mapFileName == null)
506
                                return;
507
                        File wmsmap = new File( mapFileName );
508
                        try{
509
                                //salida = new PrintStream(new BufferedOutputStream(new FileOutputStream(wmsmap)));
510
                                salida = new PrintStream(new FileOutputStream(wmsmap));        
511
                        } catch(FileNotFoundException e){
512
                                System.out.println("Fichero no encontrado.");
513
                        }
514
                        System.out.println(mapFileName+" abierto.");            
515
                        
516
                }
517
                
518
                void closeMapFile() {
519
                        if (salida != null)
520
                                salida.close();
521
                }
522
                
523
                public String getMapFileName() {
524
                        return mapFileName;
525
                }
526
                
527
                public void setMapFileName(String mapFileName) {
528
                        this.mapFileName = mapFileName;
529
                }
530
        }
531
}