Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / GeometryFactory.java @ 20860

History | View | Annotate | Download (7.55 KB)

1
/* gvSIG. Sistema de Informaci�n Geogr�fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18
 *
19
 * For more information, contact:
20
 *
21
 *  Generalitat Valenciana
22
 *   Conselleria d'Infraestructures i Transport
23
 *   Av. Blasco Ib��ez, 50
24
 *   46010 VALENCIA
25
 *   SPAIN
26
 *
27
 *      +34 963862235
28
 *   gvsig@gva.es
29
 *      www.gvsig.gva.es
30
 *
31
 *    or
32
 *
33
 *   IVER T.I. S.A
34
 *   Salamanca 50
35
 *   46005 Valencia
36
 *   Spain
37
 *
38
 *   +34 963163400
39
 *   dac@iver.es
40
 */
41
package org.gvsig.fmap.geom;
42

    
43
import java.awt.geom.AffineTransform;
44
import java.awt.geom.Arc2D;
45
import java.awt.geom.Point2D;
46

    
47
import org.gvsig.fmap.geom.aggregate.MultiPoint2D;
48
import org.gvsig.fmap.geom.aggregate.MultiSurface2D;
49
import org.gvsig.fmap.geom.aggregate.MultiCurve2D;
50
import org.gvsig.fmap.geom.aggregate.BaseMultiPrimitive;
51
import org.gvsig.fmap.geom.aggregate.MultiPoint3D;
52
import org.gvsig.fmap.geom.primitive.Circle2D;
53
import org.gvsig.fmap.geom.primitive.Curve2D;
54
import org.gvsig.fmap.geom.primitive.Curve3D;
55
import org.gvsig.fmap.geom.primitive.Ellipse2D;
56
import org.gvsig.fmap.geom.primitive.FShape;
57
import org.gvsig.fmap.geom.primitive.GeneralPathX;
58
import org.gvsig.fmap.geom.primitive.Point3D;
59
import org.gvsig.fmap.geom.primitive.Spline2D;
60
import org.gvsig.fmap.geom.primitive.Surface2D;
61
import org.gvsig.fmap.geom.primitive.Surface3D;
62
import org.gvsig.fmap.geom.util.UtilFunctions;
63

    
64

    
65

    
66

    
67
/**
68
 * Clase que crea las geometr�as, contendra un m�todo create por cada tipo de
69
 * geometria que soporte gvSIG
70
 */
