svn-gvsig-desktop / tags / v1_1_Build_1001 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / FLyrRaster.java @ 11984
History | View | Annotate | Download (24.7 KB)
1 | 11946 | maquerol | /* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|---|---|
2 | 1100 | fjp | *
|
3 | * Copyright (C) 2004 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 | * For more information, contact:
|
||
20 | *
|
||
21 | * Generalitat Valenciana
|
||
22 | * Conselleria d'Infraestructures i Transport
|
||
23 | 11946 | maquerol | * Av. Blasco Ib??ez, 50
|
24 | 1100 | fjp | * 46010 VALENCIA
|
25 | * SPAIN
|
||
26 | *
|
||
27 | * +34 963862235
|
||
28 | * gvsig@gva.es
|
||
29 | * www.gvsig.gva.es
|
||
30 | *
|
||
31 | * or
|
||
32 | *
|
||
33 | * IVER T.I. S.A
|
||
34 | * Salamanca 50
|
||
35 | * 46005 Valencia
|
||
36 | * Spain
|
||
37 | *
|
||
38 | * +34 963163400
|
||
39 | * dac@iver.es
|
||
40 | */
|
||
41 | 214 | fernando | package com.iver.cit.gvsig.fmap.layers; |
42 | |||
43 | 1040 | fjp | import java.awt.Dimension; |
44 | 1034 | vcaballero | import java.awt.Graphics2D; |
45 | import java.awt.Point; |
||
46 | 1040 | fjp | import java.awt.Rectangle; |
47 | import java.awt.geom.AffineTransform; |
||
48 | import java.awt.geom.NoninvertibleTransformException; |
||
49 | 5979 | nacho | import java.awt.geom.Point2D; |
50 | 1034 | vcaballero | import java.awt.geom.Rectangle2D; |
51 | import java.awt.image.BufferedImage; |
||
52 | 1455 | luisw | import java.io.File; |
53 | 2183 | fernando | import java.lang.reflect.Constructor; |
54 | import java.lang.reflect.InvocationTargetException; |
||
55 | 1680 | luisw | import java.util.ArrayList; |
56 | 1034 | vcaballero | |
57 | 9013 | caballero | import javax.print.attribute.PrintRequestAttributeSet; |
58 | 5761 | jmvivo | import javax.swing.ImageIcon; |
59 | |||
60 | 1680 | luisw | import org.cresques.cts.IProjection; |
61 | 5979 | nacho | import org.cresques.filter.RasterFilterStack; |
62 | 9056 | nacho | import org.cresques.io.data.Grid; |
63 | 10195 | nacho | import org.cresques.io.data.RasterBuf; |
64 | 9056 | nacho | import org.cresques.io.datastruct.Statistic; |
65 | 1680 | luisw | import org.cresques.px.Extent; |
66 | 9056 | nacho | |
67 | 1680 | luisw | import com.hardcode.driverManager.Driver; |
68 | import com.hardcode.driverManager.DriverLoadException; |
||
69 | import com.iver.cit.gvsig.fmap.DriverException; |
||
70 | 5761 | jmvivo | import com.iver.cit.gvsig.fmap.MapControl; |
71 | 1680 | luisw | import com.iver.cit.gvsig.fmap.ViewPort; |
72 | import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
||
73 | import com.iver.cit.gvsig.fmap.drivers.GeorreferencedRasterDriver; |
||
74 | 8765 | jjdelcerro | import com.iver.cit.gvsig.fmap.drivers.RasterDriver; |
75 | 5057 | ldiaz | import com.iver.cit.gvsig.fmap.layers.layerOperations.StringXMLItem; |
76 | 5200 | ldiaz | import com.iver.cit.gvsig.fmap.layers.layerOperations.XMLItem; |
77 | 9056 | nacho | import com.iver.utiles.DoubleUtilities; |
78 | 1680 | luisw | import com.iver.utiles.XMLEntity; |
79 | 5317 | fjp | import com.iver.utiles.swing.threads.Cancellable; |
80 | 1680 | luisw | |
81 | |||
82 | 526 | vcaballero | /**
|
83 | 11946 | maquerol | * Clase b?sica de capa raster.
|
84 | 526 | vcaballero | *
|
85 | * @author Vicente Caballero Navarro
|
||
86 | */
|
||
87 | 567 | luisw | public class FLyrRaster extends FLyrDefault implements RasterOperations { |
88 | 2183 | fernando | private RasterAdapter source;
|
89 | boolean isPrinting = false; |
||
90 | boolean mustTileDraw = false; |
||
91 | boolean mustTilePrint = true; |
||
92 | 5979 | nacho | private int maxTileDrawWidth = 200; |
93 | private int maxTileDrawHeight = 200; |
||
94 | 2183 | fernando | int maxTilePrintWidth = 1500; |
95 | int maxTilePrintHeight = 1500; |
||
96 | private StatusRasterInterface status = null; |
||
97 | private boolean firstLoad = false; |
||
98 | private int posX = 0, posY = 0; |
||
99 | private double posXWC = 0, posYWC = 0; |
||
100 | 2322 | igbrotru | private int r = 0, g = 0, b = 0; |
101 | 3410 | nacho | private boolean removeRasterFlag = true; |
102 | 11925 | maquerol | |
103 | 9056 | nacho | |
104 | 1034 | vcaballero | /**
|
105 | * Devuelve el RasterAdapter de la capa.
|
||
106 | *
|
||
107 | * @return RasterAdapter.
|
||
108 | */
|
||
109 | public RasterAdapter getSource() {
|
||
110 | return source;
|
||
111 | } |
||
112 | 567 | luisw | |
113 | 1034 | vcaballero | /**
|
114 | 4376 | nacho | *Redefine wakeUp de FLyrDefault
|
115 | */
|
||
116 | public void wakeUp(){ |
||
117 | 4404 | nacho | /*setName(getName());
|
118 | 4376 | nacho | setProjection(getProjection());
|
119 | try {
|
||
120 | load();
|
||
121 | } catch (DriverIOException e) {
|
||
122 | e.printStackTrace();
|
||
123 | 9056 | nacho | }*/
|
124 | 4376 | nacho | } |
125 | 9056 | nacho | |
126 | 4376 | nacho | /**
|
127 | 1034 | vcaballero | * Inserta el RasterAdapter.
|
128 | *
|
||
129 | * @param ra RasterAdapter.
|
||
130 | */
|
||
131 | public void setSource(RasterAdapter ra) { |
||
132 | source = ra; |
||
133 | } |
||
134 | 567 | luisw | |
135 | 2183 | fernando | /**
|
136 | 2623 | nacho | * Devuelve la pila de filtros aplicada sobre la capa raster.
|
137 | *
|
||
138 | * @return RasterFilterStack.
|
||
139 | */
|
||
140 | public RasterFilterStack getFilterStack(){
|
||
141 | return source.getFilterStack();
|
||
142 | } |
||
143 | 9056 | nacho | |
144 | 2623 | nacho | /**
|
145 | 2183 | fernando | * Asignar el estado del raster
|
146 | * @param status
|
||
147 | */
|
||
148 | public void setStatus(StatusRasterInterface status){ |
||
149 | this.status = status;
|
||
150 | } |
||
151 | 9056 | nacho | |
152 | 2183 | fernando | /**
|
153 | 2623 | nacho | * Asigna la pila de filtros aplicada al raster
|
154 | * @return
|
||
155 | */
|
||
156 | public void setFilterStack(RasterFilterStack stack){ |
||
157 | 9056 | nacho | source.setFilterStack(stack); |
158 | 2623 | nacho | } |
159 | 9056 | nacho | |
160 | 2623 | nacho | /**
|
161 | 2183 | fernando | * Obtiene el estado del raster
|
162 | * @return
|
||
163 | */
|
||
164 | public StatusRasterInterface getStatus(){
|
||
165 | return this.status; |
||
166 | } |
||
167 | 9056 | nacho | |
168 | 2183 | fernando | /**
|
169 | 11946 | maquerol | * Asigna la posici?n en la que se ha hecho click al mostrar
|
170 | * informaci?n del raster.
|
||
171 | * @param x Posici?n en X
|
||
172 | * @param y Posici?n en Y
|
||
173 | 2183 | fernando | */
|
174 | public void setPos(int x, int y){ |
||
175 | this.posX = x;
|
||
176 | this.posY = y;
|
||
177 | } |
||
178 | 9056 | nacho | |
179 | 2183 | fernando | /**
|
180 | 11946 | maquerol | * Asigna la posici?n en coordenadas del mundo real en la que se ha
|
181 | * hecho click al mostrar informaci?n del raster.
|
||
182 | * @param x Posici?n en X
|
||
183 | * @param y Posici?n en Y
|
||
184 | 2183 | fernando | */
|
185 | public void setPosWC(double x, double y){ |
||
186 | this.posXWC = x;
|
||
187 | this.posYWC = y;
|
||
188 | } |
||
189 | 9056 | nacho | |
190 | 2322 | igbrotru | /**
|
191 | 11946 | maquerol | * Asigna RGB en la posici?n en la que se ha
|
192 | * hecho click al mostrar informaci?n del raster.
|
||
193 | 2322 | igbrotru | * @param r valor de rojo
|
194 | * @param g valor de verde
|
||
195 | * @param b valor de azul
|
||
196 | */
|
||
197 | public void setRGB(int r, int g, int b){ |
||
198 | this.r = r;
|
||
199 | this.g = g;
|
||
200 | this.b = b;
|
||
201 | } |
||
202 | 9056 | nacho | |
203 | 567 | luisw | /*
|
204 | * @see com.iver.cit.gvsig.fmap.layers.FLayer#load()
|
||
205 | */
|
||
206 | 1034 | vcaballero | public void load() throws DriverIOException { |
207 | ((RasterFileAdapter) source).start(); |
||
208 | 1455 | luisw | ((RasterFileAdapter) source).setTransparency(getTransparency()); |
209 | 567 | luisw | } |
210 | 1034 | vcaballero | |
211 | 526 | vcaballero | /**
|
212 | * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#draw(java.awt.image.BufferedImage,
|
||
213 | * java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort,
|
||
214 | 5317 | fjp | * com.iver.utiles.swing.threads.Cancellable)
|
215 | 526 | vcaballero | */
|
216 | 659 | luisw | public void draw(BufferedImage image, Graphics2D g, ViewPort vp, |
217 | 2531 | caballero | Cancellable cancel,double scale) throws DriverException { |
218 | 9056 | nacho | if (isWithinScale(scale)){
|
219 | 671 | luisw | try {
|
220 | 2183 | fernando | if(status!=null && firstLoad){ |
221 | 5979 | nacho | if(mustTileDraw){
|
222 | Point2D p = vp.getOffset();
|
||
223 | Rectangle r = new Rectangle((int) p.getX(), (int) p.getY(), vp.getImageWidth(), vp.getImageHeight()); |
||
224 | Tiling tiles = new Tiling(maxTileDrawWidth, maxTileDrawHeight, r);
|
||
225 | tiles.setAffineTransform((AffineTransform) vp.getAffineTransform().clone());
|
||
226 | for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) { |
||
227 | // drawing part
|
||
228 | try {
|
||
229 | ViewPort vport = tiles.getTileViewPort(vp, tileNr); |
||
230 | //g.setClip(tiles.getClip(tileNr).x, tiles.getClip(tileNr).y, tiles.getClip(tileNr).width - 5, tiles.getClip(tileNr).height);
|
||
231 | ((RasterFileAdapter) source).draw(image, g, vport, cancel); |
||
232 | } catch (NoninvertibleTransformException e) { |
||
233 | e.printStackTrace(); |
||
234 | } |
||
235 | } |
||
236 | }else
|
||
237 | 9056 | nacho | ((RasterFileAdapter) source).draw(image, g, vp, cancel); |
238 | 2623 | nacho | status.applyStatus(this);
|
239 | 2183 | fernando | firstLoad = false;
|
240 | } |
||
241 | 9056 | nacho | |
242 | 5979 | nacho | if(mustTileDraw){
|
243 | Point2D p = vp.getOffset();
|
||
244 | Rectangle r = new Rectangle((int) p.getX(), (int) p.getY(), vp.getImageWidth(), vp.getImageHeight()); |
||
245 | Tiling tiles = new Tiling(maxTileDrawWidth, maxTileDrawHeight, r);
|
||
246 | tiles.setAffineTransform((AffineTransform) vp.getAffineTransform().clone());
|
||
247 | for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) { |
||
248 | // drawing part
|
||
249 | try {
|
||
250 | ViewPort vport = tiles.getTileViewPort(vp, tileNr); |
||
251 | ((RasterFileAdapter) source).draw(image, g, vport, cancel); |
||
252 | } catch (NoninvertibleTransformException e) { |
||
253 | e.printStackTrace(); |
||
254 | } |
||
255 | } |
||
256 | }else
|
||
257 | ((RasterFileAdapter) source).draw(image, g, vp, cancel); |
||
258 | 9056 | nacho | |
259 | 671 | luisw | } catch (DriverIOException e) {
|
260 | throw new DriverException(e); |
||
261 | } |
||
262 | 1034 | vcaballero | |
263 | if (getVirtualLayers() != null) { |
||
264 | 2531 | caballero | getVirtualLayers().draw(image, g, vp, cancel,scale); |
265 | 1034 | vcaballero | } |
266 | 2531 | caballero | } |
267 | 526 | vcaballero | } |
268 | 1034 | vcaballero | |
269 | /**
|
||
270 | 11946 | maquerol | * Inserta la proyecci?n.
|
271 | 1034 | vcaballero | *
|
272 | 11946 | maquerol | * @param proj Proyecci?n.
|
273 | 1034 | vcaballero | */
|
274 | 567 | luisw | public void setProjection(IProjection proj) { |
275 | super.setProjection(proj);
|
||
276 | 1034 | vcaballero | |
277 | 1466 | luisw | if (source != null && source.getDriver() != null && source.getDriver() instanceof GeorreferencedRasterDriver) { |
278 | 567 | luisw | GeorreferencedRasterDriver geoDrv = (GeorreferencedRasterDriver) source.getDriver(); |
279 | 1034 | vcaballero | |
280 | if (geoDrv.getProjection() == null) { |
||
281 | 567 | luisw | geoDrv.setProjection(proj); |
282 | 1034 | vcaballero | } |
283 | 567 | luisw | } |
284 | 562 | fernando | } |
285 | 9056 | nacho | |
286 | 1455 | luisw | public void setTransparency(int trans) { |
287 | super.setTransparency(trans);
|
||
288 | ((RasterFileAdapter) source).setTransparency(trans); |
||
289 | 6881 | cesar | if (getMapContext() != null){ |
290 | getMapContext().invalidate(); |
||
291 | 6007 | nacho | } |
292 | 1455 | luisw | } |
293 | 562 | fernando | |
294 | 2183 | fernando | public int getTransparency() { |
295 | 1455 | luisw | return ((RasterFileAdapter) source).getTransparency();
|
296 | } |
||
297 | 9056 | nacho | |
298 | 567 | luisw | /*
|
299 | 526 | vcaballero | * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#getFullExtent()
|
300 | */
|
||
301 | 651 | fernando | public Rectangle2D getFullExtent() throws DriverException { |
302 | 567 | luisw | return ((RasterFileAdapter) source).getFullExtent();
|
303 | 526 | vcaballero | } |
304 | 303 | fernando | |
305 | 2322 | igbrotru | /**
|
306 | 11946 | maquerol | * Obtiene el valor del pixel del Image en la posici?n x,y
|
307 | * @param x Posici?n x
|
||
308 | * @param y Posici?n y
|
||
309 | 2322 | igbrotru | * @return valor de pixel
|
310 | */
|
||
311 | 2347 | igbrotru | public int[] getPixel(double wcx, double wcy){ |
312 | return ((RasterFileAdapter) source).getPixel(wcx, wcy);
|
||
313 | 2322 | igbrotru | } |
314 | 9056 | nacho | |
315 | 2396 | nacho | public double getMaxX(){ |
316 | try {
|
||
317 | return this.getFullExtent().getMaxX(); |
||
318 | }catch(DriverException e){
|
||
319 | return 0D; |
||
320 | } |
||
321 | } |
||
322 | 9056 | nacho | |
323 | 2396 | nacho | public double getMaxY(){ |
324 | try {
|
||
325 | return this.getFullExtent().getMaxY(); |
||
326 | }catch(DriverException e){
|
||
327 | return 0D; |
||
328 | } |
||
329 | } |
||
330 | 9056 | nacho | |
331 | 2396 | nacho | public double getMinX(){ |
332 | try {
|
||
333 | return this.getFullExtent().getMinX(); |
||
334 | }catch(DriverException e){
|
||
335 | return 0D; |
||
336 | } |
||
337 | } |
||
338 | 9056 | nacho | |
339 | 2396 | nacho | public double getMinY(){ |
340 | try {
|
||
341 | return this.getFullExtent().getMinY(); |
||
342 | }catch(DriverException e){
|
||
343 | return 0D; |
||
344 | } |
||
345 | } |
||
346 | 9056 | nacho | |
347 | 2396 | nacho | public double getHeight(){ |
348 | try {
|
||
349 | return this.getFullExtent().getHeight(); |
||
350 | }catch(DriverException e){
|
||
351 | return 0D; |
||
352 | } |
||
353 | } |
||
354 | 9056 | nacho | |
355 | 2396 | nacho | public double getWidth(){ |
356 | try {
|
||
357 | return this.getFullExtent().getWidth(); |
||
358 | }catch(DriverException e){
|
||
359 | return 0D; |
||
360 | } |
||
361 | } |
||
362 | |||
363 | 9056 | nacho | |
364 | 1680 | luisw | /* (non-Javadoc)
|
365 | 5048 | ldiaz | * @deprecated. See String getInfo(Point p) throws DriverException
|
366 | 1680 | luisw | * @see com.iver.cit.gvsig.fmap.layers.layerOperations.InfoByPoint#queryByPoint(java.awt.Point)
|
367 | 526 | vcaballero | */
|
368 | 1680 | luisw | public String queryByPoint(Point p) throws DriverException { |
369 | 3242 | luisw2 | String data = "<file:"+normalizeAsXMLTag(getName())+">\n"; |
370 | 1680 | luisw | |
371 | ArrayList attr = source.getAttributes();
|
||
372 | 1717 | luisw | data += " <raster\n";
|
373 | data += " File=\""+((RasterFileAdapter) source).getFile()+"\"\n"; |
||
374 | 1680 | luisw | for (int i=0; i<attr.size(); i++) { |
375 | Object [] a = (Object []) attr.get(i); |
||
376 | |||
377 | 1717 | luisw | data += " "+a[0].toString()+"="; |
378 | if (a[1].toString() instanceof String) |
||
379 | data += "\""+a[1].toString()+"\"\n"; |
||
380 | else
|
||
381 | data += a[1].toString()+"\n"; |
||
382 | 1680 | luisw | } |
383 | 2183 | fernando | data += " Point=\""+posX+" , "+posY+"\"\n"; |
384 | data += " Point_WC=\""+posXWC+" , "+posYWC+"\"\n"; |
||
385 | 2322 | igbrotru | data += " RGB=\""+r+", "+g+", "+b+"\"\n"; |
386 | 1717 | luisw | data += " />\n";
|
387 | 1680 | luisw | |
388 | 3242 | luisw2 | data += "</file:"+normalizeAsXMLTag(getName())+">\n"; |
389 | 2183 | fernando | //System.out.println(data);
|
390 | 1717 | luisw | return data;
|
391 | 526 | vcaballero | } |
392 | 9056 | nacho | |
393 | 8765 | jjdelcerro | public XMLItem[] getInfo(Point p, double tolerance, Cancellable cancel) throws DriverException { |
394 | 9056 | nacho | |
395 | 6881 | cesar | Point2D pReal = getMapContext().getViewPort().toMapPoint(p);
|
396 | 6030 | nacho | Point2D px = null; |
397 | 9056 | nacho | if( pReal.getX() > this.getMinX() && |
398 | pReal.getX() < this.getMaxX() &&
|
||
399 | pReal.getY() > this.getMinY() &&
|
||
400 | 6030 | nacho | pReal.getY() < this.getMaxY()){
|
401 | ArrayList attr = source.getAttributes();
|
||
402 | int w = 0, h = 0; |
||
403 | for (int i=0; i<attr.size(); i++) { |
||
404 | Object [] a = (Object []) attr.get(i); |
||
405 | if(a[0].toString().equals("Width")) |
||
406 | w = ((Integer)a[1]).intValue(); |
||
407 | if(a[0].toString().equals("Height")) |
||
408 | h = ((Integer)a[1]).intValue(); |
||
409 | 9056 | nacho | } |
410 | 8765 | jjdelcerro | px = new Point2D.Double(); |
411 | 6030 | nacho | px.setLocation( ((pReal.getX() - this.getMinX()) * w) / getWidth(),
|
412 | ((this.getMaxY() - pReal.getY()) * h) / getHeight());
|
||
413 | } |
||
414 | 9056 | nacho | |
415 | 6030 | nacho | int[] rgb = this.getPixel(pReal.getX(), pReal.getY()); |
416 | 9056 | nacho | |
417 | StringXMLItem[] item = new StringXMLItem[1]; |
||
418 | 5048 | ldiaz | String data = "<file:"+normalizeAsXMLTag(getName())+">\n"; |
419 | 9056 | nacho | |
420 | 5048 | ldiaz | data += " <raster\n";
|
421 | 6030 | nacho | data += " View_Point=\""+p.getX()+" , "+p.getY()+"\"\n"; |
422 | 9056 | nacho | data += " World_Point=\""+DoubleUtilities.format(pReal.getX(), 5)+" , "+DoubleUtilities.format(pReal.getY(), 5)+"\"\n"; |
423 | 6030 | nacho | if(px == null) |
424 | data += " Pixel_Point=\"Out\"\n";
|
||
425 | 9056 | nacho | else{
|
426 | 8765 | jjdelcerro | data += " Pixel_Point=\""+(int)px.getX()+" , "+(int)px.getY()+"\"\n"; |
427 | 9056 | nacho | data += " Band_Value=\"";
|
428 | |||
429 | for(int i = 0; i < this.getSource().getNumBands(); i++){ |
||
430 | Object pxData = ((RasterDriver)this.getSource().getDriver()).getData((int)px.getX(), (int)px.getY(), i); |
||
431 | if(pxData != null){ |
||
432 | if(this.getSource().getDataType() >= 0 && this.getSource().getDataType() <= 3) |
||
433 | data += ((Integer)pxData).intValue()+" "; |
||
434 | |||
435 | if(this.getSource().getDataType() >= 4) |
||
436 | data += ((Float)pxData).floatValue()+" "; |
||
437 | |||
438 | if(this.getSource().getDataType() >= 5) |
||
439 | data += ((Double)pxData).doubleValue()+" "; |
||
440 | } |
||
441 | } |
||
442 | data += "\"\n";
|
||
443 | } |
||
444 | |||
445 | 8765 | jjdelcerro | data += " RGB=\""+rgb[1]+" "+rgb[2]+" "+rgb[3]+"\"\n"; |
446 | 9056 | nacho | |
447 | 5048 | ldiaz | data += " />\n";
|
448 | data += "</file:"+normalizeAsXMLTag(getName())+">\n"; |
||
449 | 9056 | nacho | |
450 | item[0] = new StringXMLItem(data, this); |
||
451 | 5057 | ldiaz | return item;
|
452 | 9056 | nacho | |
453 | } |
||
454 | |||
455 | 3242 | luisw2 | /**
|
456 | * Filters a string for being suitable as XML Tag, erasing
|
||
457 | * all not alphabetic or numeric characters.
|
||
458 | * @param s
|
||
459 | * @return string normalized
|
||
460 | */
|
||
461 | public String normalizeAsXMLTag(String s) { |
||
462 | return s.replaceAll("[^a-zA-Z0-9]",""); |
||
463 | } |
||
464 | 435 | vcaballero | |
465 | 526 | vcaballero | /**
|
466 | 2396 | nacho | * Obtiene atributos a partir de un georasterfile
|
467 | * @return
|
||
468 | */
|
||
469 | public ArrayList getAttributes() { |
||
470 | return source.getAttributes();
|
||
471 | } |
||
472 | 9056 | nacho | |
473 | 2396 | nacho | /**
|
474 | 1828 | fernando | * @throws XMLException
|
475 | 526 | vcaballero | * @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperties()
|
476 | */
|
||
477 | 1828 | fernando | public XMLEntity getXMLEntity() throws XMLException { |
478 | 526 | vcaballero | XMLEntity xml = super.getXMLEntity();
|
479 | 9056 | nacho | |
480 | 895 | vcaballero | if (source instanceof RasterFileAdapter) { |
481 | xml.putProperty("file", ((RasterFileAdapter) source).getFile());
|
||
482 | 1034 | vcaballero | } |
483 | 9056 | nacho | |
484 | 895 | vcaballero | xml.putProperty("driverName", getSource().getDriver().getName());
|
485 | 9056 | nacho | |
486 | 11946 | maquerol | //Si no hay ning?n Status aplicamos el StatusLayerRaster que se usa por defecto
|
487 | 4234 | nacho | if(status == null) |
488 | status = new StatusLayerRaster();
|
||
489 | 9056 | nacho | |
490 | 4234 | nacho | status.getXMLEntity(xml, true, this); |
491 | 9056 | nacho | |
492 | |||
493 | 526 | vcaballero | return xml;
|
494 | } |
||
495 | 9056 | nacho | |
496 | 2183 | fernando | public void setXMLEntity03(XMLEntity xml) |
497 | throws XMLException {
|
||
498 | super.setXMLEntity(xml);
|
||
499 | try {
|
||
500 | Driver d = LayerFactory.getDM().getDriver(
|
||
501 | xml.getStringProperty("driverName"));
|
||
502 | File f = new File(xml.getStringProperty("file")); |
||
503 | RasterAdapter adapter = new RasterFileAdapter(f);
|
||
504 | adapter.setDriver(d); |
||
505 | setSource(adapter); |
||
506 | 11946 | maquerol | // Para notificar al adapter-driver cual es la proyecci?n.
|
507 | 2183 | fernando | setProjection(super.getProjection());
|
508 | } catch (DriverLoadException e) {
|
||
509 | throw new XMLException(e); |
||
510 | } |
||
511 | } |
||
512 | 9056 | nacho | |
513 | 1455 | luisw | public void setXMLEntity(XMLEntity xml) |
514 | throws XMLException {
|
||
515 | super.setXMLEntity(xml);
|
||
516 | try {
|
||
517 | Driver d = LayerFactory.getDM().getDriver(
|
||
518 | xml.getStringProperty("driverName"));
|
||
519 | File f = new File(xml.getStringProperty("file")); |
||
520 | RasterAdapter adapter = new RasterFileAdapter(f);
|
||
521 | adapter.setDriver(d); |
||
522 | setSource(adapter); |
||
523 | 11946 | maquerol | // Para notificar al adapter-driver cual es la proyecci?n.
|
524 | 1466 | luisw | setProjection(super.getProjection());
|
525 | 9056 | nacho | |
526 | 4234 | nacho | //Inicializamos la clase a la que se usa por defecto para
|
527 | //compatibilidad con proyectos antiguos
|
||
528 | String claseStr = StatusLayerRaster.defaultClass;
|
||
529 | 2183 | fernando | if (xml.contains("raster.class")) { |
530 | claseStr = xml.getStringProperty("raster.class");
|
||
531 | } |
||
532 | if(status!=null) |
||
533 | status.setXMLEntity(xml, this);
|
||
534 | else{
|
||
535 | 9056 | nacho | |
536 | //Cuando cargamos un proyecto
|
||
537 | |||
538 | 2183 | fernando | if(claseStr!=null && !claseStr.equals("")){ |
539 | try{
|
||
540 | Class clase = Class.forName(claseStr); |
||
541 | Constructor constr = clase.getConstructor(null); |
||
542 | status = (StatusRasterInterface)constr.newInstance(null);
|
||
543 | if(status!=null) |
||
544 | status.setXMLEntity(xml, this);
|
||
545 | }catch(ClassNotFoundException exc){ |
||
546 | exc.printStackTrace(); |
||
547 | }catch(InstantiationException exc){ |
||
548 | exc.printStackTrace(); |
||
549 | }catch(IllegalAccessException exc){ |
||
550 | exc.printStackTrace(); |
||
551 | }catch(NoSuchMethodException exc){ |
||
552 | exc.printStackTrace(); |
||
553 | }catch(InvocationTargetException exc){ |
||
554 | exc.printStackTrace(); |
||
555 | 9056 | nacho | } |
556 | 2183 | fernando | } |
557 | } |
||
558 | firstLoad = true;
|
||
559 | 9056 | nacho | |
560 | |||
561 | 1455 | luisw | } catch (DriverLoadException e) {
|
562 | 1472 | luisw | throw new XMLException(e); |
563 | 1455 | luisw | } |
564 | } |
||
565 | 435 | vcaballero | |
566 | 569 | luisw | /* (non-Javadoc)
|
567 | * @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)
|
||
568 | */
|
||
569 | 9013 | caballero | public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel, double scale, PrintRequestAttributeSet propeties) |
570 | 1488 | luisw | throws DriverException {
|
571 | 9056 | nacho | |
572 | if (isVisible() && isWithinScale(scale)){
|
||
573 | 1488 | luisw | isPrinting = true;
|
574 | if (!mustTilePrint) {
|
||
575 | 2531 | caballero | draw(null, g, viewPort, cancel,scale);
|
576 | 1488 | luisw | } else {
|
577 | // Para no pedir imagenes demasiado grandes, vamos
|
||
578 | // a hacer lo mismo que hace EcwFile: chunkear.
|
||
579 | 11946 | maquerol | // Llamamos a drawView con cuadraditos m?s peque?os
|
580 | 1488 | luisw | // del BufferedImage ni caso, cuando se imprime viene con null
|
581 | Tiling tiles = new Tiling(maxTilePrintWidth, maxTilePrintHeight, g.getClipRect());
|
||
582 | tiles.setAffineTransform((AffineTransform) viewPort.getAffineTransform().clone());
|
||
583 | 9056 | nacho | |
584 | 11946 | maquerol | //Si es la primera lectura salvamos los valores de m?ximo y m?nimo para la aplicaci?n
|
585 | 3084 | nacho | //de realce si la imagen es de 16 bits.
|
586 | 9056 | nacho | |
587 | Statistic stats = getSource().getFilterStack().getStats(); |
||
588 | 11148 | nacho | try{
|
589 | if(stats != null) |
||
590 | stats.history.add(stats.new History(getName(), stats.minBandValue, stats.maxBandValue, stats.secondMinBandValue, stats.secondMaxBandValue)); |
||
591 | }catch(NoSuchFieldError e){ |
||
592 | e.printStackTrace(); |
||
593 | } |
||
594 | 9056 | nacho | |
595 | |||
596 | 1488 | luisw | for (int tileNr=0; tileNr < tiles.getNumTiles(); tileNr++) { |
597 | // Parte que dibuja
|
||
598 | try {
|
||
599 | ViewPort vp = tiles.getTileViewPort(viewPort, tileNr); |
||
600 | 2531 | caballero | draw(null, g, vp, cancel,scale);
|
601 | 1488 | luisw | } catch (NoninvertibleTransformException e) { |
602 | // TODO Auto-generated catch block
|
||
603 | e.printStackTrace(); |
||
604 | } |
||
605 | } |
||
606 | 9056 | nacho | |
607 | 3084 | nacho | if(stats != null){ |
608 | getSource().getFilterStack().getStats().history.clear(); |
||
609 | stats = getSource().getFilterStack().getStats(); |
||
610 | } |
||
611 | 9056 | nacho | |
612 | 1488 | luisw | } |
613 | isPrinting = false;
|
||
614 | 2531 | caballero | } |
615 | 1488 | luisw | } |
616 | |||
617 | 2531 | caballero | public void _print(Graphics2D g, ViewPort viewPort, Cancellable cancel,double scale) |
618 | 9056 | nacho | throws DriverException {
|
619 | 1040 | fjp | // Para no pedir imagenes demasiado grandes, vamos
|
620 | // a hacer lo mismo que hace EcwFile: chunkear.
|
||
621 | 11946 | maquerol | // Llamamos a drawView con cuadraditos m?s peque?os
|
622 | 1040 | fjp | // del BufferedImage ni caso, cuando se imprime viene con null
|
623 | 9056 | nacho | |
624 | 1040 | fjp | int numW, numH;
|
625 | int stepX, stepY;
|
||
626 | int xProv, yProv, wProv, hProv;
|
||
627 | double xProvD, yProvD, wProvD, hProvD;
|
||
628 | 9056 | nacho | int A = 1500; |
629 | 1152 | fjp | int H = 1500; |
630 | 1040 | fjp | int altoAux, anchoAux;
|
631 | 9056 | nacho | |
632 | 1152 | fjp | AffineTransform mat = (AffineTransform) viewPort.getAffineTransform().clone(); |
633 | 9056 | nacho | |
634 | 1040 | fjp | // Vamos a hacerlo en trozos de AxH
|
635 | Rectangle r = g.getClipRect();
|
||
636 | numW = (int) (r.width) / A;
|
||
637 | 9056 | nacho | numH = (int) (r.height) / H;
|
638 | |||
639 | |||
640 | 1040 | fjp | double[] srcPts = new double[8]; |
641 | double[] dstPts= new double[8]; |
||
642 | |||
643 | yProv = (int) r.y;
|
||
644 | for (stepY=0; stepY < numH+1; stepY++) |
||
645 | { |
||
646 | 9056 | nacho | if ((yProv + H) > r.getMaxY())
|
647 | 1040 | fjp | altoAux = (int) r.getMaxY() - yProv;
|
648 | else
|
||
649 | altoAux = H; |
||
650 | 9056 | nacho | |
651 | 1040 | fjp | xProv = (int) r.x;
|
652 | 9056 | nacho | for (stepX=0; stepX < numW+1; stepX++) |
653 | { |
||
654 | if ((xProv + A) > r.getMaxX())
|
||
655 | 1040 | fjp | anchoAux = (int) r.getMaxX() - xProv;
|
656 | else
|
||
657 | anchoAux = A; |
||
658 | 9056 | nacho | |
659 | 1040 | fjp | Rectangle newRect = new Rectangle(xProv, yProv, anchoAux, altoAux); |
660 | 9056 | nacho | |
661 | 1040 | fjp | // Parte que dibuja
|
662 | srcPts[0] = xProv;
|
||
663 | srcPts[1] = yProv;
|
||
664 | srcPts[2] = xProv + anchoAux+1; |
||
665 | srcPts[3] = yProv;
|
||
666 | srcPts[4] = xProv + anchoAux+1; |
||
667 | srcPts[5] = yProv + altoAux+1; |
||
668 | srcPts[6] = xProv;
|
||
669 | srcPts[7] = yProv + altoAux+1; |
||
670 | 9056 | nacho | |
671 | 1040 | fjp | try {
|
672 | mat.inverseTransform(srcPts, 0, dstPts, 0, 4); |
||
673 | Rectangle2D.Double rectCuadricula = new Rectangle2D.Double( |
||
674 | dstPts[0], dstPts[1], |
||
675 | 9056 | nacho | dstPts[2] - dstPts[0], dstPts[5]-dstPts[3]); |
676 | 1040 | fjp | Extent extent = new Extent(rectCuadricula);
|
677 | 9056 | nacho | |
678 | 1040 | fjp | Dimension tam = new Dimension(anchoAux+1, altoAux+1); |
679 | ViewPort vp = viewPort.cloneViewPort(); |
||
680 | 1152 | fjp | vp.setImageSize(tam); |
681 | 1040 | fjp | vp.setExtent(rectCuadricula); |
682 | 1152 | fjp | vp.setAffineTransform(mat); |
683 | 1040 | fjp | // ViewPortData vp = new ViewPortData(getProjection(), extent, tam);
|
684 | // vp.setMat(mat);
|
||
685 | // rasterList.draw(g2, vp);
|
||
686 | 9056 | nacho | |
687 | 2396 | nacho | /*System.out.println("FLyrRaster.print(): fila "+stepX+" de "
|
688 | 9056 | nacho | + numW + " , col "+stepY+" de " + numH +
|
689 | 1152 | fjp | "\n, Extent = "+vp.getExtent() + " imageSize: "
|
690 | 2396 | nacho | + tam);*/
|
691 | 2531 | caballero | draw(null, g, vp, cancel,scale);
|
692 | 9056 | nacho | |
693 | 1040 | fjp | } catch (NoninvertibleTransformException e) { |
694 | // TODO Auto-generated catch block
|
||
695 | e.printStackTrace(); |
||
696 | } |
||
697 | // Fin parte que dibuja
|
||
698 | 9056 | nacho | xProv = xProv + A; |
699 | } |
||
700 | 1040 | fjp | yProv = yProv + H; |
701 | 9056 | nacho | } |
702 | |||
703 | 569 | luisw | } |
704 | 9056 | nacho | |
705 | 2183 | fernando | /**
|
706 | 11946 | maquerol | * A?ade un fichero a la capa raster
|
707 | 2183 | fernando | * @param fileName Nombre del fichero
|
708 | */
|
||
709 | public void addFiles(String fileName){ |
||
710 | ((RasterFileAdapter) source).addFile(fileName); |
||
711 | } |
||
712 | 9056 | nacho | |
713 | 2183 | fernando | /**
|
714 | * Elimina un fichero a la capa raster
|
||
715 | * @param fileName Nombre del fichero
|
||
716 | */
|
||
717 | public void delFile(String fileName){ |
||
718 | ((RasterFileAdapter) source).delFile(fileName); |
||
719 | } |
||
720 | 9056 | nacho | |
721 | 2623 | nacho | /* (non-Javadoc)
|
722 | * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#setBand(int, int)
|
||
723 | */
|
||
724 | public void setBand(int flag, int nBand) { |
||
725 | this.getSource().setBand(flag, nBand);
|
||
726 | } |
||
727 | 9056 | nacho | |
728 | 3257 | nacho | /**
|
729 | 11946 | maquerol | * Borra de la lista de listeners el que se pasa como par?metro.
|
730 | 3257 | nacho | *
|
731 | * @param o LayerListener a borrar.
|
||
732 | *
|
||
733 | * @return True si ha sido correcto el borrado del Listener.
|
||
734 | */
|
||
735 | public boolean removeLayerListener(LayerListener o) { |
||
736 | 3410 | nacho | if(this.isRemoveRasterFlag()){ |
737 | 4404 | nacho | /*try{
|
738 | 3410 | nacho | ((RasterFileAdapter) source).stop();
|
739 | }catch(DriverIOException exc){
|
||
740 | 9056 | nacho | |
741 | 4404 | nacho | }*/
|
742 | 3410 | nacho | this.setRemoveRasterFlag(true); |
743 | 3257 | nacho | } |
744 | return super.layerListeners.remove(o); |
||
745 | } |
||
746 | 9056 | nacho | |
747 | 3410 | nacho | /**
|
748 | * @return Returns the removeRasterFlag.
|
||
749 | */
|
||
750 | public boolean isRemoveRasterFlag() { |
||
751 | return removeRasterFlag;
|
||
752 | } |
||
753 | 9056 | nacho | |
754 | 3410 | nacho | /**
|
755 | * Asigna el valor del flag que dice si destruimos la memoria del raster
|
||
756 | * al eliminarlo del TOC o no.
|
||
757 | * @param removeRasterFlag The removeRasterFlag to set.
|
||
758 | */
|
||
759 | public void setRemoveRasterFlag(boolean removeRasterFlag) { |
||
760 | this.removeRasterFlag = removeRasterFlag;
|
||
761 | } |
||
762 | 9056 | nacho | |
763 | public ImageIcon getTocImageIcon() { |
||
764 | 5761 | jmvivo | return new ImageIcon(MapControl.class.getResource("images/icolayerRaster.PNG")); |
765 | } |
||
766 | 9056 | nacho | |
767 | 6007 | nacho | /*
|
768 | * (non-Javadoc)
|
||
769 | * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#getTileSize()
|
||
770 | */
|
||
771 | public int[] getTileSize() { |
||
772 | 9056 | nacho | int[] size = {maxTileDrawWidth, maxTileDrawHeight}; |
773 | 6007 | nacho | return size;
|
774 | } |
||
775 | |||
776 | /*
|
||
777 | * (non-Javadoc)
|
||
778 | * @see com.iver.cit.gvsig.fmap.layers.RasterOperations#isTiled()
|
||
779 | */
|
||
780 | public boolean isTiled() { |
||
781 | return mustTileDraw;
|
||
782 | } |
||
783 | 9056 | nacho | |
784 | 6037 | nacho | /**
|
785 | 11946 | maquerol | * Obtiene el flag que dice si la imagen est? o no georreferenciada
|
786 | * @return true si est? georreferenciada y false si no lo est?.
|
||
787 | 6037 | nacho | */
|
788 | public boolean isGeoreferenced() { |
||
789 | return getSource().isGeoreferenced();
|
||
790 | } |
||
791 | 9056 | nacho | |
792 | /**
|
||
793 | * Obtiene el grid asociado
|
||
794 | * @return grid
|
||
795 | */
|
||
796 | public Grid getGrid(){
|
||
797 | return getSource().getGrid();
|
||
798 | } |
||
799 | |||
800 | /**
|
||
801 | * Obtiene el tipo de dato de la capa raster
|
||
802 | * @return Entero que representa el tipo de dato de la capa raster.
|
||
803 | */
|
||
804 | public int getDataType() { |
||
805 | return getSource().getDataType();
|
||
806 | } |
||
807 | 9239 | nacho | |
808 | /*
|
||
809 | * (non-Javadoc)
|
||
810 | * @see com.iver.cit.gvsig.fmap.layers.FLayer#cloneLayer()
|
||
811 | */
|
||
812 | public FLayer cloneLayer() throws Exception { |
||
813 | FLyrRaster clonedLayer = new FLyrRaster();
|
||
814 | |||
815 | File f = new File(getSource().getFiles()[0].getName()); |
||
816 | RasterAdapter adapter = new RasterFileAdapter(f);
|
||
817 | com.hardcode.driverManager.Driver driver = LayerFactory.getDM().getDriver("gvSIG Image Driver");
|
||
818 | adapter.setDriver(driver); |
||
819 | |||
820 | clonedLayer.setName(getName()); |
||
821 | clonedLayer.setVisible(isVisible()); |
||
822 | clonedLayer.setSource(adapter); |
||
823 | clonedLayer.setProjection(getProjection()); |
||
824 | |||
825 | clonedLayer.setXMLEntity(getXMLEntity()); |
||
826 | |||
827 | clonedLayer.load(); |
||
828 | |||
829 | return clonedLayer;
|
||
830 | } |
||
831 | 10195 | nacho | |
832 | /**
|
||
833 | * Set data buffer to create a memory raster driver
|
||
834 | * @param buf data buffer
|
||
835 | */
|
||
836 | public void setBuffer(RasterBuf buf){ |
||
837 | source.setBuffer(buf); |
||
838 | } |
||
839 | |||
840 | /**
|
||
841 | * Set extent to create a memory raster driver
|
||
842 | * @param ext Extent
|
||
843 | */
|
||
844 | public void setExtent(Extent ext){ |
||
845 | source.setExtent(ext); |
||
846 | } |
||
847 | 11925 | maquerol | |
848 | public boolean isReprojectable() { |
||
849 | return false; |
||
850 | } |
||
851 | |||
852 | 11937 | maquerol | public boolean reProject(MapControl mapC) { |
853 | 11925 | maquerol | |
854 | 11933 | maquerol | //Descomentar cuando se valla a habilitar la reproyecci?n de raster
|
855 | 11925 | maquerol | |
856 | // ViewPort vPort = mapC.getViewPort();
|
||
857 | // ICoordTrans ct = this.getProjection().getCT(vPort.getProjection());
|
||
858 | // GeoRasterFile grf = ((CmsRasterDriver)this.getSource().getDriver()).getRasterFile();
|
||
859 | // this.setCoordTrans(ct);
|
||
860 | // if(grf instanceof GdalFile){
|
||
861 | // if((((GdalFile)grf).isGeoreferenced()) && (((GdalFile)grf).hasProjection())){
|
||
862 | // ReprojectRaster repRaster = new ReprojectRaster(this, file, mapC);
|
||
863 | // repRaster.warp(vPort.getProjection());
|
||
864 | // return false;
|
||
865 | // }
|
||
866 | // }
|
||
867 | // System.err.println("coordTrans = " + this.getProjection().getAbrev() + " "
|
||
868 | // + vPort.getProjection().getAbrev());
|
||
869 | 11933 | maquerol | //Si no termina el m?todo por el anterior return es que no se ha reproyectado por lo que es AddLayer el
|
870 | 11925 | maquerol | //que tiene que cargar la capa.
|
871 | return true; |
||
872 | } |
||
873 | |||
874 | 214 | fernando | } |