root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / Tiling.java @ 2859
History | View | Annotate | Download (4.84 KB)
1 |
/*
|
---|---|
2 |
* Created on 16-feb-2005
|
3 |
*/
|
4 |
package com.iver.cit.gvsig.fmap.layers; |
5 |
|
6 |
import java.awt.Dimension; |
7 |
import java.awt.Rectangle; |
8 |
import java.awt.geom.AffineTransform; |
9 |
import java.awt.geom.NoninvertibleTransformException; |
10 |
import java.awt.geom.Rectangle2D; |
11 |
|
12 |
import org.cresques.px.Extent; |
13 |
|
14 |
import com.iver.cit.gvsig.fmap.ViewPort; |
15 |
|
16 |
/**
|
17 |
* C?lculo de Partes (Tiles) en las que se divide un raster grande.
|
18 |
* Se usa para imprimir rasters y capas raste remotas (WMS).
|
19 |
*
|
20 |
* Para no pedir imagenes demasiado grandes, vamos
|
21 |
* a hacer lo mismo que hace EcwFile: chunkear.
|
22 |
* Llamamos a drawView con cuadraditos m?s peque?os
|
23 |
* del BufferedImage ni caso, cuando se imprime viene con null
|
24 |
* c?digo original de Fran Pe?arrubia
|
25 |
* @author Luis W. Sevilla (sevilla_lui@gva.es)
|
26 |
*/
|
27 |
|
28 |
public class Tiling { |
29 |
boolean debug = true; |
30 |
int tileMaxWidth, tileMaxHeight;
|
31 |
int numRows, numCols;
|
32 |
double [][] srcPts; |
33 |
Rectangle [] tile; |
34 |
|
35 |
AffineTransform mat;
|
36 |
ViewPort vp; |
37 |
|
38 |
public Tiling(int tileW, int tileH, Rectangle r) { |
39 |
tileMaxWidth = tileW; |
40 |
tileMaxHeight = tileH; |
41 |
|
42 |
int stepX, stepY;
|
43 |
int xProv, yProv, wProv, hProv;
|
44 |
double xProvD, yProvD, wProvD, hProvD;
|
45 |
int altoAux, anchoAux;
|
46 |
|
47 |
// Vamos a hacerlo en trozos de AxH
|
48 |
numCols = 1+(int) (r.width) / tileMaxWidth; |
49 |
numRows = 1+(int) (r.height) / tileMaxHeight; |
50 |
|
51 |
srcPts = new double[numCols*numRows][8]; |
52 |
tile = new Rectangle[numCols*numRows]; |
53 |
|
54 |
yProv = (int) r.y;
|
55 |
for (stepY=0; stepY < numRows; stepY++) { |
56 |
if ((yProv + tileMaxHeight) > r.getMaxY())
|
57 |
altoAux = (int) r.getMaxY() - yProv;
|
58 |
else
|
59 |
altoAux = tileMaxHeight; |
60 |
|
61 |
xProv = (int) r.x;
|
62 |
for (stepX=0; stepX < numCols; stepX++) { |
63 |
if ((xProv + tileMaxWidth) > r.getMaxX())
|
64 |
anchoAux = (int) r.getMaxX() - xProv;
|
65 |
else
|
66 |
anchoAux = tileMaxWidth; |
67 |
|
68 |
Rectangle newRect = new Rectangle(xProv, yProv, anchoAux, altoAux); |
69 |
int tileCnt = stepY*numCols+stepX;
|
70 |
// Parte que dibuja
|
71 |
srcPts[tileCnt][0] = xProv;
|
72 |
srcPts[tileCnt][1] = yProv;
|
73 |
srcPts[tileCnt][2] = xProv + anchoAux+1; |
74 |
srcPts[tileCnt][3] = yProv;
|
75 |
srcPts[tileCnt][4] = xProv + anchoAux+1; |
76 |
srcPts[tileCnt][5] = yProv + altoAux+1; |
77 |
srcPts[tileCnt][6] = xProv;
|
78 |
srcPts[tileCnt][7] = yProv + altoAux+1; |
79 |
|
80 |
tile[tileCnt] = new Rectangle(xProv, yProv, anchoAux+1, altoAux+1); |
81 |
|
82 |
xProv = xProv + tileMaxWidth; |
83 |
} |
84 |
yProv = yProv + tileMaxHeight; |
85 |
} |
86 |
} |
87 |
|
88 |
public double [] getTilePts(int colNr, int rowNr) { |
89 |
return srcPts[rowNr*numCols+colNr];
|
90 |
} |
91 |
|
92 |
public double [] getTilePts(int num) { |
93 |
return srcPts[num];
|
94 |
} |
95 |
|
96 |
public Rectangle getTileSz(int colNr, int rowNr) { |
97 |
return tile[rowNr*numCols+colNr];
|
98 |
} |
99 |
|
100 |
public Rectangle getTile(int num) { |
101 |
return tile[num];
|
102 |
} |
103 |
|
104 |
/**
|
105 |
* @return Returns the numCols.
|
106 |
*/
|
107 |
public int getNumCols() { |
108 |
return numCols;
|
109 |
} |
110 |
/**
|
111 |
* @return Returns the numRows.
|
112 |
*/
|
113 |
public int getNumRows() { |
114 |
return numRows;
|
115 |
} |
116 |
|
117 |
public int getNumTiles() { return numRows*numCols; } |
118 |
/**
|
119 |
* @return Returns the tileHeight.
|
120 |
*/
|
121 |
public int getMaxTileHeight() { |
122 |
return tileMaxHeight;
|
123 |
} |
124 |
/**
|
125 |
* @return Returns the tileWidth.
|
126 |
*/
|
127 |
public int getMaxTileWidth() { |
128 |
return tileMaxWidth;
|
129 |
} |
130 |
|
131 |
public ViewPort getTileViewPort(ViewPort viewPort, int tileNr) throws NoninvertibleTransformException { |
132 |
double [] dstPts = new double[8]; |
133 |
double [] srcPts = getTilePts(tileNr); |
134 |
Rectangle tile = getTile(tileNr);
|
135 |
//Rectangle newRect = new Rectangle((int)srcPts[0], (int)srcPts[1], tileSz[0], tileSz[1]);
|
136 |
|
137 |
mat.inverseTransform(srcPts, 0, dstPts, 0, 4); |
138 |
double x = dstPts[0], w = dstPts[2] - dstPts[0]; |
139 |
double y = dstPts[1], h = dstPts[5] - dstPts[3]; |
140 |
if (w < 0) { x = dstPts[2]; w = dstPts[0] - dstPts[2]; } |
141 |
if (h < 0) { y = dstPts[5]; h = dstPts[3] - dstPts[5]; } |
142 |
Rectangle2D.Double rectCuadricula = new Rectangle2D.Double(x, y, w, h); |
143 |
//Extent extent = new Extent(rectCuadricula);
|
144 |
|
145 |
ViewPort vp = viewPort.cloneViewPort(); |
146 |
vp.setImageSize(tile.getSize()); |
147 |
//vp.setOffset(tile.getLocation());
|
148 |
vp.setExtent(rectCuadricula); |
149 |
vp.setAffineTransform(mat); |
150 |
|
151 |
if (debug)
|
152 |
System.out.println("Tiling.print(): tile "+tileNr+" de " |
153 |
+ getNumTiles() + |
154 |
"\n, Extent = "+vp.getAdjustedExtent() + " tile: " |
155 |
+ tile); |
156 |
|
157 |
return vp;
|
158 |
} |
159 |
/**
|
160 |
* @return Returns the mat.
|
161 |
*/
|
162 |
public AffineTransform getAffineTransform() { |
163 |
return mat;
|
164 |
} |
165 |
/**
|
166 |
* @param mat The mat to set.
|
167 |
*/
|
168 |
public void setAffineTransform(AffineTransform mat) { |
169 |
this.mat = mat;
|
170 |
} |
171 |
/**
|
172 |
* @return Returns the debug.
|
173 |
*/
|
174 |
public boolean isDebug() { |
175 |
return debug;
|
176 |
} |
177 |
/**
|
178 |
* @param debug The debug to set.
|
179 |
*/
|
180 |
public void setDebug(boolean debug) { |
181 |
this.debug = debug;
|
182 |
} |
183 |
} |
184 |
|
185 |
|