[Schmitzm-commits] r1121 - trunk/src/schmitzm/geotools/feature

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu Oct 14 22:22:59 CEST 2010


Author: mojays
Date: 2010-10-14 22:22:59 +0200 (Thu, 14 Oct 2010)
New Revision: 1121

Modified:
   trunk/src/schmitzm/geotools/feature/FeatureUtil.java
Log:
First version of FeatureUtil.modifyFeatureSource(.); NOT YET TESTED!


Modified: trunk/src/schmitzm/geotools/feature/FeatureUtil.java
===================================================================
--- trunk/src/schmitzm/geotools/feature/FeatureUtil.java	2010-10-14 19:58:54 UTC (rev 1120)
+++ trunk/src/schmitzm/geotools/feature/FeatureUtil.java	2010-10-14 20:22:59 UTC (rev 1121)
@@ -64,6 +64,8 @@
 import org.geotools.data.DataUtilities;
 import org.geotools.data.DefaultQuery;
 import org.geotools.data.FeatureSource;
+import org.geotools.data.FeatureWriter;
+import org.geotools.data.Transaction;
 import org.geotools.data.memory.MemoryDataStore;
 import org.geotools.factory.CommonFactoryFinder;
 import org.geotools.factory.GeoTools;
@@ -2466,7 +2468,7 @@
 	 *                 are copied in the destination, and (optionally) how the
 	 *                 attributes are modified during this procedure 
 	 */
-	public static void modifyFeatureSource(FeatureSource<SimpleFeatureType,SimpleFeature> sourceFs, AbstractDataStore destDataStore, AttributeModificationRule... destAttrRule) {
+	public static void modifyFeatureSource(FeatureSource<SimpleFeatureType,SimpleFeature> sourceFs, AbstractDataStore destDataStore, AttributeModificationRule... destAttrRule) throws IOException {
 	  SimpleFeatureType sType = sourceFs.getSchema();
       SimpleFeatureTypeBuilder typebuilder = new SimpleFeatureTypeBuilder();
       typebuilder.setName(sType.getTypeName());
@@ -2483,9 +2485,30 @@
       SimpleFeatureType dType = typebuilder.buildFeatureType();
 
       //***** Create the new FeatureSource *****
-      SimpleFeatureBuilder featureBuilder = getFeatureBuilderFromCache(dType, false);
+      destDataStore.createSchema(dType);
+      FeatureWriter<SimpleFeatureType, SimpleFeature> fWriter = destDataStore.getFeatureWriter(dType.getTypeName(), Transaction.AUTO_COMMIT);
+      FeatureCollection<SimpleFeatureType, SimpleFeature> sourceFc = sourceFs.getFeatures();
+      FeatureIterator<SimpleFeature> fIter = sourceFc.features();
+      try {
+        for (SimpleFeature sFeature = null; fIter.hasNext(); ) {
+          // Determine source feature
+          sFeature = fIter.next();
+          // Create destination feature
+          SimpleFeature dFeature = fWriter.next();
+          // Set the values in the destination feature
+          for (int destAttrIdx=0; destAttrIdx < destAttrRule.length; destAttrIdx++) {
+            AttributeModificationRule rule = destAttrRule[destAttrIdx];
+            Object sValue = sFeature.getAttribute(rule.getAttrIdx());
+            Object dValue = transformAttributeValue(sValue, rule.getNewAttrClass());
+            dFeature.setAttribute(destAttrIdx, dValue);
+          }
+          // write the destination feature
+          fWriter.write();
+        }
+      } finally {
+        sourceFc.close(fIter);
+      }
       
-      
 	  LOGGER.error("FeatureUtil.modifyFeatureSource(..) not yet implemented!");
 	  
 	}
@@ -2495,11 +2518,14 @@
 	 * @param value a value
 	 * @param destType destination type
 	 * @return {@code null} if {@code value} is {@code null} or if the transformation can
-	 *         not be applied
+	 *         not be applied; the {@code value} itself if no destination type is given or
+	 *         {@code value} is already in instance of the destination type 
 	 */
 	public static Object transformAttributeValue(Object value, Class<?> destType) {
 	  if ( value == null )
 	    return null;
+	  if ( destType == null )
+	    return value;
 	  if ( destType.isInstance(value) )
 	    return value;
 	  



More information about the Schmitzm-commits mailing list