Statistics
| Revision:

root / trunk / extensions / extPublish / src / com / iver / cit / gvsig / publish / servers / mapserver / MapServerLayerFactory.java @ 8117

History | View | Annotate | Download (12.6 KB)

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

    
3
import java.awt.BasicStroke;
4
import java.awt.Color;
5
import java.sql.SQLException;
6
import java.util.ArrayList;
7

    
8
import org.gvsig.remoteservices.conf.mapserver.MapServerConfiguration;
9
import org.gvsig.remoteservices.conf.mapserver.MapServerConfiguration.CRS;
10
import org.gvsig.remoteservices.conf.mapserver.MapServerConfiguration.ConfigFile;
11
import org.gvsig.remoteservices.conf.mapserver.MapServerConfiguration.MapClass;
12
import org.gvsig.remoteservices.conf.mapserver.MapServerConfiguration.MapLayer;
13
import org.gvsig.remoteservices.conf.mapserver.MapServerConfiguration.MetadataLayer;
14
import org.gvsig.remoteservices.conf.mapserver.MapServerConfiguration.PostgisLayer;
15

    
16
import com.iver.cit.gvsig.fmap.DriverException;
17
import com.iver.cit.gvsig.fmap.core.FShape;
18
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
19
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition;
20
import com.iver.cit.gvsig.fmap.drivers.raster.CmsRasterDriver;
21
import com.iver.cit.gvsig.fmap.drivers.shp.IndexedShpDriver;
22
import com.iver.cit.gvsig.fmap.layers.FLayer;
23
import com.iver.cit.gvsig.fmap.layers.FLayerFileVectorial;
24
import com.iver.cit.gvsig.fmap.layers.FLayerGenericVectorial;
25
import com.iver.cit.gvsig.fmap.layers.FLayerJDBCVectorial;
26
import com.iver.cit.gvsig.fmap.layers.FLyrRaster;
27
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
28
import com.iver.cit.gvsig.fmap.layers.ISpatialDB;
29
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
30
import com.iver.cit.gvsig.fmap.layers.VectorialFileAdapter;
31
import com.iver.cit.gvsig.fmap.rendering.Legend;
32
import com.iver.cit.gvsig.fmap.rendering.SingleSymbolLegend;
33
import com.iver.cit.gvsig.fmap.rendering.VectorialIntervalLegend;
34
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
35
import com.iver.cit.gvsig.publish.layers.LayerException;
36
import com.iver.utiles.StringUtilities;
37

    
38
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
39
 *
40
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
41
 *
42
 * This program is free software; you can redistribute it and/or
43
 * modify it under the terms of the GNU General Public License
44
 * as published by the Free Software Foundation; either version 2
45
 * of the License, or (at your option) any later version.
46
 *
47
 * This program is distributed in the hope that it will be useful,
48
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
49
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
50
 * GNU General Public License for more details.
51
 *
52
 * You should have received a copy of the GNU General Public License
53
 * along with this program; if not, write to the Free Software
54
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
55
 *
56
 * For more information, contact:
57
 *
58
 *  Generalitat Valenciana
59
 *   Conselleria d'Infraestructures i Transport
60
 *   Av. Blasco Ib??ez, 50
61
 *   46010 VALENCIA
62
 *   SPAIN
63
 *
64
 *      +34 963862235
65
 *   gvsig@gva.es
66
 *      www.gvsig.gva.es
67
 *
68
 *    or
69
 *
70
 *   IVER T.I. S.A
71
 *   Salamanca 50
72
 *   46005 Valencia
73
 *   Spain
74
 *
75
 *   +34 963163400
76
 *   dac@iver.es
77
 */
