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