Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / primitive / impl / DefaultEnvelope.java @ 34162

History | View | Annotate | Download (9.96 KB)

1

    
2
/* gvSIG. Geographic Information System of the Valencian Government
3
*
4
* Copyright (C) 2007-2008 Infrastructures and Transports Department
5
* of the Valencian Government (CIT)
6
*
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU General Public License
9
* as published by the Free Software Foundation; either version 2
10
* of the License, or (at your option) any later version.
11
*
12
* This program is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
* GNU General Public License for more details.
16
*
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
20
* MA  02110-1301, USA.
21
*
22
*/
23

    
24
/*
25
* AUTHORS (In addition to CIT):
26
* ${year} IVER T.I. S.A.   {{Task}}
27
*/
28

    
29
/* gvSIG. Geographic Information System of the Valencian Government
30
*
31
* Copyright (C) 2007-2008 Infrastructures and Transports Department
32
* of the Valencian Government (CIT)
33
*
34
* This program is free software; you can redistribute it and/or
35
* modify it under the terms of the GNU General Public License
36
* as published by the Free Software Foundation; either version 2
37
* of the License, or (at your option) any later version.
38
*
39
* This program is distributed in the hope that it will be useful,
40
* but WITHOUT ANY WARRANTY; without even the implied warranty of
41
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42
* GNU General Public License for more details.
43
*
44
* You should have received a copy of the GNU General Public License
45
* along with this program; if not, write to the Free Software
46
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
47
* MA  02110-1301, USA.
48
*
49
*/
50
/*
51
* AUTHORS (In addition to CIT):
52
* ${year} IVER T.I. S.A.   {{Task}}
53
*/
54
/* gvSIG. Geographic Information System of the Valencian Government
55
*
56
* Copyright (C) 2007-2008 Infrastructures and Transports Department
57
* of the Valencian Government (CIT)
58
*
59
* This program is free software; you can redistribute it and/or
60
* modify it under the terms of the GNU General Public License
61
* as published by the Free Software Foundation; either version 2
62
* of the License, or (at your option) any later version.
63
*
64
* This program is distributed in the hope that it will be useful,
65
* but WITHOUT ANY WARRANTY; without even the implied warranty of
66
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
67
* GNU General Public License for more details.
68
*
69
* You should have received a copy of the GNU General Public License
70
* along with this program; if not, write to the Free Software
71
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
72
* MA  02110-1301, USA.
73
*
74
*/
75
/*
76
* AUTHORS (In addition to CIT):
77
* ${year} IVER T.I. S.A.   {{Task}}
78
*/
79
package org.gvsig.fmap.geom.primitive.impl;
80

    
81
import java.text.MessageFormat;
82

    
83
import org.gvsig.fmap.geom.Geometry;
84
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
85
import org.gvsig.fmap.geom.Geometry.TYPES;
86
import org.gvsig.fmap.geom.GeometryLocator;
87
import org.gvsig.fmap.geom.GeometryManager;
88
import org.gvsig.fmap.geom.exception.CreateGeometryException;
89
import org.gvsig.fmap.geom.primitive.Envelope;
90
import org.gvsig.fmap.geom.primitive.GeneralPathX;
91
import org.gvsig.fmap.geom.primitive.Point;
92
import org.gvsig.fmap.geom.primitive.Surface;
93
import org.gvsig.tools.ToolsLocator;
94
import org.gvsig.tools.dynobject.DynStruct;
95
import org.gvsig.tools.persistence.PersistenceManager;
96
import org.gvsig.tools.persistence.PersistentState;
97
import org.gvsig.tools.persistence.exception.PersistenceException;
98

    
99

    
100
/**
101
 * A minimum bounding box or rectangle. Regardless of dimension, an Envelope
102
 * can be represented without ambiguity as two direct positions (coordinate
103
 * points). To encode an Envelope, it is sufficient to encode these two
104
 * points. This is consistent with all of the data types in this
105
 * specification, their state is represented by their publicly accessible
106
 * attributes.
107

108
 * @author Vicente Caballero Navarro
109
 */
