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