Revision 2003 trunk/extensions/extWCS/src/com/iver/cit/gvsig/fmap/layers/FLyrWCS.java

View differences:

FLyrWCS.java
40 40
 */
41 41
package com.iver.cit.gvsig.fmap.layers;
42 42

  
43
import java.awt.Dimension;
44 43
import java.awt.Graphics2D;
45
import java.awt.Image;
46
import java.awt.geom.Point2D;
47 44
import java.awt.geom.Rectangle2D;
48 45
import java.awt.image.BufferedImage;
49
import java.io.File;
50 46
import java.io.IOException;
51
import java.net.MalformedURLException;
52 47
import java.net.URL;
53 48

  
54 49
import javax.swing.JOptionPane;
55 50

  
56
import org.cresques.geo.ViewPortData;
57
import org.cresques.io.GdalFile;
58
import org.cresques.io.rasterOld.ComputeMinMaxFilter;
59
import org.cresques.io.rasterOld.GreyscaleRasterToImageFilter;
60
import org.cresques.io.rasterOld.LinearEnhancementFilter;
61
import org.cresques.io.rasterOld.RasterBuf;
62
import org.cresques.io.rasterOld.RasterStats;
63
import org.cresques.io.rasterOld.RasterToImageFilter;
64
import org.cresques.px.Extent;
65
import org.cresques.px.PxRaster;
66

  
67
import com.iver.andami.PluginServices;
68 51
import com.iver.cit.gvsig.fmap.DriverException;
69 52
import com.iver.cit.gvsig.fmap.ViewPort;
70 53
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
......
77 60
import com.iver.utiles.StringUtilities;
78 61
import com.iver.utiles.XMLEntity;
79 62

  
80
import es.gva.cit.jgdal.Gdal;
81
import es.gva.cit.jgdal.GdalBuffer;
82
import es.gva.cit.jgdal.GdalException;
83
import es.gva.cit.jgdal.GdalRasterBand;
84
import es.gva.cit.jgdal.GeoTransform;
85
import es.uji.lsi.wcs.client.ServerErrorResponseException;
86 63
import es.uji.lsi.wcs.client.ServerOutOfOrderException;
87 64

  
88 65
/**
......
91 68
 * @author jaume - jaume.dominguez@iver.es
92 69
 */
