Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.jts / src / main / java / org / gvsig / fmap / geom / jts / primitive / DefaultEnvelope.java @ 42388

History | View | Annotate | Download (10.4 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.jts.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 "POLYGON()";
90
        }
91
        // Para el 3D estaria mal ya que probablemente habria que devolber un cubo.
92
        StringBuilder builder = new StringBuilder();
93
        builder.append("POLYGON ((");
94
        builder.append(this.min.getX());
95
        builder.append(" ");
96
        builder.append(this.min.getY());
97
        builder.append(", ");
98
        builder.append(this.min.getX());
99
        builder.append(" ");
100
        builder.append(this.max.getY());
101
        builder.append(", ");
102
        builder.append(this.max.getX());
103
        builder.append(" ");
104
        builder.append(this.max.getY());
105
        builder.append(", ");
106
        builder.append(this.max.getX());
107
        builder.append(" ");
108
        builder.append(this.min.getY());
109
        builder.append(", ");
110
        builder.append(this.min.getX());
111
        builder.append(" ");
112
        builder.append(this.min.getY());
113
        builder.append("))");
114
        return builder.toString();
115
    }
116

    
117
    /**
118
     * Returns the center ordinate along the specified dimension.
119
     *
120
     * @param dimension DOCUMENT ME!
121
     *
122
     * @return DOCUMENT ME!
123
     */
124
    public double getCenter(int dimension) {
125
        if (isEmpty){
126
            throw new EnvelopeNotInitializedException();
127
        }
128
        return (min.getCoordinateAt(dimension) + max.getCoordinateAt(dimension)) * 0.5;
129
    }
130

    
131
    /**
132
     * Returns the envelope length along the specified dimension.
133
     *
134
     * @param dimension
135
     *
136
     * @return
137
     */
138
    public double getLength(int dimension) {
139
        if (isEmpty){
140
            throw new EnvelopeNotInitializedException();
141
        }
142
        if (max.getCoordinateAt(dimension) > min.getCoordinateAt(dimension)) {
143
            return max.getCoordinateAt(dimension) - min.getCoordinateAt(dimension);
144
        }
145

    
146
        return min.getCoordinateAt(dimension) - max.getCoordinateAt(dimension);
147
    }
148

    
149
    /**
150
     * A coordinate position consisting of all the minimal ordinates for each
151
     * dimension for all points within the Envelope.
152
     *
153
     * @return
154
     */
155
    public Point getLowerCorner() {
156
        return min;
157
    }
158

    
159
    /**
160
     * Returns the maximal ordinate along the specified dimension.
161
     *
162
     * @param dimension
163
     *
164
     * @return
165
     */
166
    public double getMaximum(int dimension) {
167
        if (isEmpty){
168
            throw new EnvelopeNotInitializedException();
169
        }
170
        return max.getCoordinateAt(dimension);
171
    }
172

    
173
    /**
174
     * Returns the minimal ordinate along the specified dimension.
175
     *
176
     * @param dimension
177
     *
178
     * @return
179
     */
180
    public double getMinimum(int dimension) {
181
        if (isEmpty){
182
            throw new EnvelopeNotInitializedException();
183
        }
184
        return min.getCoordinateAt(dimension);
185
    }
186

    
187
    /**
188
     * A coordinate position consisting of all the maximal ordinates for each
189
     * dimension for all points within the Envelope.
190
     *
191
     * @return
192
     */
193
    public Point getUpperCorner() {
194
        return max;
195
    }
196

    
197

    
198

    
199
    public Geometry getGeometry() {
200
        if (isEmpty){
201
            throw new EnvelopeNotInitializedException();
202
        }
203
        try {
204
            Surface surface = (Surface)manager.create(TYPES.SURFACE, SUBTYPES.GEOM2D);
205
            surface.addVertex((Point)min.cloneGeometry());
206
            surface.addVertex(manager.createPoint(getMaximum(0),getMinimum(1), SUBTYPES.GEOM2D));
207
            surface.addVertex((Point)max.cloneGeometry());
208
            surface.addVertex(manager.createPoint(getMinimum(0),getMaximum(1), SUBTYPES.GEOM2D));
209
            surface.addVertex((Point)min.cloneGeometry());
210
            return surface;
211
        } catch (CreateGeometryException e) {
212
            LOG.error("Error creting the surface", e);
213
        }
214
        return null;
215
    }
