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