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