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 / coerce / CoerceToEnvelope.java @ 44432

History | View | Annotate | Download (2.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
package org.gvsig.fmap.geom.jts.coerce;
25

    
26
import org.gvsig.fmap.geom.Geometry;
27
import org.gvsig.fmap.geom.GeometryLocator;
28
import org.gvsig.fmap.geom.GeometryManager;
29
import org.gvsig.fmap.geom.primitive.Envelope;
30
import org.gvsig.tools.dataTypes.CoercionException;
31
import org.gvsig.tools.dataTypes.DataTypesManager.Coercion;
32

    
33
/**
34
 * Convert a object to Envelope.
35
 *
36
 * Support convert: - Envelope to Envelope (do nothing) - Geometry to Envelope -
37
 * WKB (byte[]) to Geometry - WKT (String/toString) to Geometry
38
 *
39
 *
40
 * @author gvSIG Team
41
 * @version $Id$
42
 *
43
 */
44
@SuppressWarnings("UseSpecificCatch")
45
public class CoerceToEnvelope implements Coercion {
46

    
47
    @Override
48
    public Object coerce(Object value) throws CoercionException {
49
        try {
50
            if (value == null || value instanceof Envelope) {
51
                return value;
52
            }
53
            if (value instanceof Geometry) {
54
                return ((Geometry) value).getEnvelope();
55
            }
56
            Geometry geom;
57
            GeometryManager manager = GeometryLocator.getGeometryManager();
58
            if (value instanceof byte[]) {
59
                geom = manager.createFrom((byte[]) value);
60
            } else {
61
                geom = manager.createFrom(value.toString());
62
            }
63
            if (geom == null) {
64
                throw new CoercionException();
65
            }
66
            return geom.getEnvelope();
67
        } catch (Exception e) {
68
            throw new CoercionException(e);
69
        }
70

    
71
    }
72

    
73
}