Revision 24075 trunk/libraries/libTopology/src/org/gvsig/jts/voronoi/chew/DTriangulationForJTS.java

View differences:

DTriangulationForJTS.java
10 10
 ***********************************************/
11 11
package org.gvsig.jts.voronoi.chew;
12 12

  
13
import java.awt.geom.Point2D;
13 14
import java.util.ArrayList;
14
import java.util.Collection;
15 15
import java.util.Iterator;
16 16
import java.util.List;
17
import java.util.Set;
18 17

  
18
import org.gvsig.exceptions.BaseException;
19 19
import org.gvsig.jts.JtsUtil;
20
import org.gvsig.jts.voronoi.MessExtentVertexVisitor;
21
import org.gvsig.jts.voronoi.VoronoiAndTinInputLyr;
22
import org.gvsig.jts.voronoi.VoronoiAndTinInputLyr.VertexVisitor;
20 23
import org.gvsig.topology.Messages;
21 24

  
22 25
import com.iver.utiles.swing.threads.CancellableProgressTask;
23 26
import com.vividsolutions.jts.geom.Coordinate;
24 27
import com.vividsolutions.jts.geom.Envelope;
25
import com.vividsolutions.jts.geom.Geometry;
26
import com.vividsolutions.jts.geom.GeometryFactory;
27
import com.vividsolutions.jts.geom.LineString;
28
import com.vividsolutions.jts.geom.LinearRing;
29 28
import com.vividsolutions.jts.geom.Point;
30
import com.vividsolutions.jts.geom.Polygon;
31
import com.vividsolutions.jts.operation.polygonize.Polygonizer;
32 29

  
33 30
/**
34
 * OpenJUMP's modified version.
31
 * OpenJUMP's modified version by gvSIG project.
35 32
 * 
36
 * We have added generics to the input point's list, and passed
33
 * We have exchange List<Point2D> for VoronoiAndTinInputLyr, and passed
37 34
 * CancellableProgressTask as parameter of those methods we want to monitor its
38
 * progress.
35
 * progress. 
39 36
 * 
37
 * The TIN's implemented algorithm is incremental delaunay
38
 * triangulation, which starts with a triangle that contains all mess points, and
39
 * subdivides it with each new inserted vertex.
40 40
 * 
41 41
 * 
42
 * 
42 43
 * @author sstein
43
 * @author azabala
44
 * @author azabala(adapter to gvsig)
44 45
 * 
45 46
 * Use the class to access the delauney trinagulation by L. Paul Chew Methods of
46 47
 * the class are modified versions from DelaunayPanel.java in DelaunayAp.java.
47 48
 */
