[Schmitzm-commits] r732 - in trunk/src/schmitzm/geotools: feature styling

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Sun Feb 28 17:52:36 CET 2010


Author: alfonx
Date: 2010-02-28 17:52:35 +0100 (Sun, 28 Feb 2010)
New Revision: 732

Modified:
   trunk/src/schmitzm/geotools/feature/FeatureUtil.java
   trunk/src/schmitzm/geotools/styling/StylingUtil.java
Log:
AtlasStyler SLD parsing: Improved validation & correction of property names used in a Style against the existing Schema.

Modified: trunk/src/schmitzm/geotools/feature/FeatureUtil.java
===================================================================
--- trunk/src/schmitzm/geotools/feature/FeatureUtil.java	2010-02-28 15:15:35 UTC (rev 731)
+++ trunk/src/schmitzm/geotools/feature/FeatureUtil.java	2010-02-28 16:52:35 UTC (rev 732)
@@ -2151,8 +2151,73 @@
 		return getWrappedGeoObject((FeatureSource<SimpleFeatureType, SimpleFeature>) mapLayer
 				.getFeatureSource());
 	}
+	
+	/**
+	 * Checks whether the given attribute local name exists in the schema. If no
+	 * exact match is found, an ingnoreCase match is performed. If the schema
+	 * has no attributes, an exception is thrown.
+	 * 
+	 * @param schema
+	 *            SimpleFeatureType to check against
+	 * @param name
+	 *            localname of an attribte to look for
+	 * 
+	 * @return If no match is found, the first numeric attribute is returned.If no numeric attribute exists returns null;
+	 */
+	public static Name findBestMatchingAttributeFallBackFirstNumeric(
+			SimpleFeatureType schema, String localName) {
 
+		Name bestmatch = findBestMatchingAttribute(schema, localName);
+
+		if (bestmatch == null) {
+			Vector<String> numericalFieldNames = getNumericalFieldNames(schema);
+			if (!numericalFieldNames.isEmpty())
+				bestmatch = new NameImpl(numericalFieldNames.get(0));
+		}
+		return bestmatch;
+	}
+
+
 	/**
+	 * @return A list of numerical field names (withouth any null or "")
+	 */
+	public static Vector<String> getNumericalFieldNames(
+			final SimpleFeatureType schema) {
+		return getNumericalFieldNames(schema, false);
+	}
+
+	/**
+	 * @return A list of fields that can be used for a quantification (e.g.
+	 *         numerical fields)
+	 * 
+	 * @param empty
+	 *            if <code>true</code>, the last entry of this list will always
+	 *            be a virtual 0/"" value.
+	 */
+	public static Vector<String> getNumericalFieldNames(
+			final SimpleFeatureType schema, boolean empty) {
+		final Vector<String> numericalFieldNames = new Vector<String>();
+
+		final List<AttributeDescriptor> attributeDescs = schema
+				.getAttributeDescriptors();
+
+		for (final AttributeDescriptor pd : attributeDescs) {
+			final Class<?> c = pd.getType().getBinding();
+
+			if (c == Integer.class || c == Double.class || c == Byte.class
+					|| c == Float.class || c == Short.class || c == Long.class) {
+				numericalFieldNames.add(pd.getLocalName());
+			}
+		}
+
+		if (empty)
+			numericalFieldNames.add(0, "");
+
+		return numericalFieldNames;
+	}
+
+
+	/**
 	 * Checks whether the given attribute local name exists in the schema. If no
 	 * exact match is found, an ingnoreCase match is performed. If the schema
 	 * has no attributes, an exception is thrown.

Modified: trunk/src/schmitzm/geotools/styling/StylingUtil.java
===================================================================
--- trunk/src/schmitzm/geotools/styling/StylingUtil.java	2010-02-28 15:15:35 UTC (rev 731)
+++ trunk/src/schmitzm/geotools/styling/StylingUtil.java	2010-02-28 16:52:35 UTC (rev 732)
@@ -66,10 +66,17 @@
 import org.geotools.factory.GeoTools;
 import org.geotools.feature.FeatureCollection;
 import org.geotools.feature.GeometryAttributeType;
+import org.geotools.filter.AndImpl;
 import org.geotools.filter.BinaryComparisonAbstract;
 import org.geotools.filter.ConstantExpression;
 import org.geotools.filter.FilterAttributeExtractor;
 import org.geotools.filter.IsBetweenImpl;
+import org.geotools.filter.IsNullImpl;
+import org.geotools.filter.NotImpl;
+import org.geotools.filter.OrImpl;
+import org.geotools.filter.expression.DivideImpl;
+import org.geotools.filter.function.FilterFunction_strConcat;
+import org.geotools.filter.function.math.FilterFunction_abs;
 import org.geotools.geometry.jts.ReferencedEnvelope;
 import org.geotools.renderer.lite.RendererUtilities;
 import org.geotools.resources.i18n.Vocabulary;
@@ -1084,10 +1091,10 @@
 	public static final boolean saveStyleToSLD(Style origStyle,
 			File exportFile, Charset charset) throws TransformerException,
 			IOException {
-	    // Wenn Datei nicht mit .sld endet, die Dateierweiterung
-	    // anhängen
-	    exportFile = IOUtil.appendFileExt(exportFile, ".sld");
-	  
+		// Wenn Datei nicht mit .sld endet, die Dateierweiterung
+		// anhängen
+		exportFile = IOUtil.appendFileExt(exportFile, ".sld");
+
 		SLDTRANSFORMER.setIndentation(2);
 		// LOGGER.debug("default charset = "+ Charset.defaultCharset());
 		// SLDTRANSFORMER.setEncoding(Charset.forName("UTF-8"));
@@ -2460,6 +2467,7 @@
 				super.visit(sym);
 			};
 
+			@Override
 			public void visit(LineSymbolizer sym) {
 				sym.setGeometryPropertyName(FeatureUtil
 						.findBestMatchingAttributeFallBackFirst(schema,
@@ -2467,6 +2475,7 @@
 				super.visit(sym);
 			}
 
+			@Override
 			public void visit(PolygonSymbolizer sym) {
 				sym.setGeometryPropertyName(FeatureUtil
 						.findBestMatchingAttributeFallBackFirst(schema,
@@ -2474,6 +2483,7 @@
 				super.visit(sym);
 			}
 
+			@Override
 			public void visit(PointSymbolizer sym) {
 				sym.setGeometryPropertyName(FeatureUtil
 						.findBestMatchingAttributeFallBackFirst(schema,
@@ -2486,28 +2496,42 @@
 				sym.setGeometryPropertyName(FeatureUtil
 						.findBestMatchingAttributeFallBackFirst(schema,
 								sym.getGeometryPropertyName()).getLocalPart());
+				if (sym.getLabel() != null)
+					sym.setLabel(copy(sym.getLabel()));
+				
+				if (sym.getPriority() != null)
+					sym.setPriority(copy(sym.getPriority()));
+				
 
-				if (sym.getLabel() instanceof PropertyName) {
-					PropertyName propertyname = (PropertyName) sym.getLabel();
-					sym.setLabel(ff.property(FeatureUtil
-							.findBestMatchingAttributeFallBackFirst(schema,
-									propertyname.getPropertyName())));
-				}
+//				if (sym.getPriority() instanceof PropertyName) {
+//					PropertyName propertyname = (PropertyName) sym
+//							.getPriority();
+//					sym.setPriority(ff.property(FeatureUtil
+//							.findBestMatchingAttributeFallBackFirst(schema,
+//									propertyname.getPropertyName())));
+//				}
 
-				if (sym.getPriority() instanceof PropertyName) {
-					PropertyName propertyname = (PropertyName) sym
-							.getPriority();
-					sym.setPriority(ff.property(FeatureUtil
-							.findBestMatchingAttributeFallBackFirst(schema,
-									propertyname.getPropertyName())));
-				}
-
 				super.visit(sym);
 			}
-
+			
 			@Override
 			public Expression copy(Expression expression) {
-				if (expression instanceof PropertyName) {
+				if (expression instanceof FilterFunction_strConcat) {
+					FilterFunction_strConcat strConcat = (FilterFunction_strConcat) expression;
+					
+					List<Expression> children = strConcat.getParameters();
+					List<Expression> correctedChildren = new ArrayList<Expression>();
+					for (Expression o : children) {
+						correctedChildren.add( copy(o));
+					}
+					strConcat.setParameters(correctedChildren);
+					
+				} else
+				if (expression instanceof DivideImpl) {
+					DivideImpl divFilter = (DivideImpl) expression;
+					divFilter.setExpression1(copy(divFilter.getExpression1()));
+					divFilter.setExpression2(copy(divFilter.getExpression2()));
+				} else if (expression instanceof PropertyName) {
 					PropertyName pName = (PropertyName) expression;
 
 					Name correctedName = FeatureUtil
@@ -2516,23 +2540,45 @@
 
 					return ff.property(correctedName);
 				}
-				// if (expression instanceof AttributeExpression) {
-				// AttributeExpression aExpression = (AttributeExpression)
-				// expression;
-				// Name correctedName = FeatureUtil
-				// .findBestMatchingAttributeFallBackFirst(schema,
-				// aExpression.getPropertyName());
-				//
-				// return new AttributeExpressionImpl(correctedName
-				// .getLocalPart());
-				// }
 				return super.copy(expression);
 			}
 
 			@Override
 			protected Filter copy(Filter filter) {
-
-				if (filter instanceof BinaryComparisonAbstract) {
+				if (filter instanceof DivideImpl) {
+					DivideImpl divFilter = (DivideImpl) filter;
+					divFilter.setExpression1(copy(divFilter.getExpression1()));
+					divFilter.setExpression2(copy(divFilter.getExpression2()));
+				} else
+					if (filter instanceof IsNullImpl) {
+					IsNullImpl isNullFilter = (IsNullImpl) filter;
+					isNullFilter.setExpression1(copy(isNullFilter
+							.getExpression1()));
+				} else if (filter instanceof NotImpl) {
+					NotImpl notFilter = (NotImpl) filter;
+					List<Filter> children = notFilter.getChildren();
+					List<Filter> correctedChildren = new ArrayList<Filter>();
+					for (Filter ex : children) {
+						correctedChildren.add(copy(ex));
+					}
+					notFilter.setChildren(correctedChildren);
+				} else if (filter instanceof OrImpl) {
+					OrImpl notFilter = (OrImpl) filter;
+					List<Filter> children = notFilter.getChildren();
+					List<Filter> correctedChildren = new ArrayList<Filter>();
+					for (Filter ex : children) {
+						correctedChildren.add(copy(ex));
+					}
+					notFilter.setChildren(correctedChildren);
+				} else if (filter instanceof AndImpl) {
+					AndImpl andFilter = (AndImpl) filter;
+					List<Filter> children = andFilter.getChildren();
+					List<Filter> correctedChildren = new ArrayList<Filter>();
+					for (Filter ex : children) {
+						correctedChildren.add(copy(ex));
+					}
+					andFilter.setChildren(correctedChildren);
+				} else if (filter instanceof BinaryComparisonAbstract) {
 					BinaryComparisonAbstract binFilter = (BinaryComparisonAbstract) filter;
 					binFilter.setExpression1(copy(binFilter.getExpression1()));
 					binFilter.setExpression2(copy(binFilter.getExpression2()));
@@ -2558,13 +2604,6 @@
 	/**
 	 * Geopublisher allows to use one or two attributes for labelling, like
 	 * 
-	 * <code>
-	 
-	    <sld:Label>
-          <ogc:PropertyName>ORTSRATSBE</ogc:PropertyName>: <ogc:PropertyName>ORB_SCHLUE</ogc:PropertyName>
-        </sld:Label>
-
-        </code>
 	 * 
 	 * Returns the second attribute found in the label expression of the given
 	 * {@link TextSymbolizer} or <code>null</code>.
@@ -2596,8 +2635,9 @@
 					prop2 = attributeNames[0];
 				}
 
-//				LOGGER.debug("The second property for \n" + filterExpression + "\n is "
-//						+ prop2);
+				// LOGGER.debug("The second property for \n" + filterExpression
+				// + "\n is "
+				// + prop2);
 
 				return FilterUtil.FILTER_FAC2.property(prop2);
 			}
@@ -2795,5 +2835,4 @@
 				Double.NaN);
 	}
 
-
 }



More information about the Schmitzm-commits mailing list