Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / extensions / extPublish / src / com / iver / cit / gvsig / publish / servers / mapserver / MapServerLayerFactory.java @ 11969

History | View | Annotate | Download (13.4 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.ISymbol;
19
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
20
import com.iver.cit.gvsig.fmap.drivers.DBLayerDefinition;
21
import com.iver.cit.gvsig.fmap.drivers.raster.CmsRasterDriver;
22
import com.iver.cit.gvsig.fmap.drivers.shp.IndexedShpDriver;
23
import com.iver.cit.gvsig.fmap.layers.FLayer;
24
import com.iver.cit.gvsig.fmap.layers.FLayerFileVectorial;
25
import com.iver.cit.gvsig.fmap.layers.FLayerGenericVectorial;
26
import com.iver.cit.gvsig.fmap.layers.FLayerVectorialDB;
27
import com.iver.cit.gvsig.fmap.layers.FLyrRaster;
28
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
29
import com.iver.cit.gvsig.fmap.layers.ISpatialDB;
30
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
31
import com.iver.cit.gvsig.fmap.layers.VectorialFileAdapter;
32
import com.iver.cit.gvsig.fmap.rendering.Legend;
33
import com.iver.cit.gvsig.fmap.rendering.SingleSymbolLegend;
34
import com.iver.cit.gvsig.fmap.rendering.VectorialIntervalLegend;
35
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
36
import com.iver.cit.gvsig.publish.layers.LayerException;
37
import com.iver.utiles.StringUtilities;
38

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

    
378
                        FSymbol sym = (FSymbol) legend.getDefaultSymbol();
379
                        Color clr = sym.getColor();
380
                        Color outLineclr = sym.getOutlineColor();
381
                        mapClass.estilo = new ConfigFile.StyleMap(clr,outLineclr);
382
                        try{
383
                                mapClass.size = new Float(((BasicStroke)sym.getStroke()).getLineWidth()).intValue();
384
                        }catch(Exception e){
385
                        
386
                        }        
387
                        mapClasses.add(mapClass);
388
                }                        
389
                return mapClasses;
390
        }
391

    
392
        
393
}