Statistics
| Revision:

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

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

    
67

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

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

92
        PROJECTION
93
                "init=epsg:23030"   ##required
94
        END
95

96
        #
97
        # Start of layer definitions
98
        #
99

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

123
END # Map File    
124

125
 * @author david
126
 */
127

    
128
public abstract class MapServer {
129
        public static final String SHP_TYPE_LINE = "LINE";
130
        public static final String SHP_TYPE_POLYLINE = "POLYLINE";
131
        public static final String SHP_TYPE_POINT = "POINT";
132
        public static final String SHP_TYPE_POLYGON = "POLYGON"; 
133
        
134
        public static class RGB {
135
                int r,g,b;
136
                public RGB(int red,int green,int blue){
137
                        r=red;
138
                        g=green;
139
                        b=blue;                        
140
                }
141
                public String toString(){
142
                        return ""+r+" "+g+" "+b;
143
                }
144
        }
145
        
146
        public static class CRS extends MapServer {
147
                boolean init = false;
148
                String crs = "";
149
                public CRS(String crs, boolean init) {
150
                        this.crs = crs;
151
                        this.init =init;
152
                }
153
                
154
                public String toString() {
155
                        String str = "";
156
                        if (init)
157
                                str += "init=";
158
                        return str+crs.toLowerCase();
159
                }
160
                
161
                public void toMap() {
162
                        toMapln("PROJECTION");
163
                        tabIn();
164
                        toMapln("\""+this+"\"");
165
                        tabOut();
166
                        toMapln("END");
167
                }
168
        }
169
        
170
        static PrintStream salida = null;
171
        static String tabstop = "";
172
        public Rectangle2D extent = null;
173
        public CRS crs = null;
174
        
175
        public void setExtent(double minx, double miny, double maxx, double maxy) {
176
                this.extent = new Rectangle2D.Double(minx, miny, maxx, maxy);
177
        }
178

    
179
        public void setExtent(Rectangle2D extent) {
180
                this.extent = extent;
181
        }
182

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