Statistics
| Revision:

root / trunk / libraries / libRaster / src / org / gvsig / raster / grid / GridExtent.java @ 20871

History | View | Annotate | Download (6.69 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
        /**
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
                cellSizeY = dCellSizeY;
79
                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
                cellSizeY = dCellSizeY;
95
                recalculateNXAndNY();
96
        }
97
                
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
                return cellSizeX;
117
        }
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
                this.cellSizeX = cellSize;
125
                this.cellSizeY = cellSize;
126
                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
                m_iNY = (int) Math.floor((maxY() - minY()) / cellSizeY);
150
                m_iNX = (int) Math.floor((maxX() - minX()) / cellSizeX);
151
        }
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
                if ((extent.getCellSizeX() != this.getCellSizeX())||(extent.getCellSizeY() != this.getCellSizeY())){
165
                        return false;
166
                }
167
                dOffset = Math.abs(extent.minX() - minX());
168
                dOffsetCols = dOffset / getCellSizeX();
169
                bFitsX = (dOffsetCols == Math.floor(dOffsetCols));
170

    
171
                dOffset = Math.abs(extent.maxY() - maxY());
172
                dOffsetRows = dOffset / getCellSizeY();
173
                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
                                 cellSizeX == extent.getCellSizeX() && 
190
                                 cellSizeY == extent.getCellSizeY());
191
        }
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
                int x = (int)Math.floor((pt.getX() - minX()) / cellSizeX);
207
                int y = (int)Math.ceil((maxY() - pt.getY()) / cellSizeY);
208
                GridCell cell = new GridCell(x, y, 0.0);
209
                
210
                return cell;
211
        }
212
        
213
        public Point2D getWorldCoordsFromGridCoords(GridCell cell){
214
                double x = minX() + (cell.getX() + 0.5) * cellSizeX;
215
                double y = maxY() - (cell.getY() + 0.5) * cellSizeY;
216
                
217
                Point2D pt = new Point2D.Double(x, y);
218
                
219
                return pt;
220
        }
221

    
222
        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
}