svn-gvsig-desktop / branches / CqCMSDvp / libraries / libCq CMS for java.old / src / org / cresques / px / PxRaster.java @ 1809
History | View | Annotate | Download (14.8 KB)
1 |
/*
|
---|---|
2 |
* PxRaster.java
|
3 |
*/
|
4 |
package org.cresques.px; |
5 |
|
6 |
import java.awt.Color; |
7 |
import java.awt.Component; |
8 |
import java.awt.Graphics2D; |
9 |
import java.awt.Image; |
10 |
import java.awt.geom.GeneralPath; |
11 |
import java.awt.geom.Point2D; |
12 |
import java.awt.image.DataBuffer; |
13 |
import java.awt.image.ImageObserver; |
14 |
import java.lang.reflect.InvocationTargetException; |
15 |
import java.util.Date; |
16 |
import java.util.Vector; |
17 |
|
18 |
import org.cresques.cts.ICoordTrans; |
19 |
import org.cresques.cts.IProjection; |
20 |
import org.cresques.geo.Projected; |
21 |
import org.cresques.geo.ViewPortData; |
22 |
import org.cresques.io.GdalFile; |
23 |
import org.cresques.io.GeoRasterFile; |
24 |
import org.cresques.io.raster.ComputeMinMaxFilter; |
25 |
import org.cresques.io.raster.ComputeMinMaxImageFilter; |
26 |
import org.cresques.io.raster.RasterFilterStack; |
27 |
import org.cresques.io.raster.LinearEnhancementFilter; |
28 |
import org.cresques.io.raster.LinearEnhancementImageFilter; |
29 |
import org.cresques.io.raster.PercentTailTrimFilter; |
30 |
import org.cresques.io.raster.PercentTailTrimImageFilter; |
31 |
import org.cresques.io.raster.RasterBuf; |
32 |
import org.cresques.io.raster.RasterStats; |
33 |
import org.cresques.io.raster.RasterToImageFilter; |
34 |
import org.cresques.io.raster.TransparencyFilter; |
35 |
import org.cresques.io.raster.TransparencyImageFilter; |
36 |
|
37 |
public class PxRaster extends PxObj implements Projected { |
38 |
public GeoRasterFile geoFile = null; |
39 |
protected ImageObserver component = null; |
40 |
Vector pts = null; |
41 |
// Soporte para n bandas, visibles de 3 en 3, en ficheros separados
|
42 |
protected GeoRasterFile [] colorBand = null; |
43 |
protected int rBand = 1, gBand = 2, bBand = 3; |
44 |
|
45 |
public RasterStats stats = new RasterStats(); |
46 |
int transparente = 0x10ffff80; |
47 |
|
48 |
String vName = null; |
49 |
protected boolean pintaMarco = false; //true; |
50 |
IProjection proj = null;
|
51 |
protected Extent extentOrig = null; |
52 |
ICoordTrans rp = null;
|
53 |
|
54 |
public RasterFilterStack filterStack = new RasterFilterStack(stats); |
55 |
|
56 |
/*public boolean transparencyFilterEnabled = false;
|
57 |
public boolean enhancedFilterEnabled = false;
|
58 |
public boolean tailFilterEnabled = false;*/
|
59 |
|
60 |
/**
|
61 |
* Constructor.
|
62 |
* @param component
|
63 |
*/
|
64 |
public PxRaster(ImageObserver component) { |
65 |
this.component = component;
|
66 |
} |
67 |
|
68 |
public PxRaster(IProjection proj, String fname, ImageObserver component) { |
69 |
geoFile = GeoRasterFile.openFile(proj, fname);//loadECW(fname);
|
70 |
geoFile.setUpdatable((Component) component);
|
71 |
this.proj = proj;
|
72 |
this.component = component;
|
73 |
setExtent(geoFile.getExtent()); |
74 |
geoFile.setView(geoFile.getExtent()); |
75 |
extentOrig = extent; |
76 |
if (geoFile.getBandCount()>2) { |
77 |
setBand(GeoRasterFile.RED_BAND, 0);
|
78 |
setBand(GeoRasterFile.GREEN_BAND, 1);
|
79 |
setBand(GeoRasterFile.BLUE_BAND, 2);
|
80 |
} else {
|
81 |
setBand(GeoRasterFile.RED_BAND|GeoRasterFile.GREEN_BAND|GeoRasterFile.BLUE_BAND, 0);
|
82 |
} |
83 |
} |
84 |
|
85 |
/*public PxRaster(String fname, ImageObserver component, Extent view) {
|
86 |
geoFile = GeoRasterFile.openFile(null, fname);//loadECW(fname);
|
87 |
this.component = component;
|
88 |
setExtent(geoFile.getExtent());
|
89 |
geoFile.setView(view);
|
90 |
extentOrig = extent;
|
91 |
|
92 |
//double x = 680800,y = 4128600;
|
93 |
//setView(new Extent(x, y, x+4200, y-4200),"Huelva capital");
|
94 |
}*/
|
95 |
|
96 |
public PxRaster(IProjection proj, String [] fnames, ImageObserver component) { |
97 |
this.proj = proj;
|
98 |
this.component = component;
|
99 |
colorBand = new GeoRasterFile[fnames.length];
|
100 |
for (int i=0; i<colorBand.length; i++) { |
101 |
colorBand[i] = GeoRasterFile.openFile(proj, fnames[i]);//loadECW(fname);
|
102 |
colorBand[i].setUpdatable((Component) component);
|
103 |
setExtent(colorBand[i].getExtent()); |
104 |
colorBand[i].setView(colorBand[i].getExtent()); |
105 |
} |
106 |
geoFile = colorBand[0];
|
107 |
extentOrig = extent; |
108 |
if (fnames.length >= 3 || geoFile.getBandCount()>2) { |
109 |
setBand(GeoRasterFile.RED_BAND, 0);
|
110 |
setBand(GeoRasterFile.GREEN_BAND, 1);
|
111 |
setBand(GeoRasterFile.BLUE_BAND, 2);
|
112 |
} else {
|
113 |
setBand(GeoRasterFile.RED_BAND|GeoRasterFile.GREEN_BAND|GeoRasterFile.BLUE_BAND, 0);
|
114 |
} |
115 |
} |
116 |
|
117 |
public PxRaster(GeoRasterFile eFile, ImageObserver component, Extent view) { |
118 |
geoFile = eFile;//loadECW(fname);
|
119 |
geoFile.setUpdatable((Component) component);
|
120 |
setProjection(geoFile.getProjection()); |
121 |
this.component = component;
|
122 |
|
123 |
setExtent(geoFile.getExtent()); |
124 |
geoFile.setView(view); //geoFile.getExtent());
|
125 |
extentOrig = extent; |
126 |
if (geoFile.getBandCount()>2) { |
127 |
setBand(GeoRasterFile.RED_BAND, 0);
|
128 |
setBand(GeoRasterFile.GREEN_BAND, 1);
|
129 |
setBand(GeoRasterFile.BLUE_BAND, 2);
|
130 |
} else {
|
131 |
setBand(GeoRasterFile.RED_BAND|GeoRasterFile.GREEN_BAND|GeoRasterFile.BLUE_BAND, 0);
|
132 |
} |
133 |
} |
134 |
|
135 |
public int getBandCount() { return geoFile.getBandCount(); } |
136 |
|
137 |
public GeoRasterFile [] getFiles() { |
138 |
if (colorBand != null) |
139 |
return colorBand;
|
140 |
|
141 |
GeoRasterFile [] ret = {geoFile};
|
142 |
return ret;
|
143 |
} |
144 |
|
145 |
/**
|
146 |
* Asocia un colorBand al rojo, verde o azul.
|
147 |
* @param flag cual (o cuales) de las bandas.
|
148 |
* @param nBand que colorBand
|
149 |
*/
|
150 |
|
151 |
public void setBand(int flag, int nBand) { |
152 |
if (colorBand != null) { |
153 |
if ((flag & GeoRasterFile.RED_BAND) == GeoRasterFile.RED_BAND)
|
154 |
rBand = nBand; |
155 |
else if ((flag & GeoRasterFile.GREEN_BAND) == GeoRasterFile.GREEN_BAND) |
156 |
gBand = nBand; |
157 |
else if ((flag & GeoRasterFile.BLUE_BAND) == GeoRasterFile.BLUE_BAND) |
158 |
bBand = nBand; |
159 |
} else
|
160 |
geoFile.setBand(flag, nBand); |
161 |
} |
162 |
|
163 |
/**
|
164 |
* Devuelve el colorBand activo en la banda especificada.
|
165 |
* @param flag banda.
|
166 |
*/
|
167 |
|
168 |
public int getBand(int flag) { |
169 |
if (colorBand != null) { |
170 |
if (flag == GeoRasterFile.RED_BAND)
|
171 |
return rBand;
|
172 |
else if (flag == GeoRasterFile.GREEN_BAND) |
173 |
return gBand;
|
174 |
else if (flag == GeoRasterFile.BLUE_BAND) |
175 |
return bBand;
|
176 |
else
|
177 |
return -1; |
178 |
} else
|
179 |
return geoFile.getBand(flag);
|
180 |
} |
181 |
|
182 |
public void setDrawBorder(boolean pm) { pintaMarco = pm; } |
183 |
|
184 |
public String getFName() { |
185 |
return geoFile.getName();
|
186 |
} |
187 |
|
188 |
/**
|
189 |
* Devuelve la anchura total del fichero, en pixeles.
|
190 |
* @return ancho en pixeles
|
191 |
*/
|
192 |
public int getFWidth() { |
193 |
return geoFile.getWidth();
|
194 |
} |
195 |
|
196 |
/**
|
197 |
* Devuelve la altura total del fichero, en pixeles.
|
198 |
* @return alto en pixeles
|
199 |
*/
|
200 |
public int getFHeight() { |
201 |
return geoFile.getHeight();
|
202 |
} |
203 |
|
204 |
public void setTransparency(boolean t) {geoFile.setTransparency(t);} |
205 |
public void setTransparency(int t) {geoFile.setTransparency(t);} |
206 |
|
207 |
public int getAlpha() { return geoFile.getAlpha(); } |
208 |
|
209 |
public void setExtent(Extent e) { |
210 |
super.extent = e;
|
211 |
pts = new Vector(); |
212 |
pts.add(proj.createPoint(e.minX(), e.minY())); |
213 |
pts.add(proj.createPoint(e.maxX(), e.minY())); |
214 |
pts.add(proj.createPoint(e.maxX(), e.maxY())); |
215 |
pts.add(proj.createPoint(e.minX(), e.maxY())); |
216 |
} |
217 |
|
218 |
/**
|
219 |
* Cambia la vista (viewport) sobre el raster.
|
220 |
*
|
221 |
* @param v
|
222 |
* @param vName
|
223 |
*/
|
224 |
public void setView(Extent v, String vName) { |
225 |
geoFile.setView(v); |
226 |
this.vName = vName;
|
227 |
} |
228 |
|
229 |
/**
|
230 |
* Obtiene la escala.
|
231 |
*
|
232 |
* @param width
|
233 |
* @param height
|
234 |
* @return
|
235 |
*/
|
236 |
|
237 |
public double[] getScale(int width, int height) { |
238 |
double scale[] = new double[2]; |
239 |
scale[0] = ((double) width) /geoFile.getView().width(); |
240 |
scale[1] = ((double) height)/geoFile.getView().height(); |
241 |
return scale;
|
242 |
} |
243 |
|
244 |
/**
|
245 |
* 'Normaliza' la vista en funci?n del extent del raster.
|
246 |
*
|
247 |
* @param mat
|
248 |
* @param sz
|
249 |
*/
|
250 |
|
251 |
protected void calculateNewView(Extent sz) { |
252 |
double vx = sz.minX(), vy = sz.minY(), vx2 = sz.maxX(), vy2 = sz.maxY();
|
253 |
if (sz.minX() < extent.minX()) vx = extent.minX();
|
254 |
if (sz.minY() < extent.minY()) vy = extent.minY();
|
255 |
if (sz.maxX() > extent.maxX()) vx2 = extent.maxX();
|
256 |
if (sz.maxY() > extent.maxY()) vy2 = extent.maxY();
|
257 |
if (colorBand != null) { // multiBands |
258 |
for (int i=0; i<colorBand.length; i++) |
259 |
colorBand[i].setView(new Extent(vx, vy, vx2, vy2));
|
260 |
} else if (geoFile != null) { // Una sola banda |
261 |
geoFile.setView(new Extent(vx, vy, vx2, vy2));
|
262 |
} else { // no cargada |
263 |
System.err.println("PxRaster.calculateNewView(): Foto no cargada."); |
264 |
} |
265 |
} |
266 |
|
267 |
/**
|
268 |
* Dibuja el raster
|
269 |
*/
|
270 |
|
271 |
public void draw(Graphics2D g, ViewPortData vp) { |
272 |
Image geoImage = null; |
273 |
long t2, t1 = new Date().getTime(); |
274 |
System.out.println("PxRaster.draw(): vp.extent = "+ vp.getExtent()); |
275 |
System.out.println("PxRaster.draw(): extent = "+ getExtent()); |
276 |
if (vp.getExtent().minX()> extent.maxX()) return; |
277 |
if (vp.getExtent().minY()> extent.maxY()) return; |
278 |
if (vp.getExtent().maxX()< extent.minX()) return; |
279 |
if (vp.getExtent().maxY()< extent.minY()) return; |
280 |
calculateNewView(vp.getExtent()); |
281 |
Extent v = geoFile.getView(); |
282 |
double x = v.minX();
|
283 |
double y = v.minY();
|
284 |
double w = v.width();
|
285 |
double h = v.height();
|
286 |
System.out.println("Pinto PxRaster:" + v); |
287 |
|
288 |
double scalex = vp.mat.getScaleX() /* g.getTransform().getScaleX()*/ , |
289 |
scaley = vp.mat.getScaleY() /* g.getTransform().getScaleY() */;
|
290 |
int wImg = (int) Math.abs(w*scalex)+1, hImg = (int) Math.abs(h*scaley)+1; |
291 |
System.out.println("Sz=("+wImg+","+hImg+"); scale=("+scalex+","+scaley+")"); |
292 |
if (wImg <= 0 || hImg <= 0) return; |
293 |
|
294 |
Point2D.Double pt = new Point2D.Double(x, y+h); |
295 |
try {
|
296 |
System.out.println("Dibujando PxRaster: pt0 = "+pt); |
297 |
vp.mat.transform(pt, pt); |
298 |
System.out.println("Dibujando PxRaster: pt1 = "+pt); |
299 |
if (colorBand == null && geoFile instanceof GdalFile && |
300 |
(geoFile.getDataType() != DataBuffer.TYPE_BYTE)) {
|
301 |
System.out.println("PxRaster: Has dado con un Raster de 16 bits"); |
302 |
System.out.println("Dibujando PxRaster sz=("+wImg+","+hImg+"..."); |
303 |
RasterBuf raster = ((GdalFile) geoFile).getRaster(wImg, hImg, rp); |
304 |
t2 = new Date().getTime(); |
305 |
System.out.println("Dibujando PxRaster: "+(t2-t1)/1000D+", secs. Obteniendo"); t1 = t2; |
306 |
geoImage = renderizeRaster(raster); |
307 |
t2 = new Date().getTime(); |
308 |
System.out.println("Dibujando PxRaster: "+(t2-t1)/1000D+", secs. Filtrando/Renderizando");t1 = t2; |
309 |
|
310 |
filterStack.setDataTypeInFilter(RasterBuf.TYPE_SHORT); |
311 |
filterStack.setInitRasterBuf(raster); |
312 |
|
313 |
g.drawImage(geoImage, (int) Math.round(pt.getX()), (int) Math.round(pt.getY()), component); |
314 |
t2 = new Date().getTime(); |
315 |
System.out.println("Dibujando PxRaster: "+(t2-t1)/1000D+", secs. Dibujando"); |
316 |
} else if (colorBand != null) { // multiBands |
317 |
System.out.println("Dibujando PxRaster (Multifile) ..."); |
318 |
//if (doTransparency)
|
319 |
//geoImage = new BufferedImage(wImg, hImg, BufferedImage.TYPE_INT_ARGB);
|
320 |
//else
|
321 |
// geoImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
|
322 |
geoImage = colorBand[rBand].updateImage(wImg, hImg, rp); //, geoImage, GeoRasterFile.RED_BAND);
|
323 |
geoImage = colorBand[gBand].updateImage(wImg, hImg, rp, geoImage, GeoRasterFile.GREEN_BAND); |
324 |
geoImage = colorBand[bBand].updateImage(wImg, hImg, rp, geoImage, GeoRasterFile.BLUE_BAND); |
325 |
|
326 |
filterStack.setDataTypeInFilter(RasterBuf.TYPE_IMAGE); //Image
|
327 |
filterStack.setInitRasterBuf(geoImage); |
328 |
|
329 |
geoImage = renderizeRaster(geoImage); |
330 |
g.drawImage(geoImage, (int) Math.round(pt.getX()), (int) Math.round(pt.getY()), component); |
331 |
t2 = new Date().getTime(); |
332 |
System.out.println("Dibujando PxRaster: "+(t2-t1)/1000D+", secs."); |
333 |
|
334 |
} else if (geoFile != null) { // Una sola banda |
335 |
geoImage = geoFile.updateImage(wImg, hImg, rp); |
336 |
System.out.println("Dibujando PxRaster sz=("+wImg+","+hImg+"..."); |
337 |
|
338 |
filterStack.setDataTypeInFilter(RasterBuf.TYPE_IMAGE); //Image
|
339 |
filterStack.setInitRasterBuf(geoImage); |
340 |
|
341 |
geoImage = renderizeRaster(geoImage); |
342 |
g.drawImage(geoImage, (int) Math.round(pt.getX()), (int) Math.round(pt.getY()), component); |
343 |
t2 = new Date().getTime(); |
344 |
System.out.println("Dibujando PxRaster: "+(t2-t1)/1000D+", secs."); |
345 |
} else { // no cargada |
346 |
System.err.println("Dibujando PxRaster: Foto no cargada."); |
347 |
} |
348 |
} catch (Exception e) { |
349 |
e.printStackTrace(); |
350 |
}; |
351 |
|
352 |
if (pintaMarco) {
|
353 |
drawMarco(g, vp); |
354 |
} |
355 |
} |
356 |
|
357 |
/**
|
358 |
*
|
359 |
* @param raster
|
360 |
* @return
|
361 |
*/
|
362 |
public Image renderizeRaster(RasterBuf raster) { |
363 |
if (filterStack != null){ |
364 |
filterStack.execute(raster); |
365 |
} |
366 |
|
367 |
//Aplicamos el filtro para convertir a Image
|
368 |
RasterToImageFilter rti = new RasterToImageFilter();
|
369 |
rti.addParam("raster", raster);
|
370 |
rti.addParam("alpha", new Integer(this.getAlpha())); |
371 |
rti.execute(); |
372 |
raster = null;
|
373 |
return (Image)rti.getResult("raster"); |
374 |
} |
375 |
|
376 |
/**
|
377 |
*
|
378 |
* @param image
|
379 |
* @return
|
380 |
*/
|
381 |
public Image renderizeRaster(Image image) { |
382 |
if (filterStack != null){ |
383 |
filterStack.execute(image); |
384 |
} |
385 |
return image;
|
386 |
} |
387 |
|
388 |
/**
|
389 |
*
|
390 |
* @param g
|
391 |
* @param vp
|
392 |
*/
|
393 |
public void drawMarco(Graphics2D g, ViewPortData vp) { |
394 |
// Color color = new Color(255,222,165,128), fillColor = new Color(255,214,132,128);
|
395 |
Color color = new Color(128,128,128), fillColor = new Color(255,220,220,0x20); |
396 |
GeneralPath gp = newGP(vp);
|
397 |
g.setColor(fillColor); |
398 |
g.fill(gp); |
399 |
g.setColor(color); |
400 |
g.draw(gp); |
401 |
} |
402 |
|
403 |
private GeneralPath newGP(ViewPortData vp) { |
404 |
//if (gp != null) return;
|
405 |
GeneralPath gp = new GeneralPath(); |
406 |
Point2D.Double pt0 = new Point2D.Double(0.0, 0.0); |
407 |
Point2D.Double pt1 = new Point2D.Double(0.0, 0.0); |
408 |
Point2D.Double pt2 = new Point2D.Double(0.0, 0.0); |
409 |
Point2D.Double pt3 = new Point2D.Double(0.0, 0.0); |
410 |
vp.mat.transform((Point2D) pts.get(0), pt0); |
411 |
vp.mat.transform((Point2D) pts.get(1), pt1); |
412 |
vp.mat.transform((Point2D) pts.get(2), pt2); |
413 |
vp.mat.transform((Point2D) pts.get(3), pt3); |
414 |
// Aspa desde el extent
|
415 |
gp.moveTo( (float) pt0.getX(), (float) pt0.getY() ); |
416 |
gp.lineTo( (float) pt2.getX(), (float) pt2.getY() ); |
417 |
gp.moveTo( (float) pt1.getX(), (float) pt1.getY() ); |
418 |
gp.lineTo( (float) pt3.getX(), (float) pt3.getY() ); |
419 |
// Extent
|
420 |
gp.moveTo( (float) pt0.getX(), (float) pt0.getY() ); |
421 |
gp.lineTo( (float) pt1.getX(), (float) pt1.getY() ); |
422 |
gp.lineTo( (float) pt2.getX(), (float) pt2.getY() ); |
423 |
gp.lineTo( (float) pt3.getX(), (float) pt3.getY() ); |
424 |
if (extentOrig != extent) {
|
425 |
gp.lineTo( (float) pt0.getX(), (float) pt0.getY() ); |
426 |
Vector pts = new Vector(); |
427 |
pts.add(proj.createPoint(extentOrig.minX(), extentOrig.minY())); |
428 |
pts.add(proj.createPoint(extentOrig.maxX(), extentOrig.minY())); |
429 |
pts.add(proj.createPoint(extentOrig.maxX(), extentOrig.maxY())); |
430 |
pts.add(proj.createPoint(extentOrig.minX(), extentOrig.maxY())); |
431 |
|
432 |
vp.mat.transform((Point2D) pts.get(0), pt0); |
433 |
vp.mat.transform((Point2D) pts.get(1), pt1); |
434 |
vp.mat.transform((Point2D) pts.get(2), pt2); |
435 |
vp.mat.transform((Point2D) pts.get(3), pt3); |
436 |
gp.moveTo( (float) pt0.getX(), (float) pt0.getY() ); |
437 |
gp.lineTo( (float) pt1.getX(), (float) pt1.getY() ); |
438 |
gp.lineTo( (float) pt2.getX(), (float) pt2.getY() ); |
439 |
gp.lineTo( (float) pt3.getX(), (float) pt3.getY() ); |
440 |
} |
441 |
gp.closePath(); |
442 |
return gp;
|
443 |
} |
444 |
|
445 |
public IProjection getProjection() { return proj; } |
446 |
public void setProjection(IProjection p) { proj = p; } |
447 |
|
448 |
public void reProject(ICoordTrans rp) { |
449 |
this.rp = rp.getInverted();
|
450 |
System.out.println("PxRaster: reProject()"); |
451 |
//geoFile.reProject(rp);
|
452 |
Vector savePts = pts;
|
453 |
|
454 |
pts = new Vector(); |
455 |
extent = new Extent();
|
456 |
Point2D ptDest = null; |
457 |
for (int i=0; i<savePts.size(); i++) { |
458 |
ptDest = rp.getPDest().createPoint(0.0,0.0); |
459 |
ptDest = rp.convert((Point2D) savePts.get(i), ptDest);
|
460 |
pts.add(ptDest); |
461 |
extent.add(ptDest); |
462 |
} |
463 |
setProjection(rp.getPDest()); |
464 |
} |
465 |
} |