Statistics
| Revision:

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

History | View | Annotate | Download (9.88 KB)

1 6948 luisw2
package org.gvsig.remoteservices.conf.mapserver;
2
3
import java.awt.Dimension;
4
import java.awt.Rectangle;
5
import java.io.File;
6
import java.io.FileNotFoundException;
7
import java.io.FileOutputStream;
8
import java.io.PrintStream;
9
import java.util.ArrayList;
10
import java.util.Iterator;
11
12
import org.gvsig.remoteservices.conf.mapserver.MapServer.CRS;
13
14
15
/**
16
 * Genera un .MAP
17
 * ... como este:
18
 MAP
19
        NAME test_postgis
20
        STATUS ON
21
        SIZE 400 300
22
        #SYMBOLSET ../etc/symbols.sym
23
        EXTENT 638610.4375 4222780 789330 4484662.5
24
        UNITS METERS
25
        SHAPEPATH "/home/shapes/"
26
        IMAGECOLOR 255 255 255
27
        #FONTSET ../etc/fonts.txt
28

29
        WEB
30
                #  IMAGEPATH "/ms4w/tmp/ms_tmp/"
31
                #  IMAGEURL "/ms_tmp/"
32
                 METADATA
33
                            "wms_title"     "test postgis"  ##required
34
                            "wms_onlineresource" "http://localhost/mapserver/mapserv"   ##required
35
                            "wms_srs"       "EPSG:42304 EPSG:42101 EPSG:4269 EPSG:4326 EPSG:23030"  ##recommended
36
                  END
37
        END
38

39
        PROJECTION
40
                "init=epsg:23030"   ##required
41
        END
42

43
        #
44
        # Start of layer definitions
45
        #
46

47
        LAYER
48
                  NAME "autopistas"
49
                  DATA "the_geom from autopistas"
50
                CONNECTIONTYPE POSTGIS
51
                CONNECTION "user=david password=chkdsk dbname=betel host=localhost port=5434"
52
                STATUS ON
53
                TYPE LINE
54
                METADATA
55
                            "wms_title"  "Autopistas GV"   ##required
56
                            "wms_extent" "638610.4375 4222780 789330 4484662.5"
57
                  END
58
                  PROJECTION
59
                            "init=epsg:23030"   ##recommended
60
                  END
61
                  CLASS
62
                    NAME "Autopistas"
63
                    STYLE
64
                            COLOR 200 255 0
65
                            OUTLINECOLOR 120 120 120
66
                    END
67
              END
68
    END
69

70
END # Map File
71

72
 * @author david
73
 */
