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