Revision 545 org.gvsig.geoprocess/trunk/org.gvsig.geoprocess/org.gvsig.geoprocess.algorithm/org.gvsig.geoprocess.algorithm.base/src/main/java/org/gvsig/geoprocess/algorithm/base/core/DALFeaturePersister.java

View differences:

DALFeaturePersister.java
65 65
	private FeatureStore              store             = null;
66 66
	private String[]                  fieldNames        = null;
67 67
	private GeometryManager           geometryManager   = null;
68
	
68

  
69 69
	/**
70 70
	 * Sets the output FeatureType
71 71
	 * @param out
72
	 * @throws DataException 
72
	 * @throws DataException
73 73
	 */
74 74
	public DALFeaturePersister(FeatureStore out, String[] fieldNames) {
75 75
		this.store = out;
......
82 82
		}
83 83
		geometryManager = GeometryLocator.getGeometryManager();
84 84
	}
85
	
85

  
86 86
	/**
87
	 * Gets the list of field names 
87
	 * Gets the list of field names
88 88
	 * @return
89 89
	 */
90 90
	public String[] getFieldNames() {
......
100 100
		}
101 101
		return null;
102 102
	}
103
	
103

  
104 104
	/**
105
	 * Gets the list of field names 
105
	 * Gets the list of field names
106 106
	 * @return
107 107
	 */
108 108
	public String[] getFieldNamesWithoutGeom() {
......
115 115
					break;
116 116
				}
117 117
			}
118
			
118

  
119 119
			int cont = 0;
120 120
			String[] fieldNames = new String[nFields];
121 121
			for (int i = 0; i < attrs.length; i++) {
......
130 130
		}
131 131
		return null;
132 132
	}
133
	
133

  
134 134
	/**
135 135
	 * Adds a JTS feature to the FeatureStore
136 136
	 * @param entry
......
138 138
	 * @throws CreateGeometryException
139 139
	 * @throws DataException
140 140
	 */
141
	public EditableFeature addFeature(Feature feature1, Feature feature2, Geometry newGeom) 
141
	public EditableFeature addFeature(Feature feature1, Feature feature2, Geometry newGeom)
142 142
		throws CreateGeometryException, DataException {
143
		
143

  
144 144
		int outPutType = getOutputFeatureStore().getDefaultFeatureType()
145 145
								.getDefaultGeometryAttribute().getGeomType().getType();
146
		
146

  
147 147
		//Si el objeto que viene es un GeometryCollection hay que convertirlo a una lista de geometrias
148 148
		//solo del tipo que pueda salvarse en la capa eliminando el resto. Esto sucede sobre todo en la intersecci?n
149 149
		//que devuelve tipos mezclados. Cada uno debe salvarse en el tipo de capa de salida apropiado
......
156 156
			Sextante.addErrorToLog(e1);
157 157
		}
158 158
		org.gvsig.fmap.geom.Geometry newDalGeom = null;
159
		
160
		//Si lo que venia no era un GeometryCollection se convierte a geometr?a de DAL 
159

  
160
		//Si lo que venia no era un GeometryCollection se convierte a geometr?a de DAL
161 161
		if(dalGeomList == null) {
162 162
			GeometryOperationContext ctx = new GeometryOperationContext();
163 163
			ctx.setAttribute(FromJTS.PARAM, newGeom);
......
171 171
				return null;
172 172
			}
173 173
		}
174
		
174

  
175 175
		EditableFeature feat = store.createNewFeature();
176
		
176

  
177 177
		int globalIndex = 0;
178 178
		int localIndex = 0;
179 179
		Object value = feature1.get(localIndex);
......
182 182
			value = feature1.get(++localIndex);
183 183
			globalIndex ++;
184 184
		}
185
		
185

  
186 186
		localIndex = 0;
187 187
		value = feature2.get(localIndex);
188 188
		while(value != null && !(value instanceof org.gvsig.fmap.geom.Geometry)) {
......
206 206
		}
207 207
		return null;
208 208
	}
209
	
210
	public EditableFeature addFeature(Feature feature1, Feature feature2, org.gvsig.fmap.geom.Geometry newDalGeom) 
209

  
210
	public EditableFeature addFeature(Feature feature1, Feature feature2, org.gvsig.fmap.geom.Geometry newDalGeom)
211 211
		throws CreateGeometryException, DataException {
212 212
		EditableFeature feat = store.createNewFeature();
213
		
213

  
214 214
		int globalIndex = 0;
215 215
		int localIndex = 0;
216 216
		Object value = feature1.get(localIndex);
......
219 219
			value = feature1.get(++localIndex);
220 220
			globalIndex ++;
221 221
		}
222
		
