1 |
/******************************************************************************* |
2 |
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
* |
4 |
* This file is part of the SCHMITZM library - a collection of utility |
5 |
* classes based on Java 1.6, focusing (not only) on Java Swing |
6 |
* and the Geotools library. |
7 |
* |
8 |
* The SCHMITZM project is hosted at: |
9 |
* http://wald.intevation.org/projects/schmitzm/ |
10 |
* |
11 |
* This program is free software; you can redistribute it and/or |
12 |
* modify it under the terms of the GNU Lesser General Public License |
13 |
* as published by the Free Software Foundation; either version 3 |
14 |
* of the License, or (at your option) any later version. |
15 |
* |
16 |
* This program is distributed in the hope that it will be useful, |
17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 |
* GNU General Public License for more details. |
20 |
* |
21 |
* You should have received a copy of the GNU Lesser General Public License (license.txt) |
22 |
* along with this program; if not, write to the Free Software |
23 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
24 |
* or try this link: http://www.gnu.org/licenses/lgpl.html |
25 |
* |
26 |
* Contributors: |
27 |
* Martin O. J. Schmitz - initial API and implementation |
28 |
* Stefan A. Tzeggai - additional utility classes |
29 |
******************************************************************************/ |
30 |
package appl.util; |
31 |
|
32 |
import java.awt.image.DataBuffer; |
33 |
import java.io.Serializable; |
34 |
|
35 |
import org.geotools.referencing.crs.DefaultGeographicCRS; |
36 |
import org.opengis.referencing.crs.CoordinateReferenceSystem; |
37 |
|
38 |
import schmitzm.data.WritableGrid; |
39 |
|
40 |
/** |
41 |
* Simple immutable class that encapsulates raster metadata, |
42 |
* especially the MetaData of a {@link WritableGrid}. |
43 |
* Just a constructor and getters |
44 |
* on the fields. |
45 |
* |
46 |
* <br> |
47 |
* @see WritableGrid for details on the variable description |
48 |
* |
49 |
* @author Dominik Appl |
50 |
*/ |
51 |
public final class RasterMetaData implements Serializable { |
52 |
|
53 |
int width = 0; |
54 |
|
55 |
int height = 0; |
56 |
|
57 |
int minX = 0; |
58 |
|
59 |
int minY = 0; |
60 |
|
61 |
double realWidth = 0; |
62 |
|
63 |
double realHeight = 0; |
64 |
|
65 |
int dataType = DataBuffer.TYPE_UNDEFINED; |
66 |
|
67 |
double x = 0.0; |
68 |
|
69 |
double y = 0.0; |
70 |
|
71 |
CoordinateReferenceSystem crs = null; |
72 |
|
73 |
|
74 |
/** |
75 |
* @return Returns the CRS of the raster |
76 |
*/ |
77 |
public final CoordinateReferenceSystem getCoordinateReferenceSystem() { |
78 |
return crs; |
79 |
} |
80 |
|
81 |
/** |
82 |
* @return Returns the height in cells |
83 |
*/ |
84 |
public final int getHeight() { |
85 |
return height; |
86 |
} |
87 |
|
88 |
/** |
89 |
* @return Returns the width in cells. |
90 |
*/ |
91 |
public final int getWidth() { |
92 |
return width; |
93 |
} |
94 |
|
95 |
/** |
96 |
* @return Returns the dataType. |
97 |
*/ |
98 |
public final int getDataType() { |
99 |
return dataType; |
100 |
} |
101 |
|
102 |
/** |
103 |
* @return Returns minX (used to indicate a start index) |
104 |
* @see WritableGrid#getMinX() |
105 |
*/ |
106 |
public final int getMinX() { |
107 |
return minX; |
108 |
} |
109 |
|
110 |
/** |
111 |
* @return Returns the minY ((used to indicate a start index) |
112 |
* @see WritableGrid#getMinY() |
113 |
*/ |
114 |
public final int getMinY() { |
115 |
return minY; |
116 |
} |
117 |
|
118 |
/** |
119 |
* @return Returns the realHeight. |
120 |
*/ |
121 |
public final double getRealHeight() { |
122 |
return realHeight; |
123 |
} |
124 |
|
125 |
/** |
126 |
* @return Returns the realWidth. |
127 |
*/ |
128 |
public final double getRealWidth() { |
129 |
return realWidth; |
130 |
} |
131 |
|
132 |
/** |
133 |
* @return Returns the (geographic) x-coordinate |
134 |
*/ |
135 |
public final double getX() { |
136 |
return x; |
137 |
} |
138 |
|
139 |
|
140 |
/** |
141 |
* @return Returns the (geographic) y-coordinate |
142 |
*/ |
143 |
public final double getY() { |
144 |
return y; |
145 |
} |
146 |
|
147 |
/** |
148 |
* @param dataType the datatype |
149 |
* @param gridWidth the width of the grid (in cells) |
150 |
* @param gridHeight the height of the grid (in cells) |
151 |
* @param minX the minX (used to indicate a start index) |
152 |
* @param minY the minY (used to indicate a start index) |
153 |
* @param realWidth the real width |
154 |
* @param realHeight the real height |
155 |
* @param x the (geographic) x-coordinate |
156 |
* @param y the (geographic) y-coordinate |
157 |
* @param crs the {@link CoordinateReferenceSystem}. Use null for DefaultCRS (WGS84) |
158 |
* |
159 |
* @see WritableGrid |
160 |
*/ |
161 |
public RasterMetaData(int dataType, int gridWidth, int gridHeight, |
162 |
int minX, int minY, double x, double y, double realWidth, |
163 |
double realHeight, CoordinateReferenceSystem crs) { |
164 |
this.width = gridWidth; |
165 |
this.height = gridHeight; |
166 |
this.minX = minX; |
167 |
this.minY = minY; |
168 |
this.realWidth = realWidth; |
169 |
this.realHeight = realHeight; |
170 |
this.dataType = dataType; |
171 |
this.x = x; |
172 |
this.y = y; |
173 |
this.crs = (crs == null) ? DefaultGeographicCRS.WGS84 : crs; |
174 |
|
175 |
} |
176 |
|
177 |
/** |
178 |
* Constructs a RasterMetaData Object. The values of the real height/ width |
179 |
* are calculated out of the cellsize. The cells are assumed to be squares. |
180 |
* |
181 |
* @param dataType the datatype |
182 |
* @param gridWidth the width of the grid (in cells) |
183 |
* @param gridHeight the height of the grid (in cells) |
184 |
* @param minX the minX (used to indicate a start index) |
185 |
* @param minY the minY (used to indicate a start index) |
186 |
* @param x the (geographic) x-coordinate |
187 |
* @param y the (geographic) y-coordinate |
188 |
* @param cellSize the real size of one cell |
189 |
* @param crs the {@link CoordinateReferenceSystem}, use null for default CRS (WGS84) |
190 |
*/ |
191 |
public RasterMetaData(int dataType, int gridWidth, int gridHeight, |
192 |
int minX, int minY, double x, double y, double cellSize, CoordinateReferenceSystem crs) { |
193 |
|
194 |
this(dataType, gridWidth, gridHeight, minX, minY, x, y, gridWidth |
195 |
* cellSize, gridHeight * cellSize,crs); |
196 |
} |
197 |
|
198 |
/** |
199 |
* Constructs a RasterMetaDataObject out of the given Grid |
200 |
* @param w the source grid |
201 |
*/ |
202 |
public RasterMetaData(WritableGrid w) { |
203 |
super(); |
204 |
this.width = w.getWidth(); |
205 |
this.height = w.getHeight(); |
206 |
this.minX = w.getMinX(); |
207 |
this.minY = w.getMinY(); |
208 |
this.realWidth = w.getRealWidth(); |
209 |
this.realHeight = w.getRealHeight(); |
210 |
this.dataType = w.getSampleType(); |
211 |
this.x = w.getX(); |
212 |
this.y = w.getY(); |
213 |
this.crs = w.getCoordinateReferenceSystem(); |
214 |
} |
215 |
|
216 |
/** |
217 |
* needed for serialization |
218 |
*/ |
219 |
private RasterMetaData() { |
220 |
this(0,0,0,0,0,0,0,0,0,null); |
221 |
} |
222 |
|
223 |
/** |
224 |
* @return the real width of a raster cell |
225 |
*/ |
226 |
public final double getCellWidth() { |
227 |
return getRealWidth() / getWidth(); |
228 |
} |
229 |
|
230 |
/** |
231 |
* Checks if the given RasterMetaData object has the same values. |
232 |
* |
233 |
* @param rasterMeta must be a RasterMetaDataObject! Else ClassCastException will be thrown. |
234 |
* @return true, if all values are the same |
235 |
* @see java.lang.Object#equals(java.lang.Object) |
236 |
*/ |
237 |
@Override |
238 |
public boolean equals(Object rasterMeta) { |
239 |
RasterMetaData r = (RasterMetaData) rasterMeta; |
240 |
return ( |
241 |
this.width == r.getWidth() && |
242 |
this.height == r.getHeight() && |
243 |
this.minX == r.getMinX() && |
244 |
this.minY == r.getMinY() && |
245 |
this.realWidth == r.getRealWidth() && |
246 |
this.realHeight == r.getRealHeight() && |
247 |
this.dataType == r.getDataType() && |
248 |
this.x == r.getX() && |
249 |
this.y == r.getY() |
250 |
); |
251 |
} |
252 |
|
253 |
/** |
254 |
* @return the real height of a raster cell |
255 |
*/ |
256 |
public final double getCellHeight() { |
257 |
return getRealHeight() / getHeight(); |
258 |
} |
259 |
|
260 |
public final String toString() { |
261 |
return "MetaData: type:" + dataType + " wc:" + width + "hc:" + height |
262 |
+ " minX:" + minX + " minY:" + minY + " x:" + x + " y:" + y |
263 |
+ " rw:" + realWidth + " rh:" + realHeight; |
264 |
} |
265 |
} |