Revision 29723 trunk/extensions/extCAD/src/com/iver/cit/gvsig/gui/cad/tools/SplitGeometryCADTool.java

View differences:

SplitGeometryCADTool.java
92 92
import com.vividsolutions.jts.geom.GeometryCollection;
93 93
import com.vividsolutions.jts.geom.GeometryFactory;
94 94
import com.vividsolutions.jts.geom.LineString;
95
import com.vividsolutions.jts.geom.MultiLineString;
96
import com.vividsolutions.jts.geom.MultiPoint;
97
import com.vividsolutions.jts.geom.MultiPolygon;
98
import com.vividsolutions.jts.geom.Point;
99
import com.vividsolutions.jts.geom.Polygon;
95 100
import com.vividsolutions.jts.geom.PrecisionModel;
96 101

  
97 102

  
......
213 218
		} catch (ReadDriverException e1) {
214 219
			e1.printStackTrace();
215 220
		}
221

  
216 222
		for (int i = 0; i < auxSelectedRows.size(); i++) {
217 223
			editedRow = (IRowEdited) auxSelectedRows.get(i);
218 224
			IFeature feat = (IFeature) editedRow.getLinkedRow().cloneRow();
......
229 235
				indices.add(new Integer(editedRow.getIndex()));
230 236
				//and then, we add new features for each split geometry
231 237
				GeometryCollection gc = (GeometryCollection)splitGeo;
232
				for(int j = 0; j < gc.getNumGeometries(); j++){
233
					Geometry g = gc.getGeometryN(j);
234
					IGeometry fmapGeo = FConverter.jts_to_igeometry(g);
238
				ArrayList<Geometry> geoms0=new ArrayList<Geometry>();
239
				ArrayList<Geometry> geoms1=new ArrayList<Geometry>();
240
				if (gc.getNumGeometries()>2){
241
					Geometry[] splitGroups=createSplitGroups(jtsGeo,splittingLs);
242
					if (splitGroups.length==0){
243
						continue;
244
					}
245

  
246
					for(int j = 0; j < gc.getNumGeometries(); j++){
247
						Geometry g = gc.getGeometryN(j);
248
						if (splitGroups[0].contains(g)){
249
							geoms0.add(g);
250
						}else{
251
							geoms1.add(g);
252
						}
253
					}
254
				}else if (gc.getNumGeometries()==2){
255
					geoms0.add(gc.getGeometryN(0));
256
					geoms1.add(gc.getGeometryN(1));
257
				}else{
258
					continue;
259
				}
260
				GeometryCollection gc0=createMulti(geoms0,gc.getFactory());
261

  
262
					IGeometry fmapGeo = FConverter.jts_to_igeometry(gc0);
235 263
					DefaultFeature df = null;
236 264
					int newIdx = 0;
237
					if (j==0){
265
//					if (j==0){
238 266
						newIdx=editedRow.getIndex();
239 267
						try {
240 268
							df = new DefaultFeature(fmapGeo, feat.getAttributes(),feat.getID());
......
247 275
						} catch (ReadDriverException e) {
248 276
							NotificationManager.addError(e.getMessage(),e);
249 277
						}
250
					}else{
251

  
278
//					}else{
279
						GeometryCollection gc1=createMulti(geoms1,gc.getFactory());
280
						fmapGeo = FConverter.jts_to_igeometry(gc1);
252 281
						try {
253 282
							String newFID = vea.getNewFID();
254 283
							df = new DefaultFeature(fmapGeo, feat.getAttributes(),newFID);
......
265 294
						} catch (ReadDriverException e) {
266 295
							NotificationManager.addError(e);
267 296
						}
268
					}
297
//					}
269 298
					DefaultRowEdited newRowEdited = new DefaultRowEdited(df,
270 299
								IRowEdited.STATUS_ADDED,
271 300
									newIdx);
272 301
					vle.addSelectionCache(newRowEdited);
273
				}//for j
302
//				}//for j
274 303
			}//if splitGeo
275 304
			} catch (Exception ex) {
276 305
				PluginServices.getLogger().error("Error splitting geom "+editedRow.getIndex(), ex);
......
281 310
		getCadToolAdapter().getMapControl().getMapContext().endAtomicEvent();
282 311
	}
