Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.impl / src / main / java / org / gvsig / fmap / geom / type / impl / DefaultGeometryType.java @ 40559

History | View | Annotate | Download (7.03 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
/* gvSIG. Geographic Information System of the Valencian Government
25
*
26
* Copyright (C) 2007-2008 Infrastructures and Transports Department
27
* of the Valencian Government (CIT)
28
* 
29
* This program is free software; you can redistribute it and/or
30
* modify it under the terms of the GNU General Public License
31
* as published by the Free Software Foundation; either version 2
32
* of the License, or (at your option) any later version.
33
* 
34
* This program is distributed in the hope that it will be useful,
35
* but WITHOUT ANY WARRANTY; without even the implied warranty of
36
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37
* GNU General Public License for more details.
38
* 
39
* You should have received a copy of the GNU General Public License
40
* along with this program; if not, write to the Free Software
41
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
42
* MA  02110-1301, USA.
43
* 
44
*/
45

    
46
/*
47
* AUTHORS (In addition to CIT):
48
* 2009 {Iver T.I.}   {Task}
49
*/
50
 
51
package org.gvsig.fmap.geom.type.impl;
52

    
53
import java.lang.reflect.Constructor;
54

    
55
import org.gvsig.fmap.geom.Geometry;
56
import org.gvsig.fmap.geom.GeometryManager;
57
import org.gvsig.fmap.geom.exception.CreateGeometryException;
58
import org.gvsig.fmap.geom.type.AbstractGeometryType;
59
import org.gvsig.fmap.geom.type.GeometryType;
60

    
61
/**
62
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
63
 */
64
public class DefaultGeometryType extends AbstractGeometryType {
65
        
66
        /** 
67
         * Geometry type name
68
         */
69
        private String name;
70
                
71
        /** Class that implements this class type */
72
        private Class geometryClass;
73
        
74
        /**
75
         * The type of the geometry. The type is an abstract representation
76
         * of the object (Point, Curve...) but it is not a concrete 
77
         * representation (Point2D, Point3D...). To do that comparation
78
         * the id field is used.
79
         */
80
        private int type;
81
        
82
        /**
83
         * The subtype of the geometry. The subtype represents a set of 
84
         * geometries with a dimensional relationship (2D, 3D, 2DM...)
85
         */
86
        private int subType;        
87
                
88
        /**
89
         * Super types of a geometry. e.g: the super type of an
90
         * arc is a curve, the super type of a circle is a surface...
91
         * The supertypes are defined in the ISO 19107 
92
         * 
93
         * @see <a href="http://www.iso.org/iso/iso_catalogue/catalogue_tc/catalogue_detail.htm?csnumber=26012"
94
         * >ISO 19107< /a>
95
         */
96
        private int[] superTypes = null;
97
        
98
        /**
99
     * Super SubTypes of a geometry. e.g: the super SubType of a
100
     * geometry 3D is a geometry 2D, because the 3D extends the
101
     * behavior of a geometry 2D
102
     */
103
        private int[] superSubTypes = null;
104
        
105
    private Constructor constructor;
106
    private final Object[] parameters = { this };
107

    
108
        /**
109
     * This constructor is used by the {@link GeometryManager} when it
110
     * register a new GeometryType. It has not be used from other
111
     * parts.
112
     * @param geomClass
113
     * Geometry class (e.g: Point2D.class)
114
     * @param name
115
     * Symbolic Geometry name that is used to persist the geometry type. In some
116
     * cases, it is better to use this name because the id can change for different
117
     * application executions.              
118
     * @param id
119
     * Geometry id  
120
     * @param typeName
121
     * The geometry type name
122
     * @param type
123
     * The geometry abstract type    
124
     * @param superTypes
125
     * The superTypes of the geometry type
126
     * @param superSubTypes
127
     * The superSubtypes of the geometry type      
128
     */
129
    public DefaultGeometryType(Class geomClass, String name, int type, int subType, 
130
        int[] superTypes, int[] superSubTypes) {
131
        this.geometryClass = geomClass;
132
        if (name == null) {
133
            this.name = geomClass.getName();
134
        } else {
135
            this.name = name;
136
        }
137
        this.type = type;   
138
        this.subType = subType;
139
        this.superTypes = superTypes;
140
        this.superSubTypes = superSubTypes;
141

    
142
        Class[] parameterTypes = { GeometryType.class };
143
        try {
144
            constructor = geometryClass.getConstructor(parameterTypes);
145
        } catch (NoSuchMethodException e) {
146
            throw new RuntimeException(
147
                "Error constructor of the geometry class " + geomClass
148
                    + " with one parameter of type GeometryType not found", e);
149
        }
150
    }        
151
        
152
        /**
153
         * This constructor is used by the {@link GeometryManager} when it
154
         * register a new GeometryType. It has not be used from other
155
         * parts.
156
         * @param geomClass
157
         * Geometry class (e.g: Point2D.class)
158
         * @param name
159
         * Symbolic Geometry name that is used to persist the geometry type. In some
160
         * cases, it is better to use this name because the id can change for different
161
         * application executions.                           
162
         * @param id
163
         * Geometry id        
164
         * @param typeName
165
         * The geometry type name
166
         * @param type
167
         * The geometry abstract type                        
168
         */
169
        public DefaultGeometryType(Class geomClass, String name, int type, int subType) {
170
                this(geomClass, name, type, subType, new int[0], new int[0]);
171
        }
172
        
173
        /**
174
         * This method creates a {@link Geometry} with the type specified 
175
         * by this GeometryType. The geometry has to have a constructor
176
         * without arguments.
177
         * 
178
         * @return A new geometry
179
         * @throws CreateGeometryException 
180
         */
181
        public Geometry create() throws CreateGeometryException{
182
                try {
183
            return (Geometry) constructor.newInstance(parameters);
184
                } catch (Exception e) {
185
                        throw new CreateGeometryException(type, subType, e);
186
                } 
187
        }
188
        
189
        public Class getGeometryClass() {
190
                return geometryClass;
191
        }
192
        
193
        public String getName() {
194
                return name;
195
        }
196

    
197
        public int getType() {
198
                return type;
199
        }
200

    
201
        public int getSubType() {
202
                return subType;
203
        }
204

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

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

    
229
}
230