Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libRaster / src / org / gvsig / raster / grid / GridExtent.java @ 29786

History | View | Annotate | Download (6.69 KB)

1 10740 nacho
/*******************************************************************************
2 21495 bsanchez
                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 10740 nacho

10 21495 bsanchez
                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 10740 nacho

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

201
                cellSize = Math.min(layer.getGrid().getXCellSize(), cellSize);
202
                recalculateNXAndNY();
203
        }*/
204
205
        public GridCell getGridCoordsFromWorldCoords(Point2D pt){
206 20825 dguerrero
                int x = (int)Math.floor((pt.getX() - minX()) / cellSizeX);
207
                int y = (int)Math.ceil((maxY() - pt.getY()) / cellSizeY);
208 10740 nacho
                GridCell cell = new GridCell(x, y, 0.0);
209
210
                return cell;
211
        }
212
213
        public Point2D getWorldCoordsFromGridCoords(GridCell cell){
214 20825 dguerrero
                double x = minX() + (cell.getX() + 0.5) * cellSizeX;
215
                double y = maxY() - (cell.getY() + 0.5) * cellSizeY;
216 10740 nacho
217
                Point2D pt = new Point2D.Double(x, y);
218
219
                return pt;
220
        }
221
222 20825 dguerrero
        public double getCellSizeX() {
223
                return cellSizeX;
224
        }
225
226
        public void setCellSizeX(double cellSizeX) {
227
                this.cellSizeX = cellSizeX;
228
                recalculateNXAndNY();
229
        }
230
231
        public double getCellSizeY() {
232
                return cellSizeY;
233
        }
234
235
        public void setCellSizeY(double cellSizeY) {
236
                this.cellSizeY = cellSizeY;
237
                recalculateNXAndNY();
238
        }
239 20828 bsanchez
}