Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libTopology / src-test / org / gvsig / topology / GeometrySnapperTest.java @ 16396

History | View | Annotate | Download (3.69 KB)

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

    
58
import java.util.ArrayList;
59

    
60
import org.gvsig.jts.GeometrySnapper;
61

    
62
import com.vividsolutions.jts.geom.Coordinate;
63
import com.vividsolutions.jts.geom.Geometry;
64
import com.vividsolutions.jts.geom.GeometryFactory;
65
import com.vividsolutions.jts.geom.LineString;
66
import com.vividsolutions.jts.io.ParseException;
67
import com.vividsolutions.jts.io.WKTReader;
68

    
69

    
70
import junit.framework.TestCase;
71

    
72
public class GeometrySnapperTest extends TestCase {
73

    
74
        GeometryFactory factory = new GeometryFactory();
75
        WKTReader wktReader = new WKTReader(factory);
76
        
77
        
78
        public void tearDown() throws Exception{
79
                super.tearDown();
80
                factory = null;
81
                wktReader = null;
82
                System.gc();
83
        }
84
        
85
        public void testAverageSnap1(){
86
                Coordinate coord1 = new Coordinate(0, 0);
87
                Coordinate coord2 = new Coordinate(100, 100);
88
                Coordinate coord3 = new Coordinate(200, 200);
89
                ArrayList coords = new ArrayList();
90
                coords.add(coord1);
91
                coords.add(coord2);
92
                coords.add(coord3);
93
                int[] weights = {1,1,1};//all coordinates with the same weight
94
                
95
                GeometrySnapper snapper = new GeometrySnapper(0.1d);
96
                Coordinate averageCoord = snapper.snapAverage(coords, weights);
97
                
98
                assertTrue(averageCoord.x == (0 + 100 + 200)/3);
99
                
100
                
101
                int[] weights2 = {1,2,2, 1};//first coordinate with double weight
102
                coords.add(new Coordinate(0.5, 1.27));
103
                averageCoord = snapper.snapAverage(coords, weights2);
104
                //snapped point is nearest to the coord with higher weight
105
                assertTrue(coord1.distance(averageCoord) < coord3.distance(averageCoord));
106
        }
107
        
108
        
109
        public void testSnapManyGeometries() throws ParseException{
110
                String linestring1 = "LINESTRING (280 80, 380 240, 620 360, 820 220, 600 140, 560 240, 420 180)";
111
                String linestring2 = "LINESTRING (280.001 79.999, 420.21 179.999)";
112
                
113
                LineString geo1 = (LineString) wktReader.read(linestring1);
114
                LineString geo2 = (LineString) wktReader.read(linestring2);
115
                
116
                GeometrySnapper snapper = new GeometrySnapper(0.6);
117
                LineString[] geometries = {geo1, geo2};
118
                int[] weights = {1,0};
119
                Geometry[] snappedGeometries = snapper.snap(geometries, weights);
120
                for(int i = 0; i < snappedGeometries.length; i++){
121
                        System.out.println(snappedGeometries[i].toText());
122
                }
123
                LineString geo2orig = (LineString) wktReader.read(linestring2);
124
                assertTrue(!geo2orig.equals(geo1));
125
        }
126
        
127
        
128
        
129
}
130