Statistics
| Revision:

svn-gvsig-desktop / trunk / extensions / extPublish / src / com / iver / cit / gvsig / publish / PublishWMSControler.java @ 7102

History | View | Annotate | Download (10.2 KB)

1
package com.iver.cit.gvsig.publish;
2

    
3
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
4
 *
5
 * Copyright (C) 2004 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
import java.awt.Color;
44
import java.awt.geom.Rectangle2D;
45
import java.util.ArrayList;
46

    
47
import org.gvsig.remoteservices.conf.mapserver.MapServer;
48
import org.gvsig.remoteservices.conf.mapserver.MapServer.CRS;
49
import org.gvsig.remoteservices.conf.mapserver.MapServer.ConfigFile;
50
import org.gvsig.remoteservices.conf.mapserver.MapServer.MapClass;
51
import org.gvsig.remoteservices.conf.mapserver.MapServer.MapLayer;
52
import org.gvsig.remoteservices.conf.mapserver.MapServer.PostgisLayer;
53
import org.gvsig.remoteservices.conf.mapserver.MapServer.RGB;
54
import org.gvsig.remoteservices.conf.mapserver.MapServer.ShpLayer;
55

    
56
import com.iver.andami.PluginServices;
57
import com.iver.cit.gvsig.fmap.DriverException;
58
import com.iver.cit.gvsig.fmap.MapControl;
59
import com.iver.cit.gvsig.fmap.core.FShape;
60
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
61
import com.iver.cit.gvsig.fmap.drivers.jdbc.postgis.PostGisDriver;
62
import com.iver.cit.gvsig.fmap.drivers.shp.IndexedShpDriver;
63
import com.iver.cit.gvsig.fmap.layers.FLayer;
64
import com.iver.cit.gvsig.fmap.layers.FLayers;
65
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
66
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
67
import com.iver.cit.gvsig.fmap.layers.VectorialFileAdapter;
68
import com.iver.cit.gvsig.fmap.rendering.Legend;
69
import com.iver.cit.gvsig.fmap.rendering.SingleSymbolLegend;
70
import com.iver.cit.gvsig.fmap.rendering.VectorialIntervalLegend;
71
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
72
import com.iver.cit.gvsig.gui.View;
73
import com.iver.cit.gvsig.publish.layers.LayerFactory;
74

    
75
/**
76
 * This class implements the mapserver wms publication use case. Responsability:
77
 * This class must create the dialog, get the specific mapserver parameters and
78
 * get the view atributes in order to
79
 * 
80
 * @author jvhigon
81
 * 
82
 */