110
public abstract class DefaultEnvelope implements Envelope{
111
        public static final String PERSISTENCE_DEFINITION_NAME = "Envelope";
112
        
113
        protected static final String LOWERCORNER_FIELD = "lowerCorner";
114
        protected static final String UPPERCORNER_FIELD = "upperCorner";
115
        
116
        protected Point min;
117
    protected Point max;
118

    
119
    protected static GeometryManager manager = GeometryLocator.getGeometryManager();
120

    
121
    public DefaultEnvelope(){
122
            super();
123
    }
124

    
125
    public DefaultEnvelope(Point min, Point max){
126
            super();
127
            this.min = min;
128
            this.max = max;
129
    }
130

    
131
    public String toString() {
132
            return MessageFormat.format(
133
                            "Envelop(min={0},max={1})", 
134
                            new Object[] {
135
                                            min.toString(),
136
                                            max.toString()
137
                            }
138
            );
139
    }
140

    
141
    /**
142
     * Returns the center ordinate along the specified dimension.
143
     *
144
     * @param dimension DOCUMENT ME!
145
     *
146
     * @return DOCUMENT ME!
147
     */
148
    public double getCenter(int dimension) {
149
        return (min.getCoordinateAt(dimension) + max.getCoordinateAt(dimension)) * 0.5;
150
    }
151

    
152
    /**
153
     * Returns the envelope length along the specified dimension.
154
     *
155
     * @param dimension
156
     *
157
     * @return
158
     */
159
    public double getLength(int dimension) {
160
        if (max.getCoordinateAt(dimension) > min.getCoordinateAt(dimension)) {
161
            return max.getCoordinateAt(dimension) - min.getCoordinateAt(dimension);
162
        }
163

    
164
        return min.getCoordinateAt(dimension) - max.getCoordinateAt(dimension);
165
    }
166

    
167
    /**
168
     * A coordinate position consisting of all the minimal ordinates for each
169
     * dimension for all points within the Envelope.
170
     *
171
     * @return
172
     */
173
    public Point getLowerCorner() {
174
        return min;
175
    }
176

    
177
    /**
178
     * Returns the maximal ordinate along the specified dimension.
179
     *
180
     * @param dimension
181
     *
182
     * @return
183
     */
184
    public double getMaximum(int dimension) {
185
        return max.getCoordinateAt(dimension);
186
    }
187

    
188
    /**
189
     * Returns the minimal ordinate along the specified dimension.
190
     *
191
     * @param dimension
192
     *
193
     * @return
194
     */
195
    public double getMinimum(int dimension) {
196
        return min.getCoordinateAt(dimension);
197
    }
198

    
199
    /**
200
     * A coordinate position consisting of all the maximal ordinates for each
201
     * dimension for all points within the Envelope.
202
     *
203
     * @return
204
     */
205
    public Point getUpperCorner() {
206
        return max;
207
    }
208

    
209
        public void add(Envelope envelope) {
210
                int maxDimension = Math.min(getDimension(), envelope.getDimension());
211
                int i;
212
                for (i=0;i<maxDimension;i++){
213
                        this.min.setCoordinateAt(i,
214
                                        Math.min(this.min.getCoordinateAt(i), envelope.getMinimum(i)));
215
                        this.max.setCoordinateAt(i,
216
                                        Math.max(this.max.getCoordinateAt(i), envelope.getMaximum(i)));
217
                }
218
        }
219

    
220
        public Geometry getGeometry() {
221
                GeneralPathX gpx=new GeneralPathX();
222
                gpx.moveTo(getMinimum(0),getMinimum(1));
223
                gpx.lineTo(getMaximum(0),getMinimum(1));
224
                gpx.lineTo(getMaximum(0),getMaximum(1));
225
                gpx.lineTo(getMinimum(0),getMaximum(1));
226
                gpx.closePath();
227
                Surface surface;
228
                try {
229
                        surface = (Surface)manager.create(TYPES.SURFACE, SUBTYPES.GEOM2D);
230
                        surface.setGeneralPath(gpx);
231
                        return surface;
232
                } catch (CreateGeometryException e) {
233
                        // TODO Auto-generated catch block
234
                        e.printStackTrace();
235
                }
236
                return null;
237
        }
238

    
239
        public boolean contains(Envelope envelope) {
240
                if(envelope == null) {
241
                        return false;
242
                }
243
                for (int i = 0; i < getDimension(); i++) {
244
                        if (getMinimum(i) > envelope.getMinimum(i)
245
                                        || getMaximum(i) < envelope.getMaximum(i)) {
246
                                        return false;
247
                        }
248
                }
249
                return true;
250
        }
251

    
252
        public boolean intersects(Envelope envelope) {
253
                if(envelope == null) {
254
                        return false;
255
                }
256
                int dimension = getDimension();
257
                for (int i = 0; i < dimension; i++) {
258
                        if (getMinimum(i)>envelope.getMaximum(i)){
259
                                return false;
260
                        } else if (getMaximum(i)<envelope.getMinimum(i)){
261
                                return false;
262
                        }
263
                }
264
                return true;
265
        }
266

    
267

    
268

    
269
        public boolean equals(Object other) {
270
                if (!(other instanceof Envelope) || other == null) {
271
                        return false;
272
                }
273
                Envelope otherEnv = (Envelope) other;
274
                if (otherEnv.getDimension() != this.getDimension()) {
275
                        return false;
276
                }
277
                for (int i = 0; i < this.getDimension(); i++) {
278
                        if (otherEnv.getMinimum(i) != this.getMinimum(i)) {
279
                                return false;
280
                        }
281
                        if (otherEnv.getMaximum(i) != this.getMaximum(i)) {
282
                                return false;
283
                        }
284
                }
285
                return true;
286
        }
287

    
288
        /* (non-Javadoc)
289
         * @see org.gvsig.fmap.geom.primitive.Envelope#setLowerCorner(org.gvsig.fmap.geom.primitive.Point)
290
         */
291
        public void setLowerCorner(Point lowerCorner) {
292
                this.min = lowerCorner;
293
        }
294

    
295
        /* (non-Javadoc)
296
         * @see org.gvsig.fmap.geom.primitive.Envelope#setUpperCorner(org.gvsig.fmap.geom.primitive.Point)
297
         */
298
        public void setUpperCorner(Point upperCorner) {
299
                this.max = upperCorner;
300
        }
301
        
302
        public static void registerPersistent() {
303
                PersistenceManager manager = ToolsLocator.getPersistenceManager();
304
                if( manager.getDefinition(PERSISTENCE_DEFINITION_NAME)==null ) {
305
                        DynStruct definition = manager.addDefinition(
306
                                        DefaultEnvelope.class,
307
                                        PERSISTENCE_DEFINITION_NAME,
308
                                        "DefaultEnvelope persistence definition",
309
                                        null, 
310
                                        null
311
                        ); 
312
                        
313
                        definition.addDynFieldObject(LOWERCORNER_FIELD).setClassOfValue(Point.class).setMandatory(true);
314
                        definition.addDynFieldObject(UPPERCORNER_FIELD).setClassOfValue(Point.class).setMandatory(true);
315
                }
316
        }
317
        
318
        public void loadFromState(PersistentState state)
319
                        throws PersistenceException {
320
                setLowerCorner((Point)state.get(LOWERCORNER_FIELD));                
321
                setUpperCorner((Point)state.get(UPPERCORNER_FIELD));                
322
        }
323

    
324
        public void saveToState(PersistentState state) throws PersistenceException {
325
                state.set(LOWERCORNER_FIELD, min);        
326
                state.set(UPPERCORNER_FIELD, max);                
327
        }
328
}