216

    
217
    public boolean contains(Envelope envelope) {
218
        if (isEmpty){
219
            return false;
220
        }
221
        if((envelope == null) || (envelope.isEmpty())) {
222
            return false;
223
        }
224
        for (int i = 0; i < getDimension(); i++) {
225
            if (getMinimum(i) > envelope.getMinimum(i)
226
                || getMaximum(i) < envelope.getMaximum(i)) {
227
                return false;
228
            }
229
        }
230
        return true;
231
    }
232

    
233
    public boolean intersects(Envelope envelope) {
234
        if (isEmpty){
235
            return false;
236
        }
237
        if((envelope == null) || (envelope.isEmpty())) {
238
            return false;
239
        }
240
        int dimension = getDimension();
241
        for (int i = 0; i < dimension; i++) {
242
            if (getMinimum(i)>envelope.getMaximum(i)){
243
                return false;
244
            } else if (getMaximum(i)<envelope.getMinimum(i)){
245
                return false;
246
            }
247
        }
248
        return true;
249
    }
250

    
251
    public boolean equals(Object other) {
252
        if (!(other == null || other instanceof Envelope)) {
253
            return false;
254
        }
255
        Envelope otherEnv = (Envelope) other;
256
        if (isEmpty && otherEnv.isEmpty()){
257
            return true;
258
        }
259
        if (otherEnv.getDimension() != this.getDimension()) {
260
            return false;
261
        }
262
        for (int i = 0; i < this.getDimension(); i++) {
263
            if (otherEnv.getMinimum(i) != this.getMinimum(i)) {
264
                return false;
265
            }
266
            if (otherEnv.getMaximum(i) != this.getMaximum(i)) {
267
                return false;
268
            }
269
        }
270
        return true;
271
    }
272

    
273
    /* (non-Javadoc)
274
     * @see org.gvsig.fmap.geom.primitive.Envelope#setLowerCorner(org.gvsig.fmap.geom.primitive.Point)
275
     */
276
    public void setLowerCorner(Point lowerCorner) {
277
        this.min = lowerCorner;
278
        if (max != null){
279
            isEmpty = false;
280
        }
281
    }
282

    
283
    /* (non-Javadoc)
284
     * @see org.gvsig.fmap.geom.primitive.Envelope#setUpperCorner(org.gvsig.fmap.geom.primitive.Point)
285
     */
286
    public void setUpperCorner(Point upperCorner) {
287
        this.max = upperCorner;
288
        if (min != null){
289
            isEmpty = false;
290
        }
291
    }
292

    
293
    public static void registerPersistent() {
294
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
295
        if( manager.getDefinition(PERSISTENCE_DEFINITION_NAME)==null ) {
296
            DynStruct definition = manager.addDefinition(
297
                DefaultEnvelope.class,
298
                PERSISTENCE_DEFINITION_NAME,
299
                "DefaultEnvelope persistence definition",
300
                null,
301
                null
302
            );
303

    
304
            definition.addDynFieldObject(LOWERCORNER_FIELD).setClassOfValue(Point.class).setMandatory(true);
305
            definition.addDynFieldObject(UPPERCORNER_FIELD).setClassOfValue(Point.class).setMandatory(true);
306
        }
307
    }
308

    
309
    public void loadFromState(PersistentState state)
310
    throws PersistenceException {
311
        setLowerCorner((Point)state.get(LOWERCORNER_FIELD));
312
        setUpperCorner((Point)state.get(UPPERCORNER_FIELD));
313
    }
314

    
315
    public void saveToState(PersistentState state) throws PersistenceException {
316
        state.set(LOWERCORNER_FIELD, min);
317
        state.set(UPPERCORNER_FIELD, max);
318
    }
319

    
320
    public Object clone() throws CloneNotSupportedException {
321
        DefaultEnvelope other = (DefaultEnvelope) super.clone();
322
        if (!isEmpty){
323
            other.max = (Point) max.cloneGeometry();
324
            other.min = (Point) min.cloneGeometry();
325
        }
326
        return other;
327
    }
328

    
329
    public boolean isEmpty() {
330
        return isEmpty;
331
    }
332

    
333
    public void add(Geometry geometry) {
334
        this.add(geometry.getEnvelope());
335
    }
336

    
337
    public void clear() {
338
        isEmpty = true;
339
    }
340

    
341
}