Statistics
| Revision:

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

History | View | Annotate | Download (8.55 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.GeometryLocator;
85
import org.gvsig.fmap.geom.GeometryManager;
86
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
87
import org.gvsig.fmap.geom.Geometry.TYPES;
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

    
94

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

103
 * @author Vicente Caballero Navarro
104
 */
105
public abstract class DefaultEnvelope implements Envelope{
106
        protected Point min;
107
    protected Point max;
108

    
109
    protected static GeometryManager manager = GeometryLocator.getGeometryManager();
110

    
111
    public DefaultEnvelope(){
112
            super();
113
    }
114

    
115
    public DefaultEnvelope(Point min, Point max){
116
            super();
117
            this.min = min;
118
            this.max = max;
119
    }
120

    
121
    public String toString() {
122
            return MessageFormat.format(
123
                            "Envelop(min={0},max={1})", 
124
                            new Object[] {
125
                                            min.toString(),
126
                                            max.toString()
127
                            }
128
            );
129
    }
130

    
131
    /**
132
     * Returns the center ordinate along the specified dimension.
133
     *
134
     * @param dimension DOCUMENT ME!
135
     *
136
     * @return DOCUMENT ME!
137
     */
138
    public double getCenter(int dimension) {
139
        return (min.getCoordinateAt(dimension) + max.getCoordinateAt(dimension)) * 0.5;
140
    }
141

    
142
    /**
143
     * Returns the envelope length along the specified dimension.
144
     *
145
     * @param dimension
146
     *
147
     * @return
148
     */
149
    public double getLength(int dimension) {
150
        if (max.getCoordinateAt(dimension) > min.getCoordinateAt(dimension)) {
151
            return max.getCoordinateAt(dimension) - min.getCoordinateAt(dimension);
152
        }
153

    
154
        return min.getCoordinateAt(dimension) - max.getCoordinateAt(dimension);
155
    }
156

    
157
    /**
158
     * A coordinate position consisting of all the minimal ordinates for each
159
     * dimension for all points within the Envelope.
160
     *
161
     * @return
162
     */
163
    public Point getLowerCorner() {
164
        return min;
165
    }
166

    
167
    /**
168
     * Returns the maximal ordinate along the specified dimension.
169
     *
170
     * @param dimension
171
     *
172
     * @return
173
     */
174
    public double getMaximum(int dimension) {
175
        return max.getCoordinateAt(dimension);
176
    }
177

    
178
    /**
179
     * Returns the minimal ordinate along the specified dimension.
180
     *
181
     * @param dimension
182
     *
183
     * @return
184
     */
185
    public double getMinimum(int dimension) {
186
        return min.getCoordinateAt(dimension);
187
    }
188

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

    
199
        public void add(Envelope envelope) {
200
                int maxDimension = Math.min(getDimension(), envelope.getDimension());
201
                int i;
202
                for (i=0;i<maxDimension;i++){
203
                        this.min.setCoordinateAt(i,
204
                                        Math.min(this.min.getCoordinateAt(i), envelope.getMinimum(i)));
205
                        this.max.setCoordinateAt(i,
206
                                        Math.max(this.max.getCoordinateAt(i), envelope.getMaximum(i)));
207
                }
208
        }
209

    
210
        public Geometry getGeometry() {
211
                GeneralPathX gpx=new GeneralPathX();
212
                gpx.moveTo(getMinimum(0),getMinimum(1));
213
                gpx.lineTo(getMaximum(0),getMinimum(1));
214
                gpx.lineTo(getMaximum(0),getMaximum(1));
215
                gpx.lineTo(getMinimum(0),getMaximum(1));
216
                gpx.closePath();
217
                Surface surface;
218
                try {
219
                        surface = (Surface)manager.create(TYPES.SURFACE, SUBTYPES.GEOM2D);
220
                        surface.setGeneralPath(gpx);
221
                        return surface;
222
                } catch (CreateGeometryException e) {
223
                        // TODO Auto-generated catch block
224
                        e.printStackTrace();
225
                }
226
                return null;
227
        }
228

    
229
        public boolean contains(Envelope envelope) {
230
                if(envelope == null) {
231
                        return false;
232
                }
233
                for (int i = 0; i < getDimension(); i++) {
234
                        if (getMinimum(i) > envelope.getMinimum(i)
235
                                        || getMaximum(i) < envelope.getMaximum(i)) {
236
                                        return false;
237
                        }
238
                }
239
                return true;
240
        }
241

    
242
        public boolean intersects(Envelope envelope) {
243
                if(envelope == null) {
244
                        return false;
245
                }
246
                int dimension = getDimension();
247
                for (int i = 0; i < dimension; i++) {
248
                        if (getMinimum(i)>envelope.getMaximum(i)){
249
                                return false;
250
                        } else if (getMaximum(i)<envelope.getMinimum(i)){
251
                                return false;
252
                        }
253
                }
254
                return true;
255
        }
256

    
257

    
258

    
259
        public boolean equals(Object other) {
260
                if (!(other instanceof Envelope) || other == null) {
261
                        return false;
262
                }
263
                Envelope otherEnv = (Envelope) other;
264
                if (otherEnv.getDimension() != this.getDimension()) {
265
                        return false;
266
                }
267
                for (int i = 0; i < this.getDimension(); i++) {
268
                        if (otherEnv.getMinimum(i) != this.getMinimum(i)) {
269
                                return false;
270
                        }
271
                        if (otherEnv.getMaximum(i) != this.getMaximum(i)) {
272
                                return false;
273
                        }
274
                }
275
                return true;
276
        }
277

    
278
        /* (non-Javadoc)
279
         * @see org.gvsig.fmap.geom.primitive.Envelope#setLowerCorner(org.gvsig.fmap.geom.primitive.Point)
280
         */
281
        public void setLowerCorner(Point lowerCorner) {
282
                this.min = lowerCorner;
283
        }
284

    
285
        /* (non-Javadoc)
286
         * @see org.gvsig.fmap.geom.primitive.Envelope#setUpperCorner(org.gvsig.fmap.geom.primitive.Point)
287
         */
288
        public void setUpperCorner(Point upperCorner) {
289
                this.max = upperCorner;
290
        }
291
}