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

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu Oct 14 21:49:03 CEST 2010


Author: mojays
Date: 2010-10-14 21:49:03 +0200 (Thu, 14 Oct 2010)
New Revision: 1119

Modified:
   trunk/src/schmitzm/geotools/feature/AttributeModificationRule.java
   trunk/src/schmitzm/geotools/feature/FeatureUtil.java
Log:


Modified: trunk/src/schmitzm/geotools/feature/AttributeModificationRule.java
===================================================================
--- trunk/src/schmitzm/geotools/feature/AttributeModificationRule.java	2010-10-14 19:18:20 UTC (rev 1118)
+++ trunk/src/schmitzm/geotools/feature/AttributeModificationRule.java	2010-10-14 19:49:03 UTC (rev 1119)
@@ -3,7 +3,16 @@
 import java.util.Set;
 
 import org.geotools.data.FeatureSource;
+import org.geotools.feature.AttributeTypeFactory;
+import org.geotools.feature.GeometryAttributeType;
+import org.geotools.feature.NameImpl;
+import org.opengis.feature.type.AttributeDescriptor;
+import org.opengis.feature.type.GeometryDescriptor;
+import org.opengis.feature.type.Name;
+import org.opengis.filter.Filter;
 
+import schmitzm.geotools.FilterUtil;
+
 /**
  * Defines an attribute modification during the 
  * {@link FeatureUtil#modifyFeatureSource(org.geotools.data.FeatureSource, org.geotools.data.AbstractDataStore, AttributeModificationRule...)}
@@ -105,4 +114,47 @@
   public void setNullValues(Set<Object> nullValues) {
     this.nullValues = nullValues;
   }
+  
+  /**
+   * Applies the modifications defined by this {@link AttributeModificationRule} and
+   * creates a new {@link AttributeDescriptor}.
+   */
+  public AttributeDescriptor applyToAttributeType(AttributeDescriptor sourceAttrDescr) {
+    if ( sourceAttrDescr == null )
+      throw new UnsupportedOperationException("Can not create a new AttributeDescriptor from NULL.");
+    
+    // Change the name
+    String newName = getNewAttrName();
+    if ( newName == null )
+      newName = sourceAttrDescr.getLocalName();
+
+    // Change the type
+    Class<?> newClass = getNewAttrClass();
+    if ( newClass == null )
+      newClass = sourceAttrDescr.getType().getBinding();
+  
+    // Transform the default value to the new type
+    Object newDefaultValue = FeatureUtil.transformAttributeValue(sourceAttrDescr.getDefaultValue(),newClass);
+    
+    // User the meta data from source type
+    Object newMetaData = null;
+    // if it is a GeometryAttributeType, the CRS must be stored
+    // in the meta data
+    if ( FeatureUtil.isGeometryAttribute(sourceAttrDescr) )
+      newMetaData = ((GeometryDescriptor) sourceAttrDescr).getCoordinateReferenceSystem();
+
+    // User the meta data from source type:
+    // combine the restrictions of the attribute type for the
+    // AttributeTypeFactory
+    Filter newRestrictions = FilterUtil.FILTER_FAC.and(sourceAttrDescr.getType().getRestrictions());
+
+    return AttributeTypeFactory.newAttributeType(
+        newName,
+        newClass,
+        sourceAttrDescr.isNillable(),
+        newRestrictions,
+        newDefaultValue,
+        newMetaData
+    );
+  }
 }

Modified: trunk/src/schmitzm/geotools/feature/FeatureUtil.java
===================================================================
--- trunk/src/schmitzm/geotools/feature/FeatureUtil.java	2010-10-14 19:18:20 UTC (rev 1118)
+++ trunk/src/schmitzm/geotools/feature/FeatureUtil.java	2010-10-14 19:49:03 UTC (rev 1119)
@@ -104,6 +104,7 @@
 import schmitzm.geotools.GTUtil;
 import schmitzm.lang.LangUtil;
 import schmitzm.lang.ResourceProvider;