48 49
public class DTriangulationForJTS {
50
	
49 51
	private DelaunayTriangulation dt; // The Delaunay triangulation
50 52
	private Simplex initialTriangle; // The large initial triangle
53
	
51 54
	private int initialSize = 10000; // Controls size of initial triangle
52 55
	private boolean isVoronoi; // True iff VoD instead of DT
56
	
53 57
	private double dx = 0;
54 58
	private double dy = 0;
59
	
55 60
	private Pnt lowerLeftPnt = null;
56 61
	public boolean debug = false; // True iff printing info for debugging
57 62

  
58
	public DTriangulationForJTS(List<Point> pointList) {
59
		double argmaxx = 0;
60
		double argmaxy = 0;
61
		double argminx = 0;
62
		double argminy = 0;
63
		int count = 0;
63
	
64
	
65

  
66
	public DTriangulationForJTS(VoronoiAndTinInputLyr inputLyr, boolean onlySelection) throws BaseException{
67
	
64 68
		// -- calc coordinates of initial symplex
65
		for (Iterator iter = pointList.iterator(); iter.hasNext();) {
66
			Point pt = (Point) iter.next();
67
			if (count == 0) {
68
				argmaxx = pt.getX();
69
				argminx = pt.getX();
70
				argmaxy = pt.getY();
71
				argminy = pt.getY();
72
			} else {
73
				if (pt.getX() < argminx) {
74
					argminx = pt.getX();
75
				}
76
				if (pt.getX() > argmaxx) {
77
					argmaxx = pt.getX();
78
				}
79
				if (pt.getY() < argminy) {
80
					argminy = pt.getY();
81
				}
82
				if (pt.getY() > argmaxy) {
83
					argmaxy = pt.getY();
84
				}
85
			}
86
			count++;
87
		}
88
		this.dx = argmaxx - argminx;
89
		this.dy = argmaxy - argminy;
69
		MessExtentVertexVisitor visitor = new MessExtentVertexVisitor();	
70
		inputLyr.visitVertices(visitor, onlySelection);
71
		
72
		this.dx = visitor.getArgmaxx() - visitor.getArgminx();
73
		this.dy = visitor.getArgmaxy() - visitor.getArgminy();
74
		
90 75
		// -- the initial simplex must contain all points
91 76
		// -- take the bounding box, move the diagonals (sidewards)
92 77
		// the meeting point will be the mirrored bbox-center on the top edge
93 78
		this.initialTriangle = new Simplex(new Pnt[] {
94
				new Pnt(argminx - (1.5 * dx), argminy - dy), // lower left
95
				new Pnt(argmaxx + (1.5 * dx), argminy - dy), // lower right
96
				// new Pnt(argminx+(dx/2.0), argmaxy+(dy/2.0))}); //center, top
97
				new Pnt(argminx + (dx / 2.0), argmaxy + 1.5 * dy) }); // center,
98
																		// top
79
					new Pnt(visitor.getArgminx() - (1.5 * dx), visitor.getArgminy() - dy), // lower left
80
					new Pnt(visitor.getArgmaxx() + (1.5 * dx), visitor.getArgminy() - dy), // lower right
81
					new Pnt(visitor.getArgminx() + (dx / 2.0), visitor.getArgmaxy() + 1.5 * dy) }); // center,
82
			
99 83

  
100
		this.lowerLeftPnt = new Pnt(argminx - (1.5 * dx), argminy - dy);
84
		this.lowerLeftPnt = new Pnt(visitor.getArgminx() - (1.5 * dx), visitor.getArgminy() - dy);
101 85
		this.dt = new DelaunayTriangulation(initialTriangle);
102
		this.addPoints(pointList);
86
		this.addPoints(inputLyr, onlySelection);
103 87
	}
104 88

  
105 89
	/**
......
109 93
	 *            the envelope my extend the initial point cloud and result in a
110 94
	 *            larger initial simplex
111 95
	 */