93 70
public class FLyrWCS extends FLyrDefault {
71
	private FMapWCSAdaptor wcs = null;
94 72

  
95
	private URL host = null;
96
	private FMapWCSDriver wcs = null;
97
	private String coverageName;
98
	private Rectangle2D fullExtent, bbox;
99
	private String SRS;
100
	private String format;
101
	private String time;
102
	private String parameter;
103
	private String coverageQuery;
104
	private String coverageInfoQuery;
105
	private String lastCoverageQuery = "";
106 73
	private String label;
107
	private static final double METROSXGRADO = 111120D;
108
	private WCSParamsPanel toc;
109
	private File fCoverage = null;
74
	private WCSParamsPanel toc = null;
75
	
110 76

  
111 77
	/**
112 78
	 * Devuelve el XMLEntity con la informaci?n necesaria para reproducir la
......
118 84
	public XMLEntity getXMLEntity() throws XMLException {
119 85
		XMLEntity xml = super.getXMLEntity();
120 86

  
121
		xml.putProperty("wcs.fullExtent", StringUtilities.rect2String(fullExtent));
122
		xml.putProperty("wcs.host", host.toExternalForm());
123
		xml.putProperty("wcs.layerQuery", coverageQuery);
124
		xml.putProperty("wcs.format", format);
125
		xml.putProperty("wcs.srs", SRS);
126
		xml.putProperty("wcs.time", time);
127
		xml.putProperty("wcs.parameter", parameter);
87
		xml.putProperty("wcs.host", getHost());
88
		xml.putProperty("wcs.fullExtent",
89
			StringUtilities.rect2String(wcs.getFullExtent()));
90
		xml.putProperty("wcs.layerQuery", wcs.getCoverageQuery());
91
		xml.putProperty("wcs.format", getFormat());
92
		xml.putProperty("wcs.srs", getSRS());
93
		xml.putProperty("wcs.time", getTime());
94
		xml.putProperty("wcs.parameter", getParameter());
128 95
		xml.putProperty("wcs.label", label);
129
		xml.putProperty("wcs.coverageName", coverageName);
96
		xml.putProperty("wcs.coverageName", getCoverageName());
130 97
		return xml;
131 98
	}
132 99

  
......
141 108
	 */
142 109
	public void setXMLEntity(XMLEntity xml) throws XMLException {
143 110
		super.setXMLEntity(xml);
144
		System.out.println(" Cargo la capa WCS.");
145
		fullExtent = StringUtilities.string2Rect(xml
146
				.getStringProperty("wcs.fullExtent"));
111
		System.out.println(" Cargo la capa WCS. ... o no ;-)");
112
		setHost(xml.getStringProperty("wcs.host"));
113
		setFullExtent(StringUtilities.string2Rect(
114
			xml.getStringProperty("wcs.fullExtent")));
147 115

  
148
		try {
149
			setHost(new URL(xml.getStringProperty("wcs.host")));
150
		} catch (MalformedURLException e) {
151
			throw new XMLException(e);
152
		}
153 116

  
154
		coverageQuery = xml.getStringProperty("wcs.layerQuery");
155
		format = xml.getStringProperty("wcs.format");
156
		SRS = xml.getStringProperty("wcs.srs");
157
		time = xml.getStringProperty("wcs.time");
158
		parameter = xml.getStringProperty("wcs.parameter");
117
		wcs.setCoverageQuery(xml.getStringProperty("wcs.layerQuery"));
118
		setFormat(xml.getStringProperty("wcs.format"));
119
		setSRS(xml.getStringProperty("wcs.srs"));
120
		setTime(xml.getStringProperty("wcs.time"));
121
		setParameter(xml.getStringProperty("wcs.parameter"));
159 122
		label = xml.getStringProperty("wcs.label");
160
		coverageName = xml.getStringProperty("wcs.coverageName");
123
		setCoverageName(xml.getStringProperty("wcs.coverageName"));
161 124
	}
162 125
	
163 126
	/**
......
166 129
	 * @return Rectangle2D
167 130
	 */
168 131
	public Rectangle2D getFullExtent() throws DriverException {
169
		return fullExtent;
132
		return wcs.getFullExtent();
170 133
	}
171 134

  
172 135
	/**
......
174 137
	 */
175 138
	public synchronized void draw(BufferedImage image, Graphics2D g,
176 139
			ViewPort viewPort, Cancellable cancel) throws DriverException {
177
		if (viewPort.getExtent().getMinX() > fullExtent.getMaxX())
178
			return;
179
		if (viewPort.getExtent().getMinY() > fullExtent.getMaxY())
180
			return;
181
		if (viewPort.getExtent().getMaxX() < fullExtent.getMinX())
182
			return;
183
		if (viewPort.getExtent().getMaxY() < fullExtent.getMinY())
184
			return;
185
		setBBox(viewPort.getAdjustedExtent());
186
		
187
		double x = bbox.getMinX();
188
		double y = bbox.getMinY();
189
		double w = bbox.getWidth();
190
		double h = bbox.getHeight();
191
		
192
		double scalex = viewPort.getAffineTransform().getScaleX()	/* g.getTransform().getScaleX()*/ ,
193
			scaley = viewPort.getAffineTransform().getScaleY()		/* g.getTransform().getScaleY() */;
194
		int wImg = (int) Math.abs(w*scalex)+1, hImg = (int) Math.abs(h*scaley)+1;
195
		if (wImg <= 0 || hImg <= 0) return;
196

  
197
		setCoverageQuery(bbox, new Dimension(wImg, hImg));
198

  
199
		try {
200
			if (lastCoverageQuery.compareTo(coverageQuery) != 0) {
201
				setWCSClientCoverageQuery(coverageQuery);
202
				fCoverage = wcs.getCoverage();
203
			}
204

  
205
			if (fCoverage != null) {
206
				//drawJaume(g, viewPort, fCoverage);
207
				drawLWS(g, viewPort, fCoverage);
208
			}
209
		} catch (ServerErrorResponseException e) {
210
			JOptionPane.showMessageDialog(null, e.getMessage(), "Error",
211
					JOptionPane.ERROR_MESSAGE);
212
			throw new DriverException(e);
213
		}
140
		wcs.draw(image, g, viewPort, cancel);
214 141
	}
215
	
216
	private void drawJaume(Graphics2D g, ViewPort viewPort, File fCoverage) {
217
		Image coverage = renderizeRaster(getRaster(fCoverage));
218 142
		
219
		Point2D p1 = new Point2D.Double(bbox.getMinX(), bbox.getMaxY());
220
		//Point2D p2 = new Point2D.Double(bbox.getMaxX(), bbox.getMinY());
221

  
222
		viewPort.getAffineTransform().transform(p1, p1);
223
		//viewPort.getAffineTransform().transform(p2, p2);
224

  
225
		g.drawImage(coverage, (int) p1.getX(), (int) p1.getY(), null);
226
		/*g.setColor(Color.RED);
227
		g.drawRect((int) p1.getX(), (int) p1.getY(),
228
				((int) (p2.getX() - p1.getX())), ((int) (p2.getY() - p1
229
						.getY())));*/
230
	}
231
	
232
	/**
233
	 * Pinta usando PxRaster
234
	 */
235

  
236
	private void drawLWS(Graphics2D g, ViewPort viewPort, File fCoverage) {
237
		Extent e = new Extent(bbox);
238
		GdalFile rasterFile = new GdalFile(viewPort.getProjection(), fCoverage.getAbsolutePath());
239
		PxRaster raster = new PxRaster(rasterFile, null, rasterFile.getExtent());
240
		// ? PxRaster raster = new PxRaster(rasterFile, null, e);
241
		raster.setTransparency(false);
242
		ViewPortData vpData = new ViewPortData(viewPort.getProjection(), e, viewPort.getImageSize() );
243
		vpData.setMat(viewPort.getAffineTransform());
244
		
245
		raster.draw(g, vpData);
246
	}
247
		
248
	/*
249
	 * (non-Javadoc)
250
	 * 
251
	 * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
252
	 *      com.iver.cit.gvsig.fmap.ViewPort,
253
	 *      com.iver.cit.gvsig.fmap.operations.Cancellable)
254
	 */
255 143
	public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel)