222

  
223 223
		localIndex = 0;
224 224
		value = feature2.get(localIndex);
225 225
		while(value != null && !(value instanceof org.gvsig.fmap.geom.Geometry)) {
......
235 235
		}
236 236
		return null;
237 237
	}
238
	
238

  
239 239
	/**
240 240
	 * Converts a geometry collection from JTS to a list of geometries of the selected type.
241
	 * The GeometryCollection is converted only when it is a heterogeneous collection of geometries 
241
	 * The GeometryCollection is converted only when it is a heterogeneous collection of geometries
242 242
	 * @param col
243 243
	 * @param type
244 244
	 * @return
245
	 * @throws CreateGeometryException 
246
	 * @throws GeometryOperationException 
247
	 * @throws GeometryOperationNotSupportedException 
245
	 * @throws CreateGeometryException
246
	 * @throws GeometryOperationException
247
	 * @throws GeometryOperationNotSupportedException
248 248
	 */
249 249
	public ArrayList<org.gvsig.fmap.geom.Geometry> convertGeometryCollection2GeometryList(Geometry g, int type) throws GeometryOperationNotSupportedException, GeometryOperationException {
250
		if( g instanceof GeometryCollection && 
251
			!(g instanceof com.vividsolutions.jts.geom.MultiLineString) && 
252
			!(g instanceof com.vividsolutions.jts.geom.MultiPoint) && 
250
		if( g instanceof GeometryCollection &&
251
			!(g instanceof com.vividsolutions.jts.geom.MultiLineString) &&
252
			!(g instanceof com.vividsolutions.jts.geom.MultiPoint) &&
253 253
			!(g instanceof com.vividsolutions.jts.geom.MultiPolygon)) {
254 254
			GeometryCollection col = (GeometryCollection)g;
255 255
			ArrayList<org.gvsig.fmap.geom.Geometry> geometries = new ArrayList<org.gvsig.fmap.geom.Geometry>();
256
			
256

  
257 257
			if(type == TYPES.MULTIPOINT || type == TYPES.POINT) {
258 258
				for (int i = 0; i < col.getNumGeometries(); i++) {
259 259
					Geometry gx = col.getGeometryN(i);
260
					if( gx instanceof com.vividsolutions.jts.geom.Point || 
260
					if( gx instanceof com.vividsolutions.jts.geom.Point ||
261 261
						gx instanceof com.vividsolutions.jts.geom.MultiPoint) {
262 262
						GeometryOperationContext ctx = new GeometryOperationContext();
263 263
						ctx.setAttribute(FromJTS.PARAM, gx);
......
266 266
					}
267 267
				}
268 268
			}
269
			
269

  
270 270
			if(type == TYPES.MULTICURVE || type == TYPES.CURVE) {
271 271
				for (int i = 0; i < col.getNumGeometries(); i++) {
272 272
					Geometry gx = col.getGeometryN(i);
273
					if( gx instanceof com.vividsolutions.jts.geom.LineString || 
273
					if( gx instanceof com.vividsolutions.jts.geom.LineString ||
274 274
						gx instanceof com.vividsolutions.jts.geom.MultiLineString) {
275 275
						GeometryOperationContext ctx = new GeometryOperationContext();
276 276
						ctx.setAttribute(FromJTS.PARAM, gx);
......
282 282
			if(type == TYPES.MULTISURFACE || type == TYPES.SURFACE) {
283 283
				for (int i = 0; i < col.getNumGeometries(); i++) {
284 284
					Geometry gx = col.getGeometryN(i);
285
					if( gx instanceof com.vividsolutions.jts.geom.Polygon || 
285
					if( gx instanceof com.vividsolutions.jts.geom.Polygon ||
286 286
						gx instanceof com.vividsolutions.jts.geom.MultiPolygon) {
287 287
						GeometryOperationContext ctx = new GeometryOperationContext();
288 288
						ctx.setAttribute(FromJTS.PARAM, gx);
......
295 295
		}
296 296
		return null;
297 297
	}
298
	
298

  
299 299
	/**
300 300
	 * Converts a geometry into a multigeometry
301 301
	 * @param g
......
306 306
		if(list.size() == 0)
307 307
			return null;
308 308
		MultiPrimitive primitive = null;
309
		
309

  
310 310
		if(list.get(0) instanceof Point || list.get(0) instanceof MultiPoint) {
311 311
			primitive = (MultiPrimitive)geometryManager.create(TYPES.MULTIPOINT, SUBTYPES.GEOM2D);
312 312
		}
......
316 316
		if(list.get(0) instanceof Surface || list.get(0) instanceof MultiSurface) {
317 317
			primitive = (MultiPrimitive)geometryManager.create(TYPES.MULTISURFACE, SUBTYPES.GEOM2D);
318 318
		}
319
		
319

  
320 320
		for (int i = 0; i < list.size(); i++) {
321 321
			org.gvsig.fmap.geom.Geometry geom = list.get(i);
322 322
			if(geom instanceof MultiPrimitive) {
......
326 326
				}
327 327
			} else
328 328
				primitive.addPrimitive((Primitive)list.get(i));
329
		}	
330
		
329
		}
330

  
331 331
		return primitive;
332 332
	}
333
	
333

  
334 334
	/**
335 335
	 * Converts a geometry into a multigeometry
336 336
	 * @param g
......
357 357
		}
358 358
		return g;
359 359
	}
360
	
360

  
361 361
	/**
362 362
	 * Checks if the type of the feature store and the geometry are compatibles.
363 363
	 * @param store
......
368 368
	private boolean acceptType(FeatureStore store, org.gvsig.fmap.geom.Geometry geom) throws ReadException {
369 369
		int fType = getShapeType(store);
370 370
		int gType = geom.getType();
371
		
371

  
372 372
		if(fType == gType)
373 373
			return true;
374 374
		if( fType == org.gvsig.fmap.geom.Geometry.TYPES.MULTICURVE &&
......
407 407
		} catch (GeometryOperationException e1) {
408 408
			Sextante.addErrorToLog(e1);
409 409
		}
410
		
410

  
411 411
		//No era un GeometryCollection
412 412
		if(dalGeomList == null) {
413 413
			org.gvsig.fmap.geom.Geometry newDalGeom = null;
......
428 428
		}
429 429
		return null;
430 430
	}
431
	
431

  
432 432
	private EditableFeature insertFeatureIntoStore(Feature feature, org.gvsig.fmap.geom.Geometry newDalGeom) throws DataException {
433 433
		if(acceptType(store, newDalGeom)) {
434 434
			EditableFeature feat = store.createNewFeature(store.getDefaultFeatureType(), true);
......
451 451
		}
452 452
		return null;
453 453
	}
454
	
454

  
455 455
	/**
456 456
	 * Adds a JTS feature to the FeatureStore
457 457
	 * @param entry
......
472 472
		}
473 473
		return null;
474 474
	}
475
	
475

  
476 476
	/**
477 477
	 * Adds a JTS feature to the FeatureStore
478 478
	 * @param entry
......
482 482
	 */
483 483
	public EditableFeature addFeature(Feature feature, List<Geometry> newJTSListGeom) throws CreateGeometryException, DataException {
484 484
		List<org.gvsig.fmap.geom.Geometry> newDalListGeom = new ArrayList<org.gvsig.fmap.geom.Geometry>();
485
		
485

  
486 486
		for (int i = 0; i < newJTSListGeom.size(); i++) {
487 487
			newDalListGeom.add(GeometryUtil.jtsToGeom(newJTSListGeom.get(i)));
488 488
		}
489
		
489

  
490 490
		org.gvsig.fmap.geom.Geometry newMultiGeom = convertGeometryList2MultiGeometry(newDalListGeom);
491 491
		insertFeatureIntoStore(feature, newMultiGeom);
492 492
		return null;
493 493
	}
494
	
494

  
495 495
	/**
496 496
	 * Adds a JTS feature to the FeatureStore
497 497
	 * @param entry
......
511 511
		}
512 512
		return null;
513 513
	}
514
	
514

  
515 515
	/**
516 516
	 * Adds a geometry feature to the FeatureStore
517 517
	 * @param entry
......
529 529
		}
530 530
		return null;
531 531
	}
532
	
532

  
533 533
	/**
534 534
	 * Adds a geometry feature to the FeatureStore
535 535
	 * @param entry
......
541 541
		if(newGeom != null && acceptType(store, newGeom)) {
542 542
			EditableFeature feat = store.createNewFeature();
543 543
			for (int i = 0; i < values.size(); i++) {
544
				feat.set(names.get(i), values.get(i));				
544
				feat.set(names.get(i), values.get(i));
545 545
			}
546 546
			feat.setGeometry("GEOMETRY", convertGeometry2MultiGeometry(newGeom));
547 547
			store.insert(feat);
......
549 549
		}
550 550
		return null;
551 551
	}
552
	
552

  
553 553
	/**
554 554
	 * Adds a JTS feature to the FeatureStore
555 555
	 * @param entry
......
563 563
		if(newDalGeom != null && acceptType(store, newDalGeom)) {
564 564
			EditableFeature feat = store.createNewFeature();
565 565
			for (int i = 0; i < values.size(); i++) {
566
				feat.set(names.get(i), values.get(i));				
566
				feat.set(names.get(i), values.get(i));
567 567
			}
568 568
			feat.setGeometry("GEOMETRY", convertGeometry2MultiGeometry(newDalGeom));
569 569
			store.insert(feat);
......
571 571
		}
572 572
		return null;
573 573
	}
574
	
574

  
575 575
	/**
576 576
	 * Adds a JTS feature to the FeatureStore
577 577
	 * @param entry
......
593 593
		}
594 594
		return null;
595 595
	}
596
	
596

  
597 597
	/**
598
	 * Adds a DAL feature to the FeatureStore when the field structure in the new feature 
598
	 * Adds a DAL feature to the FeatureStore when the field structure in the new feature
599 599
	 * is different to the source field structure.
600 600
	 * @param entry
601 601
	 * @param newGeom
......
606 606
		if(acceptType(store, newGeom)) {
607 607
			//Builds a new empty feature
608 608
			EditableFeature feat = store.createNewFeature(store.getDefaultFeatureType(), true);
609
			
609

  
610 610
			//Writes only values with the same name and data type in both features
611 611
			for (int i = 0; i < fieldNames.length; i++) {
612 612
				if(feature.getType().getIndex(fieldNames[i]) != -1) {
......
618 618
					}
619 619
				}
620 620
			}
621
			
621

  
622 622
			//Sets the geometry
623 623
			feat.setGeometry(fieldNames.length, convertGeometry2MultiGeometry(newGeom));
624 624
			store.insert(feat);
......
626 626
		}
627 627
		return null;
628 628
	}
629
	
629

  
630 630
	/**
631 631
	 * Adds a DAL feature to the FeatureStore
632 632
	 * @param entry
......
637 637
	public EditableFeature addFeature(Feature feature, org.gvsig.fmap.geom.Geometry newGeom) throws CreateGeometryException, DataException {
638 638
		if(acceptType(store, newGeom)) {
639 639
			EditableFeature feat = store.createNewFeature(store.getDefaultFeatureType(), feature);
640
			feat.setGeometry(fieldNames.length, convertGeometry2MultiGeometry(newGeom));
640
			org.gvsig.fmap.geom.Geometry newMultiGeom = convertGeometry2MultiGeometry(newGeom);
641
			if (acceptType(store, newMultiGeom)){
642
				feat.setGeometry(fieldNames.length, newMultiGeom);
643
			} else {
644
				feat.setGeometry(fieldNames.length, newGeom);
645
			}
641 646
			store.insert(feat);
642 647
			return feat;
643 648
		}
644 649
		return null;
645 650
	}
646
	
651

  
647 652
	/**
648 653
	 * Adds a DAL feature to the FeatureStore
649 654
	 * @param entry
......
653 658
	 */
654 659
	public EditableFeature addGeometryToExistingFeature(EditableFeature feature, org.gvsig.fmap.geom.Geometry newGeom) throws CreateGeometryException, DataException {
655 660
		//Y esto para que c... sirve?
656
		for (int i = 0; i < fieldNames.length; i++) 
661
		for (int i = 0; i < fieldNames.length; i++)
657 662
			feature.set(fieldNames[i], feature.get(i));
658
		
663

  
659 664
		if(acceptType(store, newGeom)) {
660 665
			feature.setGeometry(fieldNames.length, newGeom);
661 666
			store.update(feature);
......
663 668
		}
664 669
		return null;
665 670
	}
666
	
671

  
667 672
	/**
668 673
	 * Ends the edition and closes the FeatureStore
669 674
	 */
......
675 680
		}
676 681
		store.dispose();
677 682
	}
678
	
683

  
679 684
	/**
680 685
	 * Gets the shape type of the selected feature store
681 686
	 * @param FeatureStore source
......
693 698
		int indexGeom = featureType.getDefaultGeometryAttributeIndex();
694 699
		return featureType.getAttributeDescriptor(indexGeom).getGeometryType();
695 700
	}
696
	
701

  
697 702
	@SuppressWarnings("deprecation")
698 703
	public int getType() {
699 704
		if(store == null)
......
707 712
		int indexGeom = featureType.getDefaultGeometryAttributeIndex();
708 713
		return featureType.getAttributeDescriptor(indexGeom).getGeometryType();
709 714
	}
710
	
715

  
711 716
	public boolean isCompatibleType(org.gvsig.fmap.geom.Geometry geom) {
712 717
		if(store == null)
713 718
			return false;
......
717 722
			return false;
718 723
		}
719 724
	}
720
	
725

  
721 726
	/**
722 727
	 * Gets the output FeatureStore
723 728
	 * @return

Also available in: Unified diff