Revision 35742 branches/v2_0_0_prep/libraries/libFMap_geometries/src/org/gvsig/fmap/geom/primitive/impl/DefaultEnvelope.java

View differences:

DefaultEnvelope.java
1 1

  
2 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
*/
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 23

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

  
29 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
*/
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 50
/*
51
* AUTHORS (In addition to CIT):
52
* ${year} IVER T.I. S.A.   {{Task}}
53
*/
51
 * AUTHORS (In addition to CIT):
52
 * ${year} IVER T.I. S.A.   {{Task}}
53
 */
54 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
*/
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 75
/*
76
* AUTHORS (In addition to CIT):
77
* ${year} IVER T.I. S.A.   {{Task}}
78
*/
76
 * AUTHORS (In addition to CIT):
77
 * ${year} IVER T.I. S.A.   {{Task}}
78
 */
79 79
package org.gvsig.fmap.geom.primitive.impl;
80 80

  
81 81
import java.text.MessageFormat;
82 82

  
83
import org.slf4j.Logger;
84
import org.slf4j.LoggerFactory;
85

  
83 86
import org.gvsig.fmap.geom.Geometry;
84 87
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
85 88
import org.gvsig.fmap.geom.Geometry.TYPES;
......
87 90
import org.gvsig.fmap.geom.GeometryManager;
88 91
import org.gvsig.fmap.geom.exception.CreateGeometryException;
89 92
import org.gvsig.fmap.geom.primitive.Envelope;
90
import org.gvsig.fmap.geom.primitive.GeneralPathX;
93
import org.gvsig.fmap.geom.primitive.EnvelopeNotInitializedException;
91 94
import org.gvsig.fmap.geom.primitive.Point;
92 95
import org.gvsig.fmap.geom.primitive.Surface;
93 96
import org.gvsig.tools.ToolsLocator;
......
108 111
 * @author Vicente Caballero Navarro
109 112
 */
110 113
public abstract class DefaultEnvelope implements Envelope, org.gvsig.tools.lang.Cloneable{
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;
114
    private static final Logger LOG =
115
        LoggerFactory.getLogger(DefaultEnvelope.class);
116

  
117
    public static final String PERSISTENCE_DEFINITION_NAME = "Envelope";
118

  
119
    protected static final String LOWERCORNER_FIELD = "lowerCorner";
120
    protected static final String UPPERCORNER_FIELD = "upperCorner";
121

  
122
    protected Point min;
117 123
    protected Point max;
118 124

  
125
    protected boolean isEmpty;
126

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

  
121 129
    public DefaultEnvelope(){
122
    	super();
130
        super();
131
        isEmpty = true;
123 132
    }
124 133

  
125 134
    public DefaultEnvelope(Point min, Point max){
126
    	super();
127
    	this.min = min;
128
    	this.max = max;
135
        super();
136
        this.min = min;
137
        this.max = max;
138
        isEmpty = false;
129 139
    }
130 140

  
131 141
    public String toString() {
132
    	return MessageFormat.format(
133
    			"Envelop(min={0},max={1})", 
134
    			new Object[] {
135
    					min.toString(),
136
    					max.toString()
137
    			}
138
    	);
142
        return MessageFormat.format(
143
            "Envelop(min={0},max={1})", 
144
            new Object[] {
145
                min.toString(),
146
                max.toString()
147
            }
148
        );
139 149
    }
140 150

  
141 151
    /**
......
146 156
     * @return DOCUMENT ME!
147 157
     */
148 158
    public double getCenter(int dimension) {
159
        if (isEmpty){
160
            throw new EnvelopeNotInitializedException();
161
        }
149 162
        return (min.getCoordinateAt(dimension) + max.getCoordinateAt(dimension)) * 0.5;
150 163
    }
151 164

  
......
157 170
     * @return
158 171
     */
159 172
    public double getLength(int dimension) {
173
        if (isEmpty){
174
            throw new EnvelopeNotInitializedException();
175
        }
160 176
        if (max.getCoordinateAt(dimension) > min.getCoordinateAt(dimension)) {
161 177
            return max.getCoordinateAt(dimension) - min.getCoordinateAt(dimension);
162 178
        }
......
182 198
     * @return
183 199
     */
184 200
    public double getMaximum(int dimension) {
201
        if (isEmpty){
202
            throw new EnvelopeNotInitializedException();
203
        }
185 204
        return max.getCoordinateAt(dimension);
186 205
    }
187 206

  
......
193 212
     * @return
194 213
     */
195 214
    public double getMinimum(int dimension) {
215
        if (isEmpty){
216
            throw new EnvelopeNotInitializedException();
217
        }
196 218
        return min.getCoordinateAt(dimension);
197 219
    }
198 220

  
......
206 228
        return max;
207 229
    }
208 230

  
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
	}
231
    public void add(Envelope envelope) {
232
        if (isEmpty){
233
            setLowerCorner((Point)envelope.getLowerCorner().cloneGeometry());
234
            setUpperCorner((Point)envelope.getUpperCorner().cloneGeometry());          
235
        }else{
236
            int maxDimension = Math.min(getDimension(), envelope.getDimension());
237
            int i;
238
            for (i=0;i<maxDimension;i++){
239
                this.min.setCoordinateAt(i,
240
                    Math.min(this.min.getCoordinateAt(i), envelope.getMinimum(i)));
241
                this.max.setCoordinateAt(i,
242
                    Math.max(this.max.getCoordinateAt(i), envelope.getMaximum(i)));
243
            }
244
        }
245
    }
219 246

  
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
	}