78
/* CVS MESSAGES:
79
 *
80
 * $Id: MapServerLayerFactory.java 8117 2006-10-17 07:11:42Z dagilgon $
81
 * $Log$
82
 * Revision 1.13  2006-10-17 07:11:42  dagilgon
83
 * a?adida resolucion para wcs
84
 *
85
 * Revision 1.12  2006/10/16 11:40:36  dagilgon
86
 * arreglado path de mapserver
87
 *
88
 * Revision 1.11  2006/10/05 13:22:23  jvhigon
89
 * Debido al refactor del nobre en la libreria de maapserver y geoserver. Mapserver --> MapserverConfig
90
 *
91
 * Revision 1.10  2006/10/03 15:10:34  jorpiell
92
 * Solo se tienen en cuanta las capas el local (no las remotas)
93
 *
94
 * Revision 1.9  2006/10/03 10:30:14  dagilgon
95
 * adaptation to branch v10
96
 *
97
 * Revision 1.8  2006/09/28 15:04:02  fjp
98
 * Usar siempre que se pueda ISymbol en lugar de FSymbol
99
 *
100
 * Revision 1.7  2006/09/19 00:30:31  luisw2
101
 * Soporte para WCS en mapserver. Pendiente de verificar que va
102
 *
103
 * Revision 1.6  2006/09/13 11:33:37  jorpiell
104
 * Solucionado un problema que hab?a cuando el relleno de las geometr?as es nulo
105
 *
106
 * Revision 1.5  2006/09/11 12:37:39  jorpiell
107
 * El shp tiene una ruta absoluta
108
 *
109
 * Revision 1.4  2006/09/11 12:09:13  jorpiell
110
 * Ya se puede almacenar la ruta donde se encuentra el ejecutable de GDAL
111
 *
112
 * Revision 1.3  2006/09/08 10:50:05  jorpiell
113
 * A?adida la opci?n de generar una capa POSTGIS
114
 *
115
 * Revision 1.2  2006/09/08 09:27:34  jorpiell
116
 * A?adida la exportaci?n de postgis
117
 *
118
 * Revision 1.1  2006/09/08 08:58:53  jorpiell
119
 * Se han renombrado y modificado algunas clases
120
 *
121
 * Revision 1.2  2006/09/07 19:13:39  jorpiell
122
 * Ya se pueden cargar im?genes
123
 *
124
 * Revision 1.1  2006/09/07 12:51:54  jorpiell
125
 * Enganchada la interfaz gr?fica con el generador de ficheros
126
 *
127
 *
128
 */
129
/**
130
 * @author Jorge Piera Llodr? (piera_jor@gva.es)
131
 */
