gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / grid / GridExtentImpl.java @ 2443
History | View | Annotate | Download (6.87 KB)
1 |
/*******************************************************************************
|
---|---|
2 |
GridExtent.java
|
3 |
Copyright (C) Victor Olaya
|
4 |
|
5 |
This program is free software; you can redistribute it and/or modify
|
6 |
it under the terms of the GNU General Public License as published by
|
7 |
the Free Software Foundation; either version 2 of the License, or
|
8 |
(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.raster.impl.grid; |
20 |
|
21 |
import java.awt.geom.Point2D; |
22 |
|
23 |
import org.gvsig.fmap.dal.coverage.datastruct.Extent; |
24 |
import org.gvsig.fmap.dal.coverage.datastruct.GridExtent; |
25 |
import org.gvsig.raster.impl.datastruct.ExtentImpl; |
26 |
/**
|
27 |
* This class defines a grid system (coordinates and cellsize)
|
28 |
* @author Victor Olaya (volaya@ya.com)
|
29 |
*/
|
30 |
public class GridExtentImpl extends ExtentImpl implements GridExtent { |
31 |
double cellSizeX = 1; |
32 |
double cellSizeY = -1; |
33 |
int m_iNX;
|
34 |
int m_iNY;
|
35 |
|
36 |
public GridExtentImpl(){}
|
37 |
|
38 |
/**
|
39 |
* Assign the extension value and cell size.
|
40 |
* @param minX minimun value in X coordinate
|
41 |
* @param minY minimun value in Y coordinate
|
42 |
* @param maxX maximun value in X coordinate
|
43 |
* @param maxY maximun value in Y coordinate
|
44 |
* @param dCellSize cell size
|
45 |
*/
|
46 |
public GridExtentImpl(double minX, double minY, double maxX, double maxY, double dCellSize) { |
47 |
super(minX, minY, maxX, maxY);
|
48 |
cellSizeX = dCellSize; |
49 |
cellSizeY = -dCellSize; |
50 |
recalculateNXAndNY(); |
51 |
} |
52 |
|
53 |
/**
|
54 |
* Assign the extension value and cell size.
|
55 |
* @param minX minimun value in X coordinate
|
56 |
* @param minY minimun value in Y coordinate
|
57 |
* @param maxX maximun value in X coordinate
|
58 |
* @param maxY maximun value in Y coordinate
|
59 |
* @param dCellSize cell size
|
60 |
*/
|
61 |
public GridExtentImpl(Extent extent, double dCellSize) { |
62 |
super(extent);
|
63 |
cellSizeX = dCellSize; |
64 |
cellSizeY = -dCellSize; |
65 |
recalculateNXAndNY(); |
66 |
} |
67 |
|
68 |
/**
|
69 |
* Assign the extension value and cell size.
|
70 |
* @param minX minimun value in X coordinate
|
71 |
* @param minY minimun value in Y coordinate
|
72 |
* @param maxX maximun value in X coordinate
|
73 |
* @param maxY maximun value in Y coordinate
|
74 |
* @param dCellSizeX cell size in X coordinate
|
75 |
* @param dCellSizeX cell size in X coordinate
|
76 |
*/
|
77 |
public GridExtentImpl(double minX, double minY, double maxX, double maxY, double dCellSizeX, double dCellSizeY) { |
78 |
super(minX, minY, maxX, maxY);
|
79 |
cellSizeX = dCellSizeX; |
80 |
cellSizeY = dCellSizeY; |
81 |
recalculateNXAndNY(); |
82 |
} |
83 |
|
84 |
/**
|
85 |
* Assign the extension value and cell size.
|
86 |
* @param minX minimun value in X coordinate
|
87 |
* @param minY minimun value in Y coordinate
|
88 |
* @param maxX maximun value in X coordinate
|
89 |
* @param maxY maximun value in Y coordinate
|
90 |
* @param dCellSizeX cell size in X coordinate
|
91 |
* @param dCellSizeX cell size in X coordinate
|
92 |
*/
|
93 |
public GridExtentImpl(Extent extent, double dCellSizeX, double dCellSizeY) { |
94 |
super(extent);
|
95 |
cellSizeX = dCellSizeX; |
96 |
cellSizeY = dCellSizeY; |
97 |
recalculateNXAndNY(); |
98 |
} |
99 |
|
100 |
public void setXRange(double dXMin, double dXMax){ |
101 |
getMin().setLocation(Math.min(dXMin, dXMax), minY());
|
102 |
getMax().setLocation(Math.max(dXMin, dXMax), maxY());
|
103 |
recalculateNXAndNY(); |
104 |
} |
105 |
|
106 |
public void setYRange(double dYMin, double dYMax){ |
107 |
getMin().setLocation(minX(), Math.min(dYMin, dYMax));
|
108 |
getMax().setLocation(maxX(), Math.max(dYMin, dYMax));
|
109 |
recalculateNXAndNY(); |
110 |
|
111 |
} |
112 |
|
113 |
/**
|
114 |
* Get cell size
|
115 |
* @return cell size in double value
|
116 |
*/
|
117 |
public double getCellSize() { |
118 |
return cellSizeX;
|
119 |
} |
120 |
|
121 |
/**
|
122 |
* Set cell size and recalculate pixel distance in both axis
|
123 |
* @param cellSize cell size in double value
|
124 |
*/
|
125 |
public void setCellSize(double cellSize) { |
126 |
this.cellSizeX = cellSize;
|
127 |
this.cellSizeY = -cellSize;
|
128 |
recalculateNXAndNY(); |
129 |
} |
130 |
|
131 |
/**
|
132 |
* Get pixel width
|
133 |
* @return A integer with the pixel width value
|
134 |
*/
|
135 |
public int getNX() { |
136 |
return m_iNX;
|
137 |
} |
138 |
|
139 |
/**
|
140 |
* Get pixel height
|
141 |
* @return A integer with the pixel height value
|
142 |
*/
|
143 |
public int getNY() { |
144 |
return m_iNY;
|
145 |
} |
146 |
|
147 |
/**
|
148 |
* Calculates pixel width and pixel height
|
149 |
*/
|
150 |
private void recalculateNXAndNY(){ |
151 |
m_iNY = (int) Math.abs(Math.floor((minY() - maxY()) / cellSizeY)); |
152 |
m_iNX = (int) Math.abs(Math.floor((maxX() - minX()) / cellSizeX)); |
153 |
} |
154 |
|
155 |
public boolean contains(double x, double y){ |
156 |
return (x >= minX() && x <= maxX() && y >= minY() && y <= maxY());
|
157 |
} |
158 |
|
159 |
public boolean fitsIn(GridExtent extent){ |
160 |
|
161 |
boolean bFitsX, bFitsY;
|
162 |
double dOffset;
|
163 |
double dOffsetCols;
|
164 |
double dOffsetRows;
|
165 |
|
166 |
if ((extent.getCellSizeX() != this.getCellSizeX())||(extent.getCellSizeY() != this.getCellSizeY())){ |
167 |
return false; |
168 |
} |
169 |
dOffset = Math.abs(extent.minX() - minX());
|
170 |
dOffsetCols = dOffset / getCellSizeX(); |
171 |
bFitsX = (dOffsetCols == Math.floor(dOffsetCols));
|
172 |
|
173 |
dOffset = Math.abs(extent.maxY() - maxY());
|
174 |
dOffsetRows = dOffset / getCellSizeY(); |
175 |
bFitsY = (dOffsetRows == Math.floor(dOffsetRows));
|
176 |
|
177 |
return bFitsX && bFitsY;
|
178 |
|
179 |
} |
180 |
|
181 |
/**
|
182 |
* Compare a extent with the current GridExtent
|
183 |
* @param extent extent to compare
|
184 |
* @return true if two extents are equals and false if not
|
185 |
*/
|
186 |
public boolean equals(GridExtent extent){ |
187 |
return (minX() == extent.minX() &&
|
188 |
maxX() == extent.maxX() && |
189 |
minY() == extent.minY() && |
190 |
maxY() == extent.maxY() && |
191 |
cellSizeX == extent.getCellSizeX() && |
192 |
cellSizeY == extent.getCellSizeY()); |
193 |
} |
194 |
|
195 |
/**
|
196 |
* Add the layer extent as current extent
|
197 |
* @param layer Layer to set the extent
|
198 |
*/
|
199 |
/*public void addRasterLayerToExtent(FLyrRaster layer){
|
200 |
getMin().setLocation(Math.min(layer.getMinX(), minX()), Math.min(layer.getMinY(), minY()));
|
201 |
getMax().setLocation(Math.max(layer.getMaxX(), maxX()), Math.max(layer.getMaxY(), maxY()));
|
202 |
|
203 |
cellSize = Math.min(layer.getGrid().getXCellSize(), cellSize);
|
204 |
recalculateNXAndNY();
|
205 |
}*/
|
206 |
|
207 |
public GridCellImpl getGridCoordsFromWorldCoords(Point2D pt){ |
208 |
int x = (int)Math.floor((pt.getX() - minX()) / cellSizeX); |
209 |
int y = (int)Math.ceil((maxY() - pt.getY()) / cellSizeY); |
210 |
GridCellImpl cell = new GridCellImpl(x, y, 0.0); |
211 |
|
212 |
return cell;
|
213 |
} |
214 |
|
215 |
public Point2D getWorldCoordsFromGridCoords(GridCellImpl cell){ |
216 |
double x = minX() + (cell.getX() + 0.5) * cellSizeX; |
217 |
double y = maxY() - (cell.getY() + 0.5) * cellSizeY; |
218 |
|
219 |
Point2D pt = new Point2D.Double(x, y); |
220 |
|
221 |
return pt;
|
222 |
} |
223 |
|
224 |
public double getCellSizeX() { |
225 |
return cellSizeX;
|
226 |
} |
227 |
|
228 |
public void setCellSizeX(double cellSizeX) { |
229 |
this.cellSizeX = cellSizeX;
|
230 |
recalculateNXAndNY(); |
231 |
} |
232 |
|
233 |
public double getCellSizeY() { |
234 |
return cellSizeY;
|
235 |
} |
236 |
|
237 |
public void setCellSizeY(double cellSizeY) { |
238 |
this.cellSizeY = cellSizeY;
|
239 |
recalculateNXAndNY(); |
240 |
} |
241 |
} |