Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.api / src / main / java / org / gvsig / fmap / geom / GeometryUtils.java @ 44241

History | View | Annotate | Download (9.92 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;
25

    
26
import java.util.logging.Level;
27
import java.util.logging.Logger;
28
import org.apache.commons.lang3.StringUtils;
29
import org.cresques.cts.IProjection;
30
import org.gvsig.fmap.geom.aggregate.MultiLine;
31
import org.gvsig.fmap.geom.aggregate.MultiPolygon;
32
import org.gvsig.fmap.geom.exception.CreateEnvelopeException;
33
import org.gvsig.fmap.geom.exception.CreateGeometryException;
34
import org.gvsig.fmap.geom.primitive.Envelope;
35
import org.gvsig.fmap.geom.primitive.Line;
36
import org.gvsig.fmap.geom.primitive.Point;
37
import org.gvsig.fmap.geom.primitive.Polygon;
38
import org.gvsig.fmap.geom.type.GeometryType;
39
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException;
40
import org.gvsig.fmap.geom.type.GeometryTypeNotValidException;
41

    
42
/**
43
 *
44
 * @author jjdelcerro
45
 */
46
@SuppressWarnings("UseSpecificCatch")
47
public class GeometryUtils {
48

    
49
    private GeometryUtils() {
50
        
51
    }
52
    
53
    public static GeometryType getGeometryType(int geometryType, int geometrySubType) {
54
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
55
        try {
56
            return geomManager.getGeometryType(geometryType, geometrySubType);
57
        } catch (Exception ex) {
58
            return null;
59
        }
60
    }
61
    
62
    public static boolean isSubtype(int geomTypeParent, int geomTypeChild) {
63
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
64
        return geomManager.isSubtype(geomTypeParent, geomTypeChild);
65
    }
66
    
67
    public static Envelope createEnvelope(int subType) {
68
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
69
        try {
70
            return geomManager.createEnvelope(subType);
71
        } catch (CreateEnvelopeException ex) {
72
            return null;
73
        }
74
    }
75
    
76
    public static Line createLine(int subType) {
77
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
78
        try {
79
            return geomManager.createLine(subType);
80
        } catch (CreateGeometryException ex) {
81
            return null;
82
        }
83
    }
84
    
85
    public static MultiLine createMultiLine(int subType) {
86
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
87
        try {
88
            return geomManager.createMultiLine(subType);
89
        } catch (CreateGeometryException ex) {
90
            return null;
91
        }
92
    }
93
    
94
    public static Polygon createPolygon(int subType) {
95
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
96
        try {
97
            return geomManager.createPolygon(subType);
98
        } catch (CreateGeometryException ex) {
99
            return null;
100
        }
101
    }
102
        
103
    public static MultiPolygon createMultiPolygon(int subType) {
104
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
105
        try {
106
            return geomManager.createMultiPolygon(subType);
107
        } catch (CreateGeometryException ex) {
108
            return null;
109
        }
110
    }
111
        
112
    public static Point createPoint(double x, double y) {
113
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
114
        try {
115
            return geomManager.createPoint(x, y, Geometry.SUBTYPES.GEOM2D);
116
        } catch (CreateGeometryException ex) {
117
            return null;
118
        }
119
    }
120
    
121
    public static Point createPoint(double x, double y, double z) {
122
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
123
        try {
124
            Point p = geomManager.createPoint(x, y, Geometry.SUBTYPES.GEOM3D);
125
            p.setCoordinateAt(Geometry.DIMENSIONS.Z, z);
126
            return p;
127
        } catch (CreateGeometryException ex) {
128
            return null;
129
        }
130
    }
131
    
132
    public static Point createPoint(double x, double y, double z, double m) {
133
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
134
        try {
135
            Point p = geomManager.createPoint(x, y, Geometry.SUBTYPES.GEOM3DM);
136
            p.setCoordinateAt(Geometry.DIMENSIONS.Z, z);
137
            p.setCoordinateAt(p.getDimension()-1, m);
138
            return p;
139
        } catch (CreateGeometryException ex) {
140
            return null;
141
        }
142
    }
143
    
144
    public static Geometry createFrom(Object data) {
145
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
146
        try {
147
            return geomManager.createFrom(data);
148
        } catch (GeometryException ex) {
149
            return null;
150
        }
151
    }
152
    
153
    public static Geometry createFrom(String wkt, String srs) {
154
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
155
        try {
156
            return geomManager.createFrom(wkt, srs);
157
        } catch (GeometryException ex) {
158
            return null;
159
        }
160
    }
161

    
162
    public static Geometry createFrom(String wkt, IProjection srs) {
163
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
164
        try {
165
            return geomManager.createFrom(wkt, srs);
166
        } catch (GeometryException ex) {
167
            return null;
168
        }
169
    }
170
    
171
    public static Geometry createFrom(String wkt) {
172
        GeometryManager geomManager = GeometryLocator.getGeometryManager();
173
        try {
174
            return geomManager.createFrom(wkt);
175
        } catch (GeometryException ex) {
176
            return null;
177
        }
178
    }
179
    
180
    public static Object convertTo(Geometry geom, String format) {
181
        try {
182
            return geom.convertTo(format);
183
        } catch (Exception ex) {
184
            return null;
185
        }
186
    }
187
        
188
    public static String toWKT(Geometry geom) {
189
        try {
190
            return geom.convertToWKT();
191
        } catch (Exception ex) {
192
            return null;
193
        }
194
    }
195

    
196
    public static byte[] toWKB(Geometry geom) {
197
        try {
198
            return geom.convertToWKB();
199
        } catch (Exception ex) {
200
            return null;
201
        }
202
    }
203
    
204
    public static byte[] toEWKB(Geometry geom) {
205
        try {
206
            return geom.convertToEWKB();
207
        } catch (Exception ex) {
208
            return null;
209
        }
210
    }
211
    
212
    public static boolean intersects(Geometry geom1, Geometry geom2) {
213
        try {
214
            return geom1.intersects(geom2);
215
        } catch (Exception ex) {
216
            return false;
217
        }
218
    }
219

    
220
    public static int getGeometryType(String typeName) {
221
        if( StringUtils.isBlank(typeName) ) {
222
            return Geometry.TYPES.UNKNOWN;
223
        }
224
        switch(typeName.toLowerCase()) {
225
            case "geometry":
226
                return Geometry.TYPES.GEOMETRY;
227
            case "point":
228
                    return Geometry.TYPES.POINT;
229
            case "curve":
230
                    return Geometry.TYPES.CURVE;
231
            case "surface":
232
                    return Geometry.TYPES.SURFACE;
233
            case "solid":
234
                    return Geometry.TYPES.SOLID;
235
            case "aggregate":
236
                    return Geometry.TYPES.AGGREGATE;
237
            case "multipoint":
238
                    return Geometry.TYPES.MULTIPOINT;
239
            case "multicurve":
240
                    return Geometry.TYPES.MULTICURVE;
241
            case "multisurface":
242
                    return Geometry.TYPES.MULTISURFACE;
243
            case "multisolid":
244
                    return Geometry.TYPES.MULTISOLID;
245
            case "circle":
246
                    return Geometry.TYPES.CIRCLE;
247
            case "arc":
248
                    return Geometry.TYPES.ARC;
249
            case "ellipse":
250
                    return Geometry.TYPES.ELLIPSE;
251
            case "spline":
252
                    return Geometry.TYPES.SPLINE;
253
            case "ellipticarc":
254
                    return Geometry.TYPES.ELLIPTICARC;
255
            case "complex":
256
                    return Geometry.TYPES.COMPLEX;
257
            case "line":
258
                    return Geometry.TYPES.LINE;
259
            case "polygon":
260
                    return Geometry.TYPES.POLYGON;
261
            case "ring":
262
                    return Geometry.TYPES.RING;
263
            case "multiline":
264
                    return Geometry.TYPES.MULTILINE;
265
            case "multipolygon":
266
                    return Geometry.TYPES.MULTIPOLYGON;
267
            case "circumference":
268
                    return Geometry.TYPES.CIRCUMFERENCE;
269
            case "periellipse":
270
                    return Geometry.TYPES.PERIELLIPSE;
271
            case "filledspline":
272
                    return Geometry.TYPES.FILLEDSPLINE;
273
            default:
274
                return Geometry.TYPES.UNKNOWN;
275
        }
276
    }
277

    
278
    public static int getGeometrySubtype(String subtype) {
279
        if( StringUtils.isBlank(subtype) ) {
280
            return Geometry.SUBTYPES.UNKNOWN;
281
        }
282
        switch(subtype.toUpperCase()) {
283
            case "GEOM2D":
284
            case "2D":
285
                    return Geometry.SUBTYPES.GEOM2D;
286
            case "GEOM3D":
287
            case "3D":
288
                    return Geometry.SUBTYPES.GEOM3D;
289
            case "GEOM2DM":
290
            case "2DM":
291
                    return Geometry.SUBTYPES.GEOM2DM;
292
            case "GEOM3DM":
293
            case "3DM":
294
                    return Geometry.SUBTYPES.GEOM3DM;
295
            default:
296
                return Geometry.SUBTYPES.UNKNOWN;
297
        }
298
    }    
299
}