svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.generalpath / src / main / java / org / gvsig / fmap / geom / generalpath / primitive / DefaultEnvelope.java @ 41675
History | View | Annotate | Download (9.76 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.generalpath.primitive; |
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 |
if( this.isEmpty ) { |
89 |
return "Envelop(empty)"; |
90 |
} |
91 |
return MessageFormat.format( |
92 |
"Envelop(min={0},max={1})",
|
93 |
new Object[] { |
94 |
min.toString(), |
95 |
max.toString() |
96 |
} |
97 |
); |
98 |
} |
99 |
|
100 |
/**
|
101 |
* Returns the center ordinate along the specified dimension.
|
102 |
*
|
103 |
* @param dimension DOCUMENT ME!
|
104 |
*
|
105 |
* @return DOCUMENT ME!
|
106 |
*/
|
107 |
public double getCenter(int dimension) { |
108 |
if (isEmpty){
|
109 |
throw new EnvelopeNotInitializedException(); |
110 |
} |
111 |
return (min.getCoordinateAt(dimension) + max.getCoordinateAt(dimension)) * 0.5; |
112 |
} |
113 |
|
114 |
/**
|
115 |
* Returns the envelope length along the specified dimension.
|
116 |
*
|
117 |
* @param dimension
|
118 |
*
|
119 |
* @return
|
120 |
*/
|
121 |
public double getLength(int dimension) { |
122 |
if (isEmpty){
|
123 |
throw new EnvelopeNotInitializedException(); |
124 |
} |
125 |
if (max.getCoordinateAt(dimension) > min.getCoordinateAt(dimension)) {
|
126 |
return max.getCoordinateAt(dimension) - min.getCoordinateAt(dimension);
|
127 |
} |
128 |
|
129 |
return min.getCoordinateAt(dimension) - max.getCoordinateAt(dimension);
|
130 |
} |
131 |
|
132 |
/**
|
133 |
* A coordinate position consisting of all the minimal ordinates for each
|
134 |
* dimension for all points within the Envelope.
|
135 |
*
|
136 |
* @return
|
137 |
*/
|
138 |
public Point getLowerCorner() { |
139 |
return min;
|
140 |
} |
141 |
|
142 |
/**
|
143 |
* Returns the maximal ordinate along the specified dimension.
|
144 |
*
|
145 |
* @param dimension
|
146 |
*
|
147 |
* @return
|
148 |
*/
|
149 |
public double getMaximum(int dimension) { |
150 |
if (isEmpty){
|
151 |
throw new EnvelopeNotInitializedException(); |
152 |
} |
153 |
return max.getCoordinateAt(dimension);
|
154 |
} |
155 |
|
156 |
/**
|
157 |
* Returns the minimal ordinate along the specified dimension.
|
158 |
*
|
159 |
* @param dimension
|
160 |
*
|
161 |
* @return
|
162 |
*/
|
163 |
public double getMinimum(int dimension) { |
164 |
if (isEmpty){
|
165 |
throw new EnvelopeNotInitializedException(); |
166 |
} |
167 |
return min.getCoordinateAt(dimension);
|
168 |
} |
169 |
|
170 |
/**
|
171 |
* A coordinate position consisting of all the maximal ordinates for each
|
172 |
* dimension for all points within the Envelope.
|
173 |
*
|
174 |
* @return
|
175 |
*/
|
176 |
public Point getUpperCorner() { |
177 |
return max;
|
178 |
} |
179 |
|
180 |
|
181 |
|
182 |
public Geometry getGeometry() {
|
183 |
if (isEmpty){
|
184 |
throw new EnvelopeNotInitializedException(); |
185 |
} |
186 |
try {
|
187 |
Surface surface = (Surface)manager.create(TYPES.SURFACE, SUBTYPES.GEOM2D); |
188 |
surface.addMoveToVertex((Point)min.cloneGeometry());
|
189 |
surface.addVertex(manager.createPoint(getMaximum(0),getMinimum(1), SUBTYPES.GEOM2D)); |
190 |
surface.addVertex((Point)max.cloneGeometry());
|
191 |
surface.addVertex(manager.createPoint(getMinimum(0),getMaximum(1), SUBTYPES.GEOM2D)); |
192 |
surface.closePrimitive(); |
193 |
return surface;
|
194 |
} catch (CreateGeometryException e) {
|
195 |
LOG.error("Error creting the surface", e);
|
196 |
} |
197 |
return null; |
198 |
} |
199 |
|
200 |
public boolean contains(Envelope envelope) { |
201 |
if (isEmpty){
|
202 |
return false; |
203 |
} |
204 |
if((envelope == null) || (envelope.isEmpty())) { |
205 |
return false; |
206 |
} |
207 |
for (int i = 0; i < getDimension(); i++) { |
208 |
if (getMinimum(i) > envelope.getMinimum(i)
|
209 |
|| getMaximum(i) < envelope.getMaximum(i)) { |
210 |
return false; |
211 |
} |
212 |
} |
213 |
return true; |
214 |
} |
215 |
|
216 |
public boolean intersects(Envelope envelope) { |
217 |
if (isEmpty){
|
218 |
return false; |
219 |
} |
220 |
if((envelope == null) || (envelope.isEmpty())) { |
221 |
return false; |
222 |
} |
223 |
int dimension = getDimension();
|
224 |
for (int i = 0; i < dimension; i++) { |
225 |
if (getMinimum(i)>envelope.getMaximum(i)){
|
226 |
return false; |
227 |
} else if (getMaximum(i)<envelope.getMinimum(i)){ |
228 |
return false; |
229 |
} |
230 |
} |
231 |
return true; |
232 |
} |
233 |
|
234 |
public boolean equals(Object other) { |
235 |
if (!(other == null || other instanceof Envelope)) { |
236 |
return false; |
237 |
} |
238 |
Envelope otherEnv = (Envelope) other; |
239 |
if (isEmpty && otherEnv.isEmpty()){
|
240 |
return true; |
241 |
} |
242 |
if (otherEnv.getDimension() != this.getDimension()) { |
243 |
return false; |
244 |
} |
245 |
for (int i = 0; i < this.getDimension(); i++) { |
246 |
if (otherEnv.getMinimum(i) != this.getMinimum(i)) { |
247 |
return false; |
248 |
} |
249 |
if (otherEnv.getMaximum(i) != this.getMaximum(i)) { |
250 |
return false; |
251 |
} |
252 |
} |
253 |
return true; |
254 |
} |
255 |
|
256 |
/* (non-Javadoc)
|
257 |
* @see org.gvsig.fmap.geom.primitive.Envelope#setLowerCorner(org.gvsig.fmap.geom.primitive.Point)
|
258 |
*/
|
259 |
public void setLowerCorner(Point lowerCorner) { |
260 |
this.min = lowerCorner;
|
261 |
if (max != null){ |
262 |
isEmpty = false;
|
263 |
} |
264 |
} |
265 |
|
266 |
/* (non-Javadoc)
|
267 |
* @see org.gvsig.fmap.geom.primitive.Envelope#setUpperCorner(org.gvsig.fmap.geom.primitive.Point)
|
268 |
*/
|
269 |
public void setUpperCorner(Point upperCorner) { |
270 |
this.max = upperCorner;
|
271 |
if (min != null){ |
272 |
isEmpty = false;
|
273 |
} |
274 |
} |
275 |
|
276 |
public static void registerPersistent() { |
277 |
PersistenceManager manager = ToolsLocator.getPersistenceManager(); |
278 |
if( manager.getDefinition(PERSISTENCE_DEFINITION_NAME)==null ) { |
279 |
DynStruct definition = manager.addDefinition( |
280 |
DefaultEnvelope.class, |
281 |
PERSISTENCE_DEFINITION_NAME, |
282 |
"DefaultEnvelope persistence definition",
|
283 |
null,
|
284 |
null
|
285 |
); |
286 |
|
287 |
definition.addDynFieldObject(LOWERCORNER_FIELD).setClassOfValue(Point.class).setMandatory(true); |
288 |
definition.addDynFieldObject(UPPERCORNER_FIELD).setClassOfValue(Point.class).setMandatory(true); |
289 |
} |
290 |
} |
291 |
|
292 |
public void loadFromState(PersistentState state) |
293 |
throws PersistenceException {
|
294 |
setLowerCorner((Point)state.get(LOWERCORNER_FIELD));
|
295 |
setUpperCorner((Point)state.get(UPPERCORNER_FIELD));
|
296 |
} |
297 |
|
298 |
public void saveToState(PersistentState state) throws PersistenceException { |
299 |
state.set(LOWERCORNER_FIELD, min); |
300 |
state.set(UPPERCORNER_FIELD, max); |
301 |
} |
302 |
|
303 |
public Object clone() throws CloneNotSupportedException { |
304 |
DefaultEnvelope other = (DefaultEnvelope) super.clone();
|
305 |
if (!isEmpty){
|
306 |
other.max = (Point) max.cloneGeometry();
|
307 |
other.min = (Point) min.cloneGeometry();
|
308 |
} |
309 |
return other;
|
310 |
} |
311 |
|
312 |
public boolean isEmpty() { |
313 |
return isEmpty;
|
314 |
} |
315 |
|
316 |
public void add(Geometry geometry) { |
317 |
this.add(geometry.getEnvelope());
|
318 |
} |
319 |
|
320 |
public void clear() { |
321 |
isEmpty = true;
|
322 |
} |
323 |
|
324 |
} |