74
75
public abstract class MapServer {
76
        public static class RGB {
77
                int r,g,b;
78
                public RGB(int red,int green,int blue){
79
                        r=red;
80
                        g=green;
81
                        b=blue;
82
                }
83
                public String toString(){
84
                        return ""+r+" "+g+" "+b;
85
                }
86
        }
87
88
        public static class CRS extends MapServer {
89
                boolean init = false;
90
                String crs = "";
91
                public CRS(String crs, boolean init) {
92
                        this.crs = crs;
93
                        this.init =init;
94
                }
95
96
                public String toString() {
97
                        String str = "";
98
                        if (init)
99
                                str += "init=";
100
                        return str+crs.toLowerCase();
101
                }
102
103
                public void toMap() {
104
                        toMapln("PROJECTION");
105
                        tabIn();
106
                        toMapln("\""+this+"\"");
107
                        tabOut();
108
                        toMapln("END");
109
                }
110
        }
111
112
        static PrintStream salida = null;
113
        static String tabstop = "";
114
        public Rectangle extent = null;
115
        public CRS crs = null;
116
117
        public void setExtent(double minx, double miny, double maxx, double maxy) {
118
                extent = new Rectangle();
119
                extent.setFrameFromDiagonal(minx, miny, maxx, maxy);
120
        }
121
122
        String extentToMapString(Rectangle ext) {
123
                if (ext != null)
124
                        return ext.getMinX()+" "+ext.getMinY()+" "+ext.getMaxX()+" "+ext.getMaxY();
125
                return "";
126
        }
127
128
        void projectionToMap(CRS prj, boolean init) {
129
                if (prj == null)
130
                        return;
131
                toMapln("PROJECTION");
132
                if (init == true)
133
                        prj.toString();
134
                tabIn();
135
                toMapln("\""+prj+"\"");
136
                tabOut();
137
                toMapln("END");
138
        }
139
140
        public String replicate(String str, int n) {
141
                int i;
142
                String ret = "";
143
                for(i=0;i<n;i++){
144
                        ret += str;
145
                }
146
                return ret;
147
        }
148
149
        public void tabIn() {
150
                tabstop += "   ";
151
        }
152
153
        public void tabOut() {
154
                tabstop = tabstop.substring(0,tabstop.length()-3);
155
        }
156
157
        void toMapln(String str){
158
                if (salida == null)
159
                        System.out.println(tabstop+str);
160
                else
161
                        salida.println(tabstop+str);
162
        }
163
164
        public static class Metadata extends MapServer {
165
                public String prefix = "wms";
166
                public String title = null;
167
168
                public void setServiceAsWFS() {
169
                        prefix = "wfs";
170
                }
171
172
                public void setServiceAsWMS() {
173
                        prefix = "wms";
174
                }
175
                public void setServiceAsWCS() {
176
                        prefix = "wcs";
177
                }
178
//
179
                void startMetadataToMap(){
180
                        toMapln("METADATA");
181
                        tabIn();
182
                        if (title != null)
183
                                toMapln("\""+prefix+"_title\" " +"\""+title+"\"");
184
                }
185
186
                void endMetadataToMap() {
187
                        tabOut();
188
                        toMapln("END");
189
                }
190
        }
191
192
        public static class MetadataWeb extends Metadata {
193
                //Atributos wms_xxxx WEB
194
                public String onlineresource = null;
195
                public String ows_schemas_location = null;
196
                void metadataToMap(){
197
                        startMetadataToMap();
198
                        toMapln("\""+prefix+"_onlineresource\" \""+onlineresource+"\"");
199
                        toMapln("\""+prefix+"_srs\" \""+crs+"\"");
200
                        if (ows_schemas_location != null)
201
                                toMapln("\"ows_schemas_location\" \""+ows_schemas_location+"\"");
202
                        endMetadataToMap();
203
                }
204
        }
205
206
        public static class MetadataLayer extends Metadata {
207
                //Atributos wms_xxxx LAYER
208
                public String metalayertitle = null;
209
                public String gml_include_items = null;
210
                void metadataToMap(){
211
                        startMetadataToMap();
212
                        if (extent != null)
213
                                toMapln("\""+prefix+"_extent\" \""+extentToMapString(extent)+"\"");
214
                        if (gml_include_items != null)
215
                                toMapln("\"gml_include_items\" \""+gml_include_items+"\"");
216
                        endMetadataToMap();
217
                }
218
        }
219
220
        public static class StyleMap extends MapServer {
221
                public RGB styleColor = null;
222
                public RGB styleColorOutline = null;
223
                public StyleMap(RGB line, RGB fill) {
224
                        styleColor = fill;
225
                        styleColorOutline = line;
226
                }
227
                void styleToMap(){
228
                        toMapln("STYLE ");
229
                        tabIn();
230
                        toMapln("COLOR "+styleColor);
231
                        toMapln("OUTLINECOLOR "+styleColorOutline);
232
                        tabOut();
233
                        toMapln("END");
234
                }
235
        }
236
237
        public static class WebMap extends MapServer{
238
                public MetadataWeb metadata = null;
239
                public String imagepath = null;
240
                public String imageurl = null;
241
                void webToMap(){
242
                        toMapln("WEB");
243
                        tabIn();
244
                        toMapln("IMAGEPATH \""+imagepath+"\"");
245
                        toMapln("IMAGEURL \""+imageurl+"\"");
246
                        metadata.metadataToMap();
247
                        tabOut();
248
                        toMapln("END");
249
                }
250
        }
251
252
        public static class MapClass extends MapServer {
253
                public String name;
254
                public StyleMap estilo = null;
255
                public String template = null;
256
                public MapClass(String n) {
257
                        name = n;
258
                }
259
                public void classToMap(){
260
                        toMapln("CLASS");
261
                        tabIn();
262
                        toMapln("NAME \""+name+"\"");
263
                        if (estilo != null)
264
                                estilo.styleToMap();
265
                        if (template != null)
266
                                toMapln("TEMPLATE "+template);
267
                        tabOut();
268
                        toMapln("END");
269
                }
270
        }
271
272
        public abstract static class MapLayer extends MapServer {
273
                public String name;
274
                public String title;
275
                public String status = "ON";
276
                public String type = null;
277
                public MapClass layerClass = null;
278
                public String data;
279
                public MetadataLayer metadata = null;
280
                public boolean dump = false;
281
                public CRS layercrs=null;
282
283
                public void setDump(boolean d) {
284
                        dump = d;
285
                }
286
287
                void startToMap() {
288
                        toMapln("LAYER");
289
                        tabIn();
290
                        toMapln("NAME \""+name+"\"");
291
                        toMapln("STATUS "+status);
292
                        if (type != null)
293
                                toMapln("TYPE "+type);
294
                        if (dump)
295
                                toMapln("DUMP TRUE # required");
296
                }
297
298
                void endToMap() {
299
                        tabOut();
300
                        toMapln("END # Layer");
301
                }
302
303
                public abstract void layerToMap();
304
        }
305
306
        public static class ShpLayer extends MapLayer {
307
                public void layerToMap() {
308
                        startToMap();
309
                        toMapln("DATA \""+data+"\"");
310
                        metadata.metadataToMap();
311
                        if (layercrs != null) layercrs.toMap();
312
                        if (layerClass != null)
313
                                layerClass.classToMap();
314
                        endToMap();
315
                }
316
        }
317
318
        public static class PostgisLayer extends MapLayer {
319
                public String user;
320
                public String pass;
321
                public String host;
322
                public String dbname;
323
                public String port;
324
325
                public String tabla;
326
                public String data;
327
328
                public void setConnParams(String user, String pass, String host,
329
                                String dbname, String port) {
330
                        this.user = user;
331
                        this.pass = pass;
332
                        this.host = host;
333
                        this.dbname = dbname;
334
                        this.port = port;
335
                }
336
337
                public void layerToMap() {
338
                        startToMap();
339
                        toMapln("DATA \""+data+"\"");
340
                        toMapln("CONNECTIONTYPE POSTGIS");
341
                        toMapln("CONNECTION \"user="+user+" password="+pass+" dbname="+dbname+" host="+host+" port="+port+"\"");
342
                        metadata.metadataToMap();
343
                        if (layercrs != null) layercrs.toMap();
344
                        if (layerClass != null)
345
                                layerClass.classToMap();
346
                        endToMap();
347
                }
348
        }
349
350
        public static class RasterLayer extends MapLayer {
351
                public void layerToMap() {
352
                        startToMap();
353
                        toMapln("DATA \""+data+"\"");
354
                        metadata.metadataToMap();
355
                        if (layercrs != null) layercrs.toMap();
356
                        if (layerClass != null)
357
                                layerClass.classToMap();
358
                        endToMap();
359
                }
360
        }
361
362
        public static class ConfigFile extends MapServer {
363
                /**
364
                 * Atributos wms_xxxx comunes a WEB y LAYER.
365
                 * @author david
366
                 */
367
368
                public String mapFileName = null;
369
                public String fName = null;
370
                public String mapName = null;
371
                public String mapStatus = null;
372
                public String mapUnits= null;
373
                public String mapShapePath;
374
                public Dimension mapSize = null;
375
                public CRS mapcrs= null;
376
                public WebMap www = null;
377
                public String symbolset = null;
378
                public String fontset = null;
379
                public RGB imageColor = null;
380
381
                public ArrayList layers = new ArrayList();
382
383
                public ConfigFile() {
384
                }
385
386
                public void generate() {
387
                        generate(null);
388
                }
389
390
                public void generate(String donde) {
391
                        openMapFile(donde);
392
                        toMapln("MAP");
393
                        tabIn();
394
                        toMapln("NAME "+mapName);
395
                        if (mapSize != null)
396
                                toMapln("SIZE "+mapSize.width+" "+mapSize.height);
397
                        toMapln("EXTENT "+extentToMapString(extent));
398
                        toMapln("STATUS "+mapStatus);
399
                        toMapln("UNITS "+mapUnits);
400
                        toMapln("SHAPEPATH \""+mapShapePath+"\"");
401
                        if (symbolset != null)
402
                                toMapln("SYMBOLSET "+symbolset);
403
                        if (symbolset != null)
404
                                toMapln("FONTSET "+symbolset);
405
                        if (imageColor != null)
406
                                toMapln("IMAGECOLOR \""+imageColor);
407
                        www.webToMap();
408
                        if (mapcrs != null)
409
                                projectionToMap(mapcrs,true);
410
                        Iterator iter = layers.iterator();
411
                        while(iter.hasNext()) {
412
                                MapLayer l = (MapLayer) iter.next();
413
                                l.layerToMap();
414
                        }
415
                        tabOut();
416
                        toMapln("END # Map File");
417
                        closeMapFile();
418
                }
419
420
                void openMapFile(String donde) {
421
                        setMapFileName(donde);
422
                        if (mapFileName == null)
423
                                return;
424
                        File wmsmap = new File( mapFileName );
425
                        try{
426
                                //salida = new PrintStream(new BufferedOutputStream(new FileOutputStream(wmsmap)));
427
                                salida = new PrintStream(new FileOutputStream(wmsmap));
428
                        } catch(FileNotFoundException e){
429
                                System.out.println("Fichero no encontrado.");
430
                        }
431
                        System.out.println(mapFileName+" abierto.");
432
433
                }
434
435
                void closeMapFile() {
436
                        if (salida != null)
437
                                salida.close();
438
                }
439
440
                public String getMapFileName() {
441
                        return mapFileName;
442
                }
443
444
                public void setMapFileName(String mapFileName) {
445
                        this.mapFileName = mapFileName;
446
                }
447
        }
448
}