112
	public DTriangulationForJTS(List<Point> pointList, Envelope envelope) {
113
		double argmaxx = 0;
114
		double argmaxy = 0;
115
		double argminx = 0;
116
		double argminy = 0;
117
		int count = 0;
118
		// -- calc coordinates of initial symplex
119
		for (Iterator iter = pointList.iterator(); iter.hasNext();) {
120
			Point pt = (Point) iter.next();
121
			if (count == 0) {
122
				argmaxx = pt.getX();
123
				argminx = pt.getX();
124
				argmaxy = pt.getY();
125
				argminy = pt.getY();
126
			} else {
127
				if (pt.getX() < argminx) {
128
					argminx = pt.getX();
129
				}
130
				if (pt.getX() > argmaxx) {
131
					argmaxx = pt.getX();
132
				}
133
				if (pt.getY() < argminy) {
134
					argminy = pt.getY();
135
				}
136
				if (pt.getY() > argmaxy) {
137
					argmaxy = pt.getY();
138
				}
139
			}
140
			count++;
141
		}
96
	public DTriangulationForJTS(VoronoiAndTinInputLyr inputLyr, boolean onlySelection, Envelope envelope) throws BaseException {
97
		MessExtentVertexVisitor visitor = new MessExtentVertexVisitor();	
98
		inputLyr.visitVertices(visitor, onlySelection);
99
		
100
		double argminx = visitor.getArgminx();
101
		double argmaxx = visitor.getArgmaxx();
102
		double argminy = visitor.getArgminy();
103
		double argmaxy = visitor.getArgmaxy();
104
		
142 105
		// -- do check also for the delivered envelope
143 106
		if (envelope.getMinX() < argminx) {
144 107
			argminx = envelope.getMinX();
......
167 130

  
168 131
		this.lowerLeftPnt = new Pnt(argminx - (1.5 * dx), argminy - dy);
169 132
		this.dt = new DelaunayTriangulation(initialTriangle);
170
		this.addPoints(pointList);
133
		this.addPoints(inputLyr, onlySelection);
171 134
	}
172 135

  
173
	public void addPoints(List<Point> pointList) {
174
		for (Iterator iter = pointList.iterator(); iter.hasNext();) {
175
			try {
176
				Geometry element = (Geometry) iter.next();
177
				if (element instanceof Point) {
178
					Point jtsPt = (Point) element;
179
					Pnt point = new Pnt(jtsPt.getX(), jtsPt.getY());
180
					dt.delaunayPlace(point);
181
				} else {
182
					if (debug)
183
						System.out.println("no point geometry");
184
				}
185
			} catch (Exception e) {
186
				if (debug)
187
					System.out.println("no geometry");
188
			}
136
	
137
	class CreateDTVertexVisitor implements VertexVisitor{
138
		DelaunayTriangulation dt;
139
		public void visit(Point2D vertex) {
140
			Point pt = JtsUtil.GEOMETRY_FACTORY.
141
					createPoint(new Coordinate(vertex.getX(), vertex.getY()));
142
			Pnt point = new Pnt(pt.getX(), pt.getY());
143
			dt.delaunayPlace(point);
189 144
		}
145
		
190 146
	}
147
	
148
	public void addPoints(VoronoiAndTinInputLyr inputLyr, boolean onlySelection) throws BaseException {
149
		CreateDTVertexVisitor visitor = new CreateDTVertexVisitor();
150
		visitor.dt = this.dt;
151
		inputLyr.visitVertices(visitor, onlySelection);
152
	}
191 153

  
192 154
	public void addPoint(double x, double y) {
193 155
		Pnt point = new Pnt(x, y);
......
196 158
		dt.delaunayPlace(point);
197 159
	}
198 160

  
161
	
162
	
199 163
	/**
200 164
	 * Draw all the Delaunay edges.
201 165
	 * 
202 166
	 * @return Arraylist with LineString geometries.
203 167
	 */
204
	public List<Geometry> drawAllDelaunay() {
205
		// Loop through all the edges of the DT (each is done twice)
206
		GeometryFactory gf = new GeometryFactory();
207
		ArrayList<Geometry> lines = new ArrayList<Geometry>();
208
		for (Iterator it = dt.iterator(); it.hasNext();) {
209
			Simplex triangle = (Simplex) it.next();
210
			for (Iterator otherIt = triangle.facets().iterator(); otherIt
211
					.hasNext();) {
212
				Set facet = (Set) otherIt.next();
213
				Pnt[] endpoint = (Pnt[]) facet.toArray(new Pnt[2]);
214
				Coordinate[] coords = new Coordinate[2];
215
				coords[0] = new Coordinate(endpoint[0].coord(0), endpoint[0]
216
						.coord(1));
217
				coords[1] = new Coordinate(endpoint[1].coord(0), endpoint[1]
218
						.coord(1));
219
				LineString ls = gf.createLineString(coords);
220
				lines.add(ls);
221
			}
222
		}
223
		return lines;
224
	}
168
//	public List<Geometry> drawAllDelaunay() {
169
//		// Loop through all the edges of the DT (each is done twice)
170
//		GeometryFactory gf = new GeometryFactory();
171
//		ArrayList<Geometry> lines = new ArrayList<Geometry>();
172
//		for (Iterator it = dt.iterator(); it.hasNext();) {
173
//			Simplex triangle = (Simplex) it.next();
174
//			for (Iterator otherIt = triangle.facets().iterator(); otherIt.hasNext();) {
175
//				Set facet = (Set) otherIt.next();
176
//				Pnt[] endpoint = (Pnt[]) facet.toArray(new Pnt[2]);
177
//				Coordinate[] coords = new Coordinate[2];
178
//				coords[0] = new Coordinate(endpoint[0].coord(0), endpoint[0]
179
//						.coord(1));
180
//				coords[1] = new Coordinate(endpoint[1].coord(0), endpoint[1]
181
//						.coord(1));
182
//				LineString ls = gf.createLineString(coords);
183
//				lines.add(ls);
184
//			}
185
//		}
186
//		return lines;
187
//	}
225 188

  
226
	public List<Geometry> getTinTriangles(
227
			CancellableProgressTask progressMonitor) {
228
		ArrayList<Geometry> triangles = new ArrayList<Geometry>();
189
	public List<Coordinate[]> getTinTriangles(CancellableProgressTask progressMonitor) {
190
		ArrayList<Coordinate[]> triangles = new ArrayList<Coordinate[]>();
229 191

  
230 192
		if (progressMonitor != null) {
231 193
			progressMonitor.setInitialStep(0);
......
251 213
				coords.add(coords.get(0));
252 214
				Coordinate[] coordsArray = new Coordinate[coords.size()];
253 215
				coords.toArray(coordsArray);
254
				Polygon polygon = JtsUtil.createPolygon(coordsArray,
255
						new int[] { 0 });
256
				triangles.add(polygon);
216
				triangles.add(coordsArray);
217
//				Polygon polygon = JtsUtil.createPolygon(coordsArray,
218
//						new int[] { 0 });
219
//				triangles.add(polygon);
257 220

  
258 221
				if (progressMonitor != null)
259 222
					progressMonitor.reportStep();
......
267 230
	 * 
268 231
	 * @return Arraylist with LineString geometries.
269 232
	 */
270
	public List<LineString> drawAllVoronoi() {
271
		GeometryFactory gf = new GeometryFactory();
272
		ArrayList<LineString> lines = new ArrayList<LineString>();
273
		// Loop through all the edges of the DT (each is done twice)
274
		for (Iterator it = dt.iterator(); it.hasNext();) {
275
			Simplex triangle = (Simplex) it.next();
276
			for (Iterator otherIt = dt.neighbors(triangle).iterator(); otherIt
277
					.hasNext();) {
278
				Simplex other = (Simplex) otherIt.next();
279
				Pnt p = Pnt.circumcenter((Pnt[]) triangle.toArray(new Pnt[0]));
280
				Pnt q = Pnt.circumcenter((Pnt[]) other.toArray(new Pnt[0]));
281
				Coordinate[] coords = new Coordinate[2];
282
				coords[0] = new Coordinate(p.coord(0), p.coord(1));
283
				coords[1] = new Coordinate(q.coord(0), q.coord(1));
284
				LineString ls = gf.createLineString(coords);
285
				lines.add(ls);
286
			}
287
		}
288
		return lines;
289
	}
233
//	public List<LineString> drawAllVoronoi() {
234
//		GeometryFactory gf = new GeometryFactory();
235
//		ArrayList<LineString> lines = new ArrayList<LineString>();
236
//		// Loop through all the edges of the DT (each is done twice)
237
//		for (Iterator it = dt.iterator(); it.hasNext();) {
238
//			Simplex triangle = (Simplex) it.next();
239
//			for (Iterator otherIt = dt.neighbors(triangle).iterator(); otherIt.hasNext();) {
240
//				Simplex other = (Simplex) otherIt.next();
241
//				Pnt p = Pnt.circumcenter((Pnt[]) triangle.toArray(new Pnt[0]));
242
//				Pnt q = Pnt.circumcenter((Pnt[]) other.toArray(new Pnt[0]));
243
//				Coordinate[] coords = new Coordinate[2];
244
//				coords[0] = new Coordinate(p.coord(0), p.coord(1));
245
//				coords[1] = new Coordinate(q.coord(0), q.coord(1));
246
//				LineString ls = gf.createLineString(coords);
247
//				lines.add(ls);
248
//			}
249
//		}
250
//		return lines;
251
//	}
290 252

  
291
	/**
292
	 * Draw all the sites (i.e., the input points) of the DT.
293
	 * 
294
	 * @return Arraylist with point geometries.
295
	 */
296
	public ArrayList drawAllSites() {
297
		// Loop through all sites of the DT
298
		// Each is done several times, about 6 times each on average; this
299
		// can be proved via Euler's formula.
300
		GeometryFactory gf = new GeometryFactory();
301
		ArrayList points = new ArrayList();
302
		for (Iterator it = dt.iterator(); it.hasNext();) {
303
			for (Iterator otherIt = ((Simplex) it.next()).iterator(); otherIt
304
					.hasNext();) {
305
				Pnt pt = (Pnt) otherIt.next();
306
				Coordinate coord = new Coordinate(pt.coord(0), pt.coord(1));
307
				Point jtsPt = gf.createPoint(coord);
308
				points.add(jtsPt);
309
			}
310
		}
311
		return points;
312
	}
253
//	/**
254
//	 * Draw all the sites (i.e., the input points) of the DT.
255
//	 * 
256
//	 * @return Arraylist with point geometries.
257
//	 */
258
//	public ArrayList drawAllSites() {
259
//		// Loop through all sites of the DT
260
//		// Each is done several times, about 6 times each on average; this
261
//		// can be proved via Euler's formula.
262
//		GeometryFactory gf = new GeometryFactory();
263
//		ArrayList points = new ArrayList();
264
//		for (Iterator it = dt.iterator(); it.hasNext();) {
265
//			for (Iterator otherIt = ((Simplex) it.next()).iterator(); otherIt
266
//					.hasNext();) {
267
//				Pnt pt = (Pnt) otherIt.next();
268
//				Coordinate coord = new Coordinate(pt.coord(0), pt.coord(1));
269
//				Point jtsPt = gf.createPoint(coord);
270
//				points.add(jtsPt);
271
//			}
272
//		}
273
//		return points;
274
//	}
313 275

  
314 276
	/**
315 277
	 * Draw all the empty circles (one for each triangle) of the DT.
316 278
	 * 
317 279
	 * @return Arraylist with polygon geometries.
318 280
	 */
319
	public ArrayList drawAllCircles() {
320
		// Loop through all triangles of the DT
321
		GeometryFactory gf = new GeometryFactory();
322
		ArrayList circles = new ArrayList();
323
		loop: for (Iterator it = dt.iterator(); it.hasNext();) {
324
			Simplex triangle = (Simplex) it.next();
325
			for (Iterator otherIt = initialTriangle.iterator(); otherIt
326
					.hasNext();) {
327
				Pnt p = (Pnt) otherIt.next();
328
				if (triangle.contains(p))
329
					continue loop;
330
			}
331
			Pnt c = Pnt.circumcenter((Pnt[]) triangle.toArray(new Pnt[0]));
332
			double radius = c.subtract((Pnt) triangle.iterator().next())
333
					.magnitude();
334
			Coordinate coord = new Coordinate(c.coord(0), c.coord(1));
335
			Point jtsPt = gf.createPoint(coord);
336
			circles.add(jtsPt.buffer(radius));
337
		}
338
		return circles;
339
	}
281
//	public ArrayList drawAllCircles() {
282
//		// Loop through all triangles of the DT
283
//		GeometryFactory gf = new GeometryFactory();
284
//		ArrayList circles = new ArrayList();
285
//		loop: for (Iterator it = dt.iterator(); it.hasNext();) {
286
//			Simplex triangle = (Simplex) it.next();
287
//			for (Iterator otherIt = initialTriangle.iterator(); otherIt
288
//					.hasNext();) {
289
//				Pnt p = (Pnt) otherIt.next();
290
//				if (triangle.contains(p))
291
//					continue loop;
292
//			}
293
//			Pnt c = Pnt.circumcenter((Pnt[]) triangle.toArray(new Pnt[0]));
294
//			double radius = c.subtract((Pnt) triangle.iterator().next())
295
//					.magnitude();
296
//			Coordinate coord = new Coordinate(c.coord(0), c.coord(1));
297
//			Point jtsPt = gf.createPoint(coord);
298
//			circles.add(jtsPt.buffer(radius));
299
//		}
300
//		return circles;
301
//	}
340 302

  
341 303
	public DelaunayTriangulation getDelaunayTriangulation() {
342 304
		return dt;
......
347 309
	 * @return the corner points of the initial simplex which is divided into
348 310
	 *         smaller simplexes by the iterative insertion of the point dataset
349 311
	 */
350
	public ArrayList getInitialSimmplexAsJTSPoints() {
351
		GeometryFactory gf = new GeometryFactory();
352
		ArrayList points = new ArrayList();
312
//	public ArrayList getInitialSimmplexAsJTSPoints() {
313
//		GeometryFactory gf = new GeometryFactory();
314
//		ArrayList points = new ArrayList();
315
//
316
//		for (Iterator otherIt = this.initialTriangle.iterator(); otherIt
317
//				.hasNext();) {
318
//			Pnt pt = (Pnt) otherIt.next();
319
//			Coordinate coord = new Coordinate(pt.coord(0), pt.coord(1));
320
//			Point jtsPt = gf.createPoint(coord);
321
//			points.add(jtsPt);
322
//		}
323
//		return points;
324
//	}
353 325

  
354
		for (Iterator otherIt = this.initialTriangle.iterator(); otherIt
355
				.hasNext();) {
356
			Pnt pt = (Pnt) otherIt.next();
357
			Coordinate coord = new Coordinate(pt.coord(0), pt.coord(1));
358
			Point jtsPt = gf.createPoint(coord);
359
			points.add(jtsPt);
360
		}
361
		return points;
362
	}
363

  
364 326
	/**
365 327
	 * the size of the box has been empirically defined to get "undistorted"
366 328
	 * outer thiessen polygons
367 329
	 * 
368 330
	 * @return a bounding box necesseray to create the final thiessenpolygons
369 331
	 */
370
	public Geometry getThiessenBoundingBox() {
371
		GeometryFactory gf = new GeometryFactory();
372
		Coordinate[] coords = new Coordinate[5];
373
		coords[0] = new Coordinate(this.lowerLeftPnt.coord(0) + 1 * this.dx,
374
				this.lowerLeftPnt.coord(1) + 0.5 * this.dy); // lowerleft
375
		coords[1] = new Coordinate(this.lowerLeftPnt.coord(0) + 3 * this.dx,
376
				this.lowerLeftPnt.coord(1) + 0.5 * this.dy); // lowerright
377
		coords[2] = new Coordinate(this.lowerLeftPnt.coord(0) + 3 * this.dx,
378
				this.lowerLeftPnt.coord(1) + 2.5 * this.dy); // topright
379
		coords[3] = new Coordinate(this.lowerLeftPnt.coord(0) + 1 * this.dx,
380
				this.lowerLeftPnt.coord(1) + 2.5 * this.dy); // topleft
381
		// -- to close linestring
382
		coords[4] = new Coordinate(this.lowerLeftPnt.coord(0) + 1 * this.dx,
383
				this.lowerLeftPnt.coord(1) + 0.5 * dy); // lowerleft
384
		LinearRing lr = gf.createLinearRing(coords);
385
		Geometry bbox = gf.createPolygon(lr, null);
386
		return bbox;
387
	}
332
//	public Geometry getThiessenBoundingBox() {
333
//		GeometryFactory gf = new GeometryFactory();
334
//		Coordinate[] coords = new Coordinate[5];
335
//		coords[0] = new Coordinate(this.lowerLeftPnt.coord(0) + 1 * this.dx,
336
//				this.lowerLeftPnt.coord(1) + 0.5 * this.dy); // lowerleft
337
//		coords[1] = new Coordinate(this.lowerLeftPnt.coord(0) + 3 * this.dx,
338
//				this.lowerLeftPnt.coord(1) + 0.5 * this.dy); // lowerright
339
//		coords[2] = new Coordinate(this.lowerLeftPnt.coord(0) + 3 * this.dx,
340
//				this.lowerLeftPnt.coord(1) + 2.5 * this.dy); // topright
341
//		coords[3] = new Coordinate(this.lowerLeftPnt.coord(0) + 1 * this.dx,
342
//				this.lowerLeftPnt.coord(1) + 2.5 * this.dy); // topleft
343
//		// -- to close linestring
344
//		coords[4] = new Coordinate(this.lowerLeftPnt.coord(0) + 1 * this.dx,
345
//				this.lowerLeftPnt.coord(1) + 0.5 * dy); // lowerleft
346
//		LinearRing lr = gf.createLinearRing(coords);
347
//		Geometry bbox = gf.createPolygon(lr, null);
348
//		return bbox;
349
//	}
388 350

  
389
	public List<Geometry> getThiessenPolys() {
390
		return getThiessenPolys(null);
391
	}
351
//	public List<Geometry> getThiessenPolys() {
352
//		return getThiessenPolys(null);
353
//	}
392 354

  
393 355
	/**
394 356
	 * Method returns thiessen polygons within a empirically enlarged bounding
......
400 362
	 * 
401 363
	 * @return
402 364
	 */
403
	public List<Geometry> getThiessenPolys(
404
			CancellableProgressTask progressMonitor) {
405
		// -- do union of all edges and use the polygonizer to create polygons
406
		// from it
407
		if (debug)
408
			System.out.println("get voronoi egdes");
409
		List<LineString> edges = this.drawAllVoronoi();
365
//	public List<Geometry> getThiessenPolys(CancellableProgressTask progressMonitor) {
366
//		// -- do union of all edges and use the polygonizer to create polygons
367
//		// from it
368
//		if (debug)
369
//			System.out.println("get voronoi egdes");
370
//		List<LineString> edges = this.drawAllVoronoi();
371
//
372
//		if (debug)
373
//			System.out.println("merge voronoi egdes to multiLineString");
374
//
375
//		Geometry lines = null;
376
//
377
//		Geometry mls = (Geometry) edges.get(0);
378
//
379
//		if (progressMonitor != null) {
380
//			progressMonitor.setInitialStep(0);
381
//			int numOfSteps = edges.size();
382
//			progressMonitor.setFinalStep(numOfSteps);
383
//			progressMonitor.setDeterminatedProcess(true);
384
//			progressMonitor.setNote(Messages
385
//					.getText("topology_clean_of_thiessen_edges"));
386
//			progressMonitor.setStatusMessage(Messages
387
//					.getText("voronoi_diagram_layer_message"));
388
//			progressMonitor.reportStep();
389
//		}
390
//
391
//		for (int i = 1; i < edges.size(); i++) {
392
//			Geometry line = (Geometry) edges.get(i);
393
//			mls = mls.union(line);
394
//			if (progressMonitor != null)
395
//				progressMonitor.reportStep();
396
//		}
397
//
398
//		lines = mls;
399
//
400
//		// Geometry[] geometries = new Geometry[edges.size()];
401
//		// edges.toArray(geometries);
402
//		// lines =
403
//		// JtsUtil.GEOMETRY_FACTORY.createGeometryCollection(geometries);
404
//		// if(lines.getClass().equals(GeometryCollection.class)){
405
//		// GeometryCollection gc = (GeometryCollection) lines;
406
//		// lines = JtsUtil.convertToMultiLineString(gc);
407
//		// }
408
//
409
//		if (debug)
410
//			System.out.println("polygonize");
411
//		Polygonizer poly = new Polygonizer();
412
//		poly.add(lines);
413
//		Collection polys = poly.getPolygons();
414
//		// -- get final polygons in bounding box (=intersection polygons with
415
//		// the bbox)
416
//		Geometry bbox = this.getThiessenBoundingBox();
417
//		if (debug)
418
//			System.out.println("get intersections and final polys..");
419
//
420
//		if (progressMonitor != null) {
421
//			progressMonitor.setInitialStep(0);
422
//			int numOfSteps = polys.size();
423
//			progressMonitor.setFinalStep(numOfSteps);
424
//			progressMonitor.setDeterminatedProcess(true);
425
//			progressMonitor.setNote(Messages
426
//					.getText("computing_thiessen_polygons"));
427
//			progressMonitor.setStatusMessage(Messages
428
//					.getText("voronoi_diagram_layer_message"));
429
//		}
430
//
431
//		ArrayList<Geometry> finalPolys = new ArrayList<Geometry>();
432
//		for (Iterator iter = polys.iterator(); iter.hasNext();) {
433
//			Geometry candPoly = (Geometry) iter.next();
434
//			Geometry intersection = bbox.intersection(candPoly);
435
//			if (progressMonitor != null)
436
//				progressMonitor.reportStep();
437
//			if (intersection != null) {
438
//				if (intersection.getArea() > 0) {
439
//					finalPolys.add(intersection);
440
//				}
441
//			}
442
//		}
443
//		if (progressMonitor != null)
444
//			progressMonitor.reportStep();
445
//		return finalPolys;
446
//	}
410 447

  
411
		if (debug)
412
			System.out.println("merge voronoi egdes to multiLineString");
413

  
414
		Geometry lines = null;
415

  
416
		Geometry mls = (Geometry) edges.get(0);
417

  
418
		if (progressMonitor != null) {
419
			progressMonitor.setInitialStep(0);
420
			int numOfSteps = edges.size();
421
			progressMonitor.setFinalStep(numOfSteps);
422
			progressMonitor.setDeterminatedProcess(true);
423
			progressMonitor.setNote(Messages
424
					.getText("topology_clean_of_thiessen_edges"));
425
			progressMonitor.setStatusMessage(Messages
426
					.getText("voronoi_diagram_layer_message"));
427
			progressMonitor.reportStep();
428
		}
429

  
430
		for (int i = 1; i < edges.size(); i++) {
431
			Geometry line = (Geometry) edges.get(i);
432
			mls = mls.union(line);
433
			if (progressMonitor != null)
434
				progressMonitor.reportStep();
435
		}
436

  
437
		lines = mls;
438

  
439
		// Geometry[] geometries = new Geometry[edges.size()];
440
		// edges.toArray(geometries);
441
		// lines =
442
		// JtsUtil.GEOMETRY_FACTORY.createGeometryCollection(geometries);
443
		// if(lines.getClass().equals(GeometryCollection.class)){
444
		// GeometryCollection gc = (GeometryCollection) lines;
445
		// lines = JtsUtil.convertToMultiLineString(gc);
446
		// }
447

  
448
		if (debug)
449
			System.out.println("polygonize");
450
		Polygonizer poly = new Polygonizer();
451
		poly.add(lines);
452
		Collection polys = poly.getPolygons();
453
		// -- get final polygons in bounding box (=intersection polygons with
454
		// the bbox)
455
		Geometry bbox = this.getThiessenBoundingBox();
456
		if (debug)
457
			System.out.println("get intersections and final polys..");
458

  
459
		if (progressMonitor != null) {
460
			progressMonitor.setInitialStep(0);
461
			int numOfSteps = polys.size();
462
			progressMonitor.setFinalStep(numOfSteps);
463
			progressMonitor.setDeterminatedProcess(true);
464
			progressMonitor.setNote(Messages
465
					.getText("computing_thiessen_polygons"));
466
			progressMonitor.setStatusMessage(Messages
467
					.getText("voronoi_diagram_layer_message"));
468
		}
469

  
470
		ArrayList<Geometry> finalPolys = new ArrayList<Geometry>();
471
		for (Iterator iter = polys.iterator(); iter.hasNext();) {
472
			Geometry candPoly = (Geometry) iter.next();
473
			Geometry intersection = bbox.intersection(candPoly);
474
			if (progressMonitor != null)
475
				progressMonitor.reportStep();
476
			if (intersection != null) {
477
				if (intersection.getArea() > 0) {
478
					finalPolys.add(intersection);
479
				}
480
			}
481
		}
482
		if (progressMonitor != null)
483
			progressMonitor.reportStep();
484
		return finalPolys;
485
	}
486

  
487 448
}

Also available in: Unified diff