Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_geometries / src-test / org / gvsig / fmap / geom / operation / tojts / toJTSTest.java @ 34899

History | View | Annotate | Download (9.71 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
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 2
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
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2009 {Iver T.I.}   {Task}
26
 */
27

    
28
package org.gvsig.fmap.geom.operation.tojts;
29

    
30
import org.gvsig.fmap.geom.GeometryLocator;
31
import org.gvsig.fmap.geom.GeometryManager;
32
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
33
import org.gvsig.fmap.geom.Geometry.TYPES;
34
import org.gvsig.fmap.geom.aggregate.MultiCurve;
35
import org.gvsig.fmap.geom.aggregate.MultiPoint;
36
import org.gvsig.fmap.geom.aggregate.MultiSurface;
37
import org.gvsig.fmap.geom.exception.CreateGeometryException;
38
import org.gvsig.fmap.geom.impl.DefaultGeometryLibrary;
39
import org.gvsig.fmap.geom.operation.GeometryOperationException;
40
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
41
import org.gvsig.fmap.geom.operation.impl.DefaultGeometryOperationLibrary;
42
import org.gvsig.fmap.geom.operation.println.PrintMultiPoint;
43
import org.gvsig.fmap.geom.operation.println.PrintPoint;
44
import org.gvsig.fmap.geom.primitive.Curve;
45
import org.gvsig.fmap.geom.primitive.GeneralPathX;
46
import org.gvsig.fmap.geom.primitive.Point;
47
import org.gvsig.fmap.geom.primitive.Surface;
48
import org.slf4j.Logger;
49
import org.slf4j.LoggerFactory;
50

    
51
import junit.framework.TestCase;
52

    
53
/**
54
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
55
 */
56
public class toJTSTest extends TestCase {
57
        final static private Logger logger = LoggerFactory.getLogger("org.gvsig");
58
        private GeometryManager manager;
59

    
60
        protected void setUp() throws Exception {
61
                super.setUp();
62

    
63
                DefaultGeometryLibrary lib = new DefaultGeometryLibrary();                
64
                lib.initialize();
65
                lib.postInitialize();
66

    
67
                DefaultGeometryOperationLibrary opLib = new DefaultGeometryOperationLibrary();
68
                opLib.initialize();
69
                opLib.postInitialize();                        
70

    
71
                manager = GeometryLocator.getGeometryManager();
72
                manager.registerGeometryOperation(ToJTS.NAME, new Point2DToJTS(), TYPES.POINT, SUBTYPES.GEOM2D);
73
                manager.registerGeometryOperation(ToJTS.NAME, new Curve2DToJTS(), TYPES.CURVE, SUBTYPES.GEOM2D);
74
                manager.registerGeometryOperation(ToJTS.NAME, new Curve2DToJTS(), TYPES.ARC, SUBTYPES.GEOM2D);
75

    
76
                manager.registerGeometryOperation(ToJTS.NAME, new Surface2DToJTS(), TYPES.SURFACE, SUBTYPES.GEOM2D);
77
                manager.registerGeometryOperation(ToJTS.NAME, new Surface2DToJTS(), TYPES.CIRCLE, SUBTYPES.GEOM2D);
78
                manager.registerGeometryOperation(ToJTS.NAME, new Surface2DToJTS(), TYPES.ELLIPSE, SUBTYPES.GEOM2D);
79
                
80
                manager.registerGeometryOperation(ToJTS.NAME, new MultiPointToJTS(), TYPES.MULTIPOINT, SUBTYPES.GEOM2D);
81

    
82
                manager.registerGeometryOperation(ToJTS.NAME, new MultiCurveToJTS(), TYPES.MULTICURVE, SUBTYPES.GEOM2D);
83

    
84
                manager.registerGeometryOperation(ToJTS.NAME, new MultiSurfaceToJTS(), TYPES.MULTISURFACE, SUBTYPES.GEOM2D);
85
        }
86

    
87
        public void testPoint() throws CreateGeometryException, GeometryOperationNotSupportedException, GeometryOperationException{
88
                Point point = (Point)manager.create(TYPES.POINT, SUBTYPES.GEOM2D);
89
                point.setCoordinateAt(0, 1.0);
90
                point.setCoordinateAt(1, 2.0);
91

    
92
                JTSGeometryOperationContext ctx = new JTSGeometryOperationContext(-1);
93

    
94
                Object obj = point.invokeOperation(ToJTS.CODE, ctx);
95
                assertNotNull(obj);
96
                assertTrue(obj instanceof com.vividsolutions.jts.geom.Point);
97
                assertEquals(1.0, ((com.vividsolutions.jts.geom.Point)obj).getX(), 0.0);
98
                assertEquals(2.0, ((com.vividsolutions.jts.geom.Point)obj).getY(), 0.0);
99
        }
100
        
101
        public void testMultiPoint() throws GeometryOperationNotSupportedException, GeometryOperationException, CreateGeometryException{
102
                Point point1 = (Point)manager.create(TYPES.POINT, SUBTYPES.GEOM3D);
103
                point1.setCoordinateAt(0, 1.0);
104
                point1.setCoordinateAt(1, 2.0);
105
                point1.setCoordinateAt(2, 3.0);
106
                
107
                Point point2 = (Point)manager.create(TYPES.POINT, SUBTYPES.GEOM3D);
108
                point2.setCoordinateAt(0, 4.0);
109
                point2.setCoordinateAt(1, 5.0);
110
                point2.setCoordinateAt(2, 6.0);
111
                
112
                MultiPoint multiPoint = (MultiPoint)manager.create(TYPES.MULTIPOINT, SUBTYPES.GEOM2D);
113
                multiPoint.addPoint(point1);
114
                multiPoint.addPoint(point2);
115
                
116
                JTSGeometryOperationContext ctx = new JTSGeometryOperationContext(-1);
117

    
118
                Object obj = multiPoint.invokeOperation(ToJTS.CODE, ctx);
119
                assertNotNull(obj);
120
                assertTrue(obj instanceof com.vividsolutions.jts.geom.MultiPoint);
121
                com.vividsolutions.jts.geom.MultiPoint gtMultiPoint = (com.vividsolutions.jts.geom.MultiPoint)obj;
122
                assertEquals(2, (int)gtMultiPoint.getNumGeometries());
123
        }
124

    
125
        public void testLineString() throws GeometryOperationNotSupportedException, GeometryOperationException, CreateGeometryException{
126
                Curve lineString = (Curve)manager.create(TYPES.CURVE, SUBTYPES.GEOM2D);
127
                GeneralPathX generalPathX = new GeneralPathX();
128
                generalPathX.moveTo(0, 0);
129
                generalPathX.lineTo(1, 1);
130
                generalPathX.lineTo(2, 1);                
131
                lineString.setGeneralPath(generalPathX);        
132

    
133
                JTSGeometryOperationContext ctx = new JTSGeometryOperationContext(-1);
134

    
135
                Object obj = lineString.invokeOperation(ToJTS.CODE, ctx);
136
                assertNotNull(obj);
137
                assertTrue(obj instanceof com.vividsolutions.jts.geom.LineString);
138
                com.vividsolutions.jts.geom.LineString gtLineString = (com.vividsolutions.jts.geom.LineString)obj;
139
                assertEquals(0, gtLineString.getCoordinates()[0].x, 0.0);
140
                assertEquals(0, gtLineString.getCoordinates()[0].y, 0.0);
141
                assertEquals(0, gtLineString.getCoordinates()[1].x, 1.0);
142
                assertEquals(0, gtLineString.getCoordinates()[1].y, 1.0);
143
                assertEquals(0, gtLineString.getCoordinates()[2].x, 2.0);
144
                assertEquals(0, gtLineString.getCoordinates()[2].y, 2.0);
145
        }
146

    
147
        public void testPolygon() throws CreateGeometryException, GeometryOperationNotSupportedException, GeometryOperationException{
148
                Surface polygon = (Surface)manager.create(TYPES.SURFACE, SUBTYPES.GEOM2D);
149
                GeneralPathX generalPathX = new GeneralPathX();
150
                generalPathX.moveTo(0, 0);
151
                generalPathX.lineTo(1, 1);
152
                generalPathX.lineTo(2, 1);
153
                polygon.setGeneralPath(generalPathX);        
154

    
155
                JTSGeometryOperationContext ctx = new JTSGeometryOperationContext(-1);
156

    
157
                Object obj = polygon.invokeOperation(ToJTS.CODE, ctx);
158
                assertNotNull(obj);
159
                assertTrue(obj instanceof com.vividsolutions.jts.geom.Polygon);
160
                com.vividsolutions.jts.geom.Polygon gtPolygon = (com.vividsolutions.jts.geom.Polygon)obj;
161
                assertEquals(0, gtPolygon.getCoordinates()[0].x, 0.0);
162
                assertEquals(0, gtPolygon.getCoordinates()[0].y, 0.0);
163
                assertEquals(0, gtPolygon.getCoordinates()[1].x, 1.0);
164
                assertEquals(0, gtPolygon.getCoordinates()[1].y, 1.0);
165
                assertEquals(0, gtPolygon.getCoordinates()[2].x, 2.0);
166
                assertEquals(0, gtPolygon.getCoordinates()[2].y, 2.0);
167
        }
168

    
169
        public void testMultiLineString() throws CreateGeometryException, GeometryOperationNotSupportedException, GeometryOperationException{
170
                Curve curve1 = (Curve)manager.create(TYPES.CURVE, SUBTYPES.GEOM2D);
171
                GeneralPathX generalPathX1 = new GeneralPathX();
172
                generalPathX1.moveTo(0, 0);
173
                generalPathX1.lineTo(1, 1);
174
                generalPathX1.lineTo(2, 1);                
175
                curve1.setGeneralPath(generalPathX1);        
176

    
177
                Curve curve2 = (Curve)manager.create(TYPES.CURVE, SUBTYPES.GEOM2D);
178
                GeneralPathX generalPathX2 = new GeneralPathX();
179
                generalPathX2.moveTo(0, 0);
180
                generalPathX2.lineTo(1, 1);
181
                generalPathX2.lineTo(2, 1);                
182
                curve2.setGeneralPath(generalPathX2);        
183

    
184
                MultiCurve multiCurve = (MultiCurve)manager.create(TYPES.MULTICURVE, SUBTYPES.GEOM2D);
185
                multiCurve.addCurve(curve1);
186
                multiCurve.addCurve(curve2);
187

    
188
                JTSGeometryOperationContext ctx = new JTSGeometryOperationContext(-1);
189

    
190
                Object obj = multiCurve.invokeOperation(ToJTS.CODE, ctx);
191
                assertNotNull(obj);
192
                assertTrue(obj instanceof com.vividsolutions.jts.geom.MultiLineString);
193
                com.vividsolutions.jts.geom.MultiLineString gtMultiLineString = (com.vividsolutions.jts.geom.MultiLineString)obj;
194
                assertEquals(2, (int)gtMultiLineString.getNumGeometries());
195
        }
196
        
197
        public void testMultiPlygon() throws CreateGeometryException, GeometryOperationNotSupportedException, GeometryOperationException{
198
                Surface surface1 = (Surface)manager.create(TYPES.SURFACE, SUBTYPES.GEOM2D);
199
                GeneralPathX generalPathX1 = new GeneralPathX();
200
                generalPathX1.moveTo(0, 0);
201
                generalPathX1.lineTo(1, 1);
202
                generalPathX1.lineTo(2, 1);                
203
                surface1.setGeneralPath(generalPathX1);        
204
                
205
                Surface surface2 = (Surface)manager.create(TYPES.SURFACE, SUBTYPES.GEOM2D);                
206
                GeneralPathX generalPathX2 = new GeneralPathX();
207
                generalPathX2.moveTo(0, 0);
208
                generalPathX2.lineTo(1, 1);
209
                generalPathX2.lineTo(2, 1);                
210
                surface2.setGeneralPath(generalPathX2);        
211
                
212
                MultiSurface multiPolygon = (MultiSurface)manager.create(TYPES.MULTISURFACE, SUBTYPES.GEOM2D);
213
                multiPolygon.addSurface(surface1);
214
                multiPolygon.addSurface(surface2);
215

    
216
                JTSGeometryOperationContext ctx = new JTSGeometryOperationContext(-1);
217

    
218
                Object obj = multiPolygon.invokeOperation(ToJTS.CODE, ctx);
219
                assertNotNull(obj);
220
                assertTrue(obj instanceof com.vividsolutions.jts.geom.MultiPolygon);
221
                com.vividsolutions.jts.geom.MultiPolygon gtMultiPolygon = (com.vividsolutions.jts.geom.MultiPolygon)obj;
222
                assertEquals(2, (int)gtMultiPolygon.getNumGeometries());
223
                assertEquals(0, gtMultiPolygon.getCoordinates()[0].x, 0.0);
224
                assertEquals(0, gtMultiPolygon.getCoordinates()[0].x, 0.0);
225
        }
226

    
227
}
228