247
    public Geometry getGeometry() {
248
        if (isEmpty){
249
            throw new EnvelopeNotInitializedException();
250
        }
251
        try {
252
            Surface surface = (Surface)manager.create(TYPES.SURFACE, SUBTYPES.GEOM2D);
253
            surface.addMoveToVertex((Point)min.cloneGeometry());
254
            surface.addVertex(manager.createPoint(getMaximum(0),getMinimum(1), SUBTYPES.GEOM2D));
255
            surface.addVertex((Point)max.cloneGeometry());
256
            surface.addVertex(manager.createPoint(getMinimum(0),getMaximum(1), SUBTYPES.GEOM2D));
257
            surface.closePrimitive();
258
            return surface;
259
        } catch (CreateGeometryException e) {
260
            LOG.error("Error creting the surface", e);
261
        }	  
262
        return null;
263
    }
238 264

  
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
	}
265
    public boolean contains(Envelope envelope) {
266
        if (isEmpty){
267
            return false;
268
        }
269
        if((envelope == null) || (envelope.isEmpty())) {
270
            return false;
271
        }
272
        for (int i = 0; i < getDimension(); i++) {
273
            if (getMinimum(i) > envelope.getMinimum(i)
274
                || getMaximum(i) < envelope.getMaximum(i)) {
275
                return false;
276
            }
277
        }
278
        return true;
279
    }
251 280

  
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
	}
281
    public boolean intersects(Envelope envelope) {
282
        if (isEmpty){
283
            return false;
284
        }
285
        if((envelope == null) || (envelope.isEmpty())) {
286
            return false;
287
        }
288
        int dimension = getDimension();
289
        for (int i = 0; i < dimension; i++) {
290
            if (getMinimum(i)>envelope.getMaximum(i)){
291
                return false;
292
            } else if (getMaximum(i)<envelope.getMinimum(i)){
293
                return false;
294
            }
295
        }
296
        return true;
297
    }
266 298

  
299
    public boolean equals(Object other) {
300
        if (!(other == null || other instanceof Envelope)) {
301
            return false;
302
        }
303
        Envelope otherEnv = (Envelope) other;
304
        if (isEmpty && otherEnv.isEmpty()){
305
            return true;
306
        }
307
        if (otherEnv.getDimension() != this.getDimension()) {
308
            return false;
309
        }
310
        for (int i = 0; i < this.getDimension(); i++) {
311
            if (otherEnv.getMinimum(i) != this.getMinimum(i)) {
312
                return false;
313
            }
314
            if (otherEnv.getMaximum(i) != this.getMaximum(i)) {
315
                return false;
316
            }
317
        }
318
        return true;
319
    }
267 320

  
321
    /* (non-Javadoc)
322
     * @see org.gvsig.fmap.geom.primitive.Envelope#setLowerCorner(org.gvsig.fmap.geom.primitive.Point)
323
     */
324
    public void setLowerCorner(Point lowerCorner) {
325
        this.min = lowerCorner;
326
        if (max != null){
327
            isEmpty = false;
328
        }
329
    }
268 330

  
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
	}
331
    /* (non-Javadoc)
332
     * @see org.gvsig.fmap.geom.primitive.Envelope#setUpperCorner(org.gvsig.fmap.geom.primitive.Point)
333
     */
334
    public void setUpperCorner(Point upperCorner) {
335
        this.max = upperCorner;
336
        if (min != null){
337
            isEmpty = false;
338
        }
339
    }
287 340

  
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
	}
341
    public static void registerPersistent() {
342
        PersistenceManager manager = ToolsLocator.getPersistenceManager();
343
        if( manager.getDefinition(PERSISTENCE_DEFINITION_NAME)==null ) {
344
            DynStruct definition = manager.addDefinition(
345
                DefaultEnvelope.class,
346
                PERSISTENCE_DEFINITION_NAME,
347
                "DefaultEnvelope persistence definition",
348
                null, 
349
                null
350
            ); 
294 351

  
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
	}
352
            definition.addDynFieldObject(LOWERCORNER_FIELD).setClassOfValue(Point.class).setMandatory(true);
353
            definition.addDynFieldObject(UPPERCORNER_FIELD).setClassOfValue(Point.class).setMandatory(true);
354
        }
355
    }
323 356

  
324
	public void saveToState(PersistentState state) throws PersistenceException {
325
		state.set(LOWERCORNER_FIELD, min);	
326
		state.set(UPPERCORNER_FIELD, max);		
327
	}
328
	
329
	public Object clone() throws CloneNotSupportedException {
330
	    DefaultEnvelope other = (DefaultEnvelope) super.clone();
331
	    other.max = (Point) max.cloneGeometry();
332
	    other.min = (Point) min.cloneGeometry();
333
	    return other;
334
	}
357
    public void loadFromState(PersistentState state)
358
    throws PersistenceException {
359
        setLowerCorner((Point)state.get(LOWERCORNER_FIELD));		
360
        setUpperCorner((Point)state.get(UPPERCORNER_FIELD));		
361
    }
362

  
363
    public void saveToState(PersistentState state) throws PersistenceException {
364
        state.set(LOWERCORNER_FIELD, min);	
365
        state.set(UPPERCORNER_FIELD, max);		
366
    }
367

  
368
    public Object clone() throws CloneNotSupportedException {
369
        DefaultEnvelope other = (DefaultEnvelope) super.clone();
370
        if (!isEmpty){        
371
            other.max = (Point) max.cloneGeometry();
372
            other.min = (Point) min.cloneGeometry();
373
        }
374
        return other;
375
    }
376

  
377
    public boolean isEmpty() {       
378
        return isEmpty;
379
    }	
335 380
}

Also available in: Unified diff