132
public class MapServerLayerFactory {
133
        
134
        public MapLayer getMapLayer(FLayer lyr)throws LayerException {
135
                MapLayer mapLayer = null;                
136
                if (lyr instanceof FLyrVect){
137
                        boolean publicable = false;
138
                        if ((lyr instanceof FLayerFileVectorial) ||
139
                                        (lyr instanceof FLayerGenericVectorial) ||
140
                                        (lyr instanceof FLayerJDBCVectorial)){
141
                                publicable = true;
142
                        }else if(lyr.getClass() == FLyrVect.class){
143
                                publicable = true;
144
                        }
145
                        if (publicable){
146
                                FLyrVect lyrVect = (FLyrVect) lyr;        
147
                                if (lyrVect.getSource().getDriver() instanceof IndexedShpDriver){
148
                                        mapLayer = new MapServerConfiguration.ShpLayer();
149
                                        initShpLayer(lyrVect,mapLayer);
150
                                }else if(lyrVect.getSource() instanceof ISpatialDB){
151
                                        mapLayer = new MapServerConfiguration.PostgisLayer();
152
                                        initPostgisLayer(lyrVect,
153
                                                        (PostgisLayer)mapLayer);
154
                                }                        
155
                                initVectorialLayer((FLyrVect)lyr,mapLayer);
156
                        }
157
                } else if (lyr instanceof FLyrRaster){
158
                        FLyrRaster lyrRaster = (FLyrRaster) lyr;
159
                        if (lyrRaster.getSource().getDriver() instanceof CmsRasterDriver){
160
                                mapLayer = new MapServerConfiguration.RasterLayer();
161
                                initCmsRasterLayer((CmsRasterDriver)lyrRaster.getSource().getDriver(),
162
                                                lyrRaster,
163
                                                mapLayer);                                
164
                        }
165
                        initRasterLayer((FLyrRaster)lyr,mapLayer);
166
                }
167
                if (mapLayer != null){
168
                        initLayer(lyr,mapLayer);
169
                }
170
                return mapLayer;
171
        }                
172
        
173
        /**
174
         * Initializes the layer with common properties
175
         * @param layer
176
         * @param mapLayer
177
         */
178
        private void initLayer(FLayer layer,MapLayer mapLayer){
179
                mapLayer.name = getLayerName(layer.getName());
180
                mapLayer.title = getLayerName(layer.getName());
181
                mapLayer.crs = new CRS(layer.getProjection().getAbrev(),true);                
182
        }
183
        
184
        /**
185
         * Initializes the raster layers
186
         * @param lyrRaster
187
         * @param mLayer
188
         */
189
        private void initRasterLayer(FLyrRaster lyrRaster,MapLayer mLayer){
190
                mLayer.extent = null;                
191
                mLayer.classList = new ArrayList();                
192
                mLayer.metadata=new MetadataLayer();
193
                mLayer.metadata.setExtent(lyrRaster.getMinX(),
194
                                lyrRaster.getMinY(),
195
                                lyrRaster.getMaxX(),
196
                                lyrRaster.getMaxY());
197
                mLayer.metadata.title=mLayer.title;
198
                mLayer.title = null;
199
        }                
200
        
201
        /**
202
         * Initializes the vectorial layers
203
         * @param lyrVect
204
         * @param mLayer
205
         * @throws LayerException 
206
         */
207
        private void initVectorialLayer(FLyrVect lyrVect,MapLayer mLayer) throws LayerException{
208
                mLayer.classList = getMapClassList(lyrVect);
209
                mLayer.metadata = new ConfigFile.MetadataLayer();
210
                try{
211
                        FSymbol sym = (FSymbol) lyrVect.getLegend().getDefaultSymbol();
212
                        mLayer.transparency = 255 - sym.getColor().getAlpha();                                
213
                }catch(NullPointerException e){
214
                        
215
                }
216
                try {
217
                        mLayer.metadata.setExtent(lyrVect.getFullExtent());
218
                } catch (DriverException e) {
219
                        // TODO Auto-generated catch block
220
                        e.printStackTrace();
221
                }                
222
        }        
223
        
224
        private void initCmsRasterLayer(CmsRasterDriver driver,FLyrRaster flyRaster,MapLayer mapLayer){
225
                mapLayer.type = "RASTER";
226
                String[] aux=driver.getFiles()[0].getName().split("/");
227
                mapLayer.data = aux[aux.length-1]; //getRasterFileName(driver.getFiles()[0].getName());
228
                mapLayer.layercrs= new MapServerConfiguration.CRS(flyRaster.getProjection().getAbrev(),true);
229
                double resx = (driver.getFiles()[0].getExtent().maxX() - driver.getFiles()[0].getExtent().minX())/driver.getFiles()[0].getWidth();
230
                double resy = (driver.getFiles()[0].getExtent().maxY() - driver.getFiles()[0].getExtent().minY())/driver.getFiles()[0].getHeight();
231
                mapLayer.metadata.resolution = resx+" "+resy;
232
        }
233
        
234
        private void initPostgisLayer(FLyrVect lyrVect,MapServerConfiguration.PostgisLayer mapLayer) throws LayerException{
235
                DBLayerDefinition layerDef =((ISpatialDB)lyrVect.getSource()).getLyrDef();
236
                mapLayer.type = getShpType(layerDef.getShapeType());
237
                try {
238
                        mapLayer.data = layerDef.getFieldGeometry() + " from " + getLayerName(lyrVect.getName());
239
                        String user = layerDef.getConnection().getMetaData().getUserName();
240
                        String pass = null;
241
                        String host = getPostgisHost(layerDef.getConnection().getMetaData().getURL());
242
                        String dbname = layerDef.getConnection().getCatalog();
243
                        String port = getPostgisPort(layerDef.getConnection().getMetaData().getURL());
244
                        mapLayer.setConnParams(user, pass, host, dbname, port);
245
                } catch (SQLException e) {
246
                        throw new LayerException(LayerException.EXC_NO_POSTGIS_PARAMS,e.getMessage());
247
                }         
248
        }        
249
        
250
        private void initShpLayer(FLyrVect lyrVect,MapServerConfiguration.MapLayer mapLayer) throws LayerException{
251
                mapLayer.type = getShpType(lyrVect);
252
                
253
                ReadableVectorial rv = lyrVect.getSource();
254
                if (rv instanceof VectorialFileAdapter) {
255
                        VectorialFileAdapter vfa = (VectorialFileAdapter) rv;
256
                        mapLayer.data = vfa.getFile().getName();
257
                }        
258
        }
259
        
260
        /**
261
         * Remove the "." from the file name
262
         * @param file
263
         * @return
264
         */
265
        private String getLayerName(String file){
266
                String[] s = file.split("\\.");
267
                if ((s != null) && (s.length > 0)){
268
                        return s[0];
269
                }
270
                return file;
271
        }
272
        
273
        private  String getPostgisHost(String url){
274
                //jdbc:postgresql://sercartlin:5432/carto_300k 
275
                String[] s = url.split(":");
276
                return StringUtilities.replace(s[2],"//","");
277
        }
278
        
279
        private String getPostgisPort(String url){
280
                //jdbc:postgresql://sercartlin:5432/carto_300k 
281
                String[] s = url.split(":");
282
                return s[3].split("/")[0];
283
        }                
284
        
285
        /**
286
         * Gets the geometry type from a shape
287
         * @param lyrVect
288
         * @return
289
         * @throws LayerException 
290
         * @throws DriverException 
291
         * @throws DriverException 
292
         */
293
        private String getShpType(FLyrVect lyrVect) throws LayerException {
294
                try {
295
                        int lyrType = lyrVect.getShapeType();
296
                        return getShpType(lyrType);
297
                } catch (DriverException e) {
298
                        throw new LayerException(LayerException.EXC_DRIVER,null);
299
                }
300
        }
301
        
302
        private String getShpType(int fshapeType) throws LayerException{
303
                switch(fshapeType){
304
                case FShape.POLYGON:
305
                        return org.gvsig.remoteservices.conf.mapserver.MapServerConfiguration.SHP_TYPE_POLYGON;
306
                case FShape.LINE:                                
307
                        return org.gvsig.remoteservices.conf.mapserver.MapServerConfiguration.SHP_TYPE_LINE;
308
                case FShape.POINT:
309
                case FShape.MULTIPOINT:
310
                        return org.gvsig.remoteservices.conf.mapserver.MapServerConfiguration.SHP_TYPE_POINT;
311
                }
312
                throw new LayerException(LayerException.EXC_NO_DETECTED_LAYER_TYPE,null);
313
        }
314
        
315
        private ArrayList getMapClassList(FLyrVect lyrVect){
316
                ArrayList mapClasses = new ArrayList();
317
                Legend legend = lyrVect.getLegend();        
318
                if (legend instanceof SingleSymbolLegend){
319
                        MapClass mapClass = new MapClass(getLayerName(lyrVect.getName()));
320
                        FSymbol sym = (FSymbol) legend.getDefaultSymbol();
321
                        Color clr = sym.getColor();
322
                        Color outLineclr = sym.getOutlineColor();
323
                        mapClass.estilo = new ConfigFile.StyleMap(clr,outLineclr);
324
                        try{
325
                                mapClass.size = new Float(((BasicStroke)sym.getStroke()).getLineWidth()).intValue();
326
                        }catch(Exception e){
327
                                
328
                        }
329
                        mapClasses.add(mapClass);
330
                }else if(legend instanceof VectorialUniqueValueLegend){
331
                        VectorialUniqueValueLegend uniqueValueLegend = (VectorialUniqueValueLegend)legend;
332
                        FSymbol[] symbols = uniqueValueLegend.getSymbols();
333
                        for (int i=0 ; i<symbols.length ; i++){
334
                                MapClass mapClass = new MapClass(symbols[i].getDescription());
335
                                FSymbol auxSym = (FSymbol) symbols[i];
336
                                Color clr = auxSym.getColor();
337
                                Color outLineclr = auxSym.getOutlineColor();
338
                                mapClass.estilo = new ConfigFile.StyleMap(clr,outLineclr);
339
                                mapClass.expression = "('[" + uniqueValueLegend.getFieldName() + "]' = '" + 
340
                                                symbols[i].getDescription() + "')";
341
                                try{
342
                                        mapClass.size = new Float(((BasicStroke)auxSym.getStroke()).getLineWidth()).intValue();
343
                                }catch(Exception e){
344
                                
345
                                }
346
                                mapClasses.add(mapClass);
347
                        }
348
                        
349
                }else if(legend instanceof VectorialIntervalLegend){
350
                        MapClass mapClass = new MapClass(getLayerName(lyrVect.getName()));
351

    
352
                        FSymbol sym = (FSymbol) legend.getDefaultSymbol();
353
                        Color clr = sym.getColor();
354
                        Color outLineclr = sym.getOutlineColor();
355
                        mapClass.estilo = new ConfigFile.StyleMap(clr,outLineclr);
356
                        try{
357
                                mapClass.size = new Float(((BasicStroke)sym.getStroke()).getLineWidth()).intValue();
358
                        }catch(Exception e){
359
                        
360
                        }        
361
                        mapClasses.add(mapClass);
362
                }                        
363
                return mapClasses;
364
        }
365

    
366
        
367
}