Revision 17036 trunk/libraries/lib3DMap/src/com/iver/ai2/gvsig3d/cacheservices/FLayerCacheService.java

View differences:

FLayerCacheService.java
36 36

  
37 37
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
38 38
import com.iver.ai2.gvsig3d.map3d.layers.Layer3DProps;
39
import com.iver.andami.messages.NotificationManager;
39 40
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
40 41
import com.iver.cit.gvsig.fmap.ViewPort;
41 42
import com.iver.cit.gvsig.fmap.drivers.wcs.FMapWCSDriver;
......
53 54
import es.upv.ai2.libjosg.planets.Planet;
54 55

  
55 56
public class FLayerCacheService extends RasterCacheService {
56
	private boolean     _loadFailed = true;
57
	private Extent      _extent;
57
	private boolean _loadFailed = true;
58
	private Extent _extent;
58 59
	private Rectangle2D _lyrExtentRect;
59
	private FLayer      _layer;
60
	private FLayer _layer;
60 61
	private IProjection _viewProj;
61
	private int         _dataType;
62
	private int _dataType;
62 63

  
63 64
	// WMS terrain
64 65
	// TODO Un toggle this comment to use WMS and WCS extensions.
65 66
	FMapWMSDriver _wmsDriver = null;
66 67
	FMapWCSDriver _wcsDriver = null;
67
	
68
	WMSStatus     _wmsStatus = null;
69
	WCSStatus     _wcsStatus = null;
70 68

  
71
	public FLayerCacheService(Planet planet, String name, FLayer layer, IProjection viewProj) {
69
	WMSStatus _wmsStatus = null;
70
	WCSStatus _wcsStatus = null;
71

  
72
	public FLayerCacheService(Planet planet, String name, FLayer layer,
73
			IProjection viewProj) {
72 74
		super(planet.getPlanetName(), name);
73 75

  
74 76
		_layer = layer;
......
76 78
		_loadFailed = true;
77 79

  
78 80
		int cacheType = CacheService.GLOBAL;
79
		if (planet.getCoordinateSystemType()==Planet.CoordinateSystemType.GEOCENTRIC)
80
		    cacheType += SPHERIC;
81
		if (planet.getCoordinateSystemType() == Planet.CoordinateSystemType.GEOCENTRIC)
82
			cacheType += SPHERIC;
81 83
		else
82
 		    cacheType += PLANE;
84
			cacheType += PLANE;
83 85
		setCacheType(cacheType);
84 86

  
85 87
		try {
......
94 96

  
95 97
		Layer3DProps props3D = Layer3DProps.getLayer3DProps(layer);
96 98
		_dataType = props3D.getType();
97
		if (_dataType==Layer3DProps.layer3DImage)
99
		if (_dataType == Layer3DProps.layer3DImage)
98 100
			setFileExtension(".png");
99 101
		else
100 102
			setFileExtension(".tif");
......
103 105

  
104 106
	}
105 107

  
106
    protected class MyCancel implements Cancellable, ICancellable {
108
	protected class MyCancel implements Cancellable, ICancellable {
107 109

  
108 110
		public boolean isCanceled() {
109 111
			return false;
......
113 115

  
114 116
		}
115 117

  
118
		/* if you don?t put and ID the wms donwload will be fail */
116 119
		public Object getID() {
117
			return null;
120
			return this;
118 121
		}
119 122

  
120
    }
123
	}
121 124

  
122
	private Image getTileFromLayer(String fName, Rectangle2D tileExtent)
123
	{
125
	private Image getTileFromLayer(String fName, Rectangle2D tileExtent) {
124 126
		BufferedImage image;
125 127

  
126
		if (_dataType==Layer3DProps.layer3DImage) {
128
		if (_dataType == Layer3DProps.layer3DImage) {
127 129
			int size = getTileSize();
128 130
			image = new BufferedImage(size, size, BufferedImage.TYPE_INT_ARGB);
129 131

  
130 132
			ViewPort vp = new ViewPort(_viewProj);
131 133
			vp.setExtent(tileExtent); // the real extent of the tile
134
			// NotificationManager.addInfo("Extent solicitado--> "
135
			// + tileExtent.getMinX() + " " + tileExtent.getMinY() + " "
136
			// + tileExtent.getMaxX() + " " + tileExtent.getMaxY());
132 137
			vp.setAdjustable(false);
133
	        vp.setImageSize(new Dimension(size, size));
138
			vp.setImageSize(new Dimension(size, size));
139
			vp.setDist1pixel(1 / 15000);
140
			// vp.setDist1pixel(1/100000000);
134 141

  
135
	        Graphics2D graphics = image.createGraphics();
136
	        Color backgColor = new Color(255, 255, 255, 0); // transparent background
137
	        graphics.setColor(backgColor);
138
	        graphics.fillRect(0, 0, size, size);
142
			Graphics2D graphics = image.createGraphics();
143
			Color backgColor = new Color(255, 255, 255, 0); // transparent
144
															// background
145
			graphics.setColor(backgColor);
146
			graphics.fillRect(0, 0, size, size);
139 147

  
140
	//        CancelDraw canceldraw = new MapControl.CancelDraw();
141
	//        canceldraw.setCanceled(false);
142
	        // For now we'll use a trick to make sure the layer is drawn
143
	        // even if it has scale dependency
144
	        // In the future, a scale corresponding to the tile should be passed
145
	        double scale = 0.5*(_layer.getMaxScale()+_layer.getMinScale());
146
	        int trans = 255;
147
	        
148
	        // The layers of remote services don�t have transparency.
149
	        if (_layer.getClass().equals(FLyrVect.class)){
150
	        	// Generate layers 3D properties
148
			// CancelDraw canceldraw = new MapControl.CancelDraw();
149
			// canceldraw.setCanceled(false);
150
			// For now we'll use a trick to make sure the layer is drawn
151
			// even if it has scale dependency
152
			// In the future, a scale corresponding to the tile should be passed
153
			double scale = 0.5 * (_layer.getMaxScale() + _layer.getMinScale());
154
			int trans = 255;
155

  
156
			// The layers of remote services don�t have transparency.
157
			if (_layer.getClass().equals(FLyrVect.class)) {
158
				// Generate layers 3D properties
151 159
				Layer3DProps props3D = Layer3DProps.getLayer3DProps(_layer);
152 160
				// Getting layer transparency
153 161
				trans = ((FLyrDefault) _layer).getTransparency();
......
158 166
				// fix opaque the layer to save in disk
159 167
				((FLyrDefault) _layer).setTransparency(255);
160 168
			}
161
	
162
	        try {
163
	        	// disabling the iterator
164
				if (_layer instanceof FLyrVect) {
165
					FLyrVect layerVect = (FLyrVect) _layer;
166
					layerVect.setUseStrategy(true);
167
					layerVect.draw(image, graphics, vp, new MyCancel(), scale);
168
				} else
169
					_layer.draw(image, graphics, vp, new MyCancel(), scale);
169

  
170
			try {
171
				// disabling the iterator
172
				// if (_layer instanceof FLyrVect) {
173
				// FLyrVect layerVect = (FLyrVect) _layer;
174
				// boolean strategyUse = layerVect.isUseStrategy();
175
				// layerVect.setUseStrategy(true);
176
				// layerVect.draw(image, graphics, vp, new MyCancel(), scale);
177
				// layerVect.setUseStrategy(strategyUse);
178
				// } else
179
				_layer.draw(image, graphics, vp, new MyCancel(), scale);
170 180
			} catch (ReadDriverException e) {
171 181
				e.printStackTrace();
172 182
			}
173 183
			// Restoring the real value of transparency
174
			 if (_layer.getClass().equals(FLyrVect.class)){
184
			if (_layer.getClass().equals(FLyrVect.class)) {
175 185
				// fix the real transparency to the layer
176 186
				((FLyrDefault) _layer).setTransparency(trans);
177 187
			}
178 188

  
179
	        try {
189
			try {
180 190
				File file = new File(fName);
181 191
				String format = getFileExtension().substring(1);
182 192
				saveCachedFile(image, format, file);
......
185 195
				return null;
186 196
			}
187 197
			return image;
188
		}
189
		else { // Elevation data
198
		} else { // Elevation data
199
			// TODO Un toggle this comment to use WCS and WMS extensions.
200
			if (_layer instanceof FLyrWMS) { // WMS Elevation
201
				try {
202
					FLyrWMS wmsLayer = (FLyrWMS) _layer;
203
					if (_wmsDriver == null) {
204

  
205
						URL host = wmsLayer.getHost();
206
						_wmsDriver = FMapWMSDriverFactory
207
								.getFMapDriverForURL(host);
208
					}
209
					if (_wmsStatus == null) {
210
						_wmsStatus = new WMSStatus();
211
						_wmsStatus.setFormat(wmsLayer.getFormat());
212
						_wmsStatus.setHeight(32);
213
						_wmsStatus.setWidth(32);
214
						_wmsStatus.setLayerNames(Utilities.createVector(
215
								wmsLayer.getLayerQuery(), ","));
216
						_wmsStatus.setSrs(wmsLayer.getSRS());
217

  
218
						HashMap props = wmsLayer.getProperties();
219
						Vector styles;
220
						Vector dimensions;
221
						styles = (Vector) (props.get("styles"));
222
						dimensions = (Vector) (props.get("dimensions"));
223
						_wmsStatus.setStyles(styles);
224
						_wmsStatus.setDimensions(dimensions);
225
						_wmsStatus.setTransparency(false);
226
						String getMapStr = wmsLayer.getOnlineResource("GetMap");
227
						_wmsStatus.setOnlineResource(getMapStr);
228
					}
229
					_wmsStatus.setExtent(tileExtent);
230
					File f = _wmsDriver.getMap(_wmsStatus, new MyCancel());
231
					if (f == null)
232
						return null;
233
					FileUtils.copy(f, new File(fName));
234

  
235
					String fileName = f.getPath();
236
					// System.out.println(fileName);
237

  
238
				} catch (Exception e) {
239
					return null;
240
				}
241
			} else if (_layer instanceof FLyrWCS) { // WCS Elevation
242
				try {
243
					FLyrWCS wcsLayer = (FLyrWCS) _layer;
244
					if (_wcsDriver == null) {
245

  
246
						URL host = wcsLayer.getHost();
247
						_wcsDriver = FMapWCSDriverFactory
248
								.getFMapDriverForURL(host);
249
					}
250
					if (_wcsStatus == null) {
251
						_wcsStatus = new WCSStatus();
252

  
253
						Hashtable props = wcsLayer.getProperties();
254

  
255
						_wcsStatus.setCoveraName((String) props.get("name"));
256
						_wcsStatus.setFormat((String) props.get("format"));
257
						_wcsStatus.setHeight(32);
258
						_wcsStatus.setWidth(32);
259
						_wcsStatus.setSrs((String) props.get("crs"));
260
						_wcsStatus.setParameters((String) props
261
								.get("parameter"));
262
						String time = (String) props.get("time");
263
						if (time.length() > 0)
264
							_wcsStatus.setTime(time);
265
						// _wcsStatus.setOnlineResource(wcsLayer.getOnlineResource("GetCoverage"));
266
					}
267
					_wcsStatus.setExtent(tileExtent);
268

  
269
					File f = _wcsDriver.getCoverage(_wcsStatus, new MyCancel());
270
					if (f == null)
271
						return null;
272

  
273
					FileUtils.copy(f, new File(fName));
274

  
275
					String fileName = f.getPath();
276
					// System.out.println(fileName);
277

  
278
				} catch (Exception e) {
279
					return null;
280
				}
281
			}
190 282
			// RASTER_GRID
191
            if (_layer instanceof FLyrRasterSE) {
192
            	//Grid grid = null;
193
			    FLyrRasterSE rasterLayer = (FLyrRasterSE)_layer;
194
			    	    
195
			    //grid = rasterLayer.getSource().getGrid();
196
            	//if (grid == null)
197
            		//return null;
198
				
199
			    BufferFactory bufferFactory = new BufferFactory(rasterLayer.getDataSource());
200
			    
201
				int bands[] = {0};
283
			if (_layer instanceof FLyrRasterSE) {
284
				// Grid grid = null;
285
				FLyrRasterSE rasterLayer = (FLyrRasterSE) _layer;
286

  
287
				// grid = rasterLayer.getSource().getGrid();
288
				// if (grid == null)
289
				// return null;
290

  
291
				BufferFactory bufferFactory = new BufferFactory(rasterLayer
292
						.getDataSource());
293

  
294
				int bands[] = { 0 };
202 295
				bufferFactory.setDrawableBands(bands);
203 296
				bufferFactory.setNoDataValue(0.0);
204 297

  
205 298
				try {
206 299
					bufferFactory.setAreaOfInterest(tileExtent.getMinX(),
207
							tileExtent.getMinY(), tileExtent.getMaxX(), tileExtent
208
									.getMaxY(), 32, 32);
300
							tileExtent.getMinY(), tileExtent.getWidth(),
301
							tileExtent.getHeight(), 32, 32);
209 302
				} catch (ArrayIndexOutOfBoundsException e1) {
210 303
					// TODO Auto-generated catch block
211 304
					e1.printStackTrace();
212 305
				} catch (InvalidSetViewException e1) {
213
//					// TODO Auto-generated catch block
306
					// // TODO Auto-generated catch block
214 307
					e1.printStackTrace();
215 308
				} catch (InterruptedException e1) {
216
//					// TODO Auto-generated catch block
309
					// // TODO Auto-generated catch block
217 310
					e1.printStackTrace();
218 311
				} catch (RasterDriverException e) {
219 312
					// TODO Auto-generated catch block
......
231 324
							.getWidth() / 32, 0, 0,
232 325
							-(tileExtent.getHeight() / 32), tileExtent
233 326
									.getMinX(), tileExtent.getMaxY());
234

  
235 327
					GeoRasterWriter grw = GeoRasterWriter.getWriter(
236 328
							bufferServer, fName, 1, at, raster.getWidth(),
237 329
							raster.getHeight(), raster.getDataType(), p, null);
......
251 343
					e.printStackTrace();
252 344
				}
253 345

  
254
				
255 346
			}
256
			// TODO  Un toggle this comment to use WCS and WMS extensions.
257
			else
258
			if (_layer instanceof FLyrWMS) { // WMS Elevation
259
				try {  
260
					FLyrWMS wmsLayer = (FLyrWMS)_layer;
261
					if (_wmsDriver == null) {
262
	
263
					    URL host = wmsLayer.getHost();
264
	  			        _wmsDriver = FMapWMSDriverFactory.getFMapDriverForURL(host);
265
					}
266
					if (_wmsStatus == null) {
267
		  			    _wmsStatus = new WMSStatus();
268
		  			    _wmsStatus.setFormat(wmsLayer.getFormat());
269
		  			    _wmsStatus.setHeight(32);
270
		  			    _wmsStatus.setWidth(32);
271
		  			    _wmsStatus.setLayerNames(Utilities.createVector(wmsLayer.getLayerQuery(),","));
272
		  			    _wmsStatus.setSrs(wmsLayer.getSRS());
273
	
274
		  			    HashMap props = wmsLayer.getProperties();
275
		  			    Vector styles;
276
		  			    Vector dimensions;
277
		  			    styles = (Vector)(props.get("styles"));
278
		  			    dimensions = (Vector)(props.get("dimensions"));
279
		  			    _wmsStatus.setStyles(styles);
280
		  			    _wmsStatus.setDimensions(dimensions);
281
		  			    _wmsStatus.setTransparency(false);
282
		  			    String getMapStr = wmsLayer.getOnlineResource("GetMap");
283
		  			    _wmsStatus.setOnlineResource(getMapStr);
284
					}
285
					_wmsStatus.setExtent(tileExtent);
286
	
287
	  			    File f = _wmsDriver.getMap(_wmsStatus, new MyCancel());
288
		  			if (f == null)
289
		  				return null;
290
		  			FileUtils.copy(f, new File(fName));
291
	
292
		  			String fileName = f.getPath();
293
//		  			System.out.println(fileName);
294
	
295
				} catch (Exception e) {
296
					return null;
297
				}
298
			}
299
			else
300
				if (_layer instanceof FLyrWCS) { // WCS Elevation
301
					try { 
302
						FLyrWCS wcsLayer = (FLyrWCS)_layer;
303
						if (_wcsDriver == null) {
304
		
305
						    URL host = wcsLayer.getHost();
306
		  			        _wcsDriver = FMapWCSDriverFactory.getFMapDriverForURL(host);
307
						}
308
						if (_wcsStatus == null) {
309
			  			    _wcsStatus = new WCSStatus();
310
			  			    
311
			  			    Hashtable props = wcsLayer.getProperties();
312

  
313
			  			    _wcsStatus.setCoveraName((String)props.get("name"));
314
			  			    _wcsStatus.setFormat((String)props.get("format"));
315
			  			    _wcsStatus.setHeight(32);
316
			  			    _wcsStatus.setWidth(32);
317
			  			    _wcsStatus.setSrs((String)props.get("crs"));
318
			  			    _wcsStatus.setParameters((String)props.get("parameter"));
319
			  			    String time = (String)props.get("time");
320
			  			    if (time.length()>0)
321
			  			        _wcsStatus.setTime(time);
322
//			  			    _wcsStatus.setOnlineResource(wcsLayer.getOnlineResource("GetCoverage"));
323
						}
324
						_wcsStatus.setExtent(tileExtent);
325

  
326
			  			File f = _wcsDriver.getCoverage(_wcsStatus, new MyCancel());
327
			  			if (f == null)
328
			  				return null;
329
		
330
			  			FileUtils.copy(f, new File(fName));
331
		
332
			  			String fileName = f.getPath();
333
//			  			System.out.println(fileName);
334
		
335
					} catch (Exception e) {
336
						return null;
337
					}
338
				}/**/
339 347
			return null; // no image
340 348
		}
341 349

  
342 350
	}
343 351

  
344 352
	public String getTileAsFName(TileNum t) throws CacheServiceException {
345
		if ((t.getLevel()==5) && (t.getX())==29 && (t.getY()==9)){
346
			System.err.println("pillado");
353

  
354
		if (_loadFailed) {
355
			throw new CacheServiceException(new Exception());
347 356
		}
348
		if(_loadFailed) { throw new CacheServiceException(new Exception()); }
349 357
		String tileId = t.numToOpTileId();
350 358
		String fName = getTileFileName(t);
351
//		System.out.println("CMTexture2D: requestTexture: "+tileId);
359
		// System.out.println("CMTexture2D: requestTexture: "+tileId);
352 360
		if (!new File(fName).exists()) {
353 361
			Rectangle2D tileExtent = computeBBox(t);
354
//			System.out.println("Extent calculado del tilenum "+ t + tileExtent.getMinX() + " "
355
//					+ tileExtent.getMinY() + " " + tileExtent.getHeight() + " " + tileExtent.getWidth());
362
			// System.out.println("Extent calculado del tilenum "+ t +
363
			// tileExtent.getMinX() + " "
364
			// + tileExtent.getMinY() + " " + tileExtent.getHeight() + " " +
365
			// tileExtent.getWidth());
356 366
			getTileFromLayer(fName, tileExtent);
357 367
		}
358 368
		return fName;
359 369
	}
360
	
361
	public String getTileAsFName(TileNum t,Rectangle2D extent) throws CacheServiceException {
362
		if(_loadFailed) { throw new CacheServiceException(new Exception()); }
370

  
371
	public String getTileAsFName(TileNum t, Rectangle2D extent)
372
			throws CacheServiceException {
373
		if (_loadFailed) {
374
			throw new CacheServiceException(new Exception());
375
		}
363 376
		String tileId = t.numToOpTileId();
364 377
		String fName = getTileFileName(t);
365
//		System.out.println("CMTexture2D: requestTexture: "+tileId);
378
		// System.out.println("CMTexture2D: requestTexture: "+tileId);
366 379
		if (!new File(fName).exists()) {
367 380
			Rectangle2D tileExtent = extent;
368 381
			getTileFromLayer(fName, tileExtent);
......
371 384
	}
372 385

  
373 386
	public Image getTileAsImage(TileNum tileNum) throws CacheServiceException {
374
		if ((tileNum.getLevel()==5) ){//&& (tileNum.getX())==29 && (tileNum.getY()==9)){
387
		if ((tileNum.getLevel() == 5)) {// && (tileNum.getX())==29 &&
388
										// (tileNum.getY()==9)){
375 389
			System.err.println("pillado");
376 390
		}
377
		
378
		if(_loadFailed) { throw new CacheServiceException(new Exception()); }
391

  
392
		if (_loadFailed) {
393
			throw new CacheServiceException(new Exception());
394
		}
379 395
		String tileId = tileNum.numToOpTileId();
380 396
		String fName = getTileFileName(tileNum);
381
//		System.out.println("CMTexture2D: requestTexture: "+tileId);
397
		// System.out.println("CMTexture2D: requestTexture: "+tileId);
382 398
		Rectangle2D tileExtent = computeBBox(tileNum);
383 399
		return getTileFromLayer(fName, tileExtent);
384 400
	}
......
386 402
	public boolean intersectsLayer(TileNum tileNum) {
387 403
		Rectangle2D tileExtent = computeBBox(tileNum);
388 404
		// TODO: use a reprojected layer extent !!!
389
//		System.err.println("TILE: ("+tileExtent.getMinX()+","+tileExtent.getMinY()+"),("+
390
//				tileExtent.getMaxX()+","+tileExtent.getMaxY()+")");
391
//		System.err.println("CAPA: ("+_lyrExtentRect.getMinX()+","+_lyrExtentRect.getMinY()+"),("+
392
//				_lyrExtentRect.getMaxX()+","+_lyrExtentRect.getMaxY()+")");
393
//		System.err.println("INTERSECTA LA CAPAAAAAA??????????:"+_lyrExtentRect.intersects(tileExtent));
394
//		return (_lyrExtentRect.intersects(tileExtent));
395
		return Math.max(_lyrExtentRect.getMinX(),tileExtent.getMinX()) <= Math.min(_lyrExtentRect.getMaxX(),tileExtent.getMaxX()) &&
396
		Math.max(Math.min(_lyrExtentRect.getMinY(),_lyrExtentRect.getMaxY()),tileExtent.getMinY()) <= Math.min(Math.max(_lyrExtentRect.getMinY(),_lyrExtentRect.getMaxY()),tileExtent.getMaxY());
405
		// System.err.println("TILE:
406
		// ("+tileExtent.getMinX()+","+tileExtent.getMinY()+"),("+
407
		// tileExtent.getMaxX()+","+tileExtent.getMaxY()+")");
408
		// System.err.println("CAPA:
409
		// ("+_lyrExtentRect.getMinX()+","+_lyrExtentRect.getMinY()+"),("+
410
		// _lyrExtentRect.getMaxX()+","+_lyrExtentRect.getMaxY()+")");
411
		// System.err.println("INTERSECTA LA
412
		// CAPAAAAAA??????????:"+_lyrExtentRect.intersects(tileExtent));
413
		// return (_lyrExtentRect.intersects(tileExtent));
414
		return Math.max(_lyrExtentRect.getMinX(), tileExtent.getMinX()) <= Math
415
				.min(_lyrExtentRect.getMaxX(), tileExtent.getMaxX())
416
				&& Math.max(Math.min(_lyrExtentRect.getMinY(), _lyrExtentRect
417
						.getMaxY()), tileExtent.getMinY()) <= Math.min(
418
						Math.max(_lyrExtentRect.getMinY(), _lyrExtentRect
419
								.getMaxY()), tileExtent.getMaxY());
397 420
	}
421

  
398 422
	public boolean intersectsLayer(Rectangle2D extent) {
399 423
		Rectangle2D tileExtent = extent;
400
	
424

  
401 425
		return _lyrExtentRect.intersects(extent);
402
//		return Math.max(_lyrExtentRect.getMinX(),tileExtent.getMinX()) <= Math.min(_lyrExtentRect.getMaxX(),tileExtent.getMaxX()) &&
403
//		Math.max(Math.min(_lyrExtentRect.getMinY(),_lyrExtentRect.getMaxY()),tileExtent.getMinY()) <= Math.min(Math.max(_lyrExtentRect.getMinY(),_lyrExtentRect.getMaxY()),tileExtent.getMaxY());
426
		// return Math.max(_lyrExtentRect.getMinX(),tileExtent.getMinX()) <=
427
		// Math.min(_lyrExtentRect.getMaxX(),tileExtent.getMaxX()) &&
428
		// Math.max(Math.min(_lyrExtentRect.getMinY(),_lyrExtentRect.getMaxY()),tileExtent.getMinY())
429
		// <=
430
		// Math.min(Math.max(_lyrExtentRect.getMinY(),_lyrExtentRect.getMaxY()),tileExtent.getMaxY());
404 431
	}
405 432

  
406 433
}

Also available in: Unified diff