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 / DefaultEnvelope.java @ 41417
History | View | Annotate | Download (9.52 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
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 3
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
|
25 |
package org.gvsig.fmap.geom.primitive.impl; |
26 |
|
27 |
import java.text.MessageFormat; |
28 |
|
29 |
import org.slf4j.Logger; |
30 |
import org.slf4j.LoggerFactory; |
31 |
|
32 |
import org.gvsig.fmap.geom.Geometry; |
33 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
34 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
35 |
import org.gvsig.fmap.geom.GeometryLocator; |
36 |
import org.gvsig.fmap.geom.GeometryManager; |
37 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
38 |
import org.gvsig.fmap.geom.primitive.Envelope; |
39 |
import org.gvsig.fmap.geom.primitive.EnvelopeNotInitializedException; |
40 |
import org.gvsig.fmap.geom.primitive.Point; |
41 |
import org.gvsig.fmap.geom.primitive.Surface; |
42 |
import org.gvsig.tools.ToolsLocator; |
43 |
import org.gvsig.tools.dynobject.DynStruct; |
44 |
import org.gvsig.tools.persistence.PersistenceManager; |
45 |
import org.gvsig.tools.persistence.PersistentState; |
46 |
import org.gvsig.tools.persistence.exception.PersistenceException; |
47 |
|
48 |
|
49 |
/**
|
50 |
* A minimum bounding box or rectangle. Regardless of dimension, an Envelope
|
51 |
* can be represented without ambiguity as two direct positions (coordinate
|
52 |
* points). To encode an Envelope, it is sufficient to encode these two
|
53 |
* points. This is consistent with all of the data types in this
|
54 |
* specification, their state is represented by their publicly accessible
|
55 |
* attributes.
|
56 |
|
57 |
* @author Vicente Caballero Navarro
|
58 |
*/
|
59 |
public abstract class DefaultEnvelope implements Envelope, org.gvsig.tools.lang.Cloneable{ |
60 |
private static final Logger LOG = |
61 |
LoggerFactory.getLogger(DefaultEnvelope.class); |
62 |
|
63 |
public static final String PERSISTENCE_DEFINITION_NAME = "Envelope"; |
64 |
|
65 |
protected static final String LOWERCORNER_FIELD = "lowerCorner"; |
66 |
protected static final String UPPERCORNER_FIELD = "upperCorner"; |
67 |
|
68 |
protected Point min; |
69 |
protected Point max; |
70 |
|
71 |
protected boolean isEmpty; |
72 |
|
73 |
protected static GeometryManager manager = GeometryLocator.getGeometryManager(); |
74 |
|
75 |
public DefaultEnvelope(){
|
76 |
super();
|
77 |
isEmpty = true;
|
78 |
} |
79 |
|
80 |
public DefaultEnvelope(Point min, Point max){ |
81 |
super();
|
82 |
this.min = min;
|
83 |
this.max = max;
|
84 |
isEmpty = false;
|
85 |
} |
86 |
|
87 |
public String toString() { |
88 |
return MessageFormat.format( |
89 |
"Envelop(min={0},max={1})",
|
90 |
new Object[] { |
91 |
min.toString(), |
92 |
max.toString() |
93 |
} |
94 |
); |
95 |
} |
96 |
|
97 |
/**
|
98 |
* Returns the center ordinate along the specified dimension.
|
99 |
*
|
100 |
* @param dimension DOCUMENT ME!
|
101 |
*
|
102 |
* @return DOCUMENT ME!
|
103 |
*/
|
104 |
public double getCenter(int dimension) { |
105 |
if (isEmpty){
|
106 |
throw new EnvelopeNotInitializedException(); |
107 |
} |
108 |
return (min.getCoordinateAt(dimension) + max.getCoordinateAt(dimension)) * 0.5; |
109 |
} |
110 |
|
111 |
/**
|
112 |
* Returns the envelope length along the specified dimension.
|
113 |
*
|
114 |
* @param dimension
|
115 |
*
|
116 |
* @return
|
117 |
*/
|
118 |
public double getLength(int dimension) { |
119 |
if (isEmpty){
|
120 |
throw new EnvelopeNotInitializedException(); |
121 |
} |
122 |
if (max.getCoordinateAt(dimension) > min.getCoordinateAt(dimension)) {
|
123 |
return max.getCoordinateAt(dimension) - min.getCoordinateAt(dimension);
|
124 |
} |
125 |
|
126 |
return min.getCoordinateAt(dimension) - max.getCoordinateAt(dimension);
|
127 |
} |
128 |
|
129 |
/**
|
130 |
* A coordinate position consisting of all the minimal ordinates for each
|
131 |
* dimension for all points within the Envelope.
|
132 |
*
|
133 |
* @return
|
134 |
*/
|
135 |
public Point getLowerCorner() { |
136 |
return min;
|
137 |
} |
138 |
|
139 |
/**
|
140 |
* Returns the maximal ordinate along the specified dimension.
|
141 |
*
|
142 |
* @param dimension
|
143 |
*
|
144 |
* @return
|
145 |
*/
|
146 |
public double getMaximum(int dimension) { |
147 |
if (isEmpty){
|
148 |
throw new EnvelopeNotInitializedException(); |
149 |
} |
150 |
return max.getCoordinateAt(dimension);
|
151 |
} |
152 |
|
153 |
/**
|
154 |
* Returns the minimal ordinate along the specified dimension.
|
155 |
*
|
156 |
* @param dimension
|
157 |
*
|
158 |
* @return
|
159 |
*/
|
160 |
public double getMinimum(int dimension) { |
161 |
if (isEmpty){
|
162 |
throw new EnvelopeNotInitializedException(); |
163 |
} |
164 |
return min.getCoordinateAt(dimension);
|
165 |
} |
166 |
|
167 |
/**
|
168 |
* A coordinate position consisting of all the maximal ordinates for each
|
169 |
* dimension for all points within the Envelope.
|
170 |
*
|
171 |
* @return
|
172 |
*/
|
173 |
public Point getUpperCorner() { |
174 |
return max;
|
175 |
} |
176 |
|
177 |
|
178 |
|
179 |
public Geometry getGeometry() {
|
180 |
if (isEmpty){
|
181 |
throw new EnvelopeNotInitializedException(); |
182 |
} |
183 |
try {
|
184 |
Surface surface = (Surface)manager.create(TYPES.SURFACE, SUBTYPES.GEOM2D); |
185 |
surface.addMoveToVertex((Point)min.cloneGeometry());
|
186 |
surface.addVertex(manager.createPoint(getMaximum(0),getMinimum(1), SUBTYPES.GEOM2D)); |
187 |
surface.addVertex((Point)max.cloneGeometry());
|
188 |
surface.addVertex(manager.createPoint(getMinimum(0),getMaximum(1), SUBTYPES.GEOM2D)); |
189 |
surface.closePrimitive(); |
190 |
return surface;
|
191 |
} catch (CreateGeometryException e) {
|
192 |
LOG.error("Error creting the surface", e);
|
193 |
} |
194 |
return null; |
195 |
} |
196 |
|
197 |
public boolean contains(Envelope envelope) { |
198 |
if (isEmpty){
|
199 |
return false; |
200 |
} |
201 |
if((envelope == null) || (envelope.isEmpty())) { |
202 |
return false; |
203 |
} |
204 |
for (int i = 0; i < getDimension(); i++) { |
205 |
if (getMinimum(i) > envelope.getMinimum(i)
|
206 |
|| getMaximum(i) < envelope.getMaximum(i)) { |
207 |
return false; |
208 |
} |
209 |
} |
210 |
return true; |
211 |
} |
212 |
|
213 |
public boolean intersects(Envelope envelope) { |
214 |
if (isEmpty){
|
215 |
return false; |
216 |
} |
217 |
if((envelope == null) || (envelope.isEmpty())) { |
218 |
return false; |
219 |
} |
220 |
int dimension = getDimension();
|
221 |
for (int i = 0; i < dimension; i++) { |
222 |
if (getMinimum(i)>envelope.getMaximum(i)){
|
223 |
return false; |
224 |
} else if (getMaximum(i)<envelope.getMinimum(i)){ |
225 |
return false; |
226 |
} |
227 |
} |
228 |
return true; |
229 |
} |
230 |
|
231 |
public boolean equals(Object other) { |
232 |
if (!(other == null || other instanceof Envelope)) { |
233 |
return false; |
234 |
} |
235 |
Envelope otherEnv = (Envelope) other; |
236 |
if (isEmpty && otherEnv.isEmpty()){
|
237 |
return true; |
238 |
} |
239 |
if (otherEnv.getDimension() != this.getDimension()) { |
240 |
return false; |
241 |
} |
242 |
for (int i = 0; i < this.getDimension(); i++) { |
243 |
if (otherEnv.getMinimum(i) != this.getMinimum(i)) { |
244 |
return false; |
245 |
} |
246 |
if (otherEnv.getMaximum(i) != this.getMaximum(i)) { |
247 |
return false; |
248 |
} |
249 |
} |
250 |
return true; |
251 |
} |
252 |
|
253 |
/* (non-Javadoc)
|
254 |
* @see org.gvsig.fmap.geom.primitive.Envelope#setLowerCorner(org.gvsig.fmap.geom.primitive.Point)
|
255 |
*/
|
256 |
public void setLowerCorner(Point lowerCorner) { |
257 |
this.min = lowerCorner;
|
258 |
if (max != null){ |
259 |
isEmpty = false;
|
260 |
} |
261 |
} |
262 |
|
263 |
/* (non-Javadoc)
|
264 |
* @see org.gvsig.fmap.geom.primitive.Envelope#setUpperCorner(org.gvsig.fmap.geom.primitive.Point)
|
265 |
*/
|
266 |
public void setUpperCorner(Point upperCorner) { |
267 |
this.max = upperCorner;
|
268 |
if (min != null){ |
269 |
isEmpty = false;
|
270 |
} |
271 |
} |
272 |
|
273 |
public static void registerPersistent() { |
274 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
275 |
if( manager.getDefinition(PERSISTENCE_DEFINITION_NAME)==null ) { |
276 |
DynStruct definition = manager.addDefinition( |
277 |
DefaultEnvelope.class, |
278 |
PERSISTENCE_DEFINITION_NAME, |
279 |
"DefaultEnvelope persistence definition",
|
280 |
null,
|
281 |
null
|
282 |
); |
283 |
|
284 |
definition.addDynFieldObject(LOWERCORNER_FIELD).setClassOfValue(Point.class).setMandatory(true); |
285 |
definition.addDynFieldObject(UPPERCORNER_FIELD).setClassOfValue(Point.class).setMandatory(true); |
286 |
} |
287 |
} |
288 |
|
289 |
public void loadFromState(PersistentState state) |
290 |
throws PersistenceException {
|
291 |
setLowerCorner((Point)state.get(LOWERCORNER_FIELD));
|
292 |
setUpperCorner((Point)state.get(UPPERCORNER_FIELD));
|
293 |
} |
294 |
|
295 |
public void saveToState(PersistentState state) throws PersistenceException { |
296 |
state.set(LOWERCORNER_FIELD, min); |
297 |
state.set(UPPERCORNER_FIELD, max); |
298 |
} |
299 |
|
300 |
public Object clone() throws CloneNotSupportedException { |
301 |
DefaultEnvelope other = (DefaultEnvelope) super.clone();
|
302 |
if (!isEmpty){
|
303 |
other.max = (Point) max.cloneGeometry();
|
304 |
other.min = (Point) min.cloneGeometry();
|
305 |
} |
306 |
return other;
|
307 |
} |
308 |
|
309 |
public boolean isEmpty() { |
310 |
return isEmpty;
|
311 |
} |
312 |
} |