256 144
			throws DriverException {
257 145
	}
258 146

  
259 147
	/**
260
	 * Recoge los par?metros de la capa y los pasa al cliente WCS. Despu?s de
261
	 * esto, ya se puede lanzar una Getcoverage
262
	 *  
263
	 */
264
	private void setWCSClientCoverageQuery(String query) {
265
		lastCoverageQuery = query;
266
		wcs.setGetCoverageParams(query);
267
	}
268

  
269
	/**
270
	 * Establece el host al que se conecta el cliente.
271
	 * 
272
	 * @param host
273
	 */
274
	public void setHost(URL host) {
275
		this.host = host;
276
		if (wcs == null)
277
			wcs = new FMapWCSDriver();
278
		wcs.setHost(host.toString());
279
	}
280

  
281
	/**
282
	 * Establece nombre de la cobertura.
283
	 * 
284
	 * @param name
285
	 */
286
	public void setCoverageName(String name) {
287
		coverageName = name;
288
	}
289

  
290
	/**
291 148
	 * Establece la etiqueta de la cobertura. La que se usar? en el TOC
292 149
	 * 
293 150
	 * @param lbl
......
297 154
	}
298 155

  
299 156
	public void setFullExtent(Rectangle2D rect) {
300
		fullExtent = rect;
157
		wcs.setFullExtent(rect);
301 158
	}
302 159

  
303 160
	/**
304
	 * Construye un objeto Image a partir de un RasterBuf
305
	 * 
306
	 * @param raster
307
	 * @return Image
308
	 */
