Statistics
| Revision:

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

History | View | Annotate | Download (8.29 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 org.gvsig.fmap.geom.Geometry;
82
import org.gvsig.fmap.geom.GeometryLocator;
83
import org.gvsig.fmap.geom.GeometryManager;
84
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
85
import org.gvsig.fmap.geom.Geometry.TYPES;
86
import org.gvsig.fmap.geom.exception.CreateGeometryException;
87
import org.gvsig.fmap.geom.primitive.Envelope;
88
import org.gvsig.fmap.geom.primitive.GeneralPathX;
89
import org.gvsig.fmap.geom.primitive.Point;
90
import org.gvsig.fmap.geom.primitive.Surface;
91

    
92

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

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

    
107
    protected static GeometryManager manager = GeometryLocator.getGeometryManager();
108

    
109
    public DefaultEnvelope(){
110
            super();
111
    }
112

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

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

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

    
142
        return min.getCoordinateAt(dimension) - max.getCoordinateAt(dimension);
143
    }
144

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

    
155
    /**
156
     * Returns the maximal ordinate along the specified dimension.
157
     *
158
     * @param dimension
159
     *
160
     * @return
161
     */
162
    public double getMaximum(int dimension) {
163
        return max.getCoordinateAt(dimension);
164
    }
165

    
166
    /**
167
     * Returns the minimal ordinate along the specified dimension.
168
     *
169
     * @param dimension
170
     *
171
     * @return
172
     */
173
    public double getMinimum(int dimension) {
174
        return min.getCoordinateAt(dimension);
175
    }
176

    
177
    /**
178
     * A coordinate position consisting of all the maximal ordinates for each
179
     * dimension for all points within the Envelope.
180
     *
181
     * @return
182
     */
183
    public Point getUpperCorner() {
184
        return max;
185
    }
186

    
187
        public void add(Envelope envelope) {
188
                int maxDimension = Math.min(getDimension(), envelope.getDimension());
189
                int i;
190
                for (i=0;i<maxDimension;i++){
191
                        this.min.setCoordinateAt(i,
192
                                        Math.min(this.min.getCoordinateAt(i), envelope.getMinimum(i)));
193
                        this.max.setCoordinateAt(i,
194
                                        Math.max(this.max.getCoordinateAt(i), envelope.getMaximum(i)));
195
                }
196
        }
197

    
198
        public Geometry getGeometry() {
199
                GeneralPathX gpx=new GeneralPathX();
200
                gpx.moveTo(getMinimum(0),getMinimum(1));
201
                gpx.lineTo(getMaximum(0),getMinimum(1));
202
                gpx.lineTo(getMaximum(0),getMaximum(1));
203
                gpx.lineTo(getMinimum(0),getMaximum(1));
204
                gpx.closePath();
205
                Surface surface;
206
                try {
207
                        surface = (Surface)manager.create(TYPES.SURFACE, SUBTYPES.GEOM2D);
208
                        surface.setGeneralPath(gpx);
209
                        return surface;
210
                } catch (CreateGeometryException e) {
211
                        // TODO Auto-generated catch block
212
                        e.printStackTrace();
213
                }
214
                return null;
215
        }
216

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

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

    
244

    
245

    
246
        public boolean equals(Object other) {
247
                if (!(other instanceof Envelope) || other == null) {
248
                        return false;
249
                }
250
                Envelope otherEnv = (Envelope) other;
251
                if (otherEnv.getDimension() != this.getDimension()) {
252
                        return false;
253
                }
254
                for (int i = 0; i < this.getDimension(); i++) {
255
                        if (otherEnv.getMinimum(i) != this.getMinimum(i)) {
256
                                return false;
257
                        }
258
                        if (otherEnv.getMaximum(i) != this.getMaximum(i)) {
259
                                return false;
260
                        }
261
                }
262
                return true;
263
        }
264

    
265
        /* (non-Javadoc)
266
         * @see org.gvsig.fmap.geom.primitive.Envelope#setLowerCorner(org.gvsig.fmap.geom.primitive.Point)
267
         */
268
        public void setLowerCorner(Point lowerCorner) {
269
                this.min = lowerCorner;
270
        }
271

    
272
        /* (non-Javadoc)
273
         * @see org.gvsig.fmap.geom.primitive.Envelope#setUpperCorner(org.gvsig.fmap.geom.primitive.Point)
274
         */
275
        public void setUpperCorner(Point upperCorner) {
276
                this.max = upperCorner;
277
        }
278
}