[Schmitzm-commits] r574 - in branches/1.0-gt2-2.6/src: schmitzm/geotools/styling skrueger/geotools/labelsearch

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Nov 25 09:09:44 CET 2009


Author: alfonx
Date: 2009-11-25 09:09:42 +0100 (Wed, 25 Nov 2009)
New Revision: 574

Modified:
   branches/1.0-gt2-2.6/src/schmitzm/geotools/styling/StylingUtil.java
   branches/1.0-gt2-2.6/src/skrueger/geotools/labelsearch/LabelSearch.java
Log:
Moved helper methods for double labelling from ASUtil to StylingUtil

Modified: branches/1.0-gt2-2.6/src/schmitzm/geotools/styling/StylingUtil.java
===================================================================
--- branches/1.0-gt2-2.6/src/schmitzm/geotools/styling/StylingUtil.java	2009-11-25 07:04:18 UTC (rev 573)
+++ branches/1.0-gt2-2.6/src/schmitzm/geotools/styling/StylingUtil.java	2009-11-25 08:09:42 UTC (rev 574)
@@ -67,6 +67,7 @@
 import org.geotools.feature.FeatureCollection;
 import org.geotools.feature.GeometryAttributeType;
 import org.geotools.filter.ConstantExpression;
+import org.geotools.filter.FilterAttributeExtractor;
 import org.geotools.geometry.jts.ReferencedEnvelope;
 import org.geotools.renderer.lite.RendererUtilities;
 import org.geotools.resources.i18n.Vocabulary;
@@ -110,9 +111,11 @@
 import org.opengis.filter.FilterFactory2;
 import org.opengis.filter.PropertyIsEqualTo;
 import org.opengis.filter.expression.Expression;
+import org.opengis.filter.expression.Function;
 import org.opengis.filter.expression.Literal;
 import org.opengis.filter.expression.PropertyName;
 
+import schmitzm.geotools.FilterUtil;
 import schmitzm.geotools.feature.FeatureUtil;
 import schmitzm.geotools.feature.FeatureUtil.GeometryForm;
 import schmitzm.geotools.grid.GridUtil;
@@ -2495,5 +2498,152 @@
 
 		return (Style) dsv.getCopy();
 	}
+	
 
