svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extCAD / src / com / iver / cit / gvsig / gui / cad / tools / EquidistanceCADTool.java @ 24429
History | View | Annotate | Download (15.5 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 com.iver.cit.gvsig.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 |
import java.util.Iterator; |
48 |
|
49 |
import org.gvsig.fmap.data.exceptions.DataException; |
50 |
import org.gvsig.fmap.data.exceptions.ReadException; |
51 |
import org.gvsig.fmap.data.feature.Feature; |
52 |
import org.gvsig.fmap.data.feature.FeatureSelection; |
53 |
import org.gvsig.fmap.data.feature.FeatureSet; |
54 |
import org.gvsig.fmap.data.feature.FeatureStore; |
55 |
import org.gvsig.fmap.geom.Geometry; |
56 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
57 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
58 |
import org.gvsig.fmap.geom.operation.tojts.ToJTS; |
59 |
import org.gvsig.fmap.geom.util.Converter; |
60 |
import org.gvsig.fmap.geom.util.UtilFunctions; |
61 |
|
62 |
import com.iver.andami.PluginServices; |
63 |
import com.iver.cit.gvsig.CADExtension; |
64 |
import com.iver.cit.gvsig.gui.cad.DefaultCADTool; |
65 |
import com.iver.cit.gvsig.gui.cad.exception.CommandException; |
66 |
import com.iver.cit.gvsig.gui.cad.tools.smc.EquidistanceCADToolContext; |
67 |
import com.iver.cit.gvsig.gui.cad.tools.smc.EquidistanceCADToolContext.EquidistanceCADToolState; |
68 |
import com.iver.cit.gvsig.layers.VectorialLayerEdited; |
69 |
import com.vividsolutions.jts.algorithm.CGAlgorithms; |
70 |
import com.vividsolutions.jts.geom.Coordinate; |
71 |
import com.vividsolutions.jts.geom.GeometryFactory; |
72 |
import com.vividsolutions.jts.geom.LineString; |
73 |
import com.vividsolutions.jts.geom.LinearRing; |
74 |
import com.vividsolutions.jts.geom.Point; |
75 |
import com.vividsolutions.jts.simplify.TopologyPreservingSimplifier; |
76 |
|
77 |
/**
|
78 |
* Herramienta para crear una geometr?a equidistante a otra.
|
79 |
*
|
80 |
* @author Vicente Caballero Navarro
|
81 |
*/
|
82 |
public class EquidistanceCADTool extends DefaultCADTool { |
83 |
private EquidistanceCADToolContext _fsm;
|
84 |
private Point2D firstPoint = new Point2D.Double(800000, 4500000); |
85 |
private Point2D secondPoint = new Point2D.Double(810000, 4500000); |
86 |
private double distance = 10; |
87 |
private double distancePos = java.lang.Double.MAX_VALUE; |
88 |
private LineString distanceLine;
|
89 |
|
90 |
/**
|
91 |
* Crea un nuevo EquidistanceCADTool.
|
92 |
*/
|
93 |
public EquidistanceCADTool() {
|
94 |
} |
95 |
|
96 |
/**
|
97 |
* M?todo de inicio, para poner el c?digo de todo lo que se requiera de una
|
98 |
* carga previa a la utilizaci?n de la herramienta.
|
99 |
*/
|
100 |
public void init() { |
101 |
_fsm = new EquidistanceCADToolContext(this); |
102 |
|
103 |
} |
104 |
|
105 |
private Coordinate[] getParallel(Point2D[] points, double distance) { |
106 |
Point2D[] pper = new Point2D[2]; |
107 |
double angle = Math.toRadians(90) |
108 |
+ UtilFunctions.getAngle(points[0], points[1]); |
109 |
pper[0] = UtilFunctions.getPoint(points[0], angle, distance); |
110 |
pper[1] = UtilFunctions.getPoint(points[1], angle, distance); |
111 |
Coordinate[] result = new Coordinate[2]; |
112 |
result[0] = new Coordinate(pper[0].getX(), pper[0].getY()); |
113 |
result[1] = new Coordinate(pper[1].getX(), pper[1].getY()); |
114 |
return result;
|
115 |
} |
116 |
|
117 |
/*
|
118 |
* (non-Javadoc)
|
119 |
*
|
120 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
|
121 |
* double, double)
|
122 |
*/
|
123 |
public void transition(double x, double y, InputEvent event) { |
124 |
_fsm.addPoint(x, y, event); |
125 |
} |
126 |
|
127 |
/*
|
128 |
* (non-Javadoc)
|
129 |
*
|
130 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
|
131 |
* double)
|
132 |
*/
|
133 |
public void transition(double d) { |
134 |
_fsm.addValue(d); |
135 |
} |
136 |
|
137 |
/*
|
138 |
* (non-Javadoc)
|
139 |
*
|
140 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#transition(com.iver.cit.gvsig.fmap.layers.FBitSet,
|
141 |
* java.lang.String)
|
142 |
*/
|
143 |
public void transition(String s) throws CommandException { |
144 |
if (!super.changeCommand(s)) { |
145 |
_fsm.addOption(s); |
146 |
} |
147 |
} |
148 |
|
149 |
/**
|
150 |
* DOCUMENT ME!
|
151 |
*/
|
152 |
public void selection() { |
153 |
FeatureSet selection = null;
|
154 |
try {
|
155 |
selection = (FeatureSet) getVLE().getFeatureStore().getSelection(); |
156 |
|
157 |
if (selection.getSize() == 0 |
158 |
&& !CADExtension |
159 |
.getCADTool() |
160 |
.getClass() |
161 |
.getName() |
162 |
.equals( |
163 |
"com.iver.cit.gvsig.gui.cad.tools.SelectionCADTool")) {
|
164 |
CADExtension.setCADTool("_selection", false); |
165 |
((SelectionCADTool) CADExtension.getCADTool()) |
166 |
.setNextTool("_Equidistance");
|
167 |
} |
168 |
} catch (ReadException e) {
|
169 |
// TODO Auto-generated catch block
|
170 |
e.printStackTrace(); |
171 |
} catch (DataException e) {
|
172 |
// TODO Auto-generated catch block
|
173 |
e.printStackTrace(); |
174 |
} |
175 |
} |
176 |
|
177 |
/**
|
178 |
* Equivale al transition del prototipo pero sin pasarle como par?metro el
|
179 |
* editableFeatureSource que ya estar? creado.
|
180 |
*
|
181 |
* @param x
|
182 |
* par?metro x del punto que se pase en esta transici?n.
|
183 |
* @param y
|
184 |
* par?metro y del punto que se pase en esta transici?n.
|
185 |
*/
|
186 |
public void addPoint(double x, double y, InputEvent event) { |
187 |
EquidistanceCADToolState actualState = (EquidistanceCADToolState) _fsm |
188 |
.getPreviousState(); |
189 |
String status = actualState.getName();
|
190 |
if (status.equals("Equidistance.Distance")) { |
191 |
firstPoint = new Point2D.Double(x, y); |
192 |
} else if (status.equals("Equidistance.SecondPointDistance")) { |
193 |
secondPoint = new Point2D.Double(x, y); |
194 |
distance = secondPoint.distance(firstPoint); |
195 |
} else if (status.equals("Equidistance.Position")) { |
196 |
VectorialLayerEdited vle = getVLE(); |
197 |
FeatureStore featureStore = null;
|
198 |
try {
|
199 |
featureStore = vle.getFeatureStore(); |
200 |
} catch (ReadException e) {
|
201 |
// TODO Auto-generated catch block
|
202 |
e.printStackTrace(); |
203 |
} |
204 |
ArrayList selectedRowAux = new ArrayList(); |
205 |
Iterator iterator = null; |
206 |
try {
|
207 |
iterator = ((FeatureSelection) featureStore.getSelection()) |
208 |
.iterator(); |
209 |
|
210 |
PluginServices.getMDIManager().setWaitCursor(); |
211 |
featureStore.beginEditingGroup(getName()); |
212 |
while (iterator.hasNext()) {
|
213 |
Feature feature = (Feature) iterator.next(); |
214 |
Geometry geometry = compute(feature, new Point2D.Double(x, |
215 |
y)); |
216 |
insertGeometry(geometry); |
217 |
} |
218 |
|
219 |
featureStore.endEditingGroup(); |
220 |
} catch (DataException e) {
|
221 |
// TODO Auto-generated catch block
|
222 |
e.printStackTrace(); |
223 |
} |
224 |
PluginServices.getMDIManager().restoreCursor(); |
225 |
} |
226 |
} |
227 |
|
228 |
private Geometry compute(Feature fea, Point2D position) { |
229 |
Geometry geometry = ((Geometry) fea.getDefaultGeometry()) |
230 |
.cloneGeometry(); |
231 |
int typeGeometry = geometry.getType();
|
232 |
com.vividsolutions.jts.geom.Geometry g = null;
|
233 |
try {
|
234 |
g = (com.vividsolutions.jts.geom.Geometry) geometry |
235 |
.invokeOperation(ToJTS.CODE, null);
|
236 |
} catch (GeometryOperationNotSupportedException e) {
|
237 |
// TODO Auto-generated catch block
|
238 |
e.printStackTrace(); |
239 |
} catch (GeometryOperationException e) {
|
240 |
// TODO Auto-generated catch block
|
241 |
e.printStackTrace(); |
242 |
} |
243 |
|
244 |
// com.vividsolutions.jts.geom.Geometry g = geometry.toJTSGeometry();
|
245 |
GeometryFactory factory = g.getFactory(); |
246 |
Coordinate[] c2 = new Coordinate[2]; |
247 |
com.vividsolutions.jts.geom.Geometry g2 = null;
|
248 |
Coordinate coordinatePosition = new Coordinate(position.getX(),
|
249 |
position.getY()); |
250 |
Point pPos = factory.createPoint(coordinatePosition);
|
251 |
switch (typeGeometry) {
|
252 |
case Geometry.TYPES.CURVE:
|
253 |
|
254 |
LineString lR = factory.createLineString(g.getCoordinates()); |
255 |
g = TopologyPreservingSimplifier.simplify(lR, 10d);
|
256 |
com.vividsolutions.jts.geom.Geometry gLine = g.getGeometryN(0);
|
257 |
Coordinate[] coordinatesLine = gLine.getCoordinates();
|
258 |
int numPointsLine = gLine.getNumPoints();
|
259 |
if (numPointsLine < 1) |
260 |
return null; |
261 |
LineString[] lineStrings = new LineString[numPointsLine - 1]; |
262 |
|
263 |
for (int j = 1; j < numPointsLine; j = j + 1) { |
264 |
c2[0] = coordinatesLine[j - 1]; |
265 |
c2[1] = coordinatesLine[j];
|
266 |
LineString lS = factory.createLineString(c2); |
267 |
if (lS.distance(pPos) < distancePos) {
|
268 |
distancePos = lS.distance(pPos); |
269 |
distanceLine = (LineString) factory.createGeometry(lS); |
270 |
} |
271 |
} |
272 |
setDistanceLine(coordinatePosition); |
273 |
|
274 |
for (int j = 1; j < numPointsLine; j = j + 1) { |
275 |
c2[0] = coordinatesLine[j - 1]; |
276 |
c2[1] = coordinatesLine[j];
|
277 |
Point2D[] points = new Point2D[2]; |
278 |
points[0] = new Point2D.Double(c2[0].x, c2[0].y); |
279 |
points[1] = new Point2D.Double(c2[1].x, c2[1].y); |
280 |
lineStrings[j - 1] = factory.createLineString(getParallel(
|
281 |
points, distance)); |
282 |
} |
283 |
|
284 |
for (int i = 0; i < lineStrings.length - 1; i++) { |
285 |
Coordinate coord = lineStrings[i].getCoordinateN(0);
|
286 |
Point2D p1 = new Point2D.Double(coord.x, coord.y); |
287 |
coord = lineStrings[i].getCoordinateN(1);
|
288 |
Point2D p2 = new Point2D.Double(coord.x, coord.y); |
289 |
coord = lineStrings[i + 1].getCoordinateN(0); |
290 |
Point2D p3 = new Point2D.Double(coord.x, coord.y); |
291 |
coord = lineStrings[i + 1].getCoordinateN(1); |
292 |
Point2D p4 = new Point2D.Double(coord.x, coord.y); |
293 |
Point2D intersection = UtilFunctions.getIntersection(p1, p2,
|
294 |
p3, p4); |
295 |
Coordinate[] coords1 = new Coordinate[2]; |
296 |
coords1[0] = lineStrings[i].getCoordinateN(0); |
297 |
coords1[1] = new Coordinate(intersection.getX(), intersection |
298 |
.getY()); |
299 |
lineStrings[i] = factory.createLineString(coords1); |
300 |
Coordinate[] coords2 = new Coordinate[2]; |
301 |
coords2[0] = coords1[1]; |
302 |
coords2[1] = lineStrings[i + 1].getCoordinateN(1); |
303 |
lineStrings[i + 1] = factory.createLineString(coords2);
|
304 |
} |
305 |
g2 = factory.createMultiLineString(lineStrings); |
306 |
return Converter.jtsToGeometry(g2);
|
307 |
case Geometry.TYPES.SURFACE:
|
308 |
case Geometry.TYPES.CIRCLE:
|
309 |
case Geometry.TYPES.ELLIPSE:
|
310 |
g = TopologyPreservingSimplifier.simplify(g, 10d);
|
311 |
com.vividsolutions.jts.geom.Geometry gPolygon = g.getGeometryN(0);
|
312 |
setDistancePolygon(gPolygon, pPos); |
313 |
Coordinate[] coordinates = gPolygon.getCoordinates();
|
314 |
int numPointsPolygon = gPolygon.getNumPoints();
|
315 |
if (numPointsPolygon < 2) |
316 |
return null; |
317 |
LineString[] polygonStrings = new LineString[numPointsPolygon]; |
318 |
for (int j = 1; j < numPointsPolygon; j = j + 1) { |
319 |
c2[0] = coordinates[j - 1]; |
320 |
c2[1] = coordinates[j];
|
321 |
Point2D[] points = new Point2D[2]; |
322 |
points[0] = new Point2D.Double(c2[0].x, c2[0].y); |
323 |
points[1] = new Point2D.Double(c2[1].x, c2[1].y); |
324 |
polygonStrings[j - 1] = factory.createLineString(getParallel(
|
325 |
points, distance)); |
326 |
} |
327 |
for (int i = 0; i < polygonStrings.length - 2; i++) { |
328 |
Coordinate coord = polygonStrings[i].getCoordinateN(0);
|
329 |
Point2D p1 = new Point2D.Double(coord.x, coord.y); |
330 |
coord = polygonStrings[i].getCoordinateN(1);
|
331 |
Point2D p2 = new Point2D.Double(coord.x, coord.y); |
332 |
coord = polygonStrings[i + 1].getCoordinateN(0); |
333 |
Point2D p3 = new Point2D.Double(coord.x, coord.y); |
334 |
coord = polygonStrings[i + 1].getCoordinateN(1); |
335 |
Point2D p4 = new Point2D.Double(coord.x, coord.y); |
336 |
Point2D intersection = UtilFunctions.getIntersection(p1, p2,
|
337 |
p3, p4); |
338 |
Coordinate[] coords1 = new Coordinate[2]; |
339 |
coords1[0] = polygonStrings[i].getCoordinateN(0); |
340 |
coords1[1] = new Coordinate(intersection.getX(), intersection |
341 |
.getY()); |
342 |
polygonStrings[i] = factory.createLineString(coords1); |
343 |
Coordinate[] coords2 = new Coordinate[2]; |
344 |
coords2[0] = coords1[1]; |
345 |
coords2[1] = polygonStrings[i + 1].getCoordinateN(1); |
346 |
polygonStrings[i + 1] = factory.createLineString(coords2);
|
347 |
} |
348 |
|
349 |
Coordinate coord = polygonStrings[0].getCoordinateN(0); |
350 |
Point2D p1 = new Point2D.Double(coord.x, coord.y); |
351 |
coord = polygonStrings[0].getCoordinateN(1); |
352 |
Point2D p2 = new Point2D.Double(coord.x, coord.y); |
353 |
coord = polygonStrings[polygonStrings.length - 2].getCoordinateN(0); |
354 |
Point2D p3 = new Point2D.Double(coord.x, coord.y); |
355 |
coord = polygonStrings[polygonStrings.length - 2].getCoordinateN(1); |
356 |
Point2D p4 = new Point2D.Double(coord.x, coord.y); |
357 |
|
358 |
Point2D intersection = UtilFunctions
|
359 |
.getIntersection(p1, p2, p3, p4); |
360 |
Coordinate[] coords1 = new Coordinate[2]; |
361 |
coords1[0] = polygonStrings[polygonStrings.length - 2] |
362 |
.getCoordinateN(1);
|
363 |
coords1[1] = new Coordinate(intersection.getX(), intersection |
364 |
.getY()); |
365 |
polygonStrings[polygonStrings.length - 1] = factory
|
366 |
.createLineString(coords1); |
367 |
Coordinate[] coords2 = new Coordinate[2]; |
368 |
coords2[0] = coords1[1]; |
369 |
coords2[1] = polygonStrings[0].getCoordinateN(1); |
370 |
polygonStrings[0] = factory.createLineString(coords2);
|
371 |
com.vividsolutions.jts.geom.Geometry geometryCollection = factory |
372 |
.createGeometryCollection(polygonStrings); |
373 |
LinearRing linearRing = factory.createLinearRing(geometryCollection |
374 |
.getCoordinates()); |
375 |
LinearRing[] holes = new LinearRing[1]; |
376 |
holes[0] = factory.createLinearRing(new Coordinate[0]); |
377 |
g2 = factory.createPolygon(linearRing, holes); |
378 |
return Converter.jtsToGeometry(g2);
|
379 |
|
380 |
case Geometry.TYPES.MULTIPOINT:
|
381 |
case Geometry.TYPES.POINT:
|
382 |
break;
|
383 |
default:
|
384 |
break;
|
385 |
} |
386 |
return null; |
387 |
} |
388 |
|
389 |
private void setDistanceLine(Coordinate position) { |
390 |
Coordinate pStart = distanceLine.getCoordinateN(0);
|
391 |
Coordinate pEnd = distanceLine.getCoordinateN(distanceLine |
392 |
.getNumPoints() - 1);
|
393 |
int pos = CGAlgorithms.computeOrientation(pStart, pEnd, position);
|
394 |
if (pos > 0) |
395 |
distance = Math.abs(distance);
|
396 |
else
|
397 |
distance = -Math.abs(distance);
|
398 |
distancePos = java.lang.Double.MAX_VALUE; |
399 |
distanceLine = null;
|
400 |
} |
401 |
|
402 |
private void setDistancePolygon( |
403 |
com.vividsolutions.jts.geom.Geometry polygon, |
404 |
com.vividsolutions.jts.geom.Geometry position) { |
405 |
boolean intersects = polygon.intersects(position);
|
406 |
if (intersects)
|
407 |
distance = -Math.abs(distance);
|
408 |
else
|
409 |
distance = Math.abs(distance);
|
410 |
} |
411 |
|
412 |
/**
|
413 |
* M?todo para dibujar la lo necesario para el estado en el que nos
|
414 |
* encontremos.
|
415 |
*
|
416 |
* @param g
|
417 |
* Graphics sobre el que dibujar.
|
418 |
* @param x
|
419 |
* par?metro x del punto que se pase para dibujar.
|
420 |
* @param y
|
421 |
* par?metro x del punto que se pase para dibujar.
|
422 |
*/
|
423 |
public void drawOperation(Graphics g, double x, double y) { |
424 |
|
425 |
} |
426 |
|
427 |
/**
|
428 |
* Add a diferent option.
|
429 |
*
|
430 |
* @param s
|
431 |
* Diferent option.
|
432 |
*/
|
433 |
public void addOption(String s) { |
434 |
// EquidistanceCADToolState actualState = (EquidistanceCADToolState)
|
435 |
// _fsm
|
436 |
// .getPreviousState();
|
437 |
// String status = actualState.getName();
|
438 |
// if (status.equals("Equidistance.Distance")) {
|
439 |
// distance=java.lang.Double.parseDouble(s);
|
440 |
// }
|
441 |
} |
442 |
|
443 |
/*
|
444 |
* (non-Javadoc)
|
445 |
*
|
446 |
* @see com.iver.cit.gvsig.gui.cad.CADTool#addvalue(double)
|
447 |
*/
|
448 |
public void addValue(double d) { |
449 |
EquidistanceCADToolState actualState = (EquidistanceCADToolState) _fsm |
450 |
.getPreviousState(); |
451 |
String status = actualState.getName();
|
452 |
if (status.equals("Equidistance.Distance")) { |
453 |
distance = d; |
454 |
} |
455 |
} |
456 |
|
457 |
public String getName() { |
458 |
return PluginServices.getText(this, "Equidistance_"); |
459 |
} |
460 |
|
461 |
public String toString() { |
462 |
return "_Equidistance"; |
463 |
} |
464 |
|
465 |
public boolean isApplicable(int shapeType) { |
466 |
if (shapeType == Geometry.TYPES.POINT
|
467 |
|| shapeType == Geometry.TYPES.MULTIPOINT) { |
468 |
return false; |
469 |
} |
470 |
return true; |
471 |
} |
472 |
} |