svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.impl / src / main / java / org / gvsig / fmap / geom / primitive / impl / Envelope3D.java @ 41423
History | View | Annotate | Download (7.99 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or modify it under
|
7 |
* the terms of the GNU General Public License as published by the Free Software
|
8 |
* Foundation; either version 3 of the License, or (at your option) any later
|
9 |
* version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful, but WITHOUT
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
|
13 |
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
|
14 |
* details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License along with
|
17 |
* this program; if not, write to the Free Software Foundation, Inc., 51
|
18 |
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
19 |
*
|
20 |
* For any additional information, do not hesitate to contact us at info AT
|
21 |
* gvsig.com, or visit our website www.gvsig.com.
|
22 |
*/
|
23 |
package org.gvsig.fmap.geom.primitive.impl; |
24 |
|
25 |
import org.cresques.cts.ICoordTrans; |
26 |
import org.gvsig.fmap.geom.primitive.Envelope; |
27 |
import org.gvsig.fmap.geom.primitive.EnvelopeNotInitializedException; |
28 |
import org.gvsig.fmap.geom.primitive.Point; |
29 |
import org.gvsig.tools.ToolsLocator; |
30 |
import org.gvsig.tools.dynobject.DynStruct; |
31 |
import org.gvsig.tools.lang.Cloneable; |
32 |
import org.gvsig.tools.persistence.PersistenceManager; |
33 |
|
34 |
/**
|
35 |
* @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
|
36 |
*/
|
37 |
public class Envelope3D extends DefaultEnvelope implements Cloneable { |
38 |
|
39 |
public static final String PERSISTENCE_DEFINITION_NAME = "Envelope3Dimensions"; |
40 |
private static final int DIMENSION = 3; |
41 |
private boolean isZInitilized = false; |
42 |
|
43 |
public Envelope3D() {
|
44 |
super();
|
45 |
} |
46 |
|
47 |
public Envelope3D(Point min, Point max) { |
48 |
super(min, max);
|
49 |
} |
50 |
|
51 |
/* (non-Javadoc)
|
52 |
* @see org.gvsig.fmap.geom.primitive.Envelope#getDimension()
|
53 |
*/
|
54 |
public int getDimension() { |
55 |
return DIMENSION;
|
56 |
} |
57 |
|
58 |
/*
|
59 |
* (non-Javadoc)
|
60 |
* @see org.gvsig.fmap.geom.primitive.Envelope#convert(org.cresques.cts.ICoordTrans)
|
61 |
*/
|
62 |
public Envelope convert(ICoordTrans trans) {
|
63 |
|
64 |
if (isEmpty) {
|
65 |
throw new EnvelopeNotInitializedException(); |
66 |
} |
67 |
|
68 |
if (trans == null) { |
69 |
// clone
|
70 |
return new Envelope2D(this.getLowerCorner(), this.getUpperCorner()); |
71 |
} |
72 |
|
73 |
// if (this.getDimension() > 2) {
|
74 |
// return null;
|
75 |
// }
|
76 |
|
77 |
// We'll reproject by taking samples like this:
|
78 |
//
|
79 |
// *---*---*---*---*
|
80 |
// | |
|
81 |
// * * * * *
|
82 |
// | |
|
83 |
// * * * * *
|
84 |
// | |
|
85 |
// * * * * *
|
86 |
// | |
|
87 |
// *---*---*---*---*
|
88 |
//
|
89 |
// This is because:
|
90 |
//
|
91 |
// - In some CRS (for example EPSG:4326) the north/south pole is a "line"
|
92 |
// while in other CRS the north/south pole is a point, so if you
|
93 |
// reproject the bounding box of the world, the result can be absurd.
|
94 |
// - Sometimes the top/bottom/right/bottom of one envelope
|
95 |
// corresponds to a strange point in the the other envelope
|
96 |
// (not even a point in the perimeter)
|
97 |
// - More generally, reprojecting usually implies a rotation (the result
|
98 |
// is a rotated envelope) so it's better to use a few
|
99 |
// samples along the perimeter.
|
100 |
double xmin = getMinimum(0); |
101 |
double ymin = getMinimum(1); |
102 |
double step_w = 0.25 * (getMaximum(0) - xmin); |
103 |
double step_h = 0.25 * (getMaximum(1) - ymin); |
104 |
|
105 |
java.awt.geom.Point2D sample = null;
|
106 |
java.awt.geom.Point2D sample_trans = null;
|
107 |
// Init with worst values
|
108 |
java.awt.geom.Point2D res_min = new java.awt.geom.Point2D.Double(
|
109 |
Double.MAX_VALUE, Double.MAX_VALUE); |
110 |
java.awt.geom.Point2D res_max = new java.awt.geom.Point2D.Double(
|
111 |
-Double.MAX_VALUE, -Double.MAX_VALUE); |
112 |
|
113 |
int added = 0; |
114 |
for (int i = 0; i < 5; i++) { |
115 |
for (int j = 0; j < 5; j++) { |
116 |
sample = new java.awt.geom.Point2D.Double(
|
117 |
xmin + i * step_w, |
118 |
ymin + j * step_h); |
119 |
sample_trans = new java.awt.geom.Point2D.Double(0, 0); |
120 |
try {
|
121 |
sample_trans = trans.convert(sample, sample_trans); |
122 |
} catch (Exception exc) { |
123 |
// Unable to convert this one: ignore
|
124 |
continue;
|
125 |
} |
126 |
// Update max/min found
|
127 |
// X
|
128 |
if (sample_trans.getX() > res_max.getX()) {
|
129 |
res_max.setLocation(sample_trans.getX(), res_max.getY()); |
130 |
} |
131 |
if (sample_trans.getX() < res_min.getX()) {
|
132 |
res_min.setLocation(sample_trans.getX(), res_min.getY()); |
133 |
} |
134 |
// Y
|
135 |
if (sample_trans.getY() > res_max.getY()) {
|
136 |
res_max.setLocation(res_max.getX(), sample_trans.getY()); |
137 |
} |
138 |
if (sample_trans.getY() < res_min.getY()) {
|
139 |
res_min.setLocation(res_min.getX(), sample_trans.getY()); |
140 |
} |
141 |
added++; |
142 |
} |
143 |
} |
144 |
|
145 |
if (added == 0) { |
146 |
//logger.error("Unable to reproject envelope with transf: " + trans.toString());
|
147 |
return null; |
148 |
} |
149 |
|
150 |
return new Envelope3D( |
151 |
new Point2DZ(res_min.getX(), res_min.getX(), this.min.getCoordinateAt(2)), |
152 |
new Point2DZ(res_max.getX(), res_max.getX(), this.max.getCoordinateAt(2)) |
153 |
); |
154 |
|
155 |
} |
156 |
|
157 |
public static void registerPersistent() { |
158 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
159 |
if (manager.getDefinition(PERSISTENCE_DEFINITION_NAME) == null) { |
160 |
DynStruct definition = manager.addDefinition( |
161 |
Envelope3D.class, |
162 |
PERSISTENCE_DEFINITION_NAME, |
163 |
"Envelope3D persistence definition",
|
164 |
null,
|
165 |
null
|
166 |
); |
167 |
|
168 |
definition.extend(manager.getDefinition(DefaultEnvelope.PERSISTENCE_DEFINITION_NAME)); |
169 |
} |
170 |
} |
171 |
|
172 |
public Object clone() throws CloneNotSupportedException { |
173 |
return super.clone(); |
174 |
} |
175 |
|
176 |
private void createPoints() { |
177 |
this.min = new Point2DZ(0, 0, 0); |
178 |
this.max = new Point2DZ(0, 0, 0); |
179 |
} |
180 |
|
181 |
public void add(Envelope envelope) { |
182 |
int i;
|
183 |
|
184 |
if (envelope == null || envelope.isEmpty()) { |
185 |
return;
|
186 |
} |
187 |
|
188 |
int maxDimension = DIMENSION;
|
189 |
|
190 |
if (envelope.getDimension() == 2) { |
191 |
maxDimension = 2;
|
192 |
} |
193 |
|
194 |
if (this.isZInitilized) { |
195 |
for (i = 0; i < maxDimension; i++) { |
196 |
this.min.setCoordinateAt(i,
|
197 |
Math.min(this.min.getCoordinateAt(i), envelope.getMinimum(i))); |
198 |
this.max.setCoordinateAt(i,
|
199 |
Math.max(this.max.getCoordinateAt(i), envelope.getMaximum(i))); |
200 |
} |
201 |
return;
|
202 |
} |
203 |
|
204 |
if (isEmpty) {
|
205 |
createPoints(); |
206 |
if (maxDimension == 3) { |
207 |
this.isZInitilized = true; |
208 |
} |
209 |
for (i = 0; i < maxDimension; i++) { |
210 |
this.min.setCoordinateAt(i, envelope.getMinimum(i));
|
211 |
this.max.setCoordinateAt(i, envelope.getMaximum(i));
|
212 |
} |
213 |
isEmpty = false;
|
214 |
} else {
|
215 |
if (maxDimension == DIMENSION) {
|
216 |
this.min.setCoordinateAt(2, envelope.getMinimum(2)); |
217 |
this.max.setCoordinateAt(2, envelope.getMaximum(2)); |
218 |
this.isZInitilized = true; |
219 |
} |
220 |
for (i = 0; i < maxDimension; i++) { |
221 |
this.min.setCoordinateAt(i,
|
222 |
Math.min(this.min.getCoordinateAt(i), envelope.getMinimum(i))); |
223 |
this.max.setCoordinateAt(i,
|
224 |
Math.max(this.max.getCoordinateAt(i), envelope.getMaximum(i))); |
225 |
} |
226 |
} |
227 |
} |
228 |
} |