+	/**
+	 * 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>.
+	 */
+	public static PropertyName getSecondPropertyName(
+			final SimpleFeatureType ft, final TextSymbolizer textSymbolizer) {
+		try {
+
+			final Expression labelExpression = textSymbolizer.getLabel();
+			if (labelExpression instanceof PropertyName) {
+				// There is no second property
+				return null;
+			} else if (labelExpression instanceof Function) {
+				final Function filterExpression = (Function) labelExpression;
+				final FilterAttributeExtractor attExVid = new FilterAttributeExtractor(
+						ft);
+
+				attExVid.visit(filterExpression, null);
+
+				final String[] attributeNames = attExVid.getAttributeNames();
+				if (attributeNames.length <= 1)
+					return null;
+
+				String prop2 = null;
+				if (filterExpression.toString().indexOf(attributeNames[0]) < filterExpression
+						.toString().indexOf(attributeNames[1])) {
+					prop2 = attributeNames[1];
+				} else {
+					prop2 = attributeNames[0];
+				}
+
+				LOGGER.debug("Thie second for \n" + filterExpression + "\n is "
+						+ prop2);
+
+				return FilterUtil.FILTER_FAC2.property(prop2);
+			}
+		}
+
+		catch (final Exception e) {
+			LOGGER.error("Reading the seconf property for labelling failed", e);
+		}
+
+		return null;
+	}
+
+	/**
+	 * 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 first attribute found in the label expression of the given
+	 * {@link TextSymbolizer}. If non is found for some reason, the method tries
+	 * to return the first attribute from the schema. We expect the layer to
+	 * have some attributes, because otherwise AS shouldn't even create the
+	 * LabellingTab.
+	 */
+	public static PropertyName getFirstPropertyName(final SimpleFeatureType ft,
+			final TextSymbolizer textSymbolizer) {
+		final Expression labelExpression = textSymbolizer.getLabel();
+		try {
+			if (labelExpression instanceof PropertyName) {
+				// There is no second property
+				return (PropertyName) labelExpression;
+			} else if (labelExpression instanceof Function) {
+				final Function filterExpression = (Function) labelExpression;
+				final FilterAttributeExtractor attExVid = new FilterAttributeExtractor(
+						ft);
+
+				attExVid.visit(filterExpression, null);
+
+				final String[] attributeNames = attExVid.getAttributeNames();
+				String prop1 = null;
+				if (attributeNames.length == 1) {
+					// Easy
+					prop1 = attributeNames[0];
+				} else {
+
+					if (filterExpression.toString().indexOf(attributeNames[0]) < filterExpression
+							.toString().indexOf(attributeNames[1])) {
+						prop1 = attributeNames[0];
+						// prop2 = attributeNames[1];
+					} else {
+						prop1 = attributeNames[1];
+						// prop2 = attributeNames[0];
+					}
+
+				}
+				return FilterUtil.FILTER_FAC2.property(prop1);
+			}
+		} catch (final Exception e) {
+			LOGGER.error("Reading the second property for labelling failed", e);
+			return null;
+		}
+
+		// TODO this is not rockhard.. could NPE
+		return FilterUtil.FILTER_FAC2.property(ft.getAttributeDescriptors()
+				.get(1).getLocalName());
+	}
+
+
+	/**
+	 * AtlasStyler has a fixed system that allows to use one or two label
+	 * attributes. If two are used, the are seperated by ": ". This mehtod stes
+	 * one or two ProperybaName fields.
+	 * 
+	 * @param symbolizer
+	 *            the {@link TextSymbolizer} to call setLabel(Expression) on.
+	 * @param literalLabelField1
+	 *            may not be null!
+	 * @param literalLabelField2
+	 *            may be <code>null</code>
+	 */
+	public static void setDoublePropertyName(final TextSymbolizer symbolizer,
+			final PropertyName literalLabelField1,
+			final PropertyName literalLabelField2) {
+		if (literalLabelField2 != null
+				&& !literalLabelField2.toString().equalsIgnoreCase("-")) {
+			final Literal trenner = FilterUtil.FILTER_FAC2.literal(": ");
+			final Function f3 = FilterUtil.FILTER_FAC2.function("strConcat",
+					trenner, literalLabelField2);
+			final Function bothExpression = FilterUtil.FILTER_FAC2.function(
+					"strConcat", literalLabelField1, f3);
+			LOGGER.debug("Extended label expression is now: " + bothExpression);
+			symbolizer.setLabel(bothExpression);
+		} else {
+			symbolizer.setLabel(literalLabelField1);
+		}
+	}
+
+
 }

Modified: branches/1.0-gt2-2.6/src/skrueger/geotools/labelsearch/LabelSearch.java
===================================================================
--- branches/1.0-gt2-2.6/src/skrueger/geotools/labelsearch/LabelSearch.java	2009-11-25 07:04:18 UTC (rev 573)
+++ branches/1.0-gt2-2.6/src/skrueger/geotools/labelsearch/LabelSearch.java	2009-11-25 08:09:42 UTC (rev 574)
@@ -95,9 +95,7 @@
 			final String propertyName = pn.getPropertyName();
 			return schema.getDescriptor(propertyName);
 		} else {
-			// When does this happen
-			throw new RuntimeException("labelExp " + labelExp
-					+ " IS NOT instanceof PropertyName!");
+			return null;
 		}
 
 	}
@@ -157,24 +155,19 @@
 								.getTextSymbolizer(ml.getStyle(), f);
 						if (ts == null)
 							continue;
-
+						
 						// TODO Evaluate the Rule that belongs to the
-						// TextSymbolizer against the feature...
-						final AttributeDescriptor labelAttribute = getLabelAttribute(
-								ts, featureSource.getSchema());
+						SimpleFeatureType schema = featureSource.getSchema();
+						PropertyName pn = StylingUtil.getFirstPropertyName(
+								schema, ts);
+						if (pn == null) continue;
+						
+						final AttributeDescriptor labelAttribute = schema
+								.getDescriptor(pn.getPropertyName());
 
-						if (labelAttribute == null) {
-							continue;
-						}
-
-						// System.out.println("labelAttrib local name" +
-						// labelAttribute.getLocalName());
-
 						final Object value = f.getAttribute(labelAttribute
 								.getLocalName());
 
-						// System.out.println("labelAttrib value " + value);
-
 						if (value == null) {
 							LOGGER
 									.info("Skipping f: getLocalName() is null for feature="



More information about the Schmitzm-commits mailing list