svn-gvsig-desktop / branches / v2_0_0_prep / extensions / extEditing / src / com / iver / cit / gvsig / gui / cad / tools / SplitGeometryCADTool.java @ 28980
History | View | Annotate | Download (11 KB)
1 |
/*
|
---|---|
2 |
* Created on 10-abr-2006
|
3 |
*
|
4 |
* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
5 |
*
|
6 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
7 |
*
|
8 |
* This program is free software; you can redistribute it and/or
|
9 |
* modify it under the terms of the GNU General Public License
|
10 |
* as published by the Free Software Foundation; either version 2
|
11 |
* of the License, or (at your option) any later version.
|
12 |
*
|
13 |
* This program is distributed in the hope that it will be useful,
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16 |
* GNU General Public License for more details.
|
17 |
*
|
18 |
* You should have received a copy of the GNU General Public License
|
19 |
* along with this program; if not, write to the Free Software
|
20 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
21 |
*
|
22 |
* For more information, contact:
|
23 |
*
|
24 |
* Generalitat Valenciana
|
25 |
* Conselleria d'Infraestructures i Transport
|
26 |
* Av. Blasco Ib??ez, 50
|
27 |
* 46010 VALENCIA
|
28 |
* SPAIN
|
29 |
*
|
30 |
* +34 963862235
|
31 |
* gvsig@gva.es
|
32 |
* www.gvsig.gva.es
|
33 |
*
|
34 |
* or
|
35 |
*
|
36 |
* IVER T.I. S.A
|
37 |
* Salamanca 50
|
38 |
* 46005 Valencia
|
39 |
* Spain
|
40 |
*
|
41 |
* +34 963163400
|
42 |
* dac@iver.es
|
43 |
*/
|
44 |
/* CVS MESSAGES:
|
45 |
*
|
46 |
* $Id:
|
47 |
* $Log:
|
48 |
*/
|
49 |
package com.iver.cit.gvsig.gui.cad.tools; |
50 |
|
51 |
import java.awt.Graphics; |
52 |
import java.awt.Graphics2D; |
53 |
import java.awt.Image; |
54 |
import java.awt.event.InputEvent; |
55 |
import java.awt.event.MouseEvent; |
56 |
import java.awt.geom.Point2D; |
57 |
import java.util.ArrayList; |
58 |
import java.util.Iterator; |
59 |
import java.util.List; |
60 |
|
61 |
import org.apache.log4j.Logger; |
62 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
63 |
import org.gvsig.fmap.dal.feature.Feature; |
64 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
65 |
import org.gvsig.fmap.geom.GeometryLocator; |
66 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
67 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
68 |
import org.gvsig.fmap.geom.operation.Draw; |
69 |
import org.gvsig.fmap.geom.operation.DrawOperationContext; |
70 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
71 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
72 |
import org.gvsig.fmap.geom.primitive.Envelope; |
73 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
74 |
import org.gvsig.fmap.geom.util.Converter; |
75 |
import org.gvsig.fmap.mapcontext.layers.SpatialCache; |
76 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
77 |
import org.gvsig.tools.locator.LocatorException; |
78 |
|
79 |
import statemap.State; |
80 |
|
81 |
import com.iver.andami.PluginServices; |
82 |
import com.iver.cit.gvsig.gui.cad.DefaultCADTool; |
83 |
import com.iver.cit.gvsig.gui.cad.exception.CommandException; |
84 |
import com.iver.cit.gvsig.gui.cad.tools.smc.SplitGeometryCADToolContext; |
85 |
import com.iver.cit.gvsig.gui.cad.tools.split.SplitStrategy; |
86 |
import com.iver.cit.gvsig.layers.VectorialLayerEdited; |
87 |
import com.vividsolutions.jts.geom.Coordinate; |
88 |
import com.vividsolutions.jts.geom.Geometry; |
89 |
import com.vividsolutions.jts.geom.GeometryCollection; |
90 |
import com.vividsolutions.jts.geom.GeometryFactory; |
91 |
import com.vividsolutions.jts.geom.LineString; |
92 |
import com.vividsolutions.jts.geom.PrecisionModel; |
93 |
|
94 |
|
95 |
/**
|
96 |
* CAD Tool which splits the selected geometries of a vectorial editing
|
97 |
* layer with a digitized polyline.
|
98 |
*
|
99 |
*
|
100 |
* @author Alvaro Zabala
|
101 |
*
|
102 |
*/
|
103 |
public class SplitGeometryCADTool extends DefaultCADTool { |
104 |
|
105 |
private static Logger logger = Logger.getLogger(SplitGeometryCADTool.class.getName()); |
106 |
|
107 |
/**
|
108 |
* String representation of this tool (used for example to active the tool
|
109 |
* in mapcontrol)
|
110 |
*/
|
111 |
public static final String SPLIT_GEOMETRY_TOOL_NAME = "_split_geometry"; |
112 |
|
113 |
|
114 |
|
115 |
/**
|
116 |
* finite state machine for this CAD tool
|
117 |
*/
|
118 |
protected SplitGeometryCADToolContext _fsm;
|
119 |
|
120 |
|
121 |
|
122 |
/**
|
123 |
* Flag to mark if the digitized line has been finished.
|
124 |
*/
|
125 |
protected boolean digitizingFinished = false; |
126 |
|
127 |
/**
|
128 |
* Collection of digitized geometries
|
129 |
*/
|
130 |
protected List<Point2D> clickedPoints; |
131 |
|
132 |
|
133 |
/**
|
134 |
* Default Constructor
|
135 |
*/
|
136 |
public SplitGeometryCADTool(){
|
137 |
|
138 |
} |
139 |
|
140 |
|
141 |
/**
|
142 |
* Initialization method.
|
143 |
*/
|
144 |
public void init() { |
145 |
digitizingFinished = false;
|
146 |
_fsm = new SplitGeometryCADToolContext(this); |
147 |
setNextTool(SplitGeometryCADTool.SPLIT_GEOMETRY_TOOL_NAME); |
148 |
} |
149 |
|
150 |
|
151 |
|
152 |
public boolean isDigitizingFinished() { |
153 |
return digitizingFinished;
|
154 |
} |
155 |
|
156 |
public String toString() { |
157 |
return SplitGeometryCADTool.SPLIT_GEOMETRY_TOOL_NAME;
|
158 |
} |
159 |
|
160 |
|
161 |
|
162 |
public void finishDigitizedLine(){ |
163 |
} |
164 |
// public ArrayList getSelectedRows(){
|
165 |
// VectorialLayerEdited vle = getVLE();
|
166 |
// ArrayList selection = vle.getSelectedRow();
|
167 |
// if( selection.size() == 0){
|
168 |
// VectorialEditableAdapter vea = vle.getVEA();
|
169 |
// try {
|
170 |
// FBitSet bitset = vea.getSelection();
|
171 |
// for (int j = bitset.nextSetBit(0);j >= 0;j = bitset.nextSetBit(j + 1)){
|
172 |
// IRowEdited rowEd = vea.getRow(j);
|
173 |
// selection.add(rowEd);
|
174 |
// }
|
175 |
// } catch (ExpansionFileReadException e) {
|
176 |
// e.printStackTrace();
|
177 |
// } catch (ReadDriverException e) {
|
178 |
// e.printStackTrace();
|
179 |
// }
|
180 |
// }//selection size
|
181 |
// return selection;
|
182 |
// }
|
183 |
// public ArrayList getSelectedRows(){
|
184 |
// return (ArrayList) CADUtil.getSelectedFeatures(getVLE());
|
185 |
// }
|
186 |
public Coordinate[] getPoint2DAsCoordinates(Point2D[] point2d) { |
187 |
Coordinate[] solution = new Coordinate[point2d.length]; |
188 |
for (int i = 0; i < point2d.length; i++) { |
189 |
solution[i] = new Coordinate(point2d[i].getX(), point2d[i].getY());
|
190 |
} |
191 |
return solution;
|
192 |
} |
193 |
public void splitSelectedGeometryWithDigitizedLine(){ |
194 |
Point2D[] clickedPts = new Point2D[this.clickedPoints.size()]; |
195 |
clickedPoints.toArray(clickedPts); |
196 |
Coordinate[] digitizedCoords = getPoint2DAsCoordinates(clickedPts);
|
197 |
LineString splittingLs = new GeometryFactory(
|
198 |
new PrecisionModel(10000)).createLineString(digitizedCoords); |
199 |
try {
|
200 |
VectorialLayerEdited vle = getVLE(); |
201 |
FeatureStore store=vle.getFeatureStore(); |
202 |
Iterator<Feature> selected = store.getFeatureSelection().iterator();
|
203 |
|
204 |
getCadToolAdapter().getMapControl().getMapContext().beginAtomicEvent(); |
205 |
store.beginEditingGroup(getName()); |
206 |
while (selected.hasNext()) {
|
207 |
Feature feature = (Feature) selected.next(); |
208 |
org.gvsig.fmap.geom.Geometry ig = feature.getDefaultGeometry(); |
209 |
Geometry jtsGeo = Converter.geometryToJts(ig); |
210 |
if (jtsGeo==null) |
211 |
return;
|
212 |
Geometry splitGeo = SplitStrategy.splitOp(jtsGeo, splittingLs); |
213 |
if(splitGeo instanceof GeometryCollection |
214 |
&& ((GeometryCollection)splitGeo).getNumGeometries()>1){
|
215 |
|
216 |
GeometryCollection gc = (GeometryCollection)splitGeo; |
217 |
for(int j = 0; j < gc.getNumGeometries(); j++){ |
218 |
Geometry g = gc.getGeometryN(j); |
219 |
org.gvsig.fmap.geom.Geometry fmapGeo = Converter.jtsToGeometry(g); |
220 |
|
221 |
if (j==0){ |
222 |
EditableFeature eFeature=feature.getEditable(); |
223 |
eFeature.setGeometry(store.getDefaultFeatureType().getDefaultGeometryAttributeName(), fmapGeo); |
224 |
store.update(eFeature); |
225 |
}else{
|
226 |
|
227 |
EditableFeature newFeature=store.createNewFeature(store.getDefaultFeatureType(), feature); |
228 |
newFeature.setGeometry(store.getDefaultFeatureType().getDefaultGeometryAttributeName(), fmapGeo); |
229 |
store.insert(newFeature); |
230 |
SpatialCache spatialCache=((FLyrVect)vle.getLayer()).getSpatialCache(); |
231 |
Envelope envelope = fmapGeo.getEnvelope(); |
232 |
spatialCache.insert(envelope,fmapGeo); |
233 |
} |
234 |
}//for j
|
235 |
}//if splitGeo
|
236 |
|
237 |
} |
238 |
|
239 |
store.endEditingGroup(); |
240 |
|
241 |
getCadToolAdapter().getMapControl().getMapContext().endAtomicEvent(); |
242 |
} catch (Exception ex) { |
243 |
PluginServices.getLogger().error("Error splitting geom", ex);
|
244 |
} |
245 |
} |
246 |
|
247 |
public void end(){ |
248 |
getCadToolAdapter().refreshEditedLayer(); |
249 |
init(); |
250 |
} |
251 |
|
252 |
|
253 |
public void addOption(String s) { |
254 |
State actualState = _fsm.getPreviousState(); |
255 |
String status = actualState.getName();
|
256 |
if (s.equals(PluginServices.getText(this, "cancel"))) { |
257 |
init(); |
258 |
return;
|
259 |
} |
260 |
if (status.equals("TopologicalEdition.FirstPoint")) { |
261 |
return;
|
262 |
} |
263 |
init(); |
264 |
|
265 |
} |
266 |
|
267 |
public void addPoint(double x, double y, InputEvent event) { |
268 |
|
269 |
State actualState = _fsm.getPreviousState(); |
270 |
String status = actualState.getName();
|
271 |
if (status.equals("SplitGeometry.FirstPoint")) { |
272 |
clickedPoints = new ArrayList<Point2D>(); |
273 |
clickedPoints.add(new Point2D.Double(x,y)); |
274 |
} else if (status.equals("SplitGeometry.DigitizingLine")) { |
275 |
clickedPoints.add(new Point2D.Double(x,y)); |
276 |
if (event != null && ((MouseEvent) event).getClickCount() == 2) { |
277 |
digitizingFinished = true;
|
278 |
finishDigitizedLine(); |
279 |
splitSelectedGeometryWithDigitizedLine(); |
280 |
end(); |
281 |
} |
282 |
} |
283 |
} |
284 |
|
285 |
public void addValue(double d) { |
286 |
} |
287 |
|
288 |
|
289 |
/**
|
290 |
* Draws a polyline with the clicked digitized points in the specified graphics.
|
291 |
*
|
292 |
* @param g2 graphics on to draw the polyline
|
293 |
* @param x last x mouse pointer position
|
294 |
* @param y last y mouse pointer position
|
295 |
*/
|
296 |
protected void drawPolyLine(Graphics2D g, double x, double y) { |
297 |
GeneralPathX gpx = |
298 |
new GeneralPathX(GeneralPathX.WIND_EVEN_ODD,
|
299 |
clickedPoints.size()); |
300 |
Point2D firstPoint = clickedPoints.get(0); |
301 |
gpx.moveTo(firstPoint.getX(), firstPoint.getY()); |
302 |
for (int i = 1; i < clickedPoints.size(); i++) { |
303 |
Point2D clickedPoint = clickedPoints.get(i);
|
304 |
gpx.lineTo(clickedPoint.getX(), clickedPoint.getY()); |
305 |
|
306 |
} |
307 |
gpx.lineTo(x, y); |
308 |
org.gvsig.fmap.geom.Geometry geom; |
309 |
try {
|
310 |
geom = GeometryLocator.getGeometryManager().createCurve(gpx, SUBTYPES.GEOM2D); |
311 |
DrawOperationContext doc=new DrawOperationContext();
|
312 |
doc.setGraphics((Graphics2D)g);
|
313 |
doc.setViewPort(getCadToolAdapter().getMapControl().getViewPort()); |
314 |
doc.setSymbol(DefaultCADTool.geometrySelectSymbol); |
315 |
geom.invokeOperation(Draw.CODE,doc); |
316 |
} catch (GeometryOperationNotSupportedException e) {
|
317 |
e.printStackTrace(); |
318 |
} catch (GeometryOperationException e) {
|
319 |
e.printStackTrace(); |
320 |
} catch (LocatorException e) {
|
321 |
e.printStackTrace(); |
322 |
} catch (CreateGeometryException e) {
|
323 |
e.printStackTrace(); |
324 |
} |
325 |
} |
326 |
|
327 |
|
328 |
|
329 |
|
330 |
public void drawOperation(Graphics g, double x, double y) { |
331 |
State actualState = _fsm.getState(); |
332 |
String status = actualState.getName();
|
333 |
|
334 |
// draw splitting line
|
335 |
if ((status.equals("SplitGeometry.DigitizingLine"))) { |
336 |
drawPolyLine((Graphics2D) g, x, y);
|
337 |
} |
338 |
|
339 |
// draw selection
|
340 |
try {
|
341 |
Image imgSel = getVLE().getSelectionImage();
|
342 |
if (imgSel != null) |
343 |
g.drawImage(imgSel, 0, 0, null); |
344 |
} catch (Exception e) { |
345 |
PluginServices.getLogger().error("Error drawing Editing Selection", e);
|
346 |
} |
347 |
} |
348 |
|
349 |
public String getName() { |
350 |
return PluginServices.getText(this, "split_geometry_shell"); |
351 |
} |
352 |
|
353 |
|
354 |
public void transition(double x, double y, InputEvent event) { |
355 |
try {
|
356 |
_fsm.addPoint(x, y, event); |
357 |
} catch (Exception e) { |
358 |
init(); |
359 |
} |
360 |
|
361 |
} |
362 |
|
363 |
|
364 |
public void transition(double d) { |
365 |
_fsm.addValue(d); |
366 |
} |
367 |
|
368 |
public void transition(String s) throws CommandException { |
369 |
if (!super.changeCommand(s)) { |
370 |
_fsm.addOption(s); |
371 |
} |
372 |
} |
373 |
|
374 |
} |