Statistics
| Revision:

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

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

    
76
/**
77
 * Genera un .MAP
78
 * ... como este:
79
 MAP
80
        NAME test_postgis
81
        STATUS ON
82
        SIZE 400 300
83
        #SYMBOLSET ../etc/symbols.sym
84
        EXTENT 638610.4375 4222780 789330 4484662.5
85
        UNITS METERS
86
        SHAPEPATH "/home/shapes/"
87
        IMAGECOLOR 255 255 255
88
        #FONTSET ../etc/fonts.txt
89

90
        WEB
91
                #  IMAGEPATH "/ms4w/tmp/ms_tmp/"
92
                #  IMAGEURL "/ms_tmp/"
93
                 METADATA
94
                            "wms_title"     "test postgis"  ##required
95
                            "wms_onlineresource" "http://localhost/mapserver/mapserv"   ##required
96
                            "wms_srs"       "EPSG:42304 EPSG:42101 EPSG:4269 EPSG:4326 EPSG:23030"  ##recommended
97
                  END
98
        END
99

100
        PROJECTION
101
                "init=epsg:23030"   ##required
102
        END
103

104
        #
105
        # Start of layer definitions
106
        #
107

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

131
END # Map File    
132

133
 * @author david
134
 */
135

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

    
187
        public void setExtent(Rectangle2D extent) {
188
                this.extent = extent;
189
        }
190

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