309
	public Image renderizeRaster(RasterBuf raster) {
310
		RasterStats stats = new RasterStats(raster.getBandNr());
311
		int alpha = 255; // transparencia
312
		boolean debug = false;
313
		ComputeMinMaxFilter filter = new ComputeMinMaxFilter(raster, stats);
314

  
315
		LinearEnhancementFilter lEFilter = new LinearEnhancementFilter(raster,
316
				stats);
317
		Image image = null;
318
		if (raster.getBandNr() == 1)
319
			image = new GreyscaleRasterToImageFilter(raster, alpha).getImage();
320
		else
321
			image = new RasterToImageFilter(raster, alpha).getImage();
322

  
323
		if (debug)
324
			stats.pinta();
325
		return image;
326
	}
327

  
328
	/**
329
	 * Computa el r?ster del fichero geogr?fico
330
	 * 
331
	 * @param coverage
332
	 * @return RasterBuf
333
	 */
334
	private RasterBuf getRaster(File coverage) {
335
		RasterBuf raster = null;
336

  
337
		Gdal migdal = new Gdal();
338
		GeoTransform gt = null;
339
		int ancho = 0, alto = 0;
340
		GdalRasterBand mirasterband = null;
341
		int rastercount = 0;
342

  
343
		try {
344
			migdal.open(coverage.getAbsolutePath(), Gdal.GA_ReadOnly);
345
		} catch (Exception ge) {
346
			ge.printStackTrace();
347
		}
348

  
349
		try {
350
			gt = migdal.getGeoTransform();
351
			//	System.out.println("Origin =
352
			// ("+gt.adfgeotransform[0]+","+gt.adfgeotransform[3]+")");
353
			//	System.out.println("Pixel Size =
354
			// ("+gt.adfgeotransform[1]+","+gt.adfgeotransform[5]+")");
355
		} catch (GdalException e) {
356
			System.out
357
					.println("I can't obtain the array geoTransform for this image");
358
		}
359

  
360
		try {
361
			rastercount = migdal.getRasterCount();
362
			ancho = migdal.getRasterXSize();
363
			alto = migdal.getRasterYSize();
364
			int ngcp = migdal.getGCPCount();
365
			//System.out.println("NGCP="+ngcp);
366
		} catch (GdalException ge) {
367
			ge.printStackTrace();
368
			//...
369
		}
370

  
371
		try {
372
			int dataType = migdal.getRasterBand(1).getRasterDataType();
373
			GdalBuffer buf = null;
374

  
375
			/*
376
			 * Evita el problema de que haya m?s de 3 bandas en el fichero esto
377
			 * deberia hacerse asignando a las 3 bandas del raster una banda
378
			 * distinta del fichero (no necesariamente 1->0, 2->1 y 3->2
379
			 */
380

  
381
			if (rastercount > 3)
382
				rastercount = 3;
383
			if (dataType == Gdal.GDT_Byte) {
384
				raster = new RasterBuf(RasterBuf.TYPE_INT, ancho, alto,
385
						rastercount, null);
386
				for (int iBand = 0; iBand < rastercount; iBand++) {
387
					mirasterband = migdal.getRasterBand(iBand + 1);
388
					buf = mirasterband.readRaster(0, 0, ancho, alto, ancho,
389
							alto, dataType);
390
					for (int y = 0; y < alto; y++)
391
						for (int x = 0; x < ancho; x++)
392
							raster.setPixelInt(x, y, iBand, buf.buffByte[y
393
									* ancho + x] & 0xff);
394
				}
395
				migdal.close();
396
			} else if (dataType == Gdal.GDT_CInt16
397
					|| dataType == Gdal.GDT_UInt16
398
					|| dataType == Gdal.GDT_Int16) {
399
				raster = new RasterBuf(RasterBuf.TYPE_INT, ancho, alto,
400
						rastercount, null);
401
				for (int iBand = 0; iBand < rastercount; iBand++) {
402
					// System.out.println("Banda:"+iBand);
403
					mirasterband = migdal.getRasterBand(iBand + 1);
404
					buf = mirasterband.readRaster(0, 0, ancho, alto, ancho,
405
							alto, dataType);
406
					for (int y = 0; y < alto; y++)
407
						for (int x = 0; x < ancho; x++)
408
							raster.setPixelInt(x, y, iBand, buf.buffShort[y
409
									* ancho + x] & 0xffff);
410
				}
411
				migdal.close();
412
			}
413
		} catch (GdalException e1) {
414
			e1.printStackTrace();
415
		}
416
		return raster;
417
	}