+import schmitzm.temp.BaseTypeUtil;
 import skrueger.geotools.StyledFeaturesInterface;
 
 import com.vividsolutions.jts.geom.Geometry;
@@ -696,8 +697,8 @@
 		Object metaData = null;
 		// if it is a GeometryAttributeType, the CRS must be stored
 		// in the meta data
-		if (aType instanceof GeometryAttributeType)
-			metaData = ((GeometryAttributeType) aType).getCoordinateSystem();
+		if (aType instanceof GeometryDescriptor)
+			metaData = ((GeometryDescriptor)aType).getCoordinateReferenceSystem();
 
 		// combine the restrictions of the attribute type for the
 		// AttributeTypeFactory
@@ -2458,16 +2459,65 @@
 	 * specified, are copied to the destination {@link FeatureSource}. So even
 	 * if an attribute should be transfered unchanged, there must be an
 	 * {@link AttributeModificationRule} for it!!
-	 * 
-	 * @param fs source {@link FeatureSource}
+	 * This <b>also</b> applies to the geometry attribute!! 
+	 * @param sourceFs source {@link FeatureSource}
 	 * @param destDataStore destination for the new {@link FeatureSource}
-	 * @param destAttr defines which attributes of the source {@link FeatureSource}
+	 * @param destAttrRule defines which attributes of the source {@link FeatureSource}
 	 *                 are copied in the destination, and (optionally) how the
 	 *                 attributes are modified during this procedure 
 	 */
-	public static void modifyFeatureSource(FeatureSource<SimpleFeatureType,SimpleFeature> fs, AbstractDataStore destDataStore, AttributeModificationRule... destAttr) {
+	public static void modifyFeatureSource(FeatureSource<SimpleFeatureType,SimpleFeature> sourceFs, AbstractDataStore destDataStore, AttributeModificationRule... destAttrRule) {
+	  SimpleFeatureType sType = sourceFs.getSchema();
+      SimpleFeatureTypeBuilder builder = new SimpleFeatureTypeBuilder();
+      builder.setName(sType.getTypeName());
+      
+      //***** Create the new attribute type for the destination FeatureSource *****
+      // Loop the attributes, which should be transfered to the
+      // destination FeatureSource
+      for (AttributeModificationRule rule : destAttrRule) {
+        AttributeDescriptor sDescr = sType.getDescriptor(rule.getAttrIdx());
+        AttributeDescriptor dDescr = rule.applyToAttributeType(sDescr);
+        builder.add(dDescr);
+      }
+      // create the FeatureType for the destination FeatureSource
+      SimpleFeatureType dType = builder.buildFeatureType();
+
+      //***** Create the new FeatureSource *****
+
+      
 	  throw new UnsupportedOperationException("FeatureUtil.modifyFeatureSource(..) not yet implemented!");
 	}
+	
+	/**
+	 * Transforms a value to another type.
+	 * @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
+	 */
+	public static Object transformAttributeValue(Object value, Class<?> destType) {
+	  if ( value == null )
+	    return null;
+	  if ( destType.isInstance(value) )
+	    return value;
+	  
+	  // convert to string --> easy!
+	  if ( String.class.equals(destType) )
+	    return value.toString();
+	  // convert to number
+	  if ( Number.class.isAssignableFrom(destType) ) {
+	    if ( value instanceof Number )
+	      return BaseTypeUtil.convertNumber((Number)value,(Class<Number>)destType);
+        if ( value instanceof String )
+          return BaseTypeUtil.convertFromString((String)value,(Class<Number>)destType);
+	  }
+	  
+	  // TODO: implement more transformation rules
+	  
+	  LOGGER.warn("Can not apply attribute value transformation: "+LangUtil.getSimpleClassName(value)+" --> "+LangUtil.getSimpleClassName(destType)+" (for '"+value+"')");
+	    
+	  return null;
+	}
 
 	// /**
 	// * Extrahiert alle Geometrien aus einer FeatureCollection. Fuer jedes



More information about the Schmitzm-commits mailing list