root / trunk / libraries / libTopology / src / org / gvsig / jts / voronoi / ChenVoronoiStrategy.java @ 21244
History | View | Annotate | Download (7.85 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.Collection; |
55 |
import java.util.Enumeration; |
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.chen.Coord; |
63 |
import org.gvsig.jts.voronoi.chen.Vertex; |
64 |
import org.gvsig.jts.voronoi.chen.Voronoi; |
65 |
import org.gvsig.jts.voronoi.chen.VoronoiSegment; |
66 |
|
67 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
68 |
import com.hardcode.gdbms.engine.values.Value; |
69 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
70 |
import com.iver.cit.gvsig.fmap.core.DefaultFeature; |
71 |
import com.iver.cit.gvsig.fmap.core.IFeature; |
72 |
import com.iver.cit.gvsig.fmap.core.IGeometry; |
73 |
import com.iver.utiles.swing.threads.CancellableProgressTask; |
74 |
import com.vividsolutions.jts.geom.Coordinate; |
75 |
import com.vividsolutions.jts.geom.Geometry; |
76 |
import com.vividsolutions.jts.geom.GeometryCollection; |
77 |
import com.vividsolutions.jts.geom.LineString; |
78 |
import com.vividsolutions.jts.operation.polygonize.Polygonizer; |
79 |
|
80 |
/**
|
81 |
* It uses voronoi's classes from 1998 Ping-Che Chen's 1998 applet
|
82 |
*
|
83 |
* @author Alvaro Zabala
|
84 |
*
|
85 |
*/
|
86 |
public class ChenVoronoiStrategy implements VoronoiStrategy { |
87 |
/**
|
88 |
* Creates a voronoi diagram in form of a collection of features.
|
89 |
*
|
90 |
* Each feature has the next attributes: fid- voronoi feature idenfifier
|
91 |
* associatedVertex- fid of the vertex which generates the voronoi polygon
|
92 |
*
|
93 |
* @param inputLyr
|
94 |
* @return
|
95 |
* @throws ReadDriverException
|
96 |
*/
|
97 |
public List<IFeature> createTin(VoronoiAndTinInputLyr inputLyr, |
98 |
boolean onlySelection,
|
99 |
CancellableProgressTask progressTask) throws ReadDriverException {
|
100 |
|
101 |
List<IFeature> solution = new ArrayList<IFeature>(); |
102 |
int numPoints = inputLyr.getSource().getShapeCount();
|
103 |
|
104 |
|
105 |
List<Coord> coordsList = new ArrayList<Coord>(); |
106 |
for (int i = 0; i < numPoints; i++) { |
107 |
if(onlySelection){
|
108 |
if(! inputLyr.getRecordset().getSelection().get(i))
|
109 |
continue;
|
110 |
} |
111 |
Point2D point = inputLyr.getPoint(i);
|
112 |
Coord coord = new Coord();
|
113 |
coord.x = point.getX(); |
114 |
coord.y = point.getY(); |
115 |
coordsList.add(coord); |
116 |
} |
117 |
Coord[] coords = new Coord[coordsList.size()]; |
118 |
coordsList.toArray(coords); |
119 |
|
120 |
Vertex[] vertices = Voronoi.generate(coords);
|
121 |
for (int i = 0; i < vertices.length; i++) { |
122 |
Vertex vertex = vertices[i]; |
123 |
Geometry jtsGeom = createJtsGeom(vertex); |
124 |
if (jtsGeom == null) { |
125 |
System.err.println(i
|
126 |
+ "nth vertex has associated a null voronoi polygon");
|
127 |
continue;
|
128 |
} |
129 |
|
130 |
IGeometry geom = NewFConverter.toFMap(jtsGeom); |
131 |
Value fid = ValueFactory.createValue(i); |
132 |
Value associatedVertex = ValueFactory |
133 |
.createValue(vertex.originalOrder); |
134 |
Value[] values = new Value[] { fid, associatedVertex }; |
135 |
DefaultFeature feature = new DefaultFeature(geom, values, new UID() |
136 |
.toString()); |
137 |
solution.add(feature); |
138 |
} |
139 |
|
140 |
return solution;
|
141 |
} |
142 |
|
143 |
/**
|
144 |
* Receives a voronoi vertex and builds a geometry collection with all
|
145 |
* polygons and dangling lines associated to this voronoi vertex.
|
146 |
*
|
147 |
* TODO SI NO SE CONSIGUE FORMAR POLIGONO, FUSIONAR LOS LINESTRINGS CON UN
|
148 |
* LINESTRINGSEWER
|
149 |
*
|
150 |
* @param voronoiVertex
|
151 |
* @return
|
152 |
*/
|
153 |
private Geometry createJtsGeom(Vertex voronoiVertex) {
|
154 |
GeometryCollection solution = null;
|
155 |
List<Geometry> geometries = new ArrayList<Geometry>(); |
156 |
Polygonizer polygonizer = new Polygonizer();
|
157 |
List lineStringCollection = new ArrayList(); |
158 |
Enumeration i = voronoiVertex.edges.elements();
|
159 |
while (i.hasMoreElements()) {
|
160 |
VoronoiSegment s = (VoronoiSegment) i.nextElement(); |
161 |
Vertex v1 = s.v1; |
162 |
Coordinate c1 = new Coordinate(v1.x, v1.y);
|
163 |
Vertex v2 = s.v2; |
164 |
Coordinate c2 = new Coordinate(v2.x, v2.y);
|
165 |
if (c1.equals2D(c2)) {
|
166 |
System.out.println("El segmento " + s.toString() |
167 |
+ " tiene los dos vertices iguales");
|
168 |
continue;
|
169 |
} |
170 |
LineString line = JtsUtil.GEOMETRY_FACTORY |
171 |
.createLineString(new Coordinate[] { c1, c2 }); |
172 |
lineStringCollection.add(line); |
173 |
}// while
|
174 |
if (lineStringCollection.size() == 0) |
175 |
return null; |
176 |
polygonizer.add(lineStringCollection); |
177 |
Collection polygons = polygonizer.getPolygons();
|
178 |
geometries.addAll(polygons); |
179 |
Collection danglingLines = polygonizer.getDangles();
|
180 |
geometries.addAll(danglingLines); |
181 |
Geometry[] geomsArray = new Geometry[geometries.size()]; |
182 |
geometries.toArray(geomsArray); |
183 |
solution = JtsUtil.GEOMETRY_FACTORY |
184 |
.createGeometryCollection(geomsArray); |
185 |
return solution;
|
186 |
} |
187 |
|
188 |
public List<IFeature> createThiessenPolygons( |
189 |
VoronoiAndTinInputLyr inputLyr, |
190 |
boolean onlySelection,
|
191 |
CancellableProgressTask progressTask) |
192 |
throws BaseException {
|
193 |
|
194 |
List<IFeature> solution = new ArrayList<IFeature>(); |
195 |
int numPoints = inputLyr.getSource().getShapeCount();
|
196 |
List<Coord> coordsList = new ArrayList<Coord>(); |
197 |
for (int i = 0; i < numPoints; i++) { |
198 |
if(onlySelection){
|
199 |
if(! inputLyr.getRecordset().getSelection().get(i))
|
200 |
continue;
|
201 |
} |
202 |
Point2D point = inputLyr.getPoint(i);
|
203 |
Coord coord = new Coord();
|
204 |
coord.x = point.getX(); |
205 |
coord.y = point.getY(); |
206 |
coordsList.add(coord); |
207 |
} |
208 |
Coord[] coords = new Coord[coordsList.size()]; |
209 |
coordsList.toArray(coords); |
210 |
Vertex[] vertices = Voronoi.generate(coords);
|
211 |
List<Geometry> geometries = new ArrayList<Geometry>(); |
212 |
for (int i = 0; i < vertices.length; i++) { |
213 |
Vertex vertex = vertices[i]; |
214 |
Geometry jtsGeom = createJtsGeom(vertex); |
215 |
geometries.add(jtsGeom); |
216 |
} |
217 |
|
218 |
List<Geometry> thiessen = Voronoier.getThiessenPolygons(geometries, progressTask);
|
219 |
for (int i = 0; i < thiessen.size(); i++) { |
220 |
Geometry jtsGeom = thiessen.get(i); |
221 |
IGeometry geom = NewFConverter.toFMap(jtsGeom); |
222 |
Value fid = ValueFactory.createValue(i); |
223 |
Value associatedVertex = ValueFactory |
224 |
.createValue(i); |
225 |
Value[] values = new Value[] { fid, associatedVertex }; |
226 |
DefaultFeature feature = new DefaultFeature(geom, values, new UID() |
227 |
.toString()); |
228 |
solution.add(feature); |
229 |
} |
230 |
|
231 |
return solution;
|
232 |
|
233 |
|
234 |
} |
235 |
|
236 |
public List<IFeature> createThiessenPolygons(VoronoiAndTinInputLyr inputLyr, boolean onlySelection) |
237 |
throws BaseException {
|
238 |
return createThiessenPolygons(inputLyr, onlySelection, null); |
239 |
} |
240 |
|
241 |
public List<IFeature> createTin(VoronoiAndTinInputLyr inputLyr, boolean onlySelection) |
242 |
throws BaseException {
|
243 |
return createTin(inputLyr, onlySelection, null); |
244 |
} |
245 |
|
246 |
} |