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 @ 45673
History | View | Annotate | Download (8.8 KB)
1 | 42260 | fdiaz | /**
|
---|---|---|---|
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; |
||
25 | |||
26 | import java.lang.reflect.Constructor; |
||
27 | 45673 | jjdelcerro | import java.util.Objects; |
28 | 42260 | fdiaz | |
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 | 45673 | jjdelcerro | * @author gvSIG Team
|
37 | 42260 | fdiaz | */
|
38 | 45673 | jjdelcerro | @SuppressWarnings("UseSpecificCatch") |
39 | 42260 | fdiaz | public class DefaultGeometryType extends AbstractGeometryType { |
40 | 42283 | fdiaz | |
41 | 45673 | jjdelcerro | /**
|
42 | * Geometry type name
|
||
43 | */
|
||
44 | private String name; |
||
45 | 42283 | fdiaz | |
46 | 45673 | jjdelcerro | /**
|
47 | * Class that implements this class type
|
||
48 | */
|
||
49 | private Class geometryClass; |
||
50 | 42283 | fdiaz | |
51 | 45673 | jjdelcerro | /**
|
52 | * The type of the geometry. The type is an abstract representation of the
|
||
53 | * object (Point, Curve...) but it is not a concrete representation
|
||
54 | * (Point2D, Point3D...). To do that comparation the id field is used.
|
||
55 | */
|
||
56 | private int type; |
||
57 | 42283 | fdiaz | |
58 | 45673 | jjdelcerro | /**
|
59 | * The subtype of the geometry. The subtype represents a set of geometries
|
||
60 | * with a dimensional relationship (2D, 3D, 2DM...)
|
||
61 | */
|
||
62 | private int subType; |
||
63 | 42283 | fdiaz | |
64 | 45673 | jjdelcerro | /**
|
65 | * Super types of a geometry. e.g: the super type of an arc is a curve, the
|
||
66 | * super type of a circle is a surface... The supertypes are defined in the
|
||
67 | * ISO 19107
|
||
68 | *
|
||
69 | * @see <a href="http://www.iso.org/iso/iso_catalogue/catalogue_tc/catalogue_detail.htm?csnumber=26012"
|
||
70 | * >ISO 19107< /a>
|
||
71 | */
|
||
72 | private int[] superTypes = null; |
||
73 | 42283 | fdiaz | |
74 | 45673 | jjdelcerro | /**
|
75 | * Super SubTypes of a geometry. e.g: the super SubType of a geometry 3D is
|
||
76 | * a geometry 2D, because the 3D extends the behavior of a geometry 2D
|
||
77 | 42260 | fdiaz | */
|
78 | 45673 | jjdelcerro | private int[] superSubTypes = null; |
79 | 42283 | fdiaz | |
80 | private Constructor constructorWithGeometryType = null; |
||
81 | private Constructor constructorWithoutParameters = null; |
||
82 | 45673 | jjdelcerro | private final Object[] parameters = {this}; |
83 | 42260 | fdiaz | |
84 | 45673 | jjdelcerro | /**
|
85 | * This constructor is used by the {@link GeometryManager} when it register
|
||
86 | * a new GeometryType.It has not be used from other parts.
|
||
87 | *
|
||
88 | * @param geomClass Geometry class (e.g: Point2D.class)
|
||
89 | * @param name Symbolic Geometry name that is used to persist the geometry
|
||
90 | * type. In some cases, it is better to use this name because the id can
|
||
91 | * change for different application executions.
|
||
92 | * @param type The geometry abstract type
|
||
93 | * @param subType
|
||
94 | * @param superTypes The superTypes of the geometry type
|
||
95 | * @param superSubTypes The superSubtypes of the geometry type
|
||
96 | 42260 | fdiaz | */
|
97 | 42283 | fdiaz | public DefaultGeometryType(Class geomClass, String name, int type, int subType, |
98 | 45673 | jjdelcerro | int[] superTypes, int[] superSubTypes) { |
99 | 42260 | fdiaz | this.geometryClass = geomClass;
|
100 | if (name == null) { |
||
101 | this.name = geomClass.getName();
|
||
102 | } else {
|
||
103 | this.name = name;
|
||
104 | } |
||
105 | 42283 | fdiaz | this.type = type;
|
106 | 42260 | fdiaz | this.subType = subType;
|
107 | this.superTypes = superTypes;
|
||
108 | this.superSubTypes = superSubTypes;
|
||
109 | |||
110 | 45673 | jjdelcerro | Class[] parameterTypes = {GeometryType.class}; |
111 | 42260 | fdiaz | try {
|
112 | 42283 | fdiaz | constructorWithGeometryType = geometryClass.getConstructor(parameterTypes); |
113 | 42260 | fdiaz | } catch (NoSuchMethodException e) { |
114 | 42283 | fdiaz | try {
|
115 | constructorWithoutParameters = geometryClass.getConstructor(); |
||
116 | } catch (NoSuchMethodException e1) { |
||
117 | throw new RuntimeException( |
||
118 | 45673 | jjdelcerro | "Error constructor of the geometry class " + geomClass
|
119 | 42283 | fdiaz | + " with one parameter of type GeometryType not found", e);
|
120 | } |
||
121 | 42260 | fdiaz | } |
122 | 42283 | fdiaz | } |
123 | |||
124 | 45673 | jjdelcerro | /**
|
125 | * This constructor is used by the {@link GeometryManager} when it register
|
||
126 | * a new GeometryType.It has not be used from other parts.
|
||
127 | *
|
||
128 | * @param geomClass Geometry class (e.g: Point2D.class)
|
||
129 | * @param name Symbolic Geometry name that is used to persist the geometry
|
||
130 | * type. In some cases, it is better to use this name because the id can
|
||
131 | * change for different application executions.
|
||
132 | * @param type The geometry abstract type
|
||
133 | * @param subType
|
||
134 | */
|
||
135 | public DefaultGeometryType(Class geomClass, String name, int type, int subType) { |
||
136 | this(geomClass, name, type, subType, new int[0], new int[0]); |
||
137 | } |
||
138 | 42283 | fdiaz | |
139 | 45673 | jjdelcerro | /**
|
140 | * This method creates a {@link Geometry} with the type specified by this
|
||
141 | * GeometryType. The geometry has to have a constructor without arguments.
|
||
142 | *
|
||
143 | * @return A new geometry
|
||
144 | * @throws CreateGeometryException
|
||
145 | */
|
||
146 | @Override
|
||
147 | public Geometry create() throws CreateGeometryException { |
||
148 | try {
|
||
149 | if (constructorWithGeometryType != null) { |
||
150 | return (Geometry) constructorWithGeometryType.newInstance(parameters);
|
||
151 | } else {
|
||
152 | return (Geometry) constructorWithoutParameters.newInstance();
|
||
153 | } |
||
154 | } catch (Exception e) { |
||
155 | throw new CreateGeometryException(type, subType, e); |
||
156 | } |
||
157 | } |
||
158 | 42283 | fdiaz | |
159 | 45673 | jjdelcerro | @Override
|
160 | public Class getGeometryClass() { |
||
161 | return geometryClass;
|
||
162 | } |
||
163 | 42283 | fdiaz | |
164 | 45673 | jjdelcerro | @Override
|
165 | public String getName() { |
||
166 | return name;
|
||
167 | } |
||
168 | |||
169 | @Override
|
||
170 | public String getFullName() { |
||
171 | String subtypename;
|
||
172 | switch (this.subType) { |
||
173 | case Geometry.SUBTYPES.GEOM2D:
|
||
174 | subtypename = "2D";
|
||
175 | break;
|
||
176 | case Geometry.SUBTYPES.GEOM2DM:
|
||
177 | subtypename = "2DM";
|
||
178 | break;
|
||
179 | case Geometry.SUBTYPES.GEOM3D:
|
||
180 | subtypename = "3D";
|
||
181 | break;
|
||
182 | case Geometry.SUBTYPES.GEOM3DM:
|
||
183 | subtypename = "3DM";
|
||
184 | break;
|
||
185 | default:
|
||
186 | subtypename = "subtype" + this.subType; |
||
187 | } |
||
188 | if (name.endsWith(subtypename)) {
|
||
189 | try {
|
||
190 | return name.substring(0, name.length() - subtypename.length()) + ":" + subtypename; |
||
191 | } catch (Throwable th) { |
||
192 | //
|
||
193 | 42756 | jjdelcerro | } |
194 | 45673 | jjdelcerro | } |
195 | return name + ":" + subtypename; |
||
196 | } |
||
197 | 42260 | fdiaz | |
198 | 45673 | jjdelcerro | @Override
|
199 | public int getType() { |
||
200 | return type;
|
||
201 | } |
||
202 | 42260 | fdiaz | |
203 | 45673 | jjdelcerro | @Override
|
204 | public int getSubType() { |
||
205 | return subType;
|
||
206 | } |
||
207 | 42260 | fdiaz | |
208 | 45673 | jjdelcerro | @Override
|
209 | 42260 | fdiaz | public boolean isTypeOf(int geometryType) { |
210 | 45673 | jjdelcerro | if (type == geometryType) {
|
211 | 42260 | fdiaz | return true; |
212 | } |
||
213 | 45673 | jjdelcerro | for (int i = 0; i < superTypes.length; i++) { |
214 | if (superTypes[i] == geometryType) {
|
||
215 | 42260 | fdiaz | return true; |
216 | } |
||
217 | } |
||
218 | return Geometry.TYPES.GEOMETRY == geometryType;
|
||
219 | } |
||
220 | |||
221 | 45673 | jjdelcerro | @Override
|
222 | 42260 | fdiaz | public boolean isSubTypeOf(int geometrySubType) { |
223 | 45673 | jjdelcerro | if (subType == geometrySubType) {
|
224 | 42260 | fdiaz | return true; |
225 | } |
||
226 | 45673 | jjdelcerro | for (int i = 0; i < superSubTypes.length; i++) { |
227 | if (superSubTypes[i] == geometrySubType) {
|
||
228 | 42260 | fdiaz | return true; |
229 | } |
||
230 | } |
||
231 | return false; |
||
232 | } |
||
233 | |||
234 | 45673 | jjdelcerro | @Override
|
235 | 42392 | jjdelcerro | public int getDimension() { |
236 | 45673 | jjdelcerro | switch (this.subType) { |
237 | 42392 | jjdelcerro | case Geometry.SUBTYPES.GEOM2D:
|
238 | return 2; |
||
239 | case Geometry.SUBTYPES.GEOM3D:
|
||
240 | case Geometry.SUBTYPES.GEOM2DM:
|
||
241 | return 3; |
||
242 | case Geometry.SUBTYPES.GEOM3DM:
|
||
243 | return 4; |
||
244 | default:
|
||
245 | return -1; |
||
246 | } |
||
247 | } |
||
248 | 45673 | jjdelcerro | |
249 | @Override
|
||
250 | public boolean equals(Object obj) { |
||
251 | if (!(obj instanceof GeometryType)) { |
||
252 | return false; |
||
253 | } |
||
254 | GeometryType other = (GeometryType) obj; |
||
255 | if (!this.name.equals(other.getName())) { |
||
256 | return false; |
||
257 | } |
||
258 | if (!this.geometryClass.getName().equals(other.getGeometryClass().getName())) { |
||
259 | return false; |
||
260 | } |
||
261 | if (this.type != other.getType()) { |
||
262 | return false; |
||
263 | } |
||
264 | if (this.subType != other.getSubType()) { |
||
265 | return false; |
||
266 | } |
||
267 | return true; |
||
268 | } |
||
269 | |||
270 | @Override
|
||
271 | public int hashCode() { |
||
272 | int hash = 7; |
||
273 | hash = 79 * hash + Objects.hashCode(this.name); |
||
274 | hash = 79 * hash + Objects.hashCode(this.geometryClass.getName()); |
||
275 | hash = 79 * hash + this.type; |
||
276 | hash = 79 * hash + this.subType; |
||
277 | return hash;
|
||
278 | } |
||
279 | |||
280 | 42260 | fdiaz | } |