root / trunk / libraries / libRaster / src / org / gvsig / raster / grid / GridExtent.java @ 13177
History | View | Annotate | Download (5.14 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 |
|
26 |
|
27 |
/**
|
28 |
* This class defines a grid system (coordinates and cellsize)
|
29 |
* @author Victor Olaya (volaya@ya.com)
|
30 |
*/
|
31 |
public class GridExtent extends Extent{ |
32 |
|
33 |
double cellSize = 1; |
34 |
int m_iNX;
|
35 |
int m_iNY;
|
36 |
|
37 |
public GridExtent(){}
|
38 |
|
39 |
/**
|
40 |
* Assign the extension value and cell size.
|
41 |
* @param minX minimun value in X coordinate
|
42 |
* @param minY minimun value in Y coordinate
|
43 |
* @param maxX maximun value in X coordinate
|
44 |
* @param maxY maximun value in Y coordinate
|
45 |
* @param dCellSize cell size
|
46 |
*/
|
47 |
public GridExtent(double minX, double minY, double maxX, |
48 |
double maxY, double dCellSize){ |
49 |
super(minX, minY, maxX, maxY);
|
50 |
this.cellSize = dCellSize;
|
51 |
recalculateNXAndNY(); |
52 |
} |
53 |
|
54 |
/**
|
55 |
* Assign the extension value and cell size.
|
56 |
* @param minX minimun value in X coordinate
|
57 |
* @param minY minimun value in Y coordinate
|
58 |
* @param maxX maximun value in X coordinate
|
59 |
* @param maxY maximun value in Y coordinate
|
60 |
* @param dCellSize cell size
|
61 |
*/
|
62 |
public GridExtent(Extent extent, double dCellSize){ |
63 |
super(extent);
|
64 |
this.cellSize = dCellSize;
|
65 |
recalculateNXAndNY(); |
66 |
} |
67 |
|
68 |
public void setXRange(double dXMin, double dXMax){ |
69 |
getMin().setLocation(Math.min(dXMin, dXMax), minY());
|
70 |
getMax().setLocation(Math.max(dXMin, dXMax), maxY());
|
71 |
recalculateNXAndNY(); |
72 |
} |
73 |
|
74 |
public void setYRange(double dYMin, double dYMax){ |
75 |
getMin().setLocation(minX(), Math.min(dYMin, dYMax));
|
76 |
getMax().setLocation(maxX(), Math.max(dYMin, dYMax));
|
77 |
recalculateNXAndNY(); |
78 |
|
79 |
} |
80 |
|
81 |
/**
|
82 |
* Get cell size
|
83 |
* @return cell size in double value
|
84 |
*/
|
85 |
public double getCellSize() { |
86 |
return cellSize;
|
87 |
} |
88 |
|
89 |
/**
|
90 |
* Set cell size and recalculate pixel distance in both axis
|
91 |
* @param cellSize cell size in double value
|
92 |
*/
|
93 |
public void setCellSize(double cellSize) { |
94 |
this.cellSize = cellSize;
|
95 |
recalculateNXAndNY(); |
96 |
} |
97 |
|
98 |
/**
|
99 |
* Get pixel width
|
100 |
* @return A integer with the pixel width value
|
101 |
*/
|
102 |
public int getNX() { |
103 |
return m_iNX;
|
104 |
} |
105 |
|
106 |
/**
|
107 |
* Get pixel height
|
108 |
* @return A integer with the pixel height value
|
109 |
*/
|
110 |
public int getNY() { |
111 |
return m_iNY;
|
112 |
} |
113 |
|
114 |
/**
|
115 |
* Calculates pixel width and pixel height
|
116 |
*/
|
117 |
private void recalculateNXAndNY(){ |
118 |
m_iNY = (int) Math.floor((maxY() - minY()) / cellSize); |
119 |
m_iNX = (int) Math.floor((maxX() - minX()) / cellSize); |
120 |
} |
121 |
|
122 |
public boolean contains(double x, double y){ |
123 |
return (x >= minX() && x <= maxX() && y >= minY() && y <= maxY());
|
124 |
} |
125 |
|
126 |
public boolean fitsIn(GridExtent extent){ |
127 |
|
128 |
boolean bFitsX, bFitsY;
|
129 |
double dOffset;
|
130 |
double dOffsetCols;
|
131 |
double dOffsetRows;
|
132 |
|
133 |
if (extent.getCellSize() != this.getCellSize()){ |
134 |
return false; |
135 |
} |
136 |
dOffset = Math.abs(extent.minX() - minX());
|
137 |
dOffsetCols = dOffset / getCellSize(); |
138 |
bFitsX = (dOffsetCols == Math.floor(dOffsetCols));
|
139 |
|
140 |
dOffset = Math.abs(extent.maxY() - maxY());
|
141 |
dOffsetRows = dOffset / getCellSize(); |
142 |
bFitsY = (dOffsetRows == Math.floor(dOffsetRows));
|
143 |
|
144 |
return bFitsX && bFitsY;
|
145 |
|
146 |
} |
147 |
|
148 |
/**
|
149 |
* Compare a extent with the current GridExtent
|
150 |
* @param extent extent to compare
|
151 |
* @return true if two extents are equals and false if not
|
152 |
*/
|
153 |
public boolean equals(GridExtent extent){ |
154 |
return (minX() == extent.minX() &&
|
155 |
maxX() == extent.maxX() && |
156 |
minY() == extent.minY() && |
157 |
maxY() == extent.maxY() && |
158 |
cellSize == extent.getCellSize()); |
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()) / cellSize); |
175 |
int y = (int)Math.ceil((maxY() - pt.getY()) / cellSize); |
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) * cellSize; |
183 |
double y = maxY() - (cell.getY() + 0.5) * cellSize; |
184 |
|
185 |
Point2D pt = new Point2D.Double(x, y); |
186 |
|
187 |
return pt;
|
188 |
} |
189 |
|
190 |
} |