418

  
419
	public void setBBox(Rectangle2D rect) {
420
		double x1 = rect.getMinX();
421
		double y1 = rect.getMinY();
422
		double x2 = rect.getMaxX();
423
		double y2 = rect.getMaxY();
424
		if (x1 < fullExtent.getMinX())
425
			x1 = fullExtent.getMinX();
426
		if (y1 < fullExtent.getMinY())
427
			y1 = fullExtent.getMinY();
428
		if (x2 > fullExtent.getMaxX())
429
			x2 = fullExtent.getMaxX();
430
		if (y2 > fullExtent.getMaxY())
431
			y2 = fullExtent.getMaxY();
432

  
433
		if (bbox == null)
434
			bbox = new Rectangle2D.Double();
435

  
436
		bbox.setRect(x1, y1, x2 - x1, y2 - y1);
437
	}
438

  
439
	public void setFormat(String format) {
440
		this.format = format;
441
	}
442

  
443
	public void setSRS(String srs) {
444
		this.SRS = srs;
445
	}
446

  
447
	public String getSRS() {
448
		return this.SRS;
449
	}
450

  
451
	public FMapWCSDriver getWCSClient() {
452
		return wcs;
453
	}
454

  
455
	/**
456
	 * Para establecer los valores iniciales que se incluiran en la consulta.
457
	 * 
458
	 * @param layersRectangle
459
	 * @param srs
460
	 * @param format
461
	 * @param parametro
462
	 * @param times
463
	 */
464
	public void setCoverageQuery(Rectangle2D bBox, Dimension sz) {
465
		String coverage = "COVERAGE=" + coverageName;
466
		String crs = "&CRS=" + SRS;
467
		String boundingBox = "&BBOX=" + bBox.getMinX() + "," + bBox.getMinY()
468
				+ "," + bBox.getMaxX() + "," + bBox.getMaxY();
469
		String time = this.time != null ? "&" + this.time : "";
470
		String param = parameter != null ? "&" + parameter : "";
471
		double res = bBox.getWidth() / sz.getWidth();
472
		// Calculo cutre para ver si son grados (o metros). Si da menos de 180
473
		// es que son grados y lo paso a metros. (LWS)
474
		if (bBox.getMaxX() < 181D)
475
			res *= METROSXGRADO;
476
		System.out.println(" resolucion = " + res);
477
		String resX = "&RESX=" + res;
478
		String resY = "&RESY=" + res;
479
		
480
		//		String width = "&WIDTH="+ (int) sz.getWidth();
481
		//		String height = "&HEIGHT="+ (int) sz.getHeight();
482
		
483
		String format = "&FORMAT=" + this.format;
484
		coverageQuery = coverage + crs + boundingBox + time + param + resX	+ resY + format;
485
	}
486

  
487
	/**
488
	 * @param times
489
	 */
490
	public void setTimes(String times) {
491
		this.time = times;
492
	}
493

  
494
	public void setParameter(String prm) {
495
		this.parameter = prm;
496
	}
