Statistics
| Revision:

root / trunk / libraries / libTopology / src / org / gvsig / jts / voronoi / IncrementalTinVoronoiStrategy.java @ 21244

History | View | Annotate | Download (6.11 KB)

1
/*
2
 * Created on 10-abr-2006
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: 
47
 * $Log: 
48
 */
49
package org.gvsig.jts.voronoi;
50

    
51
import java.awt.geom.Point2D;
52
import java.rmi.server.UID;
53
import java.util.ArrayList;
54
import java.util.Iterator;
55
import java.util.LinkedList;
56
import java.util.List;
57

    
58
import org.gvsig.exceptions.BaseException;
59
import org.gvsig.fmap.core.NewFConverter;
60
import org.gvsig.jts.JtsUtil;
61
import org.gvsig.jts.voronoi.Voronoier.VoronoiStrategy;
62
import org.gvsig.jts.voronoi.dt.IncrementalDT;
63
import org.gvsig.jts.voronoi.dt.TriangleDT;
64

    
65
import com.hardcode.gdbms.engine.values.Value;
66
import com.hardcode.gdbms.engine.values.ValueFactory;
67
import com.iver.cit.gvsig.fmap.core.DefaultFeature;
68
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
69
import com.iver.cit.gvsig.fmap.core.IFeature;
70
import com.iver.cit.gvsig.fmap.core.IGeometry;
71
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
72
import com.iver.utiles.swing.threads.CancellableProgressTask;
73
import com.vividsolutions.jts.geom.Coordinate;
74
import com.vividsolutions.jts.geom.Geometry;
75
import com.vividsolutions.jts.geom.LineString;
76

    
77
public class IncrementalTinVoronoiStrategy implements VoronoiStrategy {
78

    
79
        public List<IFeature> createThiessenPolygons(
80
                        VoronoiAndTinInputLyr inputLyr,
81
                        boolean onlySelection,
82
                        CancellableProgressTask progressMonitor) throws BaseException {
83

    
84
                IncrementalDT triangulator = new IncrementalDT();
85
                int numberOfPoints = inputLyr.getSource().getShapeCount();
86
                for (int i = 0; i < numberOfPoints; i++) {
87
                        if(onlySelection){
88
                                if(! inputLyr.getRecordset().getSelection().get(i))
89
                                        continue;
90
                        }
91
                        Point2D point = inputLyr.getPoint(i);
92
                        Coordinate coord = new Coordinate(point.getX(), point.getY());
93
                        triangulator.insertPoint(coord);
94
                }
95
                
96
                List<Geometry> trianglesEdges = new ArrayList<Geometry>();
97
                
98
                LinkedList triangles = triangulator.triangles.getLinkedList();
99
                Iterator trianglesIterator = triangles.iterator();
100
                
101
                while (trianglesIterator.hasNext()) {
102
                        TriangleDT t = (TriangleDT) trianglesIterator.next();
103

    
104
                        Coordinate first = new Coordinate(t.A.x, t.A.y);
105
                        Coordinate second = new Coordinate(t.B.x, t.B.y);
106
                        Coordinate third = new Coordinate(t.C.x, t.C.y);
107
                        
108
                        Coordinate[] coords = new Coordinate[4];
109
                        coords[0] = first;
110
                        coords[1] = second;
111
                        coords[2] = third;
112
                        coords[3] = (Coordinate) first.clone();
113
                        
114
                        LineString line = JtsUtil.GEOMETRY_FACTORY.createLineString(coords);
115
                        
116
                        trianglesEdges.add(line);
117
                        
118
                }// while
119
                
120
                List<Geometry> thiessenPolygons = Voronoier.getThiessenPolygons(trianglesEdges, progressMonitor);
121
                
122
                List<IFeature> solution = new ArrayList<IFeature>();
123
                for(int i = 0; i < thiessenPolygons.size(); i++){
124
                        Geometry geometry = thiessenPolygons.get(i);
125
                        IGeometry polygon = NewFConverter.toFMap(geometry);
126
                        Value fid = ValueFactory.createValue(i);
127
                        Value associatedVertex = ValueFactory.createValue(i);
128
                        Value[] values = new Value[] { fid, associatedVertex };
129
                        DefaultFeature feature = new DefaultFeature(polygon, values,
130
                                        new UID().toString());
131
                        solution.add(feature);
132
                }
133
                return solution;
134
                
135
        }
136

    
137
        public List<IFeature> createThiessenPolygons(VoronoiAndTinInputLyr inputLyr, boolean onlySelection)
138
                        throws BaseException {
139
                return createThiessenPolygons(inputLyr, onlySelection, null);
140
        }
141

    
142
        public List<IFeature> createTin(VoronoiAndTinInputLyr inputLyr, boolean onlySelection)
143
                        throws BaseException {
144
                return createThiessenPolygons(inputLyr, onlySelection, null);
145
        }
146

    
147
        public List<IFeature> createTin(VoronoiAndTinInputLyr inputLyr, 
148
                        boolean onlySelection,
149
                        CancellableProgressTask progressMonitor) throws BaseException {
150

    
151
                List<IFeature> solution = new ArrayList<IFeature>();
152
                IncrementalDT triangulator = new IncrementalDT();
153
                int numberOfPoints = inputLyr.getSource().getShapeCount();
154
                for (int i = 0; i < numberOfPoints; i++) {
155
                        if(onlySelection){
156
                                if(! inputLyr.getRecordset().getSelection().get(i))
157
                                        continue;
158
                        }
159
                        Point2D point = inputLyr.getPoint(i);
160
                        Coordinate coord = new Coordinate(point.getX(), point.getY());
161
                        triangulator.insertPoint(coord);
162
                }
163
                LinkedList triangles = triangulator.triangles.getLinkedList();
164
                Iterator trianglesIterator = triangles.iterator();
165
                int idx = 0;
166
                while (trianglesIterator.hasNext()) {
167
                        TriangleDT t = (TriangleDT) trianglesIterator.next();
168

    
169
                        GeneralPathX gpx = new GeneralPathX();
170
                        gpx.moveTo(t.A.x, t.A.y);
171
                        gpx.lineTo(t.B.x, t.B.y);
172
                        gpx.lineTo(t.C.x, t.C.y);
173
                        gpx.closePath();
174

    
175
                        IGeometry polygon = ShapeFactory.createPolygon2D(gpx);
176
                        Value fid = ValueFactory.createValue(idx);
177
                        Value associatedVertex = ValueFactory.createValue(idx);
178
                        Value[] values = new Value[] { fid, associatedVertex };
179
                        DefaultFeature feature = new DefaultFeature(polygon, values,
180
                                        new UID().toString());
181
                        solution.add(feature);
182
                        idx++;
183
                }// while
184
                return solution;
185
        }
186
}