283 312

  
313
	private GeometryCollection createMulti(ArrayList<Geometry> geoms,GeometryFactory factory) {
314
		if (geoms.size()==0)
315
			return null;
316
		if (geoms.get(0) instanceof Polygon){
317
			return new MultiPolygon((Polygon[])geoms.toArray(new Polygon[0]),factory);
318
		}else if (geoms.get(0) instanceof LineString){
319
			return new MultiLineString((LineString[])geoms.toArray(new LineString[0]),factory);
320
		}else if (geoms.get(0) instanceof Point){
321
			return new MultiPoint((Point[])geoms.toArray(new Point[0]),factory);
322
		}
323
		return null;
324
	}
325

  
326

  
327
	private Geometry[] createSplitGroups(Geometry splitGeo, LineString splittingLs) {
328
		try{
329
		Geometry[] geomsJTS=new Geometry[2];
330
		Geometry r=splitGeo.getEnvelope();
331
		Geometry splitG = SplitStrategy.splitOp(r, splittingLs);
332
		if(splitG instanceof GeometryCollection
333
				&& ((GeometryCollection)splitG).getNumGeometries()>1){
334
			GeometryCollection gc = (GeometryCollection)splitG;
335
			for(int j = 0; j < gc.getNumGeometries(); j++){
336
				geomsJTS[j]=gc.getGeometryN(j);
337
			}
338
		}
339
		return geomsJTS;
340
		}catch (Exception e) {
341
			NotificationManager.showMessageError(PluginServices.getText(this, "line_not_cross_rectangle"), e);
342
		}
343
		return null;
344
	}
345

  
284 346
	public void end(){
285 347
		getCadToolAdapter().refreshEditedLayer();
286 348
		init();
287 349
	}
288 350

  
289

  
290 351
	public void addOption(String s) {
291 352
		State actualState = _fsm.getPreviousState();
292 353
		String status = actualState.getName();
......
351 412

  
352 413

  
353 414
	public void drawOperation(Graphics g, double x, double y) {
354
		State actualState = _fsm.getState();
355
        String status = actualState.getName();
415
		try{
416
			State actualState = _fsm.getState();
417
			String status = actualState.getName();
418
			drawRectangleOfSplit((Graphics2D) g);
419
			// draw splitting line
420
			if ((status.equals("SplitGeometry.DigitizingLine"))) {
421
				drawPolyLine((Graphics2D) g, x, y);
422
			}
423
		}catch (Exception e) {
424
			// TODO: handle exception
425
		}
426
		// draw selection
427
		try {
428
			Image imgSel = getVLE().getSelectionImage();
429
			if (imgSel != null)
430
				g.drawImage(imgSel, 0, 0, null);
431
		} catch (Exception e) {
432
			PluginServices.getLogger().error("Error drawing Editing Selection", e);
433
		}
434
	}
356 435

  
357
        // draw splitting line
358
        if ((status.equals("SplitGeometry.DigitizingLine"))) {
359
        	drawPolyLine((Graphics2D) g, x, y);
360
         }
436
	private void drawRectangleOfSplit(Graphics2D g) {
437
		ArrayList selectedRows = getSelectedRows();
438
		for (int i = 0; i < selectedRows.size(); i++) {
439
			IRowEdited editedRow = (IRowEdited) selectedRows.get(i);
440
			IFeature feat = (IFeature) editedRow.getLinkedRow();
441
			IGeometry ig = feat.getGeometry();
442
			Geometry jtsG=ig.toJTSGeometry();
443
			if (jtsG !=null && jtsG instanceof GeometryCollection && jtsG.getNumGeometries()>1){
444
				Rectangle2D r=ig.getBounds2D();
445
				GeneralPathX gpx = new GeneralPathX();
446
				gpx.moveTo(r.getX(), r.getY());
447
				gpx.lineTo(r.getMaxX(), r.getY());
448
				gpx.lineTo(r.getMaxX(), r.getMaxY());
449
				gpx.lineTo(r.getX(), r.getMaxY());
450
				gpx.closePath();
451
				ShapeFactory.createPolygon2D(gpx).draw((Graphics2D) g,
452
						getCadToolAdapter().getMapControl().getViewPort(),
453
						DefaultCADTool.axisReferencesSymbol);
454
			}
455
		}
361 456

  
362
        // draw selection
363
        try {
364
        	Image imgSel = getVLE().getSelectionImage();
365
        	if (imgSel != null)
366
        		g.drawImage(imgSel, 0, 0, null);
367
        } catch (Exception e) {
368
        	PluginServices.getLogger().error("Error drawing Editing Selection", e);
369
        }
370 457
	}
371 458

  
459

  
372 460
	public String getName() {
373 461
		return PluginServices.getText(this, "split_geometry_shell");
374 462
	}
......
393 481
			_fsm.addOption(s);
394 482
		}
395 483
	}
396

  
397 484
}

Also available in: Unified diff