71
public class GeometryFactory {
72
        /**
73
         * Crea una geometr�a que contiene como shape un punto 2D.
74
         *
75
         * @param x Coordenada x.
76
         * @param y Coordenada y.
77
         *
78
         * @return Geometr�a.
79
         */
80
        public static Geometry createPoint2D(double x, double y) {
81
                return new org.gvsig.fmap.geom.primitive.Point2D(null, null, x, y);
82
        }
83
        
84
        public static Geometry createPoint2D(org.gvsig.fmap.geom.primitive.Point2D p) {
85
                return p;
86
        }
87

    
88

    
89
        /**
90
         * Crea una geometr�a que contiene como shape un Multipunto 2D.
91
         *
92
         * @param x Coordenada x.
93
         * @param y Coordenada y.
94
         *
95
         * @return Geometr�a.
96
         */
97
        public static Geometry createMultipoint2D(double[] x, double[] y) {
98
                return new MultiPoint2D(null, null, x, y);
99
        }
100

    
101
        /**
102
         * Crea una geometr�a que contiene como shape un punto 3D.
103
         *
104
         * @param x Coordenada x.
105
         * @param y Coordenada y.
106
         * @param z Coordenada z.
107
         *
108
         * @return Geometr�a.
109
         */
110
        public static Geometry createPoint3D(double x, double y, double z) {
111
                return new Point3D(null, null, x, y, z);
112
        }
113

    
114
        /**
115
         * Crea una geometr�a que contiene como shape un Multipunto 3D.
116
         *
117
         * @param x Coordenada x.
118
         * @param y Coordenada y.
119
         * @param z Coordenada z.
120
         *
121
         * @return Geometr�a.
122
         */
123
        public static Geometry createMultipoint3D(double[] x, double[] y,
124
                double[] z) {
125
                return new MultiPoint3D(null, null, x, y, z);
126
        }
127

    
128
        /**
129
         * Crea una geometr�a que contiene como shape un Polil�nea 2D.
130
         *
131
         * @param shape GeneralPathX.
132
         *
133
         * @return Geometr�a.
134
         */
135
        public static Geometry createPolyline2D(GeneralPathX shape) {
136
                return new Curve2D(null, null, shape);
137
        }
138

    
139
        /**
140
         * Crea una geometr�a que contiene como shape un Polil�nea 3D.
141
         *
142
         * @param shape GeneralPathX.
143
         * @param pZ Vector de Z.
144
         *
145
         * @return Geometr�a.
146
         */
147
        public static Geometry createPolyline3D(GeneralPathX shape, double[] pZ) {
148
                return new Curve3D(null, null, shape, pZ);
149
        }
150
        
151
        /**
152
         * Creates a multiline form a set of lines
153
         * @param polylines
154
         * The lines
155
         * @return
156
         * A multiline
157
         */
158
        public static Geometry createMultiPolyline(Curve2D[] polylines) {
159
                return new MultiCurve2D(null, null, polylines);
160
        }
161
        
162
        /**
163
         * Creates a multiline form a set of lines
164
         * @param polylines
165
         * The lines
166
         * @return
167
         * A multiline
168
         */
169
        public static Geometry createMultiPolygon(Surface2D[] polygons) {
170
                return new MultiSurface2D(null, null, polygons);
171
        }
172
        
173
        /**
174
         * Creates a multigeometry form a set of geometries
175
         * @param geometries
176
         * The geometries
177
         * @return
178
         * A multigeometry
179
         */
180
        public static Geometry createMultiGeometry(Geometry[] geometries) {
181
                return new BaseMultiPrimitive(null, null, geometries);
182
        }
183
        
184
        /**
185
         * Crea una geometr�a que contiene como shape un Pol�gono 3D.
186
         *
187
         * @param shape GeneralPathX.
188
         * @param pZ Vector de Z.
189
         *
190
         * @return Geometr�a.
191
         */
192
        public static Geometry createPolygon3D(GeneralPathX shape, double[] pZ) {
193
                return new Surface3D(null, null, shape, pZ);
194
        }
195

    
196
        /**
197
         * Crea una geometr�a que contiene como shape un Pol�gono 2D.
198
         *
199
         * @param shape GeneralPathX.
200
         *
201
         * @return Geometr�a.
202
         */
203
        public static Geometry createPolygon2D(GeneralPathX shape) {
204
                return new Surface2D(null, null, shape);
205
        }
206

    
207
        /**
208
         * Crea una geometr�a que contiene como shape un Pol�gono 2D.
209
         *
210
         * @param shape FPolyline2D closed (you must be sure it is really closed).
211
         *
212
         * @return Geometr�a.
213
         */
214
        public static Geometry createPolygon2D(Curve2D shape) {
215
                return new Surface2D(null, null, shape.getGeneralPathX());
216
        }
217

    
218
        /**
219
         * Crea a partir de un FShape una geometr�a.
220
         *
221
         * @param shp FShape.
222
         *
223
         * @return Geometr�a.
224
         */
225
        public static Geometry createGeometry(FShape shp) {
226
                return (Geometry) shp;
227
        }
228
   
229
    public static Geometry createCircle(java.awt.geom.Point2D center, java.awt.geom.Point2D r){
230
                double radio = center.distance(r);
231
                return createCircle(center, radio);
232
        }
233

    
234
        public static Geometry createCircle(Point2D center, double radio){
235
                Arc2D.Double arc = new Arc2D.Double(center.getX()-radio, center.getY() - radio,
236
                                2 * radio, 2 * radio, 0, 360, Arc2D.OPEN);
237

    
238
                return new Circle2D(null, null, new GeneralPathX(arc),center,radio);
239
        }
240
        public static Geometry createCircle(Point2D p1, Point2D p2, Point2D p3){
241
                Point2D center = UtilFunctions.getCenter(p1, p2, p3);
242
                if (center!=null)
243
                return createCircle(center,p1);
244
                return null;
245
        }
246
        
247
        public static Geometry createArc(Point2D p1, Point2D p2, Point2D p3){
248
                Arc2D arco = UtilFunctions.createArc(p1, p2, p3);
249
                if (arco == null) return null;
250
                return new org.gvsig.fmap.geom.primitive.Arc2D(null, null, new GeneralPathX(arco),p1,p2,p3);
251
        }
252
        
253
        public static Geometry createEllipse(Point2D axis1Start, Point2D axis1End, double axis2Length){
254
                double xAxis = axis1Start.distance(axis1End);
255
                Arc2D.Double arc = new Arc2D.Double(axis1Start.getX(),
256
                                axis1Start.getY() - axis2Length, xAxis, 2 * axis2Length, 0, 360, Arc2D.OPEN);
257
                // TODO Línea comentada para quitar warning: Point2D rotationPoint = new Point2D.Double(axis1Start.getX() + xAxis /2, axis1Start.getY());
258
                double angle = UtilFunctions.getAngle(axis1Start, axis1End);
259
                AffineTransform mT = AffineTransform.getRotateInstance(angle, axis1Start.getX(), axis1Start.getY());
260
                GeneralPathX gp = new GeneralPathX(arc);
261
                gp.transform(mT);
262

    
263
                return new org.gvsig.fmap.geom.primitive.Ellipse2D(null, null, new GeneralPathX(gp),axis1Start,axis1End,axis2Length);
264
        }
265
        public static Geometry createSpline2D(Point2D[] points) {
266
                return new Spline2D(null, null, points);
267
        }
268
}