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 / DefaultGeometryType.java @ 42756

History | View | Annotate | Download (7.49 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

    
25
package org.gvsig.fmap.geom.jts;
26

    
27
import java.lang.reflect.Constructor;
28

    
29
import org.gvsig.fmap.geom.Geometry;
30
import org.gvsig.fmap.geom.GeometryManager;
31
import org.gvsig.fmap.geom.exception.CreateGeometryException;
32
import org.gvsig.fmap.geom.type.AbstractGeometryType;
33
import org.gvsig.fmap.geom.type.GeometryType;
34

    
35
/**
36
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
37
 */
38
public class DefaultGeometryType extends AbstractGeometryType {
39

    
40
        /**
41
         * Geometry type name
42
         */
43
        private String name;
44

    
45
        /** Class that implements this class type */
46
        private Class geometryClass;
47

    
48
        /**
49
         * The type of the geometry. The type is an abstract representation
50
         * of the object (Point, Curve...) but it is not a concrete
51
         * representation (Point2D, Point3D...). To do that comparation
52
         * the id field is used.
53
         */
54
        private int type;
55

    
56
        /**
57
         * The subtype of the geometry. The subtype represents a set of
58
         * geometries with a dimensional relationship (2D, 3D, 2DM...)
59
         */
60
        private int subType;
61

    
62
        /**
63
         * Super types of a geometry. e.g: the super type of an
64
         * arc is a curve, the super type of a circle is a surface...
65
         * The supertypes are defined in the ISO 19107
66
         *
67
         * @see <a href="http://www.iso.org/iso/iso_catalogue/catalogue_tc/catalogue_detail.htm?csnumber=26012"
68
         * >ISO 19107< /a>
69
         */
70
        private int[] superTypes = null;
71

    
72
        /**
73
     * Super SubTypes of a geometry. e.g: the super SubType of a
74
     * geometry 3D is a geometry 2D, because the 3D extends the
75
     * behavior of a geometry 2D
76
     */
77
        private int[] superSubTypes = null;
78

    
79
    private Constructor constructorWithGeometryType = null;
80
    private Constructor constructorWithoutParameters = null;
81
    private final Object[] parameters = { this };
82

    
83
        /**
84
     * This constructor is used by the {@link GeometryManager} when it
85
     * register a new GeometryType. It has not be used from other
86
     * parts.
87
     * @param geomClass
88
     * Geometry class (e.g: Point2D.class)
89
     * @param name
90
     * Symbolic Geometry name that is used to persist the geometry type. In some
91
     * cases, it is better to use this name because the id can change for different
92
     * application executions.
93
     * @param id
94
     * Geometry id
95
     * @param typeName
96
     * The geometry type name
97
     * @param type
98
     * The geometry abstract type
99
     * @param superTypes
100
     * The superTypes of the geometry type
101
     * @param superSubTypes
102
     * The superSubtypes of the geometry type
103
     */
104
    public DefaultGeometryType(Class geomClass, String name, int type, int subType,
105
        int[] superTypes, int[] superSubTypes) {
106
        this.geometryClass = geomClass;
107
        if (name == null) {
108
            this.name = geomClass.getName();
109
        } else {
110
            this.name = name;
111
        }
112
        this.type = type;
113
        this.subType = subType;
114
        this.superTypes = superTypes;
115
        this.superSubTypes = superSubTypes;
116

    
117
        Class[] parameterTypes = { GeometryType.class };
118
        try {
119
            constructorWithGeometryType = geometryClass.getConstructor(parameterTypes);
120
        } catch (NoSuchMethodException e) {
121
            try {
122
                constructorWithoutParameters = geometryClass.getConstructor();
123
            } catch (NoSuchMethodException e1) {
124
                throw new RuntimeException(
125
                    "Error constructor of the geometry class " + geomClass
126
                        + " with one parameter of type GeometryType not found", e);
127
            }
128
        }
129
    }
130

    
131
        /**
132
         * This constructor is used by the {@link GeometryManager} when it
133
         * register a new GeometryType. It has not be used from other
134
         * parts.
135
         * @param geomClass
136
         * Geometry class (e.g: Point2D.class)
137
         * @param name
138
         * Symbolic Geometry name that is used to persist the geometry type. In some
139
         * cases, it is better to use this name because the id can change for different
140
         * application executions.
141
         * @param id
142
         * Geometry id
143
         * @param typeName
144
         * The geometry type name
145
         * @param type
146
         * The geometry abstract type
147
         */
148
        public DefaultGeometryType(Class geomClass, String name, int type, int subType) {
149
                this(geomClass, name, type, subType, new int[0], new int[0]);
150
        }
151

    
152
        /**
153
         * This method creates a {@link Geometry} with the type specified
154
         * by this GeometryType. The geometry has to have a constructor
155
         * without arguments.
156
         *
157
         * @return A new geometry
158
         * @throws CreateGeometryException
159
         */
160
        public Geometry create() throws CreateGeometryException{
161
                try {
162
                    if(constructorWithGeometryType!=null){
163
                        return (Geometry) constructorWithGeometryType.newInstance(parameters);
164
                    } else {
165
                        return (Geometry) constructorWithoutParameters.newInstance();
166
                    }
167
                } catch (Exception e) {
168
                        throw new CreateGeometryException(type, subType, e);
169
                }
170
        }
171

    
172
        public Class getGeometryClass() {
173
                return geometryClass;
174
        }
175

    
176
        public String getName() {
177
                return name;
178
        }
179
        
180
        public String getFullName() {
181
            String subtypename;
182
            switch(this.subType) {
183
                case Geometry.SUBTYPES.GEOM2D:
184
                    subtypename="2D";
185
                    break;
186
                case Geometry.SUBTYPES.GEOM2DM:
187
                    subtypename="2DM";
188
                    break;
189
                case Geometry.SUBTYPES.GEOM3D:
190
                    subtypename="3D";
191
                    break;
192
                case Geometry.SUBTYPES.GEOM3DM:
193
                    subtypename="3DM";
194
                    break;
195
                default:
196
                    subtypename="subtype"+this.subType;
197
            }
198
            return name+":"+subtypename;
199
        }
200

    
201
        public int getType() {
202
                return type;
203
        }
204

    
205
        public int getSubType() {
206
                return subType;
207
        }
208

    
209
    public boolean isTypeOf(int geometryType) {
210
        if (type == geometryType){
211
            return true;
212
        }
213
        for (int i=0 ; i<superTypes.length ; i++){
214
            if(superTypes[i] == geometryType){
215
                return true;
216
            }
217
        }
218
        return Geometry.TYPES.GEOMETRY == geometryType;
219
    }
220

    
221
    public boolean isSubTypeOf(int geometrySubType) {
222
        if (subType == geometrySubType){
223
            return true;
224
        }
225
        for (int i=0 ; i<superSubTypes.length ; i++){
226
            if(superSubTypes[i] == geometrySubType){
227
                return true;
228
            }
229
        }
230
        return false;
231
    }
232

    
233
    public int getDimension() {
234
        switch(this.subType) {
235
            case Geometry.SUBTYPES.GEOM2D:
236
                return 2;
237
            case Geometry.SUBTYPES.GEOM3D:
238
            case Geometry.SUBTYPES.GEOM2DM:
239
                return 3;
240
            case Geometry.SUBTYPES.GEOM3DM:
241
                return 4;
242
            default:
243
                return -1;
244
        }
245
    }
246
}
247