gvsig-raster / org.gvsig.raster / trunk / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.impl / src / main / java / org / gvsig / raster / impl / datastruct / ExtentImpl.java @ 1025
History | View | Annotate | Download (16.5 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.raster.impl.datastruct; |
23 |
|
24 |
import java.awt.geom.Point2D; |
25 |
import java.awt.geom.Rectangle2D; |
26 |
import java.text.DecimalFormat; |
27 |
|
28 |
import org.gvsig.fmap.dal.coverage.datastruct.Extent; |
29 |
import org.gvsig.fmap.dal.coverage.datastruct.GridExtent; |
30 |
import org.gvsig.raster.impl.grid.GridExtentImpl; |
31 |
import org.gvsig.tools.ToolsLocator; |
32 |
import org.gvsig.tools.dynobject.DynStruct; |
33 |
import org.gvsig.tools.persistence.PersistenceManager; |
34 |
import org.gvsig.tools.persistence.PersistentState; |
35 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
36 |
|
37 |
/**
|
38 |
* Clase que getiona el extent de una imagen
|
39 |
*
|
40 |
* @author Luis W.Sevilla (sevilla_lui@gva.es)
|
41 |
*/
|
42 |
public class ExtentImpl implements Extent { |
43 |
public static final String PERSISTENT_NAME = "Extent_Persistent"; |
44 |
public static final String PERSISTENT_DESCRIPTION = "Extent Persistent"; |
45 |
|
46 |
Point2D min = null; |
47 |
Point2D max = null; |
48 |
|
49 |
Point2D ul = new Point2D.Double(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY); |
50 |
Point2D lr = new Point2D.Double(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY); |
51 |
Point2D ur = new Point2D.Double(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY); |
52 |
Point2D ll = new Point2D.Double(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY); |
53 |
|
54 |
/**
|
55 |
* Constructor sin par?metros
|
56 |
*/
|
57 |
public ExtentImpl() {
|
58 |
min = new Point2D.Double(999999999.0, 999999999.0); |
59 |
max = new Point2D.Double(-999999999.0, -999999999.0); |
60 |
} |
61 |
|
62 |
/**
|
63 |
* Constructor
|
64 |
* @param pt1 punto que representa la esquina superior izquierda
|
65 |
* @param pt2 punto que representa la esquina inferior derecha
|
66 |
*/
|
67 |
public ExtentImpl(Point2D ul, Point2D lr) { |
68 |
this.ul = ul;
|
69 |
this.lr = lr;
|
70 |
newExtent(ul.getX(), ul.getY(), lr.getX(), lr.getY()); |
71 |
} |
72 |
|
73 |
/**
|
74 |
* Constructor
|
75 |
* @param ul punto que representa la esquina superior izquierda
|
76 |
* @param lr punto que representa la esquina inferior derecha
|
77 |
* @param ur punto que representa la esquina superior derecha
|
78 |
* @param ll punto que representa la esquina inferior izquierda
|
79 |
*/
|
80 |
public ExtentImpl(Point2D ul, Point2D lr, Point2D ur, Point2D ll) { |
81 |
this.ul = ul;
|
82 |
this.lr = lr;
|
83 |
this.ur = ur;
|
84 |
this.ll = ll;
|
85 |
min = new Point2D.Double( |
86 |
Math.min(Math.min(ul.getX(), lr.getX()), Math.min(ur.getX(), ll.getX())), |
87 |
Math.min(Math.min(ul.getY(), lr.getY()), Math.min(ur.getY(), ll.getY()))); |
88 |
max = new Point2D.Double( |
89 |
Math.max(Math.max(ul.getX(), lr.getX()), Math.max(ur.getX(), ll.getX())), |
90 |
Math.max(Math.max(ul.getY(), lr.getY()), Math.max(ur.getY(), ll.getY()))); |
91 |
} |
92 |
|
93 |
/**
|
94 |
* Contructor
|
95 |
* @param x1 punto que representa la coordenada X de la esquina superior izquierda
|
96 |
* @param y1 punto que representa la coordenada Y de la esquina superior izquierda
|
97 |
* @param x2 punto que representa la coordenada X de la esquina inferior derecha
|
98 |
* @param y2 punto que representa la coordenada Y de la esquina inferior derecha
|
99 |
*/
|
100 |
public ExtentImpl(double x1, double y1, double x2, double y2) { |
101 |
ul.setLocation(x1, y1); |
102 |
lr.setLocation(x2, y2); |
103 |
newExtent(x1, y1, x2, y2); |
104 |
} |
105 |
|
106 |
/**
|
107 |
* Constructor
|
108 |
* @param r Rectangulo 2D
|
109 |
*/
|
110 |
public ExtentImpl(Rectangle2D r) { |
111 |
ul.setLocation(r.getX(), r.getY() + r.getHeight()); |
112 |
lr.setLocation(r.getX() + r.getWidth(), r.getY()); |
113 |
newExtent(r.getX(), r.getY(), r.getX() + r.getWidth(), |
114 |
r.getY() + r.getHeight()); |
115 |
} |
116 |
|
117 |
/**
|
118 |
* Constructor de copia
|
119 |
* @param ext Objeto Extent
|
120 |
*/
|
121 |
public ExtentImpl(Extent ext) {
|
122 |
ul.setLocation(ext.getULX(), ext.getULY()); |
123 |
lr.setLocation(ext.getLRX(), ext.getLRY()); |
124 |
ur.setLocation(ext.getURX(), ext.getURY()); |
125 |
ll.setLocation(ext.getLLX(), ext.getLLY()); |
126 |
min = new Point2D.Double(ext.minX(), ext.minY()); |
127 |
max = new Point2D.Double(ext.maxX(), ext.maxY()); |
128 |
} |
129 |
|
130 |
/*
|
131 |
* (non-Javadoc)
|
132 |
* @see org.gvsig.fmap.dal.coverage.datastruct.Extent#encloseBoundinBoxes(org.gvsig.fmap.dal.coverage.datastruct.Extent)
|
133 |
*/
|
134 |
public Extent encloseBoundinBoxes(Extent ext) {
|
135 |
Point2D pUl = new Point2D.Double( |
136 |
Math.min(getULX(), ext.getULX()),
|
137 |
Math.max(getULY(), ext.getULY()));
|
138 |
Point2D pLr = new Point2D.Double( |
139 |
Math.max(getLRX(), ext.getLRX()),
|
140 |
Math.min(getLRY(), ext.getLRY()));
|
141 |
return new ExtentImpl(pUl, pLr); |
142 |
} |
143 |
|
144 |
/*
|
145 |
* (non-Javadoc)
|
146 |
* @see org.gvsig.fmap.dal.coverage.datastruct.Extent#intersection(org.gvsig.fmap.dal.coverage.datastruct.Extent)
|
147 |
*/
|
148 |
public Extent intersection(Extent ext) {
|
149 |
if(intersects(ext)) {
|
150 |
Point2D pUl = new Point2D.Double( |
151 |
Math.max(getULX(), ext.getULX()),
|
152 |
Math.min(getULY(), ext.getULY()));
|
153 |
Point2D pLr = new Point2D.Double( |
154 |
Math.min(getLRX(), ext.getLRX()),
|
155 |
Math.max(getLRY(), ext.getLRY()));
|
156 |
return new ExtentImpl(pUl, pLr); |
157 |
} |
158 |
return null; |
159 |
} |
160 |
|
161 |
/*
|
162 |
* (non-Javadoc)
|
163 |
* @see org.gvsig.fmap.dal.coverage.datastruct.Extent#intersects(org.gvsig.fmap.dal.coverage.datastruct.Extent)
|
164 |
*/
|
165 |
public boolean intersects(Extent ext) { |
166 |
if (getMin().getX() > ext.getMax().getX())
|
167 |
return false; |
168 |
else if (getMax().getX() < ext.getMin().getX()) |
169 |
return false; |
170 |
if (getMin().getY() > ext.getMax().getY())
|
171 |
return false; |
172 |
else if (getMax().getY() < ext.getMin().getY()) |
173 |
return false; |
174 |
return true; |
175 |
} |
176 |
|
177 |
/*ax
|
178 |
* (non-Javadoc)
|
179 |
* @see org.gvsig.fmap.dal.coverage.datastruct.Extent#getGridExtent(double)
|
180 |
*/
|
181 |
public GridExtent getGridExtent(double cellSize) { |
182 |
return new GridExtentImpl(this, cellSize); |
183 |
} |
184 |
|
185 |
/**
|
186 |
* Crea un objeto extent identico y lo retorna
|
187 |
* @return Objeto extent
|
188 |
*/
|
189 |
public Object clone() { |
190 |
ExtentImpl e = (ExtentImpl) clone(); |
191 |
e.min = (Point2D) min.clone();
|
192 |
e.max = (Point2D) max.clone();
|
193 |
|
194 |
e.ul = (Point2D) ul.clone();
|
195 |
e.lr = (Point2D) lr.clone();
|
196 |
e.ur = (Point2D) ur.clone();
|
197 |
e.ll = (Point2D) ll.clone();
|
198 |
return e;
|
199 |
} |
200 |
|
201 |
private void newExtent(double x1, double y1, double x2, double y2) { |
202 |
min = new Point2D.Double(Math.min(x1, x2), Math.min(y1, y2)); |
203 |
max = new Point2D.Double(Math.max(x1, x2), Math.max(y1, y2)); |
204 |
} |
205 |
|
206 |
/**
|
207 |
* Obtiene la coordenada X m?nima
|
208 |
* @return valor de la coordenada X m?nima
|
209 |
*/
|
210 |
public double minX() { |
211 |
return min.getX();
|
212 |
} |
213 |
|
214 |
/**
|
215 |
* Obtiene la coordenada Y m?nima
|
216 |
* @return valor de la coordenada X m?nima
|
217 |
*/
|
218 |
public double minY() { |
219 |
return min.getY();
|
220 |
} |
221 |
|
222 |
/**
|
223 |
* Obtiene la coordenada X m?xima
|
224 |
* @return valor de la coordenada X m?xima
|
225 |
*/
|
226 |
public double maxX() { |
227 |
return max.getX();
|
228 |
} |
229 |
|
230 |
/**
|
231 |
* Obtiene la coordenada Y m?xima
|
232 |
* @return valor de la coordenada Y m?xima
|
233 |
*/
|
234 |
public double maxY() { |
235 |
return max.getY();
|
236 |
} |
237 |
|
238 |
/**
|
239 |
* Obtiene el punto m?nimo
|
240 |
* @return m?nimo
|
241 |
*/
|
242 |
public Point2D getMin() { |
243 |
return min;
|
244 |
} |
245 |
|
246 |
/**
|
247 |
* Obtiene el punto m?ximo
|
248 |
* @return m?ximo
|
249 |
*/
|
250 |
public Point2D getMax() { |
251 |
return max;
|
252 |
} |
253 |
|
254 |
public boolean isAt(Point2D pt) { |
255 |
if (pt.getX() < minX())
|
256 |
return false; |
257 |
|
258 |
if (pt.getX() > maxX())
|
259 |
return false; |
260 |
|
261 |
if (pt.getY() < minY())
|
262 |
return false; |
263 |
|
264 |
if (pt.getY() > maxY())
|
265 |
return false; |
266 |
|
267 |
return true; |
268 |
} |
269 |
|
270 |
public double width() { |
271 |
return Math.abs(maxX() - minX()); |
272 |
} |
273 |
|
274 |
public double height() { |
275 |
return Math.abs(maxY() - minY()); |
276 |
} |
277 |
|
278 |
/**
|
279 |
* Obtiene el centro en X del extent
|
280 |
* @return
|
281 |
*/
|
282 |
public double getCenterX() { |
283 |
return minX() + (width() / 2); |
284 |
} |
285 |
|
286 |
/**
|
287 |
* Obtiene el centro en Y del extent
|
288 |
* @return
|
289 |
*/
|
290 |
public double getCenterY() { |
291 |
return minY() + (height() / 2); |
292 |
} |
293 |
|
294 |
/**
|
295 |
* Verifica un punto, y modifica el extent si no est? incluido
|
296 |
*/
|
297 |
public void add(Point2D pt) { |
298 |
if (pt == null) { |
299 |
return;
|
300 |
} |
301 |
|
302 |
min.setLocation(Math.min(pt.getX(), minX()), Math.min(pt.getY(), minY())); |
303 |
max.setLocation(Math.max(pt.getX(), maxX()), Math.max(pt.getY(), maxY())); |
304 |
} |
305 |
|
306 |
public void add(Extent ext) { |
307 |
if (ext == null) { |
308 |
return;
|
309 |
} |
310 |
|
311 |
min.setLocation(Math.min(ext.minX(), minX()),
|
312 |
Math.min(ext.minY(), minY()));
|
313 |
max.setLocation(Math.max(ext.maxX(), maxX()),
|
314 |
Math.max(ext.maxY(), maxY()));
|
315 |
} |
316 |
|
317 |
/**
|
318 |
* Obtiene la escala
|
319 |
* @param width Ancho
|
320 |
* @param height Alto
|
321 |
* @return
|
322 |
*/
|
323 |
public double[] getScale(int width, int height) { |
324 |
return getScale((double) width, (double) height); |
325 |
} |
326 |
|
327 |
public double[] getScale(double width, double height) { |
328 |
double[] scale = new double[2]; |
329 |
scale[0] = ((float) width) / width(); |
330 |
scale[1] = ((float) height) / height(); |
331 |
|
332 |
return scale;
|
333 |
} |
334 |
|
335 |
public Rectangle2D toRectangle2D() { |
336 |
return new Rectangle2D.Double(minX(), minY(), width(), height()); |
337 |
} |
338 |
|
339 |
public String toString() { |
340 |
DecimalFormat format = new DecimalFormat("####.000"); |
341 |
|
342 |
return "Extent: (" + format.format(minX()) + "," + |
343 |
format.format(minY()) + "), (" + format.format(maxX()) + "," + |
344 |
format.format(maxY()) + ")";
|
345 |
} |
346 |
|
347 |
public interface Has { |
348 |
public Extent getExtent();
|
349 |
} |
350 |
|
351 |
/**
|
352 |
* Obtiene la coordenada X de la esquina superior izquierda. Esta, en condiciones normales
|
353 |
* conincide con el minimo en X pero cuando un raster est? rotado esto puede variar.
|
354 |
* @return ulx
|
355 |
*/
|
356 |
public double getULX() { |
357 |
if(ul.getX() == -999999999.0) |
358 |
return minX();
|
359 |
return ul.getX();
|
360 |
} |
361 |
|
362 |
/**
|
363 |
* Obtiene la coordenada Y de la esquina superior izquierda. Esta, en condiciones normales
|
364 |
* conincide con el maximo en Y pero cuando un raster est? rotado esto puede variar.
|
365 |
* @return uly
|
366 |
*/
|
367 |
public double getULY() { |
368 |
if(ul.getY() == -999999999.0) |
369 |
return maxY();
|
370 |
return ul.getY();
|
371 |
} |
372 |
|
373 |
/**
|
374 |
* Obtiene la coordenada X de la esquina inferior derecha. Esta, en condiciones normales
|
375 |
* conincide con el m?ximo en X pero cuando un raster est? rotado esto puede variar.
|
376 |
* @return lrx
|
377 |
*/
|
378 |
public double getLRX() { |
379 |
if(lr.getX() == -999999999.0) |
380 |
return maxX();
|
381 |
return lr.getX();
|
382 |
} |
383 |
|
384 |
/**
|
385 |
* Obtiene la coordenada Y de la esquina inferior derecha. Esta, en condiciones normales
|
386 |
* conincide con el minimo en Y pero cuando un raster est? rotado esto puede variar.
|
387 |
* @return lry
|
388 |
*/
|
389 |
public double getLRY() { |
390 |
if(lr.getX() == -999999999.0) |
391 |
return minY();
|
392 |
return lr.getY();
|
393 |
} |
394 |
|
395 |
/**
|
396 |
* Obtiene la coordenada X de la esquina superior derecha.
|
397 |
* @return urx
|
398 |
*/
|
399 |
public double getURX() { |
400 |
return ur.getX();
|
401 |
} |
402 |
|
403 |
/**
|
404 |
* Obtiene la coordenada Y de la esquina superior derecha.
|
405 |
* @return uly
|
406 |
*/
|
407 |
public double getURY() { |
408 |
return ur.getY();
|
409 |
} |
410 |
|
411 |
/**
|
412 |
* Obtiene la coordenada X de la esquina inferior izquierda.
|
413 |
* @return lrx
|
414 |
*/
|
415 |
public double getLLX() { |
416 |
return ll.getX();
|
417 |
} |
418 |
|
419 |
/**
|
420 |
* Obtiene la coordenada Y de la esquina inferior izquierda.
|
421 |
* @return lly
|
422 |
*/
|
423 |
public double getLLY() { |
424 |
return ll.getY();
|
425 |
} |
426 |
|
427 |
/**
|
428 |
* Asigna la coordenada X de la esquina superior izquierda al m?nimo X
|
429 |
*/
|
430 |
public void setULXToMin() { |
431 |
ul.setLocation(minX(), ul.getY()); |
432 |
lr.setLocation(maxX(), lr.getY()); |
433 |
} |
434 |
|
435 |
/**
|
436 |
* Asigna la coordenada X de la esquina superior izquierda al m?ximo X
|
437 |
*/
|
438 |
public void setULXToMax() { |
439 |
ul.setLocation(maxX(), ul.getY()); |
440 |
lr.setLocation(minX(), lr.getY()); |
441 |
} |
442 |
|
443 |
/**
|
444 |
* Asigna la coordenada Y de la esquina superior izquierda al m?nimo Y
|
445 |
*/
|
446 |
public void setULYToMin() { |
447 |
ul.setLocation(ul.getX(), minY()); |
448 |
lr.setLocation(lr.getX(), maxY()); |
449 |
} |
450 |
|
451 |
/**
|
452 |
* Asigna la coordenada Y de la esquina superior izquierda al m?ximo Y
|
453 |
*/
|
454 |
public void setULYToMax() { |
455 |
ul.setLocation(ul.getX(), maxY()); |
456 |
lr.setLocation(lr.getX(), minY()); |
457 |
} |
458 |
|
459 |
/**
|
460 |
* Asigna la coordenada X de la esquina inferior derecha al m?nimo X
|
461 |
*/
|
462 |
public void setLRXToMin() { |
463 |
lr.setLocation(minX(), lr.getY()); |
464 |
ul.setLocation(maxX(), ul.getY()); |
465 |
|
466 |
} |
467 |
|
468 |
/**
|
469 |
* Asigna la coordenada X de la esquina inferior derecha al m?ximo X
|
470 |
*/
|
471 |
public void setLRXToMax() { |
472 |
lr.setLocation(maxX(), lr.getY()); |
473 |
ul.setLocation(minX(), ul.getY()); |
474 |
} |
475 |
|
476 |
/**
|
477 |
* Asigna la coordenada Y de la esquina inferior derecha al m?nimo Y
|
478 |
*/
|
479 |
public void setLRYToMin() { |
480 |
lr.setLocation(lr.getX(), minY()); |
481 |
ul.setLocation(ul.getX(), maxX()); |
482 |
} |
483 |
|
484 |
/**
|
485 |
* Asigna la coordenada Y de la esquina inferior derecha al m?ximo Y
|
486 |
*/
|
487 |
public void setLRYToMax() { |
488 |
lr.setLocation(lr.getX(), maxY()); |
489 |
ul.setLocation(ul.getX(), minY()); |
490 |
} |
491 |
|
492 |
/*
|
493 |
* (non-Javadoc)
|
494 |
* @see org.gvsig.tools.persistence.Persistent#loadFromState(org.gvsig.tools.persistence.PersistentState)
|
495 |
*/
|
496 |
public void loadFromState(PersistentState state) |
497 |
throws PersistenceException {
|
498 |
double ulx = state.getDouble("ulx"); |
499 |
double uly = state.getDouble("uly"); |
500 |
double lrx = state.getDouble("lrx"); |
501 |
double lry = state.getDouble("lry"); |
502 |
double urx = state.getDouble("urx"); |
503 |
double ury = state.getDouble("ury"); |
504 |
double llx = state.getDouble("llx"); |
505 |
double lly = state.getDouble("lly"); |
506 |
ul.setLocation(ulx, uly); |
507 |
lr.setLocation(lrx, lry); |
508 |
ur.setLocation(urx, ury); |
509 |
ll.setLocation(llx, lly); |
510 |
if( ur.getX() != Double.POSITIVE_INFINITY && |
511 |
ur.getY() != Double.POSITIVE_INFINITY &&
|
512 |
ll.getX() != Double.POSITIVE_INFINITY &&
|
513 |
ll.getY() != Double.POSITIVE_INFINITY) {
|
514 |
min = new Point2D.Double( |
515 |
Math.min(Math.min(ul.getX(), lr.getX()), Math.min(ur.getX(), ll.getX())), |
516 |
Math.min(Math.min(ul.getY(), lr.getY()), Math.min(ur.getY(), ll.getY()))); |
517 |
max = new Point2D.Double( |
518 |
Math.max(Math.max(ul.getX(), lr.getX()), Math.max(ur.getX(), ll.getX())), |
519 |
Math.max(Math.max(ul.getY(), lr.getY()), Math.max(ur.getY(), ll.getY()))); |
520 |
} else {
|
521 |
newExtent(ulx, uly, lrx, lry); |
522 |
} |
523 |
} |
524 |
|
525 |
/*
|
526 |
* (non-Javadoc)
|
527 |
* @see org.gvsig.tools.persistence.Persistent#saveToState(org.gvsig.tools.persistence.PersistentState)
|
528 |
*/
|
529 |
public void saveToState(PersistentState state) throws PersistenceException { |
530 |
state.set("ulx", getULX());
|
531 |
state.set("uly", getULY());
|
532 |
state.set("lrx", getLRX());
|
533 |
state.set("lry", getLRY());
|
534 |
state.set("urx", getURX());
|
535 |
state.set("ury", getURY());
|
536 |
state.set("llx", getLLX());
|
537 |
state.set("lly", getLLY());
|
538 |
} |
539 |
|
540 |
public static void registerPersistence() { |
541 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
542 |
DynStruct definition = manager.getDefinition(PERSISTENT_NAME); |
543 |
if( definition == null ) { |
544 |
definition = manager.addDefinition( |
545 |
Extent.class, |
546 |
PERSISTENT_NAME, |
547 |
PERSISTENT_DESCRIPTION, |
548 |
null,
|
549 |
null
|
550 |
); |
551 |
|
552 |
definition.addDynFieldDouble("ulx").setMandatory(true); |
553 |
definition.addDynFieldDouble("uly").setMandatory(true); |
554 |
definition.addDynFieldDouble("lrx").setMandatory(true); |
555 |
definition.addDynFieldDouble("lry").setMandatory(true); |
556 |
definition.addDynFieldDouble("urx").setMandatory(false); |
557 |
definition.addDynFieldDouble("ury").setMandatory(false); |
558 |
definition.addDynFieldDouble("llx").setMandatory(false); |
559 |
definition.addDynFieldDouble("lly").setMandatory(false); |
560 |
} |
561 |
} |
562 |
} |