svn-gvsig-desktop / branches / v10 / extensions / extPublish / src / com / iver / cit / gvsig / publish / servers / mapserver / MapServerLayerFactory.java @ 10874
History | View | Annotate | Download (13.3 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.FLayerJDBCVectorial; |
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 10874 2007-03-23 08:52:35Z dagilgon $
|
82 |
* $Log$
|
83 |
* Revision 1.15.2.2 2007-03-23 08:52:35 dagilgon
|
84 |
* *** empty log message ***
|
85 |
*
|
86 |
* Revision 1.15.2.1 2006/11/15 04:11:03 jjdelcerro
|
87 |
* *** empty log message ***
|
88 |
*
|
89 |
* Revision 1.15 2006/10/17 11:51:09 dagilgon
|
90 |
* outputformats
|
91 |
*
|
92 |
* Revision 1.14 2006/10/17 08:03:18 dagilgon
|
93 |
* resolution?
|
94 |
*
|
95 |
* Revision 1.13 2006/10/17 07:11:42 dagilgon
|
96 |
* a?adida resolucion para wcs
|
97 |
*
|
98 |
* Revision 1.12 2006/10/16 11:40:36 dagilgon
|
99 |
* arreglado path de mapserver
|
100 |
*
|
101 |
* Revision 1.11 2006/10/05 13:22:23 jvhigon
|
102 |
* Debido al refactor del nobre en la libreria de maapserver y geoserver. Mapserver --> MapserverConfig
|
103 |
*
|
104 |
* Revision 1.10 2006/10/03 15:10:34 jorpiell
|
105 |
* Solo se tienen en cuanta las capas el local (no las remotas)
|
106 |
*
|
107 |
* Revision 1.9 2006/10/03 10:30:14 dagilgon
|
108 |
* adaptation to branch v10
|
109 |
*
|
110 |
* Revision 1.8 2006/09/28 15:04:02 fjp
|
111 |
* Usar siempre que se pueda ISymbol en lugar de FSymbol
|
112 |
*
|
113 |
* Revision 1.7 2006/09/19 00:30:31 luisw2
|
114 |
* Soporte para WCS en mapserver. Pendiente de verificar que va
|
115 |
*
|
116 |
* Revision 1.6 2006/09/13 11:33:37 jorpiell
|
117 |
* Solucionado un problema que hab?a cuando el relleno de las geometr?as es nulo
|
118 |
*
|
119 |
* Revision 1.5 2006/09/11 12:37:39 jorpiell
|
120 |
* El shp tiene una ruta absoluta
|
121 |
*
|
122 |
* Revision 1.4 2006/09/11 12:09:13 jorpiell
|
123 |
* Ya se puede almacenar la ruta donde se encuentra el ejecutable de GDAL
|
124 |
*
|
125 |
* Revision 1.3 2006/09/08 10:50:05 jorpiell
|
126 |
* A?adida la opci?n de generar una capa POSTGIS
|
127 |
*
|
128 |
* Revision 1.2 2006/09/08 09:27:34 jorpiell
|
129 |
* A?adida la exportaci?n de postgis
|
130 |
*
|
131 |
* Revision 1.1 2006/09/08 08:58:53 jorpiell
|
132 |
* Se han renombrado y modificado algunas clases
|
133 |
*
|
134 |
* Revision 1.2 2006/09/07 19:13:39 jorpiell
|
135 |
* Ya se pueden cargar im?genes
|
136 |
*
|
137 |
* Revision 1.1 2006/09/07 12:51:54 jorpiell
|
138 |
* Enganchada la interfaz gr?fica con el generador de ficheros
|
139 |
*
|
140 |
*
|
141 |
*/
|
142 |
/**
|
143 |
* @author Jorge Piera Llodr? (piera_jor@gva.es)
|
144 |
*/
|
145 |
public class MapServerLayerFactory { |
146 |
|
147 |
public MapLayer getMapLayer(FLayer lyr)throws LayerException { |
148 |
MapLayer mapLayer = null;
|
149 |
if (lyr instanceof FLyrVect){ |
150 |
boolean publicable = false; |
151 |
if ((lyr instanceof FLayerFileVectorial) || |
152 |
(lyr instanceof FLayerGenericVectorial) ||
|
153 |
(lyr instanceof FLayerJDBCVectorial)){
|
154 |
publicable = true;
|
155 |
}else if(lyr.getClass() == FLyrVect.class){ |
156 |
publicable = true;
|
157 |
} |
158 |
if (publicable){
|
159 |
FLyrVect lyrVect = (FLyrVect) lyr; |
160 |
if (lyrVect.getSource().getDriver() instanceof IndexedShpDriver){ |
161 |
mapLayer = new MapServerConfiguration.ShpLayer();
|
162 |
initShpLayer(lyrVect,mapLayer); |
163 |
}else if(lyrVect.getSource() instanceof ISpatialDB){ |
164 |
mapLayer = new MapServerConfiguration.PostgisLayer();
|
165 |
initPostgisLayer(lyrVect, |
166 |
(PostgisLayer)mapLayer); |
167 |
} |
168 |
initVectorialLayer((FLyrVect)lyr,mapLayer); |
169 |
} |
170 |
} else if (lyr instanceof FLyrRaster){ |
171 |
FLyrRaster lyrRaster = (FLyrRaster) lyr; |
172 |
if (lyrRaster.getSource().getDriver() instanceof CmsRasterDriver){ |
173 |
mapLayer = new MapServerConfiguration.RasterLayer();
|
174 |
mapLayer.metadata=new MetadataLayer();
|
175 |
initCmsRasterLayer((CmsRasterDriver)lyrRaster.getSource().getDriver(), |
176 |
lyrRaster, |
177 |
mapLayer); |
178 |
} |
179 |
initRasterLayer((FLyrRaster)lyr,mapLayer); |
180 |
} |
181 |
if (mapLayer != null){ |
182 |
initLayer(lyr,mapLayer); |
183 |
} |
184 |
return mapLayer;
|
185 |
} |
186 |
|
187 |
/**
|
188 |
* Initializes the layer with common properties
|
189 |
* @param layer
|
190 |
* @param mapLayer
|
191 |
*/
|
192 |
private void initLayer(FLayer layer,MapLayer mapLayer){ |
193 |
mapLayer.name = getLayerName(layer.getName()); |
194 |
mapLayer.title = getLayerName(layer.getName()); |
195 |
mapLayer.crs = new CRS(layer.getProjection().getAbrev(),true); |
196 |
if(mapLayer.metadata !=null){ |
197 |
mapLayer.metadata.name = mapLayer.name; |
198 |
mapLayer.metadata.label = mapLayer.name; |
199 |
} |
200 |
} |
201 |
|
202 |
/**
|
203 |
* Initializes the raster layers
|
204 |
* @param lyrRaster
|
205 |
* @param mLayer
|
206 |
*/
|
207 |
private void initRasterLayer(FLyrRaster lyrRaster,MapLayer mLayer){ |
208 |
mLayer.extent = null;
|
209 |
mLayer.classList = new ArrayList(); |
210 |
mLayer.metadata.setExtent(lyrRaster.getMinX(), |
211 |
lyrRaster.getMinY(), |
212 |
lyrRaster.getMaxX(), |
213 |
lyrRaster.getMaxY()); |
214 |
mLayer.metadata.title=mLayer.title; |
215 |
mLayer.title = null;
|
216 |
} |
217 |
|
218 |
/**
|
219 |
* Initializes the vectorial layers
|
220 |
* @param lyrVect
|
221 |
* @param mLayer
|
222 |
* @throws LayerException
|
223 |
*/
|
224 |
private void initVectorialLayer(FLyrVect lyrVect,MapLayer mLayer) throws LayerException{ |
225 |
mLayer.classList = getMapClassList(lyrVect); |
226 |
mLayer.metadata = new ConfigFile.MetadataLayer();
|
227 |
try{
|
228 |
FSymbol sym = (FSymbol) lyrVect.getLegend().getDefaultSymbol(); |
229 |
mLayer.transparency = 255 - sym.getColor().getAlpha();
|
230 |
}catch(NullPointerException e){ |
231 |
|
232 |
} |
233 |
try {
|
234 |
mLayer.metadata.setExtent(lyrVect.getFullExtent()); |
235 |
} catch (DriverException e) {
|
236 |
// TODO Auto-generated catch block
|
237 |
e.printStackTrace(); |
238 |
} |
239 |
} |
240 |
|
241 |
private void initCmsRasterLayer(CmsRasterDriver driver,FLyrRaster flyRaster,MapLayer mapLayer){ |
242 |
mapLayer.type = "RASTER";
|
243 |
String[] aux=driver.getFiles()[0].getName().split("/"); |
244 |
mapLayer.data = aux[aux.length-1]; //getRasterFileName(driver.getFiles()[0].getName()); |
245 |
mapLayer.layercrs= new MapServerConfiguration.CRS(flyRaster.getProjection().getAbrev(),true); |
246 |
double resx = (driver.getFiles()[0].getExtent().maxX() - driver.getFiles()[0].getExtent().minX())/driver.getFiles()[0].getWidth(); |
247 |
double resy = (driver.getFiles()[0].getExtent().maxY() - driver.getFiles()[0].getExtent().minY())/driver.getFiles()[0].getHeight(); |
248 |
mapLayer.metadata.resolution = resx+" "+resy;
|
249 |
mapLayer.metadata.setRangeset("bands");
|
250 |
mapLayer.metadata.bandCount = Integer.toString( driver.getNumBands());
|
251 |
mapLayer.metadata.formats = "GEOTIFF GEOTIFFINT16 IMG";
|
252 |
mapLayer.metadata.nativeFormat = "raw_binary";
|
253 |
mapLayer.metadata.name = mapLayer.name; |
254 |
mapLayer.metadata.label = mapLayer.name; |
255 |
} |
256 |
|
257 |
private void initPostgisLayer(FLyrVect lyrVect,MapServerConfiguration.PostgisLayer mapLayer) throws LayerException{ |
258 |
DBLayerDefinition layerDef =((ISpatialDB)lyrVect.getSource()).getLyrDef(); |
259 |
mapLayer.type = getShpType(layerDef.getShapeType()); |
260 |
try {
|
261 |
mapLayer.data = layerDef.getFieldGeometry() + " from " + getLayerName(lyrVect.getName());
|
262 |
String user = layerDef.getConnection().getMetaData().getUserName();
|
263 |
String pass = null; |
264 |
String host = getPostgisHost(layerDef.getConnection().getMetaData().getURL());
|
265 |
String dbname = layerDef.getConnection().getCatalog();
|
266 |
String port = getPostgisPort(layerDef.getConnection().getMetaData().getURL());
|
267 |
mapLayer.setConnParams(user, pass, host, dbname, port); |
268 |
} catch (SQLException e) { |
269 |
throw new LayerException(LayerException.EXC_NO_POSTGIS_PARAMS,e.getMessage()); |
270 |
} |
271 |
} |
272 |
|
273 |
private void initShpLayer(FLyrVect lyrVect,MapServerConfiguration.MapLayer mapLayer) throws LayerException{ |
274 |
mapLayer.type = getShpType(lyrVect); |
275 |
|
276 |
ReadableVectorial rv = lyrVect.getSource(); |
277 |
if (rv instanceof VectorialFileAdapter) { |
278 |
VectorialFileAdapter vfa = (VectorialFileAdapter) rv; |
279 |
mapLayer.data = vfa.getFile().getName(); |
280 |
} |
281 |
} |
282 |
|
283 |
/**
|
284 |
* Remove the "." from the file name
|
285 |
* @param file
|
286 |
* @return
|
287 |
*/
|
288 |
private String getLayerName(String file){ |
289 |
String[] s = file.split("\\."); |
290 |
if ((s != null) && (s.length > 0)){ |
291 |
return s[0]; |
292 |
} |
293 |
return file;
|
294 |
} |
295 |
|
296 |
private String getPostgisHost(String url){ |
297 |
//jdbc:postgresql://sercartlin:5432/carto_300k
|
298 |
String[] s = url.split(":"); |
299 |
return StringUtilities.replace(s[2],"//",""); |
300 |
} |
301 |
|
302 |
private String getPostgisPort(String url){ |
303 |
//jdbc:postgresql://sercartlin:5432/carto_300k
|
304 |
String[] s = url.split(":"); |
305 |
return s[3].split("/")[0]; |
306 |
} |
307 |
|
308 |
/**
|
309 |
* Gets the geometry type from a shape
|
310 |
* @param lyrVect
|
311 |
* @return
|
312 |
* @throws LayerException
|
313 |
* @throws DriverException
|
314 |
* @throws DriverException
|
315 |
*/
|
316 |
private String getShpType(FLyrVect lyrVect) throws LayerException { |
317 |
try {
|
318 |
int lyrType = lyrVect.getShapeType();
|
319 |
return getShpType(lyrType);
|
320 |
} catch (DriverException e) {
|
321 |
throw new LayerException(LayerException.EXC_DRIVER,null); |
322 |
} |
323 |
} |
324 |
|
325 |
private String getShpType(int fshapeType) throws LayerException{ |
326 |
switch(fshapeType){
|
327 |
case FShape.POLYGON:
|
328 |
return org.gvsig.remoteservices.conf.mapserver.MapServerConfiguration.SHP_TYPE_POLYGON;
|
329 |
case FShape.LINE:
|
330 |
return org.gvsig.remoteservices.conf.mapserver.MapServerConfiguration.SHP_TYPE_LINE;
|
331 |
case FShape.POINT:
|
332 |
case FShape.MULTIPOINT:
|
333 |
return org.gvsig.remoteservices.conf.mapserver.MapServerConfiguration.SHP_TYPE_POINT;
|
334 |
} |
335 |
throw new LayerException(LayerException.EXC_NO_DETECTED_LAYER_TYPE,null); |
336 |
} |
337 |
|
338 |
private ArrayList getMapClassList(FLyrVect lyrVect){ |
339 |
ArrayList mapClasses = new ArrayList(); |
340 |
Legend legend = lyrVect.getLegend(); |
341 |
if (legend instanceof SingleSymbolLegend){ |
342 |
MapClass mapClass = new MapClass(getLayerName(lyrVect.getName()));
|
343 |
FSymbol sym = (FSymbol) legend.getDefaultSymbol(); |
344 |
Color clr = sym.getColor();
|
345 |
Color outLineclr = sym.getOutlineColor();
|
346 |
mapClass.estilo = new ConfigFile.StyleMap(clr,outLineclr);
|
347 |
try{
|
348 |
mapClass.size = new Float(((BasicStroke)sym.getStroke()).getLineWidth()).intValue(); |
349 |
}catch(Exception e){ |
350 |
|
351 |
} |
352 |
mapClasses.add(mapClass); |
353 |
}else if(legend instanceof VectorialUniqueValueLegend){ |
354 |
VectorialUniqueValueLegend uniqueValueLegend = (VectorialUniqueValueLegend)legend; |
355 |
ISymbol[] symbols = uniqueValueLegend.getSymbols();
|
356 |
for (int i=0 ; i<symbols.length ; i++){ |
357 |
MapClass mapClass = new MapClass(symbols[i].getDescription());
|
358 |
FSymbol auxSym = (FSymbol) symbols[i]; |
359 |
Color clr = auxSym.getColor();
|
360 |
Color outLineclr = auxSym.getOutlineColor();
|
361 |
mapClass.estilo = new ConfigFile.StyleMap(clr,outLineclr);
|
362 |
mapClass.expression = "('[" + uniqueValueLegend.getFieldName() + "]' = '" + |
363 |
symbols[i].getDescription() + "')";
|
364 |
try{
|
365 |
mapClass.size = new Float(((BasicStroke)auxSym.getStroke()).getLineWidth()).intValue(); |
366 |
}catch(Exception e){ |
367 |
|
368 |
} |
369 |
mapClasses.add(mapClass); |
370 |
} |
371 |
|
372 |
}else if(legend instanceof VectorialIntervalLegend){ |
373 |
MapClass mapClass = new MapClass(getLayerName(lyrVect.getName()));
|
374 |
|
375 |
FSymbol sym = (FSymbol) legend.getDefaultSymbol(); |
376 |
Color clr = sym.getColor();
|
377 |
Color outLineclr = sym.getOutlineColor();
|
378 |
mapClass.estilo = new ConfigFile.StyleMap(clr,outLineclr);
|
379 |
try{
|
380 |
mapClass.size = new Float(((BasicStroke)sym.getStroke()).getLineWidth()).intValue(); |
381 |
}catch(Exception e){ |
382 |
|
383 |
} |
384 |
mapClasses.add(mapClass); |
385 |
} |
386 |
return mapClasses;
|
387 |
} |
388 |
|
389 |
|
390 |
} |