root / trunk / extensions / extRasterTools-SE / src / org / gvsig / fmap / layers / FLyrRasterSE.java @ 10940
History | View | Annotate | Download (28 KB)
1 | 10740 | nacho | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2007 IVER T.I. and Generalitat Valenciana.
|
||
4 | *
|
||
5 | * This program is free software; you can redistribute it and/or
|
||
6 | * modify it under the terms of the GNU General Public License
|
||
7 | * as published by the Free Software Foundation; either version 2
|
||
8 | * of the License, or (at your option) any later version.
|
||
9 | *
|
||
10 | * This program is distributed in the hope that it will be useful,
|
||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
13 | * GNU General Public License for more details.
|
||
14 | *
|
||
15 | * You should have received a copy of the GNU General Public License
|
||
16 | * along with this program; if not, write to the Free Software
|
||
17 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
||
18 | */
|
||
19 | package org.gvsig.fmap.layers; |
||
20 | |||
21 | import java.awt.Dimension; |
||
22 | import java.awt.Graphics2D; |
||
23 | import java.awt.Point; |
||
24 | import java.awt.Rectangle; |
||
25 | import java.awt.geom.AffineTransform; |
||
26 | import java.awt.geom.NoninvertibleTransformException; |
||
27 | import java.awt.geom.Point2D; |
||
28 | import java.awt.geom.Rectangle2D; |
||
29 | import java.awt.image.BufferedImage; |
||
30 | import java.io.File; |
||
31 | import java.io.IOException; |
||
32 | import java.lang.reflect.Constructor; |
||
33 | import java.lang.reflect.InvocationTargetException; |
||
34 | import java.util.ArrayList; |
||
35 | |||
36 | import javax.print.attribute.PrintRequestAttributeSet; |
||
37 | import javax.swing.ImageIcon; |
||
38 | |||
39 | import org.cresques.cts.IProjection; |
||
40 | import org.cresques.filter.RasterFilterStack; |
||
41 | import org.gvsig.fmap.drivers.GenericRasterDriver; |
||
42 | import org.gvsig.fmap.drivers.GeoRasterDriver; |
||
43 | import org.gvsig.raster.dataaccess.DataSource; |
||
44 | 10940 | nacho | import org.gvsig.raster.dataset.IBuffer; |
45 | import org.gvsig.raster.dataset.RasterDriverException; |
||
46 | import org.gvsig.raster.dataset.RasterMultiDataset; |
||
47 | import org.gvsig.raster.dataset.properties.DatasetMetadata; |
||
48 | 10740 | nacho | import org.gvsig.raster.grid.GridTransparency; |
49 | import org.gvsig.raster.grid.filter.RasterFilterList; |
||
50 | import org.gvsig.raster.grid.render.Rendering; |
||
51 | import org.gvsig.raster.shared.Extent; |
||
52 | import org.gvsig.raster.shared.IRasterDataset; |
||
53 | import org.gvsig.raster.shared.IRasterProperties; |
||
54 | |||
55 | import com.hardcode.driverManager.Driver; |
||
56 | import com.hardcode.driverManager.DriverLoadException; |
||
57 | import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
||
58 | import com.hardcode.gdbms.engine.data.driver.DriverException; |
||
59 | import com.iver.andami.messages.NotificationManager; |
||
60 | import com.iver.cit.gvsig.fmap.MapControl; |
||
61 | import com.iver.cit.gvsig.fmap.ViewPort; |
||
62 | import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
||
63 | import com.iver.cit.gvsig.fmap.drivers.RasterDriver; |
||
64 | import com.iver.cit.gvsig.fmap.layers.FLyrDefault; |
||
65 | import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
||
66 | import com.iver.cit.gvsig.fmap.layers.LayerListener; |
||
67 | import com.iver.cit.gvsig.fmap.layers.RasterOperations; |
||
68 | import com.iver.cit.gvsig.fmap.layers.StatusLayerRaster; |
||
69 | import com.iver.cit.gvsig.fmap.layers.StatusRasterInterface; |
||
70 | import com.iver.cit.gvsig.fmap.layers.Tiling; |
||
71 | import com.iver.cit.gvsig.fmap.layers.XMLException; |
||
72 | import com.iver.cit.gvsig.fmap.layers.layerOperations.StringXMLItem; |
||
73 | import com.iver.cit.gvsig.fmap.layers.layerOperations.XMLItem; |
||
74 | import com.iver.utiles.XMLEntity; |
||
75 | import com.iver.utiles.swing.threads.Cancellable; |
||
76 | |||
77 | |||
78 | /**
|
||
79 | *
|
||
80 | * @author Nacho Brodin (nachobrodin@gmail.com)
|
||
81 | *
|
||
82 | */
|
||
83 | //TODO: RasterOperations FMap es a extinguir.
|
||
84 | public class FLyrRasterSE extends FLyrDefault implements RasterOperations, IRasterProperties, IRasterDataset{ |
||
85 | private RasterSEAdapter source;
|
||
86 | boolean isPrinting = false; |
||
87 | boolean mustTileDraw = false; |
||
88 | boolean mustTilePrint = true; |
||
89 | private int maxTileDrawWidth = 200; |
||
90 | private int maxTileDrawHeight = 200; |
||
91 | int maxTilePrintWidth = 1500; |
||
92 | int maxTilePrintHeight = 1500; |
||
93 | private StatusRasterInterface status = null; |
||
94 | private boolean firstLoad = false; |
||
95 | private int posX = 0, posY = 0; |
||
96 | private double posXWC = 0, posYWC = 0; |
||
97 | private int r = 0, g = 0, b = 0; |
||
98 | private boolean removeRasterFlag = true; |
||
99 | |||
100 | /**
|
||
101 | * Crea una capa Raster a partir del nombre driver, fichero y proyecci?n.
|
||
102 | * @param layerName Nombre de la capa.
|
||
103 | * @param d RasterDriver.
|
||
104 | * @param f Fichero.
|
||
105 | * @param proj Proyecci?n.
|
||
106 | * @return Nueva capa de tipo raster.
|
||
107 | * @throws DriverIOException
|
||
108 | */
|
||
109 | public static FLyrRasterSE createLayer(String layerName, GenericRasterDriver d, |
||
110 | File f, IProjection proj) {
|
||
111 | RasterSEAdapter adapter = new RasterSEFileAdapter(f);
|
||
112 | adapter.setDriver(d); |
||
113 | |||
114 | FLyrRasterSE capa = new FLyrRasterSE();
|
||
115 | capa.setName(layerName); |
||
116 | capa.setSource(adapter); |
||
117 | capa.setProjection(proj); |
||
118 | |||
119 | capa.load(); |
||
120 | |||
121 | return capa;
|
||
122 | } |
||
123 | |||
124 | /**
|
||
125 | * Devuelve el RasterAdapter de la capa.
|
||
126 | *
|
||
127 | * @return RasterAdapter.
|
||
128 | */
|
||
129 | public RasterSEAdapter getSource() {
|
||
130 | return source;
|
||
131 | } |
||
132 | |||
133 | /**
|
||
134 | *Redefine wakeUp de FLyrDefault
|
||
135 | */
|
||
136 | public void wakeUp(){ |
||
137 | } |
||
138 | |||
139 | /**
|
||
140 | * Inserta el RasterAdapter.
|
||
141 | *
|
||
142 | * @param ra RasterAdapter.
|
||
143 | */
|
||
144 | public void setSource(RasterSEAdapter ra) { |
||
145 | source = ra; |
||
146 | } |
||
147 | |||
148 | /**
|
||
149 | * Devuelve la pila de filtros aplicada sobre la capa raster.
|
||
150 | *
|
||
151 | * @return RasterFilterStack.
|
||
152 | */
|
||
153 | public RasterFilterStack getFilterStack(){
|
||
154 | return null; |
||
155 | } |
||
156 | |||
157 | /**
|
||
158 | * Asignar el estado del raster
|
||
159 | * @param status
|
||
160 | */
|
||
161 | public void setStatus(StatusRasterInterface status){ |
||
162 | this.status = status;
|
||
163 | } |
||
164 | |||
165 | /**
|
||
166 | * Asigna la pila de filtros aplicada al raster
|
||
167 | * @return
|
||
168 | */
|
||
169 | public void setFilterStack(RasterFilterStack stack){ |
||
170 | //source.setFilterStack(stack);
|
||
171 | } |
||
172 | |||
173 | /**
|
||
174 | * Obtiene el estado del raster
|
||
175 | * @return
|
||
176 | */
|
||
177 | public StatusRasterInterface getStatus(){
|
||
178 | return this.status; |
||
179 | } |
||
180 | |||
181 | /**
|
||
182 | * Asigna la posici?n en la que se ha hecho click al mostrar
|
||
183 | * informaci?n del raster.
|
||
184 | * @param x Posici?n en X
|
||
185 | * @param y Posici?n en Y
|
||
186 | */
|
||
187 | public void setPos(int x, int y){ |
||
188 | this.posX = x;
|
||
189 | this.posY = y;
|
||
190 | } |
||
191 | |||
192 | /**
|
||
193 | * Asigna la posici?n en coordenadas del mundo real en la que se ha
|
||
194 | * hecho click al mostrar informaci?n del raster.
|
||
195 | * @param x Posici?n en X
|
||
196 | * @param y Posici?n en Y
|
||
197 | */
|
||
198 | public void setPosWC(double x, double y){ |
||
199 | this.posXWC = x;
|
||
200 | this.posYWC = y;
|
||
201 | } |
||
202 | |||
203 | /**
|
||
204 | * Asigna RGB en la posici?n en la que se ha
|
||
205 | * hecho click al mostrar informaci?n del raster.
|
||
206 | * @param r valor de rojo
|
||
207 | * @param g valor de verde
|
||
208 | * @param b valor de azul
|
||
209 | */
|
||
210 | public void setRGB(int r, int g, int b){ |
||
211 | this.r = r;
|
||
212 | this.g = g;
|
||
213 | this.b = b;
|
||
214 | } |
||
215 | |||
216 | /*
|
||
217 | * @see com.iver.cit.gvsig.fmap.layers.FLayer#load()
|
||
218 | */
|
||
219 | public void load() { |
||
220 | try {
|
||
221 | ((RasterSEFileAdapter) source).start(); |
||
222 | } catch (DriverIOException e) {
|
||
223 | NotificationManager.addError("Error en la carga de capa", e);
|
||
224 | } |
||
225 | ((RasterSEFileAdapter) source).setTransparency(getTransparency()); |
||
226 | } |
||
227 | |||
228 | /**
|
||
229 | * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#draw(java.awt.image.BufferedImage,
|
||
230 | * java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort,
|
||
231 | * com.iver.utiles.swing.threads.Cancellable)
|
||
232 | */
|
||
233 | public void draw(BufferedImage image, Graphics2D g, ViewPort vp, |
||
234 | Cancellable cancel,double scale){
|
||
235 | if (isWithinScale(scale)){
|
||
236 | try {
|
||
237 | if(status!=null && firstLoad){ |
||
238 | if(mustTileDraw){
|
||
239 | Point2D p = vp.getOffset();
|
||
240 | Rectangle r = new Rectangle((int) p.getX(), (int) p.getY(), vp.getImageWidth(), vp.getImageHeight()); |
||
241 | Tiling tiles = new Tiling(maxTileDrawWidth, maxTileDrawHeight, r);
|
||
242 | tiles.setAffineTransform((AffineTransform) vp.getAffineTransform().clone());
|
||
243 | for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) { |
||
244 | // drawing part
|
||
245 | try {
|
||
246 | ViewPort vport = tiles.getTileViewPort(vp, tileNr); |
||
247 | //g.setClip(tiles.getClip(tileNr).x, tiles.getClip(tileNr).y, tiles.getClip(tileNr).width - 5, tiles.getClip(tileNr).height);
|
||
248 | ((RasterSEFileAdapter) source).draw(image, g, vport, cancel); |
||
249 | } catch (NoninvertibleTransformException e) { |
||
250 | e.printStackTrace(); |
||
251 | } |
||
252 | } |
||
253 | }else
|
||
254 | ((RasterSEFileAdapter) source).draw(image, g, vp, cancel); |
||
255 | status.applyStatus(this);
|
||
256 | firstLoad = false;
|
||
257 | } |
||
258 | |||
259 | if(mustTileDraw){
|
||
260 | Point2D p = vp.getOffset();
|
||
261 | Rectangle r = new Rectangle((int) p.getX(), (int) p.getY(), vp.getImageWidth(), vp.getImageHeight()); |
||
262 | Tiling tiles = new Tiling(maxTileDrawWidth, maxTileDrawHeight, r);
|
||
263 | tiles.setAffineTransform((AffineTransform) vp.getAffineTransform().clone());
|
||
264 | for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) { |
||
265 | // drawing part
|
||
266 | try {
|
||
267 | ViewPort vport = tiles.getTileViewPort(vp, tileNr); |
||
268 | ((RasterSEFileAdapter) source).draw(image, g, vport, cancel); |
||
269 | } catch (NoninvertibleTransformException e) { |
||
270 | e.printStackTrace(); |
||
271 | } |
||
272 | } |
||
273 | }else
|
||
274 | ((RasterSEFileAdapter) source).draw(image, g, vp, cancel); |
||
275 | |||
276 | } catch (DriverIOException e) {
|
||
277 | NotificationManager.addError("Error dibujando", e);
|
||
278 | } |
||
279 | |||
280 | if (getVirtualLayers() != null) { |
||
281 | try {
|
||
282 | getVirtualLayers().draw(image, g, vp, cancel,scale); |
||
283 | } catch (ReadDriverException e) {
|
||
284 | NotificationManager.addError("Error dibujando", e);
|
||
285 | } |
||
286 | } |
||
287 | } |
||
288 | } |
||
289 | |||
290 | /**
|
||
291 | * Inserta la proyecci?n.
|
||
292 | *
|
||
293 | * @param proj Proyecci?n.
|
||
294 | */
|
||
295 | public void setProjection(IProjection proj) { |
||
296 | super.setProjection(proj);
|
||
297 | |||
298 | if (source != null && source.getDriver() != null && source.getDriver() instanceof GeoRasterDriver) { |
||
299 | GeoRasterDriver geoDrv = (GeoRasterDriver) source.getDriver(); |
||
300 | |||
301 | if (geoDrv.getProjection() == null) { |
||
302 | geoDrv.setProjection(proj); |
||
303 | } |
||
304 | } |
||
305 | } |
||
306 | |||
307 | public void setTransparency(int trans) { |
||
308 | super.setTransparency(trans);
|
||
309 | ((RasterSEFileAdapter) source).setTransparency(trans); |
||
310 | if (getMapContext() != null){ |
||
311 | getMapContext().invalidate(); |
||
312 | } |
||
313 | } |
||
314 | |||
315 | public int getTransparency() { |
||
316 | return ((RasterSEFileAdapter) source).getTransparency();
|
||
317 | } |
||
318 | |||
319 | /*
|
||
320 | * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#getFullExtent()
|
||
321 | */
|
||
322 | public Rectangle2D getFullExtent(){ |
||
323 | return ((RasterSEFileAdapter) source).getFullExtent();
|
||
324 | } |
||
325 | |||
326 | /**
|
||
327 | * Obtiene el valor del pixel del Image en la posici?n x,y
|
||
328 | * @param x Posici?n x
|
||
329 | * @param y Posici?n y
|
||
330 | * @return valor de pixel
|
||
331 | */
|
||
332 | public int[] getPixel(double wcx, double wcy){ |
||
333 | return ((RasterSEFileAdapter) source).getPixel(wcx, wcy);
|
||
334 | } |
||
335 | |||
336 | public double getMaxX(){ |
||
337 | return this.getFullExtent().getMaxX(); |
||
338 | } |
||
339 | |||
340 | public double getMaxY(){ |
||
341 | return this.getFullExtent().getMaxY(); |
||
342 | } |
||
343 | |||
344 | public double getMinX(){ |
||
345 | return this.getFullExtent().getMinX(); |
||
346 | } |
||
347 | |||
348 | public double getMinY(){ |
||
349 | return this.getFullExtent().getMinY(); |
||
350 | } |
||
351 | |||
352 | public double getHeight(){ |
||
353 | return this.getFullExtent().getHeight(); |
||
354 | } |
||
355 | |||
356 | public double getWidth(){ |
||
357 | return this.getFullExtent().getWidth(); |
||
358 | } |
||
359 | |||
360 | |||
361 | /* (non-Javadoc)
|
||
362 | * @deprecated. See String getInfo(Point p) throws DriverException
|
||
363 | * @see com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint#queryByPoint(java.awt.Point)
|
||
364 | */
|
||
365 | public String queryByPoint(Point p) throws DriverException { |
||
366 | String data = "<file:"+normalizeAsXMLTag(getName())+">\n"; |
||
367 | |||
368 | ArrayList attr = source.getAttributes();
|
||
369 | data += " <raster\n";
|
||
370 | data += " File=\""+((RasterSEFileAdapter) source).getFile()+"\"\n"; |
||
371 | for (int i=0; i<attr.size(); i++) { |
||
372 | Object [] a = (Object []) attr.get(i); |
||
373 | |||
374 | data += " "+a[0].toString()+"="; |
||
375 | if (a[1].toString() instanceof String) |
||
376 | data += "\""+a[1].toString()+"\"\n"; |
||
377 | else
|
||
378 | data += a[1].toString()+"\n"; |
||
379 | } |
||
380 | data += " Point=\""+posX+" , "+posY+"\"\n"; |
||
381 | data += " Point_WC=\""+posXWC+" , "+posYWC+"\"\n"; |
||
382 | data += " RGB=\""+r+", "+g+", "+b+"\"\n"; |
||
383 | data += " />\n";
|
||
384 | |||
385 | data += "</file:"+normalizeAsXMLTag(getName())+">\n"; |
||
386 | //System.out.println(data);
|
||
387 | return data;
|
||
388 | } |
||
389 | |||
390 | public XMLItem[] getInfo(Point p, double tolerance, Cancellable cancel) { |
||
391 | |||
392 | Point2D pReal = getMapContext().getViewPort().toMapPoint(p);
|
||
393 | Point2D px = null; |
||
394 | if( pReal.getX() > this.getMinX() && |
||
395 | pReal.getX() < this.getMaxX() &&
|
||
396 | pReal.getY() > this.getMinY() &&
|
||
397 | pReal.getY() < this.getMaxY()){
|
||
398 | ArrayList attr = source.getAttributes();
|
||
399 | int w = 0, h = 0; |
||
400 | for (int i=0; i<attr.size(); i++) { |
||
401 | Object [] a = (Object []) attr.get(i); |
||
402 | if(a[0].toString().equals("Width")) |
||
403 | w = ((Integer)a[1]).intValue(); |
||
404 | if(a[0].toString().equals("Height")) |
||
405 | h = ((Integer)a[1]).intValue(); |
||
406 | } |
||
407 | px = new Point2D.Double(); |
||
408 | px.setLocation( ((pReal.getX() - this.getMinX()) * w) / getWidth(),
|
||
409 | ((this.getMaxY() - pReal.getY()) * h) / getHeight());
|
||
410 | } |
||
411 | |||
412 | int[] rgb = this.getPixel(pReal.getX(), pReal.getY()); |
||
413 | |||
414 | StringXMLItem[] item = new StringXMLItem[1]; |
||
415 | String data = "<file:"+normalizeAsXMLTag(getName())+">\n"; |
||
416 | |||
417 | data += " <raster\n";
|
||
418 | data += " View_Point=\""+p.getX()+" , "+p.getY()+"\"\n"; |
||
419 | data += " World_Point=\""+pReal.getX()+" , "+pReal.getY()+"\"\n"; |
||
420 | if(px == null) |
||
421 | data += " Pixel_Point=\"Out\"\n";
|
||
422 | else
|
||
423 | data += " Pixel_Point=\""+(int)px.getX()+" , "+(int)px.getY()+"\"\n"; |
||
424 | data += " RGB=\""+rgb[1]+" "+rgb[2]+" "+rgb[3]+"\"\n"; |
||
425 | data += " Band_Value=\"";
|
||
426 | if(this.getSource().getDataType() >= 0 && this.getSource().getDataType() <= 3){ |
||
427 | for(int i = 0; i < this.getSource().getNumBands(); i++) |
||
428 | data += ((Integer)((RasterDriver)this.getSource().getDriver()).getData((int)px.getX(), (int)px.getY(), i)).intValue()+" "; |
||
429 | } |
||
430 | if(this.getSource().getDataType() >= 4){ |
||
431 | for(int i = 0; i < this.getSource().getNumBands(); i++) |
||
432 | data += ((Float)((RasterDriver)this.getSource().getDriver()).getData((int)px.getX(), (int)px.getY(), i)).floatValue()+" "; |
||
433 | } |
||
434 | if(this.getSource().getDataType() >= 5){ |
||
435 | for(int i = 0; i < this.getSource().getNumBands(); i++) |
||
436 | data += ((Double)((RasterDriver)this.getSource().getDriver()).getData((int)px.getX(), (int)px.getY(), i)).doubleValue()+" "; |
||
437 | } |
||
438 | data += "\"\n";
|
||
439 | data += " />\n";
|
||
440 | data += "</file:"+normalizeAsXMLTag(getName())+">\n"; |
||
441 | |||
442 | item[0] = new StringXMLItem(data, this); |
||
443 | return item;
|
||
444 | |||
445 | } |
||
446 | |||
447 | /**
|
||
448 | * Filters a string for being suitable as XML Tag, erasing
|
||
449 | * all not alphabetic or numeric characters.
|
||
450 | * @param s
|
||
451 | * @return string normalized
|
||
452 | */
|
||
453 | public String normalizeAsXMLTag(String s) { |
||
454 | return s.replaceAll("[^a-zA-Z0-9]",""); |
||
455 | } |
||
456 | |||
457 | /**
|
||
458 | * Obtiene atributos a partir de un georasterfile
|
||
459 | * @return
|
||
460 | */
|
||
461 | public ArrayList getAttributes() { |
||
462 | return source.getAttributes();
|
||
463 | } |
||
464 | |||
465 | /**
|
||
466 | * @throws XMLException
|
||
467 | * @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperties()
|
||
468 | */
|
||
469 | public XMLEntity getXMLEntity() throws XMLException { |
||
470 | XMLEntity xml = super.getXMLEntity();
|
||
471 | |||
472 | if (source instanceof RasterSEFileAdapter) { |
||
473 | xml.putProperty("file", ((RasterSEFileAdapter) source).getFile());
|
||
474 | } |
||
475 | |||
476 | xml.putProperty("driverName", getSource().getDriver().getName());
|
||
477 | |||
478 | //Si no hay ning?n Status aplicamos el StatusLayerRaster que se usa por defecto
|
||
479 | if(status == null) |
||
480 | status = new StatusLayerRaster();
|
||
481 | |||
482 | status.getXMLEntity(xml, true, this); |
||
483 | |||
484 | |||
485 | return xml;
|
||
486 | } |
||
487 | |||
488 | public void setXMLEntity03(XMLEntity xml) |
||
489 | throws XMLException {
|
||
490 | super.setXMLEntity(xml);
|
||
491 | try {
|
||
492 | Driver d = LayerFactory.getDM().getDriver(
|
||
493 | xml.getStringProperty("driverName"));
|
||
494 | File f = new File(xml.getStringProperty("file")); |
||
495 | RasterSEAdapter adapter = new RasterSEFileAdapter(f);
|
||
496 | adapter.setDriver(d); |
||
497 | setSource(adapter); |
||
498 | // Para notificar al adapter-driver cual es la proyecci?n.
|
||
499 | setProjection(super.getProjection());
|
||
500 | } catch (DriverLoadException e) {
|
||
501 | throw new XMLException(e); |
||
502 | } |
||
503 | } |
||
504 | |||
505 | public void setXMLEntity(XMLEntity xml) |
||
506 | throws XMLException {
|
||
507 | super.setXMLEntity(xml);
|
||
508 | try {
|
||
509 | Driver d = LayerFactory.getDM().getDriver(
|
||
510 | xml.getStringProperty("driverName"));
|
||
511 | File f = new File(xml.getStringProperty("file")); |
||
512 | RasterSEAdapter adapter = new RasterSEFileAdapter(f);
|
||
513 | adapter.setDriver(d); |
||
514 | setSource(adapter); |
||
515 | // Para notificar al adapter-driver cual es la proyecci?n.
|
||
516 | setProjection(super.getProjection());
|
||
517 | |||
518 | //Inicializamos la clase a la que se usa por defecto para
|
||
519 | //compatibilidad con proyectos antiguos
|
||
520 | String claseStr = StatusLayerRaster.defaultClass;
|
||
521 | if (xml.contains("raster.class")) { |
||
522 | claseStr = xml.getStringProperty("raster.class");
|
||
523 | } |
||
524 | if(status!=null) |
||
525 | status.setXMLEntity(xml, this);
|
||
526 | else{
|
||
527 | |||
528 | //Cuando cargamos un proyecto
|
||
529 | |||
530 | if(claseStr!=null && !claseStr.equals("")){ |
||
531 | try{
|
||
532 | Class clase = Class.forName(claseStr); |
||
533 | Constructor constr = clase.getConstructor(null); |
||
534 | status = (StatusRasterInterface)constr.newInstance(null);
|
||
535 | if(status!=null) |
||
536 | status.setXMLEntity(xml, this);
|
||
537 | }catch(ClassNotFoundException exc){ |
||
538 | exc.printStackTrace(); |
||
539 | }catch(InstantiationException exc){ |
||
540 | exc.printStackTrace(); |
||
541 | }catch(IllegalAccessException exc){ |
||
542 | exc.printStackTrace(); |
||
543 | }catch(NoSuchMethodException exc){ |
||
544 | exc.printStackTrace(); |
||
545 | }catch(InvocationTargetException exc){ |
||
546 | exc.printStackTrace(); |
||
547 | } |
||
548 | } |
||
549 | } |
||
550 | firstLoad = true;
|
||
551 | |||
552 | |||
553 | } catch (DriverLoadException e) {
|
||
554 | throw new XMLException(e); |
||
555 | } |
||
556 | } |
||
557 | |||
558 | /* (non-Javadoc)
|
||
559 | * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort, com.iver.cit.gvsig.fmap.operations.Cancellable)
|
||
560 | */
|
||
561 | public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale, PrintRequestAttributeSet propeties){ |
||
562 | |||
563 | if (isVisible() && isWithinScale(scale)){
|
||
564 | isPrinting = true;
|
||
565 | if (!mustTilePrint) {
|
||
566 | draw(null, g, viewPort, cancel,scale);
|
||
567 | } else {
|
||
568 | // Para no pedir imagenes demasiado grandes, vamos
|
||
569 | // a hacer lo mismo que hace EcwFile: chunkear.
|
||
570 | // Llamamos a drawView con cuadraditos m?s peque?os
|
||
571 | // del BufferedImage ni caso, cuando se imprime viene con null
|
||
572 | Tiling tiles = new Tiling(maxTilePrintWidth, maxTilePrintHeight, g.getClipBounds());
|
||
573 | tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
|
||
574 | |||
575 | //Si es la primera lectura salvamos los valores de m?ximo y m?nimo para la aplicaci?n
|
||
576 | //de realce si la imagen es de 16 bits.
|
||
577 | |||
578 | //RasterStats stats = getSource().getFilterStack().getStats();
|
||
579 | //if(stats != null)
|
||
580 | //stats.history.add(stats.new History(getName(), stats.minBandValue, stats.maxBandValue, stats.secondMinBandValue, stats.secondMaxBandValue));
|
||
581 | |||
582 | |||
583 | for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) { |
||
584 | // Parte que dibuja
|
||
585 | try {
|
||
586 | ViewPort vp = tiles.getTileViewPort(viewPort, tileNr); |
||
587 | draw(null, g, vp, cancel,scale);
|
||
588 | } catch (NoninvertibleTransformException e) { |
||
589 | // TODO Auto-generated catch block
|
||
590 | e.printStackTrace(); |
||
591 | } |
||
592 | } |
||
593 | |||
594 | /*if(stats != null){
|
||
595 | getSource().getFilterStack().getStats().history.clear();
|
||
596 | stats = getSource().getFilterStack().getStats();
|
||
597 | }*/
|
||
598 | |||
599 | } |
||
600 | isPrinting = false;
|
||
601 | } |
||
602 | } |
||
603 | |||
604 | public void _print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale) |
||
605 | throws DriverException {
|
||
606 | // Para no pedir imagenes demasiado grandes, vamos
|
||
607 | // a hacer lo mismo que hace EcwFile: chunkear.
|
||
608 | // Llamamos a drawView con cuadraditos m?s peque?os
|
||
609 | // del BufferedImage ni caso, cuando se imprime viene con null
|
||
610 | |||
611 | int numW, numH;
|
||
612 | int stepX, stepY;
|
||
613 | int xProv, yProv, wProv, hProv;
|
||
614 | double xProvD, yProvD, wProvD, hProvD;
|
||
615 | int A = 1500; |
||
616 | int H = 1500; |
||
617 | int altoAux, anchoAux;
|
||
618 | |||
619 | AffineTransform mat = (AffineTransform) viewPort.getAffineTransform().clone(); |
||
620 | |||
621 | // Vamos a hacerlo en trozos de AxH
|
||
622 | Rectangle r = g.getClipBounds();
|
||
623 | numW = (int) (r.width) / A;
|
||
624 | numH = (int) (r.height) / H;
|
||
625 | |||
626 | |||
627 | double[] srcPts = new double[8]; |
||
628 | double[] dstPts= new double[8]; |
||
629 | |||
630 | yProv = (int) r.y;
|
||
631 | for (stepY=0; stepY < numH+1; stepY++) |
||
632 | { |
||
633 | if ((yProv + H) > r.getMaxY())
|
||
634 | altoAux = (int) r.getMaxY() - yProv;
|
||
635 | else
|
||
636 | altoAux = H; |
||
637 | |||
638 | xProv = (int) r.x;
|
||
639 | for (stepX=0; stepX < numW+1; stepX++) |
||
640 | { |
||
641 | if ((xProv + A) > r.getMaxX())
|
||
642 | anchoAux = (int) r.getMaxX() - xProv;
|
||
643 | else
|
||
644 | anchoAux = A; |
||
645 | |||
646 | Rectangle newRect = new Rectangle(xProv, yProv, anchoAux, altoAux); |
||
647 | |||
648 | // Parte que dibuja
|
||
649 | srcPts[0] = xProv;
|
||
650 | srcPts[1] = yProv;
|
||
651 | srcPts[2] = xProv + anchoAux+1; |
||
652 | srcPts[3] = yProv;
|
||
653 | srcPts[4] = xProv + anchoAux+1; |
||
654 | srcPts[5] = yProv + altoAux+1; |
||
655 | srcPts[6] = xProv;
|
||
656 | srcPts[7] = yProv + altoAux+1; |
||
657 | |||
658 | try {
|
||
659 | mat.inverseTransform(srcPts, 0, dstPts, 0, 4); |
||
660 | Rectangle2D.Double rectCuadricula = new Rectangle2D.Double( |
||
661 | dstPts[0], dstPts[1], |
||
662 | dstPts[2] - dstPts[0], dstPts[5]-dstPts[3]); |
||
663 | Extent extent = new Extent(rectCuadricula);
|
||
664 | |||
665 | Dimension tam = new Dimension(anchoAux+1, altoAux+1); |
||
666 | ViewPort vp = viewPort.cloneViewPort(); |
||
667 | vp.setImageSize(tam); |
||
668 | vp.setExtent(rectCuadricula); |
||
669 | vp.setAffineTransform(mat); |
||
670 | draw(null, g, vp, cancel,scale);
|
||
671 | |||
672 | } catch (NoninvertibleTransformException e) { |
||
673 | e.printStackTrace(); |
||
674 | } |
||
675 | // Fin parte que dibuja
|
||
676 | xProv = xProv + A; |
||
677 | } |
||
678 | yProv = yProv + H; |
||
679 | } |
||
680 | |||
681 | } |
||
682 | |||
683 | /* (non-Javadoc)
|
||
684 | * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setBand(int, int)
|
||
685 | */
|
||
686 | public void setBand(int flag, int nBand) { |
||
687 | this.getSource().setBand(flag, nBand);
|
||
688 | } |
||
689 | |||
690 | /**
|
||
691 | * Borra de la lista de listeners el que se pasa como par?metro.
|
||
692 | *
|
||
693 | * @param o LayerListener a borrar.
|
||
694 | *
|
||
695 | * @return True si ha sido correcto el borrado del Listener.
|
||
696 | */
|
||
697 | public boolean removeLayerListener(LayerListener o) { |
||
698 | if(this.isRemoveRasterFlag()){ |
||
699 | this.setRemoveRasterFlag(true); |
||
700 | } |
||
701 | return super.layerListeners.remove(o); |
||
702 | } |
||
703 | |||
704 | /**
|
||
705 | * @return Returns the removeRasterFlag.
|
||
706 | */
|
||
707 | public boolean isRemoveRasterFlag() { |
||
708 | return removeRasterFlag;
|
||
709 | } |
||
710 | |||
711 | /**
|
||
712 | * Asigna el valor del flag que dice si destruimos la memoria del raster
|
||
713 | * al eliminarlo del TOC o no.
|
||
714 | * @param removeRasterFlag The removeRasterFlag to set.
|
||
715 | */
|
||
716 | public void setRemoveRasterFlag(boolean removeRasterFlag) { |
||
717 | this.removeRasterFlag = removeRasterFlag;
|
||
718 | } |
||
719 | |||
720 | public ImageIcon getTocImageIcon() { |
||
721 | return new ImageIcon(MapControl.class.getResource("images/icolayerRaster.PNG")); |
||
722 | } |
||
723 | |||
724 | /*
|
||
725 | * (non-Javadoc)
|
||
726 | * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getTileSize()
|
||
727 | */
|
||
728 | public int[] getTileSize() { |
||
729 | int[] size = {maxTileDrawWidth, maxTileDrawHeight}; |
||
730 | return size;
|
||
731 | } |
||
732 | |||
733 | /*
|
||
734 | * (non-Javadoc)
|
||
735 | * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#isTiled()
|
||
736 | */
|
||
737 | public boolean isTiled() { |
||
738 | return mustTileDraw;
|
||
739 | } |
||
740 | |||
741 | /**
|
||
742 | * Obtiene el flag que dice si la imagen est? o no georreferenciada
|
||
743 | * @return true si est? georreferenciada y false si no lo est?.
|
||
744 | */
|
||
745 | public boolean isGeoreferenced() { |
||
746 | return getSource().isGeoreferenced();
|
||
747 | } |
||
748 | |||
749 | /**
|
||
750 | * Get datasource object
|
||
751 | * @return
|
||
752 | */
|
||
753 | public DataSource getDatasource(){ |
||
754 | return getSource().getDatasource();
|
||
755 | } |
||
756 | |||
757 | /**
|
||
758 | * Get render object
|
||
759 | * @return Rendering
|
||
760 | */
|
||
761 | public Rendering getRender(){
|
||
762 | return this.getSource().getRender(); |
||
763 | } |
||
764 | |||
765 | /*
|
||
766 | * (non-Javadoc)
|
||
767 | * @see org.gvsig.fmap.raster.IRasterOperations#getPXHeight()
|
||
768 | */
|
||
769 | public double getPxHeight() { |
||
770 | return getSource().getDatasource().getHeight();
|
||
771 | } |
||
772 | |||
773 | /*
|
||
774 | * (non-Javadoc)
|
||
775 | * @see org.gvsig.fmap.raster.IRasterOperations#getPxWidth()
|
||
776 | */
|
||
777 | public double getPxWidth() { |
||
778 | return getSource().getDatasource().getWidth();
|
||
779 | } |
||
780 | |||
781 | /*
|
||
782 | * (non-Javadoc)
|
||
783 | * @see org.gvsig.fmap.raster.IGeoDimension#getWCHeight()
|
||
784 | */
|
||
785 | public double getWCHeight() { |
||
786 | return this.getHeight(); |
||
787 | } |
||
788 | |||
789 | /*
|
||
790 | * (non-Javadoc)
|
||
791 | * @see org.gvsig.fmap.raster.IGeoDimension#getWCWidth()
|
||
792 | */
|
||
793 | public double getWCWidth() { |
||
794 | return this.getWidth(); |
||
795 | } |
||
796 | |||
797 | /*
|
||
798 | * (non-Javadoc)
|
||
799 | * @see org.gvsig.fmap.raster.IRasterFile#getFileSize()
|
||
800 | */
|
||
801 | public long[] getFileSize(){ |
||
802 | int nFiles = getDatasource().getGeoRasterMultiFile().getDatasetCount();
|
||
803 | long[] s = new long[nFiles]; |
||
804 | for(int i = 0; i < nFiles; i++) |
||
805 | s[i] = getDatasource().getGeoRasterMultiFile().getDataset(i).getFileSize(); |
||
806 | return s;
|
||
807 | } |
||
808 | |||
809 | /*
|
||
810 | * (non-Javadoc)
|
||
811 | * @see org.gvsig.fmap.raster.IRasterFile#getFileName()
|
||
812 | */
|
||
813 | public String[] getFileName(){ |
||
814 | int nFiles = getDatasource().getGeoRasterMultiFile().getDatasetCount();
|
||
815 | String[] s = new String[nFiles]; |
||
816 | for(int i = 0; i < nFiles; i++) |
||
817 | s[i] = getDatasource().getGeoRasterMultiFile().getDataset(i).getFName(); |
||
818 | return s;
|
||
819 | } |
||
820 | |||
821 | /*
|
||
822 | * (non-Javadoc)
|
||
823 | * @see org.gvsig.fmap.raster.IRasterFile#getFileCount()
|
||
824 | */
|
||
825 | public int getFileCount(){ |
||
826 | return getDatasource().getGeoRasterMultiFile().getDatasetCount();
|
||
827 | } |
||
828 | |||
829 | /*
|
||
830 | * (non-Javadoc)
|
||
831 | * @see org.gvsig.fmap.raster.IRasterFile#getFileFormat()
|
||
832 | */
|
||
833 | public String getFileFormat(){ |
||
834 | String fName = getDatasource().getGeoRasterMultiFile().getDataset(0).getFName(); |
||
835 | int index = fName.lastIndexOf(".") + 1; |
||
836 | String ext = null; |
||
837 | if(index > 0) |
||
838 | ext = fName.substring(fName.lastIndexOf(".") + 1, fName.length()); |
||
839 | return ext;
|
||
840 | } |
||
841 | |||
842 | /*
|
||
843 | * (non-Javadoc)
|
||
844 | * @see org.gvsig.fmap.raster.IRasterOperations#getBandCount()
|
||
845 | */
|
||
846 | public int getBandCount(){ |
||
847 | return getDatasource().getGeoRasterMultiFile().getBandCount();
|
||
848 | } |
||
849 | |||
850 | /*
|
||
851 | * (non-Javadoc)
|
||
852 | * @see org.gvsig.fmap.raster.IRasterOperations#getDatatype()
|
||
853 | */
|
||
854 | public int[] getDataType(){ |
||
855 | return getDatasource().getGeoRasterMultiFile().getDataType();
|
||
856 | } |
||
857 | |||
858 | /*
|
||
859 | * (non-Javadoc)
|
||
860 | * @see org.gvsig.fmap.raster.IRasterRendering#getRenderTransparency()
|
||
861 | */
|
||
862 | public GridTransparency getRenderTransparency(){
|
||
863 | return getRender().getLastTransparency();
|
||
864 | } |
||
865 | |||
866 | /*
|
||
867 | * (non-Javadoc)
|
||
868 | * @see org.gvsig.fmap.raster.IRasterDataset#getGeoRasterMultiDataset()
|
||
869 | */
|
||
870 | public RasterMultiDataset getGeoRasterMultiDataset() {
|
||
871 | return getDatasource().getGeoRasterMultiFile();
|
||
872 | } |
||
873 | |||
874 | public Extent getFullRasterExtent(){
|
||
875 | return new Extent(((RasterSEFileAdapter) source).getFullExtent()); |
||
876 | } |
||
877 | |||
878 | /*
|
||
879 | * (non-Javadoc)
|
||
880 | * @see org.gvsig.fmap.raster.IRasterDataset#addFile(java.lang.String)
|
||
881 | */
|
||
882 | public void addFile(String fileName){ |
||
883 | try {
|
||
884 | ((RasterSEFileAdapter) source).addFile(fileName); |
||
885 | } catch (IOException e) { |
||
886 | NotificationManager.addError(e.getMessage(), e); |
||
887 | } |
||
888 | } |
||
889 | |||
890 | /*
|
||
891 | * (non-Javadoc)
|
||
892 | * @see org.gvsig.fmap.raster.IRasterDataset#delFile(java.lang.String)
|
||
893 | */
|
||
894 | public void delFile(String fileName){ |
||
895 | ((RasterSEFileAdapter) source).delFile(fileName); |
||
896 | } |
||
897 | |||
898 | /*
|
||
899 | * (non-Javadoc)
|
||
900 | * @see org.gvsig.fmap.raster.IRasterDataset#getInfo(java.lang.String)
|
||
901 | */
|
||
902 | public Object getInfo(String key) { |
||
903 | if(key.equals("DriverName")) |
||
904 | return getSource().getDriver().getName();
|
||
905 | return null; |
||
906 | } |
||
907 | |||
908 | /*
|
||
909 | * (non-Javadoc)
|
||
910 | * @see org.gvsig.fmap.raster.IRasterRendering#getRenderFilterList()
|
||
911 | */
|
||
912 | public RasterFilterList getRenderFilterList(){
|
||
913 | return getRender().getFilterList();
|
||
914 | } |
||
915 | |||
916 | /*
|
||
917 | * (non-Javadoc)
|
||
918 | * @see org.gvsig.raster.shared.IRasterGeoOperations#getPixelSizeX()
|
||
919 | */
|
||
920 | public double[] getPxWidthByDataset() { |
||
921 | return getSource().getDatasource().getGeoRasterMultiFile().getWidth();
|
||
922 | } |
||
923 | |||
924 | /*
|
||
925 | * (non-Javadoc)
|
||
926 | * @see org.gvsig.raster.shared.IRasterGeoOperations#getPixelSizeY()
|
||
927 | */
|
||
928 | public double[] getPxHeightByDataset() { |
||
929 | return getSource().getDatasource().getGeoRasterMultiFile().getHeight();
|
||
930 | } |
||
931 | |||
932 | /*
|
||
933 | * (non-Javadoc)
|
||
934 | * @see org.gvsig.raster.shared.IRasterOperations#getMetadata()
|
||
935 | */
|
||
936 | public DatasetMetadata[] getMetadata() { |
||
937 | int count = source.getDatasource().getGeoRasterMultiFile().getDatasetCount();
|
||
938 | DatasetMetadata[] metadata = new DatasetMetadata[count]; |
||
939 | for (int i=0; i<count; i++) { |
||
940 | metadata[i] = source.getDatasource().getGeoRasterMultiFile().getDataset(i).getMetadata(); |
||
941 | } |
||
942 | return metadata;
|
||
943 | } |
||
944 | |||
945 | /*
|
||
946 | * (non-Javadoc)
|
||
947 | * @see org.gvsig.raster.shared.IRasterOperations#getBandCountFromDataset()
|
||
948 | */
|
||
949 | public int[] getBandCountFromDataset() { |
||
950 | int count = source.getDatasource().getGeoRasterMultiFile().getDatasetCount();
|
||
951 | int[] bands = new int[count]; |
||
952 | for (int i=0; i<count; i++) { |
||
953 | bands[i] = source.getDatasource().getGeoRasterMultiFile().getDataset(i).getBandCount(); |
||
954 | } |
||
955 | return bands;
|
||
956 | } |
||
957 | |||
958 | /*
|
||
959 | * (non-Javadoc)
|
||
960 | * @see org.gvsig.raster.shared.IRasterOperations#getColourInterpretation(int, int)
|
||
961 | */
|
||
962 | public String getColourInterpretation(int band, int dataset) { |
||
963 | if (source.getDatasource().getGeoRasterMultiFile().getColorInterpretation(dataset) == null) return "Undefined"; |
||
964 | return source.getDatasource().getGeoRasterMultiFile().getColorInterpretation(dataset).get(band);
|
||
965 | } |
||
966 | |||
967 | public String getStringProjection() { |
||
968 | try {
|
||
969 | return source.getDatasource().getGeoRasterMultiFile().getStringProjection();
|
||
970 | } catch (RasterDriverException e) {
|
||
971 | // TODO Auto-generated catch block
|
||
972 | e.printStackTrace(); |
||
973 | } |
||
974 | return null; |
||
975 | } |
||
976 | 10940 | nacho | |
977 | 10740 | nacho | } |