root / branches / v2_0_0_prep / extensions / extEditing / src / org / gvsig / editing / gui / cad / tools / EquidistanceCADTool.java @ 29616
History | View | Annotate | Download (15.8 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package org.gvsig.editing.gui.cad.tools; |
42 |
|
43 |
import java.awt.Graphics; |
44 |
import java.awt.event.InputEvent; |
45 |
import java.awt.geom.Point2D; |
46 |
import java.util.ArrayList; |
47 |
|
48 |
import org.gvsig.andami.PluginServices; |
49 |
import org.gvsig.editing.CADExtension; |
50 |
import org.gvsig.editing.gui.cad.DefaultCADTool; |
51 |
import org.gvsig.editing.gui.cad.exception.CommandException; |
52 |
import org.gvsig.editing.gui.cad.tools.smc.EquidistanceCADToolContext; |
53 |
import org.gvsig.editing.gui.cad.tools.smc.EquidistanceCADToolContext.EquidistanceCADToolState; |
54 |
import org.gvsig.editing.layers.VectorialLayerEdited; |
55 |
import org.gvsig.fmap.dal.exception.DataException; |
56 |
import org.gvsig.fmap.dal.exception.ReadException; |
57 |
import org.gvsig.fmap.dal.feature.DisposableIterator; |
58 |
import org.gvsig.fmap.dal.feature.Feature; |
59 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
60 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
61 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
62 |
import org.gvsig.fmap.geom.Geometry; |
63 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
64 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
65 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
66 |
import org.gvsig.fmap.geom.operation.tojts.ToJTS; |
67 |
import org.gvsig.fmap.geom.util.Converter; |
68 |
import org.gvsig.fmap.geom.util.UtilFunctions; |
69 |
|
70 |
import com.vividsolutions.jts.algorithm.CGAlgorithms; |
71 |
import com.vividsolutions.jts.geom.Coordinate; |
72 |
import com.vividsolutions.jts.geom.GeometryFactory; |
73 |
import com.vividsolutions.jts.geom.LineString; |
74 |
import com.vividsolutions.jts.geom.LinearRing; |
75 |
import com.vividsolutions.jts.geom.Point; |
76 |
import com.vividsolutions.jts.simplify.TopologyPreservingSimplifier; |
77 |
|
78 |
/**
|
79 |
* Herramienta para crear una geometr?a equidistante a otra.
|
80 |
*
|
81 |
* @author Vicente Caballero Navarro
|
82 |
*/
|
83 |
public class EquidistanceCADTool extends DefaultCADTool { |
84 |
private EquidistanceCADToolContext _fsm;
|
85 |
private Point2D firstPoint = new Point2D.Double(800000, 4500000); |
86 |
private Point2D secondPoint = new Point2D.Double(810000, 4500000); |
87 |
private double distance = 10; |
88 |
private double distancePos = java.lang.Double.MAX_VALUE; |
89 |
private LineString distanceLine;
|
90 |
|
91 |
/**
|
92 |
* Crea un nuevo EquidistanceCADTool.
|
93 |
*/
|
94 |
public EquidistanceCADTool() {
|
95 |
} |
96 |
|
97 |
/**
|
98 |
* M?todo de inicio, para poner el c?digo de todo lo que se requiera de una
|
99 |
* carga previa a la utilizaci?n de la herramienta.
|
100 |
*/
|
101 |
public void init() { |
102 |
_fsm = new EquidistanceCADToolContext(this); |
103 |
|
104 |
} |
105 |
|
106 |
private Coordinate[] getParallel(Point2D[] points, double distance) { |
107 |
Point2D[] pper = new Point2D[2]; |
108 |
double angle = Math.toRadians(90) |
109 |
+ UtilFunctions.getAngle(points[0], points[1]); |
110 |
pper[0] = UtilFunctions.getPoint(points[0], angle, distance); |
111 |
pper[1] = UtilFunctions.getPoint(points[1], angle, distance); |
112 |
Coordinate[] result = new Coordinate[2]; |
113 |
result[0] = new Coordinate(pper[0].getX(), pper[0].getY()); |
114 |
result[1] = new Coordinate(pper[1].getX(), pper[1].getY()); |
115 |
return result;
|
116 |
} |
117 |
|
118 |
/*
|
119 |
* (non-Javadoc)
|
120 |
*
|
121 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
|
122 |
* double, double)
|
123 |
*/
|
124 |
public void transition(double x, double y, InputEvent event) { |
125 |
_fsm.addPoint(x, y, event); |
126 |
} |
127 |
|
128 |
/*
|
129 |
* (non-Javadoc)
|
130 |
*
|
131 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
|
132 |
* double)
|
133 |
*/
|
134 |
public void transition(double d) { |
135 |
_fsm.addValue(d); |
136 |
} |
137 |
|
138 |
/*
|
139 |
* (non-Javadoc)
|
140 |
*
|
141 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
|
142 |
* java.lang.String)
|
143 |
*/
|
144 |
public void transition(String s) throws CommandException { |
145 |
if (!super.changeCommand(s)) { |
146 |
_fsm.addOption(s); |
147 |
} |
148 |
} |
149 |
|
150 |
/**
|
151 |
* DOCUMENT ME!
|
152 |
*/
|
153 |
public void selection() { |
154 |
FeatureSet selection = null;
|
155 |
try {
|
156 |
selection = (FeatureSet) getVLE().getFeatureStore().getSelection(); |
157 |
|
158 |
if (selection.getSize() == 0 |
159 |
&& !CADExtension |
160 |
.getCADTool() |
161 |
.getClass() |
162 |
.getName() |
163 |
.equals( |
164 |
"com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
|
165 |
CADExtension.setCADTool("_selection", false); |
166 |
((SelectionCADTool) CADExtension.getCADTool()) |
167 |
.setNextTool("_Equidistance");
|
168 |
} |
169 |
} catch (ReadException e) {
|
170 |
// TODO Auto-generated catch block
|
171 |
e.printStackTrace(); |
172 |
} catch (DataException e) {
|
173 |
// TODO Auto-generated catch block
|
174 |
e.printStackTrace(); |
175 |
} |
176 |
} |
177 |
|
178 |
/**
|
179 |
* Equivale al transition del prototipo pero sin pasarle como par?metro el
|
180 |
* editableFeatureSource que ya estar? creado.
|
181 |
*
|
182 |
* @param x
|
183 |
* par?metro x del punto que se pase en esta transici?n.
|
184 |
* @param y
|
185 |
* par?metro y del punto que se pase en esta transici?n.
|
186 |
*/
|
187 |
public void addPoint(double x, double y, InputEvent event) { |
188 |
EquidistanceCADToolState actualState = (EquidistanceCADToolState) _fsm |
189 |
.getPreviousState(); |
190 |
String status = actualState.getName();
|
191 |
if (status.equals("Equidistance.Distance")) { |
192 |
firstPoint = new Point2D.Double(x, y); |
193 |
} else if (status.equals("Equidistance.SecondPointDistance")) { |
194 |
secondPoint = new Point2D.Double(x, y); |
195 |
distance = secondPoint.distance(firstPoint); |
196 |
} else if (status.equals("Equidistance.Position")) { |
197 |
VectorialLayerEdited vle = getVLE(); |
198 |
FeatureStore featureStore = null;
|
199 |
try {
|
200 |
featureStore = vle.getFeatureStore(); |
201 |
} catch (ReadException e) {
|
202 |
// TODO Auto-generated catch block
|
203 |
e.printStackTrace(); |
204 |
} |
205 |
ArrayList selectedRowAux = new ArrayList(); |
206 |
DisposableIterator iterator = null;
|
207 |
try {
|
208 |
iterator = ((FeatureSelection) featureStore.getSelection()) |
209 |
.iterator(); |
210 |
|
211 |
PluginServices.getMDIManager().setWaitCursor(); |
212 |
featureStore.beginEditingGroup(getName()); |
213 |
while (iterator.hasNext()) {
|
214 |
Feature feature = (Feature) iterator.next(); |
215 |
Geometry geometry = compute(feature, new Point2D.Double(x, |
216 |
y)); |
217 |
insertAndSelectGeometry(geometry); |
218 |
} |
219 |
|
220 |
featureStore.endEditingGroup(); |
221 |
} catch (DataException e) {
|
222 |
// TODO Auto-generated catch block
|
223 |
e.printStackTrace(); |
224 |
} catch (CreateGeometryException e) {
|
225 |
// TODO Auto-generated catch block
|
226 |
e.printStackTrace(); |
227 |
} finally {
|
228 |
if (iterator != null) { |
229 |
iterator.dispose(); |
230 |
} |
231 |
} |
232 |
PluginServices.getMDIManager().restoreCursor(); |
233 |
} |
234 |
} |
235 |
|
236 |
private Geometry compute(Feature fea, Point2D position) throws CreateGeometryException { |
237 |
Geometry geometry = (fea.getDefaultGeometry()) |
238 |
.cloneGeometry(); |
239 |
int typeGeometry = geometry.getType();
|
240 |
com.vividsolutions.jts.geom.Geometry g = null;
|
241 |
try {
|
242 |
g = (com.vividsolutions.jts.geom.Geometry) geometry |
243 |
.invokeOperation(ToJTS.CODE, null);
|
244 |
} catch (GeometryOperationNotSupportedException e) {
|
245 |
// TODO Auto-generated catch block
|
246 |
e.printStackTrace(); |
247 |
} catch (GeometryOperationException e) {
|
248 |
// TODO Auto-generated catch block
|
249 |
e.printStackTrace(); |
250 |
} |
251 |
|
252 |
// com.vividsolutions.jts.geom.Geometry g = geometry.toJTSGeometry();
|
253 |
GeometryFactory factory = g.getFactory(); |
254 |
Coordinate[] c2 = new Coordinate[2]; |
255 |
com.vividsolutions.jts.geom.Geometry g2 = null;
|
256 |
Coordinate coordinatePosition = new Coordinate(position.getX(),
|
257 |
position.getY()); |
258 |
Point pPos = factory.createPoint(coordinatePosition);
|
259 |
switch (typeGeometry) {
|
260 |
case Geometry.TYPES.CURVE:
|
261 |
|
262 |
LineString lR = factory.createLineString(g.getCoordinates()); |
263 |
g = TopologyPreservingSimplifier.simplify(lR, 10d);
|
264 |
com.vividsolutions.jts.geom.Geometry gLine = g.getGeometryN(0);
|
265 |
Coordinate[] coordinatesLine = gLine.getCoordinates();
|
266 |
int numPointsLine = gLine.getNumPoints();
|
267 |
if (numPointsLine < 1) { |
268 |
return null; |
269 |
} |
270 |
LineString[] lineStrings = new LineString[numPointsLine - 1]; |
271 |
|
272 |
for (int j = 1; j < numPointsLine; j = j + 1) { |
273 |
c2[0] = coordinatesLine[j - 1]; |
274 |
c2[1] = coordinatesLine[j];
|
275 |
LineString lS = factory.createLineString(c2); |
276 |
if (lS.distance(pPos) < distancePos) {
|
277 |
distancePos = lS.distance(pPos); |
278 |
distanceLine = (LineString) factory.createGeometry(lS); |
279 |
} |
280 |
} |
281 |
setDistanceLine(coordinatePosition); |
282 |
|
283 |
for (int j = 1; j < numPointsLine; j = j + 1) { |
284 |
c2[0] = coordinatesLine[j - 1]; |
285 |
c2[1] = coordinatesLine[j];
|
286 |
Point2D[] points = new Point2D[2]; |
287 |
points[0] = new Point2D.Double(c2[0].x, c2[0].y); |
288 |
points[1] = new Point2D.Double(c2[1].x, c2[1].y); |
289 |
lineStrings[j - 1] = factory.createLineString(getParallel(
|
290 |
points, distance)); |
291 |
} |
292 |
|
293 |
for (int i = 0; i < lineStrings.length - 1; i++) { |
294 |
Coordinate coord = lineStrings[i].getCoordinateN(0);
|
295 |
Point2D p1 = new Point2D.Double(coord.x, coord.y); |
296 |
coord = lineStrings[i].getCoordinateN(1);
|
297 |
Point2D p2 = new Point2D.Double(coord.x, coord.y); |
298 |
coord = lineStrings[i + 1].getCoordinateN(0); |
299 |
Point2D p3 = new Point2D.Double(coord.x, coord.y); |
300 |
coord = lineStrings[i + 1].getCoordinateN(1); |
301 |
Point2D p4 = new Point2D.Double(coord.x, coord.y); |
302 |
Point2D intersection = UtilFunctions.getIntersection(p1, p2,
|
303 |
p3, p4); |
304 |
Coordinate[] coords1 = new Coordinate[2]; |
305 |
coords1[0] = lineStrings[i].getCoordinateN(0); |
306 |
coords1[1] = new Coordinate(intersection.getX(), intersection |
307 |
.getY()); |
308 |
lineStrings[i] = factory.createLineString(coords1); |
309 |
Coordinate[] coords2 = new Coordinate[2]; |
310 |
coords2[0] = coords1[1]; |
311 |
coords2[1] = lineStrings[i + 1].getCoordinateN(1); |
312 |
lineStrings[i + 1] = factory.createLineString(coords2);
|
313 |
} |
314 |
g2 = factory.createMultiLineString(lineStrings); |
315 |
return Converter.jtsToGeometry(g2);
|
316 |
case Geometry.TYPES.SURFACE:
|
317 |
case Geometry.TYPES.CIRCLE:
|
318 |
case Geometry.TYPES.ELLIPSE:
|
319 |
g = TopologyPreservingSimplifier.simplify(g, 10d);
|
320 |
com.vividsolutions.jts.geom.Geometry gPolygon = g.getGeometryN(0);
|
321 |
setDistancePolygon(gPolygon, pPos); |
322 |
Coordinate[] coordinates = gPolygon.getCoordinates();
|
323 |
int numPointsPolygon = gPolygon.getNumPoints();
|
324 |
if (numPointsPolygon < 2) { |
325 |
return null; |
326 |
} |
327 |
LineString[] polygonStrings = new LineString[numPointsPolygon]; |
328 |
for (int j = 1; j < numPointsPolygon; j = j + 1) { |
329 |
c2[0] = coordinates[j - 1]; |
330 |
c2[1] = coordinates[j];
|
331 |
Point2D[] points = new Point2D[2]; |
332 |
points[0] = new Point2D.Double(c2[0].x, c2[0].y); |
333 |
points[1] = new Point2D.Double(c2[1].x, c2[1].y); |
334 |
polygonStrings[j - 1] = factory.createLineString(getParallel(
|
335 |
points, distance)); |
336 |
} |
337 |
for (int i = 0; i < polygonStrings.length - 2; i++) { |
338 |
Coordinate coord = polygonStrings[i].getCoordinateN(0);
|
339 |
Point2D p1 = new Point2D.Double(coord.x, coord.y); |
340 |
coord = polygonStrings[i].getCoordinateN(1);
|
341 |
Point2D p2 = new Point2D.Double(coord.x, coord.y); |
342 |
coord = polygonStrings[i + 1].getCoordinateN(0); |
343 |
Point2D p3 = new Point2D.Double(coord.x, coord.y); |
344 |
coord = polygonStrings[i + 1].getCoordinateN(1); |
345 |
Point2D p4 = new Point2D.Double(coord.x, coord.y); |
346 |
Point2D intersection = UtilFunctions.getIntersection(p1, p2,
|
347 |
p3, p4); |
348 |
Coordinate[] coords1 = new Coordinate[2]; |
349 |
coords1[0] = polygonStrings[i].getCoordinateN(0); |
350 |
coords1[1] = new Coordinate(intersection.getX(), intersection |
351 |
.getY()); |
352 |
polygonStrings[i] = factory.createLineString(coords1); |
353 |
Coordinate[] coords2 = new Coordinate[2]; |
354 |
coords2[0] = coords1[1]; |
355 |
coords2[1] = polygonStrings[i + 1].getCoordinateN(1); |
356 |
polygonStrings[i + 1] = factory.createLineString(coords2);
|
357 |
} |
358 |
|
359 |
Coordinate coord = polygonStrings[0].getCoordinateN(0); |
360 |
Point2D p1 = new Point2D.Double(coord.x, coord.y); |
361 |
coord = polygonStrings[0].getCoordinateN(1); |
362 |
Point2D p2 = new Point2D.Double(coord.x, coord.y); |
363 |
coord = polygonStrings[polygonStrings.length - 2].getCoordinateN(0); |
364 |
Point2D p3 = new Point2D.Double(coord.x, coord.y); |
365 |
coord = polygonStrings[polygonStrings.length - 2].getCoordinateN(1); |
366 |
Point2D p4 = new Point2D.Double(coord.x, coord.y); |
367 |
|
368 |
Point2D intersection = UtilFunctions
|
369 |
.getIntersection(p1, p2, p3, p4); |
370 |
Coordinate[] coords1 = new Coordinate[2]; |
371 |
coords1[0] = polygonStrings[polygonStrings.length - 2] |
372 |
.getCoordinateN(1);
|
373 |
coords1[1] = new Coordinate(intersection.getX(), intersection |
374 |
.getY()); |
375 |
polygonStrings[polygonStrings.length - 1] = factory
|
376 |
.createLineString(coords1); |
377 |
Coordinate[] coords2 = new Coordinate[2]; |
378 |
coords2[0] = coords1[1]; |
379 |
coords2[1] = polygonStrings[0].getCoordinateN(1); |
380 |
polygonStrings[0] = factory.createLineString(coords2);
|
381 |
com.vividsolutions.jts.geom.Geometry geometryCollection = factory |
382 |
.createGeometryCollection(polygonStrings); |
383 |
LinearRing linearRing = factory.createLinearRing(geometryCollection |
384 |
.getCoordinates()); |
385 |
LinearRing[] holes = new LinearRing[1]; |
386 |
holes[0] = factory.createLinearRing(new Coordinate[0]); |
387 |
g2 = factory.createPolygon(linearRing, holes); |
388 |
return Converter.jtsToGeometry(g2);
|
389 |
|
390 |
case Geometry.TYPES.MULTIPOINT:
|
391 |
case Geometry.TYPES.POINT:
|
392 |
break;
|
393 |
default:
|
394 |
break;
|
395 |
} |
396 |
return null; |
397 |
} |
398 |
|
399 |
private void setDistanceLine(Coordinate position) { |
400 |
Coordinate pStart = distanceLine.getCoordinateN(0);
|
401 |
Coordinate pEnd = distanceLine.getCoordinateN(distanceLine |
402 |
.getNumPoints() - 1);
|
403 |
int pos = CGAlgorithms.computeOrientation(pStart, pEnd, position);
|
404 |
if (pos > 0) { |
405 |
distance = Math.abs(distance);
|
406 |
} else {
|
407 |
distance = -Math.abs(distance);
|
408 |
} |
409 |
distancePos = java.lang.Double.MAX_VALUE; |
410 |
distanceLine = null;
|
411 |
} |
412 |
|
413 |
private void setDistancePolygon( |
414 |
com.vividsolutions.jts.geom.Geometry polygon, |
415 |
com.vividsolutions.jts.geom.Geometry position) { |
416 |
boolean intersects = polygon.intersects(position);
|
417 |
if (intersects) {
|
418 |
distance = -Math.abs(distance);
|
419 |
} else {
|
420 |
distance = Math.abs(distance);
|
421 |
} |
422 |
} |
423 |
|
424 |
/**
|
425 |
* M?todo para dibujar la lo necesario para el estado en el que nos
|
426 |
* encontremos.
|
427 |
*
|
428 |
* @param g
|
429 |
* Graphics sobre el que dibujar.
|
430 |
* @param x
|
431 |
* par?metro x del punto que se pase para dibujar.
|
432 |
* @param y
|
433 |
* par?metro x del punto que se pase para dibujar.
|
434 |
*/
|
435 |
public void drawOperation(Graphics g, double x, double y) { |
436 |
|
437 |
} |
438 |
|
439 |
/**
|
440 |
* Add a diferent option.
|
441 |
*
|
442 |
* @param s
|
443 |
* Diferent option.
|
444 |
*/
|
445 |
public void addOption(String s) { |
446 |
// EquidistanceCADToolState actualState = (EquidistanceCADToolState)
|
447 |
// _fsm
|
448 |
// .getPreviousState();
|
449 |
// String status = actualState.getName();
|
450 |
// if (status.equals("Equidistance.Distance")) {
|
451 |
// distance=java.lang.Double.parseDouble(s);
|
452 |
// }
|
453 |
} |
454 |
|
455 |
/*
|
456 |
* (non-Javadoc)
|
457 |
*
|
458 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
|
459 |
*/
|
460 |
public void addValue(double d) { |
461 |
EquidistanceCADToolState actualState = (EquidistanceCADToolState) _fsm |
462 |
.getPreviousState(); |
463 |
String status = actualState.getName();
|
464 |
if (status.equals("Equidistance.Distance")) { |
465 |
distance = d; |
466 |
} |
467 |
} |
468 |
|
469 |
public String getName() { |
470 |
return PluginServices.getText(this, "Equidistance_"); |
471 |
} |
472 |
|
473 |
public String toString() { |
474 |
return "_Equidistance"; |
475 |
} |
476 |
|
477 |
public boolean isApplicable(int shapeType) { |
478 |
if (shapeType == Geometry.TYPES.POINT
|
479 |
|| shapeType == Geometry.TYPES.MULTIPOINT) { |
480 |
return false; |
481 |
} |
482 |
return true; |
483 |
} |
484 |
} |