83
public class PublishWMSControler {
84
        private View view = null;
85

    
86
        private MapControl mapCtrl = null;
87

    
88
        private FLayers layers = null;
89

    
90
        /**
91
         * Constructor
92
         * 
93
         */
94
        public PublishWMSControler() {
95
                view = (View) PluginServices.getMDIManager().getActiveWindow();
96
                mapCtrl = view.getMapControl();
97
                layers = mapCtrl.getMapContext().getLayers();
98
        }
99

    
100
        /**
101
         * 
102
         * @param url
103
         * @param path
104
         * @param mapFile
105
         * @param symbolsFile
106
         * @param fontsFile
107
         * @param ImageURL
108
         * @param imagePath
109
         */
110
        public void publish(String onlineResource, String path, String mapFile,
111
                        String symbolsFile, String fontsFile, String ImageURL,
112
                        String imagePath) {
113
                
114
                ConfigFile map = getConfigFile(path);                
115
                map.www = getWebMap(onlineResource,imagePath,ImageURL);
116

    
117
                for (int i = 0; i < layers.getLayersCount(); i++) {
118
                        FLayer lyr = layers.getLayer(i);
119
                        MapLayer mapLayer = createLayer(lyr);
120
                        if (lyr != null){
121
                                map.layers.add(mapLayer);
122
                        }
123

    
124
                } 
125
                map.generate(mapFile);                
126
        }
127
        
128
        private MapLayer createLayer(FLayer layer){
129
                MapLayer mLayer = LayerFactory.getMapLayer(layer);
130
                mLayer.name = getLayerName(layer.getName());
131
                mLayer.title = layer.getName();
132
                if (layer instanceof FLyrVect){
133
                        createLayerVect((FLyrVect)layer,mLayer);
134
                }
135
                return mLayer;
136
        }
137
        
138
        private void createLayerVect(FLyrVect lyrVect,MapLayer mLayer){
139
                ReadableVectorial rv = lyrVect.getSource();
140
                if (rv instanceof VectorialFileAdapter) {
141
                        VectorialFileAdapter vfa = (VectorialFileAdapter) rv;
142
                        mLayer.data = vfa.getFile().getName();
143
                }
144
                
145
                mLayer.type = getShpType(lyrVect);
146
                mLayer.classList = getMapClassList(lyrVect);
147
                mLayer.metadata = new ConfigFile.MetadataLayer();
148
                mLayer.transparency = 255 - lyrVect.getLegend().getDefaultSymbol().getColor().getAlpha();                                
149
                        
150
                try {
151
                        mLayer.metadata.setExtent(lyrVect.getFullExtent());
152
                } catch (DriverException e) {
153
                        // TODO Auto-generated catch block
154
                        e.printStackTrace();
155
                }                
156
        }
157
        
158
        private ArrayList getMapClassList(FLyrVect lyrVect){
159
                ArrayList mapClasses = new ArrayList();
160
                Legend legend = lyrVect.getLegend();        
161
                if (legend instanceof SingleSymbolLegend){
162
                        MapClass mapClass = new MapClass(getLayerName(lyrVect.getName()));
163
                        Color clr = legend.getDefaultSymbol().getColor();
164
                        Color outLineclr = legend.getDefaultSymbol().getOutlineColor();
165
                        mapClass.estilo = new ConfigFile.StyleMap(new RGB(outLineclr.getRed(),
166
                                        outLineclr.getGreen(), outLineclr.getBlue()), new RGB(clr.getRed(),
167
                                                        clr.getGreen(), clr.getBlue()));
168
                        mapClasses.add(mapClass);
169
                }else if(legend instanceof VectorialUniqueValueLegend){
170
                        VectorialUniqueValueLegend uniqueValueLegend = (VectorialUniqueValueLegend)legend;
171
                        FSymbol[] symbols = uniqueValueLegend.getSymbols();
172
                        for (int i=0 ; i<symbols.length ; i++){
173
                                MapClass mapClass = new MapClass(symbols[i].getDescription());
174
                                Color clr = symbols[i].getColor();
175
                                Color outLineclr = symbols[i].getOutlineColor();
176
                                mapClass.estilo = new ConfigFile.StyleMap(new RGB(outLineclr.getRed(),
177
                                                outLineclr.getGreen(), outLineclr.getBlue()), new RGB(clr.getRed(),
178
                                                                clr.getGreen(), clr.getBlue()));
179
                                mapClass.expression = "('[" + uniqueValueLegend.getFieldName() + "]' = '" + 
180
                                                symbols[i].getDescription() + "')";
181
                                mapClasses.add(mapClass);
182
                        }
183
                        
184
                }else if(legend instanceof VectorialIntervalLegend){
185
                        MapClass mapClass = new MapClass(getLayerName(lyrVect.getName()));
186
                        Color clr = legend.getDefaultSymbol().getColor();
187
                        Color outLineclr = legend.getDefaultSymbol().getOutlineColor();
188
                        mapClass.estilo = new ConfigFile.StyleMap(new RGB(outLineclr.getRed(),
189
                                        outLineclr.getGreen(), outLineclr.getBlue()), new RGB(clr.getRed(),
190
                                                        clr.getGreen(), clr.getBlue()));
191
                        mapClasses.add(mapClass);
192
                }                        
193
                return mapClasses;
194
        }
195
        
196
        private ConfigFile getConfigFile(String path){
197
                ConfigFile map = new ConfigFile();
198
                map.mapName = "TEST_MAPSERVER";
199
                map.mapStatus = "ON";
200
                map.mapUnits = "METERS";
201
                map.mapShapePath = path;
202
                map.mapcrs = new CRS(mapCtrl.getMapContext().getViewPort()
203
                                .getProjection().getAbrev(), true);
204
                map.setExtent(mapCtrl.getMapContext().getViewPort().getExtent());
205
                return map;
206
        }
207
        
208
        private MapServer.WebMap getWebMap(String onlineResource,
209
                        String imagePath,
210
                        String imageURL){
211
                MapServer.WebMap webMap = new MapServer.WebMap();
212
                webMap.imagepath = imagePath;
213
                webMap.imageurl = imageURL;
214
                webMap.metadata = new MapServer.MetadataWeb();
215
                webMap.metadata.crs = new CRS(mapCtrl.getMapContext().getViewPort()
216
                                .getProjection().getAbrev(), false);
217

    
218
                webMap.metadata.title = "test shape";
219
                webMap.metadata.onlineresource = onlineResource;
220
                return webMap;
221
        }
222
        
223
        /**
224
         * Remove the "." from the file name
225
         * @param file
226
         * @return
227
         */
228
        private String getLayerName(String file){
229
                String[] s = file.split("\\.");
230
                if ((s != null) && (s.length > 0)){
231
                        return s[0];
232
                }
233
                return file;
234
        }
235
        
236
        /**
237
         * Gets the geometry type from a shape
238
         * @param lyrVect
239
         * @return
240
         */
241
        private String getShpType(FLyrVect lyrVect){
242
                try {
243
                        int lyrType = lyrVect.getShapeType();
244
                        switch(lyrType){
245
                        case (FShape.POLYGON):
246
                                return MapServer.SHP_TYPE_POLYGON;
247
                        case FShape.LINE:                                
248
                                return MapServer.SHP_TYPE_LINE;
249
                        case (FShape.POINT | FShape.MULTIPOINT):
250
                                return MapServer.SHP_TYPE_POINT;
251
                        }
252
                } catch (DriverException e) {
253
                        // TODO Auto-generated catch block
254
                        e.printStackTrace();
255
                }
256
                return MapServer.SHP_TYPE_POLYGON;
257
        }
258
}
259

    
260
// print specific mapserver's atributes
261
/*
262
 * System.out.println("Ruta mapFile:"+ mapFile); System.out.println("Ruta
263
 * symbolsFile:" + symbolsFile); System.out.println("Ruta fonstFile:" +
264
 * fontsFile); System.out.println("Ruta onlineResource:" + onlineResource );
265
 * System.out.println("Ruta onlineResource:" + shapePath);
266
 * 
267
 * //print atributes for all layers for (int i= 0; i < layers.getLayersCount();
268
 * i++ ){ FLayer lyr= layers.getLayer(i); //name layer
269
 * System.out.println("Nombre de la capa: " + lyr.getName());
270
 * 
271
 * //vectorial file path FLyrVect lyrvec = (FLyrVect) lyr; ReadableVectorial rv =
272
 * lyrvec.getSource(); if (rv instanceof VectorialFileAdapter){
273
 * VectorialFileAdapter vfa = (VectorialFileAdapter) rv;
274
 * System.out.println("Absolute Path: " + vfa.getFile().getAbsoluteFile());
275
 * System.out.println("File Name: " + vfa.getFile().getName()); }
276
 *  // extext Rectangle2D rect = null; try { rect = lyr.getFullExtent(); } catch
277
 * (DriverException e) { // TODO Auto-generated catch block e.printStackTrace(); }
278
 * System.out.println("Extent: " + rect.getMinX() + " " + rect.getMinY() + " " +
279
 * rect.getMaxX() + " " + rect.getMaxY());
280
 * 
281
 * 
282
 * //maxscale System.out.println("Maxscale: " + lyr.getMaxScale());
283
 * 
284
 * //minscale System.out.println("Minscale: " + lyr.getMinScale());
285
 * 
286
 * //color and transparency if(lyr instanceof FLyrVect){
287
 * System.out.println("This is a vectorial datasource"); FLyrVect lyrvec2 =
288
 * (FLyrVect) lyr; Color clr =
289
 * lyrvec2.getLegend().getDefaultSymbol().getColor(); System.out.println("R: " +
290
 * clr.getRed() + " G: " + clr.getGreen() + " B: " + clr.getBlue());
291
 * 
292
 * int alpha = lyrvec2.getLegend().getDefaultSymbol().getColor().getAlpha();
293
 * System.out.println("Tranparency: " + (255 - alpha));
294
 * 
295
 * }else{ System.out.println("Esto no es una capa vectorial"); }
296
 * 
297
 * //srs viewPort
298
 * System.out.println(mapCtrl.getMapContext().getViewPort().getProjection().getAbrev());
299
 */