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