root / trunk / extensions / extPublish / src / com / iver / cit / gvsig / publish / servers / mapserver / MapServerPublishControler.java @ 8139
History | View | Annotate | Download (12.4 KB)
1 |
package com.iver.cit.gvsig.publish.servers.mapserver; |
---|---|
2 |
|
3 |
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
|
4 |
*
|
5 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
6 |
*
|
7 |
* This program is free software; you can redistribute it and/or
|
8 |
* modify it under the terms of the GNU General Public License
|
9 |
* as published by the Free Software Foundation; either version 2
|
10 |
* of the License, or (at your option) any later version.
|
11 |
*
|
12 |
* This program is distributed in the hope that it will be useful,
|
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15 |
* GNU General Public License for more details.
|
16 |
*
|
17 |
* You should have received a copy of the GNU General Public License
|
18 |
* along with this program; if not, write to the Free Software
|
19 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
20 |
*
|
21 |
* For more information, contact:
|
22 |
*
|
23 |
* Generalitat Valenciana
|
24 |
* Conselleria d'Infraestructures i Transport
|
25 |
* Av. Blasco Ib��ez, 50
|
26 |
* 46010 VALENCIA
|
27 |
* SPAIN
|
28 |
*
|
29 |
* +34 963862235
|
30 |
* gvsig@gva.es
|
31 |
* www.gvsig.gva.es
|
32 |
*
|
33 |
* or
|
34 |
*
|
35 |
* IVER T.I. S.A
|
36 |
* Salamanca 50
|
37 |
* 46005 Valencia
|
38 |
* Spain
|
39 |
*
|
40 |
* +34 963163400
|
41 |
* dac@iver.es
|
42 |
*/
|
43 |
import java.awt.geom.Rectangle2D; |
44 |
import java.io.File; |
45 |
import java.io.IOException; |
46 |
import java.util.ArrayList; |
47 |
import java.util.Hashtable; |
48 |
import java.util.Iterator; |
49 |
import java.util.SortedSet; |
50 |
import java.util.TreeSet; |
51 |
|
52 |
import org.gvsig.remoteservices.conf.mapserver.MapServerConfiguration.CRS; |
53 |
import org.gvsig.remoteservices.conf.mapserver.MapServerConfiguration.ConfigFile; |
54 |
import org.gvsig.remoteservices.conf.mapserver.MapServerConfiguration.MapLayer; |
55 |
import org.gvsig.remoteservices.conf.mapserver.MapServerConfiguration.MetadataWeb; |
56 |
import org.gvsig.remoteservices.conf.mapserver.MapServerConfiguration.WebMap; |
57 |
import org.gvsig.remoteservices.conf.mapserver.test.AddFieldToDBF; |
58 |
|
59 |
import com.iver.cit.gvsig.fmap.drivers.RasterDriver; |
60 |
import com.iver.cit.gvsig.fmap.layers.FLayer; |
61 |
import com.iver.cit.gvsig.fmap.layers.FLayers; |
62 |
import com.iver.cit.gvsig.fmap.layers.FLyrRaster; |
63 |
import com.iver.cit.gvsig.publish.layers.LayerException; |
64 |
import com.iver.cit.gvsig.publish.layers.LayerLoader; |
65 |
import com.iver.cit.gvsig.publish.persistence.PublishPersistence; |
66 |
import com.iver.cit.gvsig.publish.servers.GenericServer; |
67 |
import com.iver.cit.gvsig.publish.servers.PublishControler; |
68 |
import com.iver.utiles.StringUtilities; |
69 |
import com.iver.utiles.swing.jcomboServer.ServerData; |
70 |
|
71 |
/**
|
72 |
* This class implements the mapserver publication. It has to
|
73 |
* retrieve the current layers from gvSIG and create the
|
74 |
* map file
|
75 |
*
|
76 |
* @author jvhigon
|
77 |
*
|
78 |
*/
|
79 |
public class MapServerPublishControler extends PublishControler{ |
80 |
private String dataPath = null; //"C:/ms4w/test/"; |
81 |
private String fileIndex = null; //indexDir + "index.shp"; |
82 |
private ConfigFile map = null; |
83 |
//private String dirIndex = "/home/knoppix"; // CHANGE THIS !!: we'll use the same as the mapFile!!
|
84 |
/**
|
85 |
* Constructor
|
86 |
*/
|
87 |
public MapServerPublishControler(String service) { |
88 |
super(service);
|
89 |
} |
90 |
|
91 |
/*
|
92 |
* (non-Javadoc)
|
93 |
* @see com.iver.cit.gvsig.publish.servers.PublishControler#publish(com.iver.utiles.swing.jcomboServer.ServerData)
|
94 |
*/
|
95 |
public Object publish(ServerData serverData) { |
96 |
return publish(serverData.getServerAddress(),
|
97 |
serverData.getProperty(GenericServer.GENERIC_SERVER_PATH), |
98 |
serverData.getProperty(MapServer.WMS_MAPFILE), |
99 |
serverData.getProperty(MapServer.WMS_SYMBOLS), |
100 |
serverData.getProperty(MapServer.WMS_FONTS), |
101 |
serverData.getProperty(MapServer.WMS_IMAGEURL), |
102 |
serverData.getProperty(MapServer.WMS_IMAGEPATH)); |
103 |
} |
104 |
|
105 |
/**
|
106 |
*
|
107 |
* @param url
|
108 |
* @param path
|
109 |
* @param mapFile
|
110 |
* @param symbolsFile
|
111 |
* @param fontsFile
|
112 |
* @param ImageURL
|
113 |
* @param imagePath
|
114 |
* @throws LayerException
|
115 |
* @throws LayerException
|
116 |
*/
|
117 |
private ConfigFile publish(String onlineResource, String path, String mapFile, |
118 |
String symbolsFile, String fontsFile, String ImageURL, |
119 |
String imagePath) {
|
120 |
dataPath = path; |
121 |
|
122 |
map = getConfigFile(dataPath); |
123 |
map.www = getWebMapVect(onlineResource,imagePath,ImageURL); |
124 |
|
125 |
for (int i = 0; i < getLayers().getLayersCount(); i++) { |
126 |
FLayer lyr = getLayers().getLayer(i); |
127 |
if (lyr.isVisible()){
|
128 |
try{
|
129 |
if (lyr instanceof FLayers){ |
130 |
ArrayList layers = publishGroupLayer(map,(FLayers)lyr);
|
131 |
for (int j=0 ; j<layers.size() ; j++){ |
132 |
MapLayer mapLayer = (MapLayer)layers.get(j); |
133 |
if (mapLayer != null){ |
134 |
map.add(mapLayer); |
135 |
} |
136 |
} |
137 |
}else{
|
138 |
MapLayer mapLayer = publishSimpleLayer(lyr); |
139 |
if (mapLayer != null){ |
140 |
map.add(mapLayer); |
141 |
} |
142 |
} |
143 |
}catch(LayerException e){
|
144 |
|
145 |
} |
146 |
} |
147 |
} |
148 |
map.generate(mapFile); |
149 |
return map;
|
150 |
} |
151 |
|
152 |
private MapLayer publishSimpleLayer(FLayer layer) throws LayerException{ |
153 |
MapLayer mapLayer = new MapServerLayerFactory().getMapLayer(layer);
|
154 |
if (mapLayer != null){ |
155 |
if (getService().compareTo(ServerData.SERVER_TYPE_WMS) == 0){ |
156 |
mapLayer.metadata.setServiceAsWMS(); |
157 |
} else if(getService().compareTo(ServerData.SERVER_TYPE_WFS) == 0){ |
158 |
mapLayer.metadata.setServiceAsWFS(); |
159 |
mapLayer.metadata.gml_include_items = "all";
|
160 |
mapLayer.setDump(true);
|
161 |
mapLayer.classList = new ArrayList(); |
162 |
} else if(getService().compareTo(ServerData.SERVER_TYPE_WCS) == 0){ |
163 |
mapLayer.metadata.setServiceAsWCS(); |
164 |
mapLayer.setDump(true);
|
165 |
} |
166 |
} |
167 |
return mapLayer;
|
168 |
} |
169 |
|
170 |
private ArrayList publishGroupLayer(ConfigFile config,FLayers layers) throws LayerException{ |
171 |
ArrayList mapLayers = new ArrayList(); |
172 |
if (layers.getName().startsWith("[")){ |
173 |
if (layers.getName().startsWith("[" + LayerLoader.GROUP_LAYER_TIME)){ |
174 |
mapLayers.add(publishTimeLayer(config,layers)); |
175 |
}else if(layers.getName().startsWith("[" + LayerLoader.GROUP_LAYER_MOSAIC)){ |
176 |
mapLayers.add(publishMosaicLayer(config,layers)); |
177 |
}//else if(layers.getName().startsWith("[" + LayerLoader.GROUP_LAYER_MOSAIC)){
|
178 |
// mapLayers.add(publishWCSLayer(config,layers));
|
179 |
//}
|
180 |
else{
|
181 |
for (int i=0 ; i<layers.getLayersCount() ; i++){ |
182 |
mapLayers.add(publishSimpleLayer(layers.getLayer(i))); |
183 |
} |
184 |
} |
185 |
}else{
|
186 |
for (int i=0 ; i<layers.getLayersCount() ; i++){ |
187 |
mapLayers.add(publishSimpleLayer(layers.getLayer(i))); |
188 |
} |
189 |
} |
190 |
return mapLayers;
|
191 |
} |
192 |
|
193 |
private MapLayer publishWCSLayer(ConfigFile config,FLayers layers) throws LayerException{ |
194 |
//fileIndex = layers.getName().split("]")[1] + "_index.shp";
|
195 |
MapLayer mapLayer = publishSimpleLayer(layers.getLayer(0));
|
196 |
mapLayer.name = layers.getName().split("]")[1]; |
197 |
mapLayer.tileIndex = fileIndex; |
198 |
mapLayer.tileItem = "location";
|
199 |
mapLayer.data = layers.getName().split("]")[1]; |
200 |
mapLayer.metadata.name = mapLayer.name; |
201 |
mapLayer.metadata.label = mapLayer.name; |
202 |
//maplayer.metadata.srs = mapLayer.name;
|
203 |
Rectangle2D extent = map.extent;
|
204 |
mapLayer.metadata.setExtent(extent); |
205 |
//mapLayer.metadata.formats = mapLayer.name;
|
206 |
//mapLayer.metadata.nativeFormat = mapLayer.name;
|
207 |
mapLayer.metadata.setRangeset(" ");
|
208 |
//mapLayer.metadata.bandCount = mapLayer.name;
|
209 |
|
210 |
//falta alimentar los metadata...
|
211 |
|
212 |
return mapLayer;
|
213 |
} |
214 |
|
215 |
private MapLayer publishMosaicLayer(ConfigFile config,FLayers layers) throws LayerException{ |
216 |
fileIndex = layers.getName().split("]")[1] + "_index.shp"; |
217 |
MapLayer mapLayer = publishSimpleLayer(layers.getLayer(0));
|
218 |
String[] sFiles = new String[layers.getLayersCount()]; |
219 |
for (int i=0 ; i<sFiles.length ; i++){ |
220 |
sFiles[i] = getRasterFile(layers.getLayer(i)); |
221 |
} |
222 |
|
223 |
mapLayer.name = layers.getName().split("]")[1]; |
224 |
mapLayer.tileIndex = fileIndex; |
225 |
mapLayer.tileItem = "location";
|
226 |
//mapLayer.data = layers.getName().split("]")[1];
|
227 |
mapLayer.data=null;
|
228 |
mapLayer.metadata.title = mapLayer.name; |
229 |
Rectangle2D extent = map.extent;
|
230 |
/*try {
|
231 |
extent = layers.getFullExtent();
|
232 |
} catch (DriverException e) {
|
233 |
throw new LayerException(LayerException.EXC_NO_DETECTED_EXTENT,e.getMessage());
|
234 |
}*/
|
235 |
mapLayer.metadata.setExtent(extent); |
236 |
//We'll write on the same directory as mapfile
|
237 |
|
238 |
generateIndex(dataPath + "/" + fileIndex,sFiles,null); |
239 |
return mapLayer;
|
240 |
} |
241 |
|
242 |
private MapLayer publishTimeLayer(ConfigFile config,FLayers layers) throws LayerException{ |
243 |
fileIndex = layers.getName().split("]")[1] + "_index.shp"; |
244 |
MapLayer mapLayer = publishSimpleLayer(layers.getLayer(0));
|
245 |
|
246 |
SortedSet times = new TreeSet(); |
247 |
Hashtable files = new Hashtable(); |
248 |
times.add(getTime(layers.getLayer(0).getName()));
|
249 |
files.put(getTime(layers.getLayer(0).getName()),getRasterFile(layers.getLayer(0))); |
250 |
for (int i = 1; i < layers.getLayersCount(); i++) { |
251 |
times.add(getTime(layers.getLayer(i).getName())); |
252 |
files.put(getTime(layers.getLayer(i).getName()),getRasterFile(layers.getLayer(i))); |
253 |
} |
254 |
//Recuperar los files y los times en orden:
|
255 |
Iterator it = times.iterator();
|
256 |
String[] sFiles = new String[times.size()]; |
257 |
String[] sTimes = new String[times.size()]; |
258 |
int i = 0; |
259 |
String timeExtent = ""; |
260 |
while (it.hasNext()) {
|
261 |
sTimes[i] = (String)it.next();
|
262 |
sFiles[i] = (String)files.get(sTimes[i]);
|
263 |
timeExtent = timeExtent + sTimes[i]; |
264 |
if (i < sTimes.length - 1){ |
265 |
timeExtent = timeExtent + ",";
|
266 |
} |
267 |
i++; |
268 |
} |
269 |
String sOptions = layers.getName().split("]")[0]; |
270 |
String[] options = sOptions.split(","); |
271 |
mapLayer.name = layers.getName().split("]")[1]; |
272 |
config.www.metadata.timeFormat = options[1];
|
273 |
mapLayer.tileIndex = fileIndex; |
274 |
mapLayer.tileItem = "location";
|
275 |
//mapLayer.data = layers.getName().split("]")[1];
|
276 |
mapLayer.data=null;
|
277 |
mapLayer.metadata.title = mapLayer.name; |
278 |
mapLayer.metadata.timeDefault = options[2];
|
279 |
mapLayer.metadata.timeExtent = timeExtent; |
280 |
mapLayer.metadata.timeItem = "date";
|
281 |
generateIndex(dataPath + "/" + fileIndex,sFiles,sTimes);
|
282 |
return mapLayer;
|
283 |
} |
284 |
|
285 |
|
286 |
|
287 |
private String getRasterFile(FLayer layer){ |
288 |
RasterDriver driver = ((RasterDriver)((FLyrRaster)layer).getSource().getDriver()); |
289 |
return driver.getFiles()[0].getName(); |
290 |
} |
291 |
|
292 |
private void generateIndex(String fileIndex,String[] fNames, String[] times){ |
293 |
String cmd = getGDalPath() + "gdaltindex"; |
294 |
String params = fileIndex;
|
295 |
for (int i=0; i<fNames.length; i++) |
296 |
params += " "+fNames[i];
|
297 |
System.out.println("Generating index.shp ..."); |
298 |
try {
|
299 |
deleteIndexFile(fileIndex); |
300 |
System.out.println(cmd+" "+params); |
301 |
Runtime.getRuntime().exec(cmd+" "+params); |
302 |
} catch (IOException e) { |
303 |
// TODO Auto-generated catch block
|
304 |
e.printStackTrace(); |
305 |
} |
306 |
if (times != null){ |
307 |
try {
|
308 |
Thread.sleep(200); |
309 |
} catch (InterruptedException e) { |
310 |
// TODO Auto-generated catch block
|
311 |
e.printStackTrace(); |
312 |
} |
313 |
AddFieldToDBF dbfFile = new AddFieldToDBF();
|
314 |
String dbfFileName = fileIndex.substring(0,fileIndex.length()-4)+".dbf"; |
315 |
dbfFile.add(dbfFileName, "date", times);
|
316 |
} |
317 |
System.out.println("done."); |
318 |
} |
319 |
|
320 |
private void deleteIndexFile(String fileIndex) { |
321 |
File f = new File(fileIndex); |
322 |
if (f.exists()) {
|
323 |
String root = fileIndex.substring(0,fileIndex.length()-4); |
324 |
String [] exts = {"shp", "dbf", "shx"}; |
325 |
for (int i=0; i<exts.length; i++) { |
326 |
f = new File(root+"."+exts[i]); |
327 |
f.delete(); |
328 |
} |
329 |
} |
330 |
} |
331 |
|
332 |
private String getTime(String layerName){ |
333 |
String time = layerName.split("]")[0]; |
334 |
return StringUtilities.replace(time,"[",""); |
335 |
} |
336 |
|
337 |
private ConfigFile getConfigFile(String path){ |
338 |
ConfigFile map = new ConfigFile();
|
339 |
map.mapName = "TEST_MAPSERVER";
|
340 |
map.mapStatus = "ON";
|
341 |
map.mapUnits = "METERS";
|
342 |
map.mapShapePath = path; |
343 |
map.mapcrs = new CRS(getMapCtrl().getMapContext().getViewPort()
|
344 |
.getProjection().getAbrev(), true);
|
345 |
map.setExtent(getMapCtrl().getMapContext().getViewPort().getAdjustedExtent()); |
346 |
return map;
|
347 |
} |
348 |
|
349 |
private WebMap getWebMapVect(String onlineResource, |
350 |
String imagePath,
|
351 |
String imageURL){
|
352 |
WebMap webMap = new WebMap();
|
353 |
webMap.imagepath = imagePath; |
354 |
webMap.imageurl = imageURL; |
355 |
webMap.metadata = new MetadataWeb();
|
356 |
webMap.metadata.crs = new CRS(getMapCtrl().getMapContext().getViewPort()
|
357 |
.getProjection().getAbrev(), false);
|
358 |
webMap.metadata.title = "Published Layers";
|
359 |
webMap.metadata.onlineresource = onlineResource; |
360 |
if (getService().compareTo(ServerData.SERVER_TYPE_WMS) == 0){ |
361 |
webMap.metadata.setServiceAsWMS(); |
362 |
} else if(getService().compareTo(ServerData.SERVER_TYPE_WFS) == 0){ |
363 |
webMap.metadata.setServiceAsWFS(); |
364 |
} else if(getService().compareTo(ServerData.SERVER_TYPE_WCS) == 0){ |
365 |
webMap.metadata.setServiceAsWCS(); |
366 |
} |
367 |
return webMap;
|
368 |
} |
369 |
|
370 |
private String getGDalPath(){ |
371 |
PublishPersistence persistence = new PublishPersistence(this); |
372 |
return persistence.getGdalPath();
|
373 |
} |
374 |
|
375 |
|
376 |
|
377 |
|
378 |
} |
379 |
|