497

  
498
	/**
499 161
	 * Recupera el nom de la capa mostrada al TOC. El text per al nom no ?s el
500 162
	 * nom de la covertura sin? el texte que apareix al camp de text del wizard,
501 163
	 * que es correspon amb la "label" que el servidor especifica per a la
......
522 184
		if (toc == null) {
523 185
			try {
524 186
				WCSWizardDataSource dataSource = new WCSDataSourceAdapter();
525
				WCSWizardData data = dataSource.detalles(host);
187
				WCSWizardData data = dataSource.detalles( new URL(getHost()) );
526 188

  
527 189
				toc = new WCSParamsPanel();
528 190
					
......
532 194
				
533 195
				toc.getLstCoverages().setListData(data.getCoverageNames());
534 196
				
535
				int index = toc.getCoverageIndex(coverageName);
197
				int index = toc.getCoverageIndex(wcs.getCoverageName());
536 198
				if (index != -1)
537 199
					toc.getLstCoverages().setSelectedIndex(index);
538 200
				
539 201
				toc.actualizaWizard();
540 202
				
541
				index = toc.getSRSIndex(SRS);
203
				index = toc.getSRSIndex(wcs.getSRS());
542 204
				if (index != -1)
543 205
					toc.getLstCRSs().setSelectedIndex(index);
544
				index = toc.getFormatIndex(format);
206
				index = toc.getFormatIndex(wcs.getFormat());
545 207
				if (index != -1)
546 208
					toc.getLstFormats().setSelectedIndex(index);
547
				if (time != null)
548
					toc.getLstSelectedTimes().setListData(time.split("=")[1].split(","));
209
				if (wcs.getTime() != null)
210
					toc.getLstSelectedTimes().setListData(wcs.getTime().split("=")[1].split(","));
549 211
				/* vale, i ara.. per al cas d'intervals continus? se m'ocorre
550 212
				 * fer un text field. Per? aix? ja vindr? si ha de vindre.
551 213
				 */
552
				if (parameter!= null){
553
					String[] s = parameter.split("=");
214
				if (wcs.getParameter()!= null){
215
					String[] s = wcs.getParameter().split("=");
554 216
					String pName = s[0];
555 217
					String[] pVals = s[1].split(",");
556 218
					index = toc.getParamIndex(pName);
557 219
					toc.getCmbParam().setSelectedIndex(index);
558
					toc.actualizaListaParametros(coverageName);
220
					toc.actualizaListaParametros(wcs.getCoverageName());
559 221
					int[] indexes = new int[pVals.length];
560 222
					for (int i = 0; i < pVals.length; i++) {
561 223
						indexes[i] = toc.getValueIndex(pVals[i]);
......
576 238
		}
577 239
		return toc;
578 240
	}
241
	public String getCoverageName() {
242
		return wcs.getCoverageName();
243
	}
244
	public String getFormat() {
245
		return wcs.getFormat();
246
	}
247
	public String getHost() {
248
		return wcs.getHost();
249
	}
250
	public String getParameter() {
251
		return wcs.getParameter();
252
	}
253
	public String getSRS() {
254
		return wcs.getSRS();
255
	}
256
	public String getTime() {
257
		return wcs.getTime();
258
	}
259
	public void setFormat(String format) {
260
		wcs.setFormat(format);
261
	}
262
	public void setHost(String host) {
263
		if (wcs == null) {
264
			wcs = new FMapWCSAdaptor();
265
			wcs.setDriver(new FMapWCSDriver());
266
		}
267
		wcs.setHost(host);
268
	}
269
	public void setParameter(String parameter) {
270
		wcs.setParameter(parameter);
271
	}
272
	public void setSRS(String srs) {
273
		wcs.setSRS(srs);
274
	}
275
	public void setTime(String time) {
276
		wcs.setTime(time);
277
	}
278
	public void setCoverageName(String coverageName) {
279
		wcs.setCoverageName(coverageName);
280
	}
281
	
579 282
}

Also available in: Unified diff