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