[Schmitzm-commits] r598 - in branches/1.0-gt2-2.6/src/skrueger/geotools: . labelsearch

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Tue Dec 1 15:12:11 CET 2009


Author: alfonx
Date: 2009-12-01 15:12:11 +0100 (Tue, 01 Dec 2009)
New Revision: 598

Modified:
   branches/1.0-gt2-2.6/src/skrueger/geotools/XMapPane.java
   branches/1.0-gt2-2.6/src/skrueger/geotools/labelsearch/LabelSearch.java
Log:
Improved the label seach. Since GEOT-2859 has been comittet we now use the LIKE and strToUpper function. Also the layer's filter has not been respected earlier.

Modified: branches/1.0-gt2-2.6/src/skrueger/geotools/XMapPane.java
===================================================================
--- branches/1.0-gt2-2.6/src/skrueger/geotools/XMapPane.java	2009-12-01 13:27:16 UTC (rev 597)
+++ branches/1.0-gt2-2.6/src/skrueger/geotools/XMapPane.java	2009-12-01 14:12:11 UTC (rev 598)
@@ -2274,7 +2274,7 @@
 		final CoordinateReferenceSystem mapCRS = getMapContext()
 				.getCoordinateReferenceSystem();
 		final CoordinateReferenceSystem fCRS = features.getSchema()
-				.getGeometryDescriptor().getCoordinateReferenceSystem();
+				.getCoordinateReferenceSystem();
 
 		ReferencedEnvelope _mapArea;
 		if (mapArea == null)

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-12-01 13:27:16 UTC (rev 597)
+++ branches/1.0-gt2-2.6/src/skrueger/geotools/labelsearch/LabelSearch.java	2009-12-01 14:12:11 UTC (rev 598)
@@ -40,6 +40,8 @@
 import org.geotools.data.DefaultQuery;
 import org.geotools.data.FeatureSource;
 import org.geotools.feature.FeatureCollection;
+import org.geotools.filter.text.cql2.CQL;
+import org.geotools.filter.text.cql2.CQLException;
 import org.geotools.map.MapLayer;
 import org.geotools.styling.Style;
 import org.geotools.styling.TextSymbolizer;
@@ -50,6 +52,9 @@
 import org.opengis.filter.expression.Expression;
 import org.opengis.filter.expression.PropertyName;
 
+import com.sun.jndi.toolkit.dir.SearchFilter;
+
+import schmitzm.geotools.feature.FeatureUtil;
 import schmitzm.geotools.styling.StylingUtil;
 import schmitzm.lang.LangUtil;
 import schmitzm.lang.ResourceProvider;
@@ -79,30 +84,31 @@
 		this.mapPane = mapPane;
 	}
 
-	/**
-	 * The Attribute that provides the labels for this text symbolizer.
-	 */
-	private AttributeDescriptor getLabelAttribute(final TextSymbolizer ts,
-			final SimpleFeatureType schema) {
-		if (ts == null) {
-			// This layer has no labels
-			return null;
-		}
+	//
+	// /**
+	// * The Attribute that provides the labels for this text symbolizer.
+	// */
+	// private AttributeDescriptor getLabelAttribute(final TextSymbolizer ts,
+	// final SimpleFeatureType schema) {
+	// if (ts == null) {
+	// // This layer has no labels
+	// return null;
+	// }
+	//
+	// final Expression labelExp = ts.getLabel();
+	// if (labelExp instanceof PropertyName) {
+	// final PropertyName pn = (PropertyName) labelExp;
+	// final String propertyName = pn.getPropertyName();
+	// return schema.getDescriptor(propertyName);
+	// } else {
+	// return null;
+	// }
+	//
+	// }
 
-		final Expression labelExp = ts.getLabel();
-		if (labelExp instanceof PropertyName) {
-			final PropertyName pn = (PropertyName) labelExp;
-			final String propertyName = pn.getPropertyName();
-			return schema.getDescriptor(propertyName);
-		} else {
-			return null;
-		}
-
-	}
-
 	public List<SearchResult> search(final String string) {
 
-		final String searchMe = string.toLowerCase();
+		final String searchMe = string.toUpperCase();
 
 		final ArrayList<SearchResult> hits = new ArrayList<SearchResult>();
 
@@ -122,91 +128,90 @@
 					continue;
 				}
 
+				// We only deal with one TextSymbolizer so far:
+				TextSymbolizer ts = allTS.get(0);
+
 				final FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) ml
 						.getFeatureSource();
 
-				final String typeName = featureSource.getSchema().getName()
-						.getLocalPart();
+				SimpleFeatureType schema = featureSource.getSchema();
 
-				// Expression labelExp = ts.getLabel();
-				// ff.like(labelExp, "*"+searchMe+"*");
-				// FeatureCollection features =
-				// ml.getFeatureSource().getFeatures(
-				// new DefaultQuery(typeName, ff.like(labelExp,
-				// "*"+searchMe+"*"), properties));
+				final String typeName = schema.getName().getLocalPart();
 
-				final FeatureCollection<SimpleFeatureType, SimpleFeature> features = featureSource
-						.getFeatures(new DefaultQuery(typeName, Filter.INCLUDE));
+				PropertyName prop1 = StylingUtil.getFirstPropertyName(schema,
+						ts);
+				PropertyName prop2 = StylingUtil.getSecondPropertyName(schema,
+						ts);
 
-				// new MemoryDataStore().getFeatureSource(typeName)
+				if (StylingUtil.getFirstPropertyName(schema, ts) == null) {
+					// At least one property field we need
+					continue;
+				}
 
-				/**
-				 * We do the comparison NOT with a ff.like, because that doesn't
-				 * support case insensitivity and i don't find a lower or UPPER
-				 * function.
-				 */
+				Filter searchFilter;
+				String[] properties = new String[] { schema.getGeometryDescriptor().getLocalName(), prop1.getPropertyName() };
+				// Only one property used...
+
+				searchFilter = CQL.toFilter("strToUpperCase "
+						+ prop1.getPropertyName() + "  LIKE '%" + searchMe
+						+ "%'");
+
+				if (prop2 != null) {
+					Filter searchFilter2 = CQL.toFilter("strToUpperCase "
+							+ prop2.getPropertyName() + "  LIKE '%" + searchMe
+							+ "%'");
+
+					searchFilter = FeatureUtil.FILTER_FACTORY2.or(searchFilter,
+							searchFilter2);
+
+					properties = LangUtil.extendArray(properties, prop2
+							.getPropertyName());
+				}
+
+				// Add the layer's filter if it exists
+				Filter layerFilter = ml.getQuery().getFilter();
+				if (layerFilter != null && layerFilter != Filter.INCLUDE) {
+					searchFilter = FeatureUtil.FILTER_FACTORY2.and(layerFilter,
+							searchFilter);
+
+				}
+				
+				LOGGER.info("Searching for "+searchFilter.toString());
+
+				FeatureCollection<SimpleFeatureType, SimpleFeature> features = (FeatureCollection<SimpleFeatureType, SimpleFeature>) ml
+						.getFeatureSource().getFeatures(
+								new DefaultQuery(typeName, searchFilter,
+										properties));
+
 				final Iterator<SimpleFeature> fi = features.iterator();
 				try {
-
 					while (fi.hasNext()) {
 						final SimpleFeature f = fi.next();
 
-						final TextSymbolizer ts = StylingUtil
-								.getTextSymbolizer(ml.getStyle(), f);
-						if (ts == null)
-							continue;
-						
-						// TODO Evaluate the Rule that belongs to the
-						SimpleFeatureType schema = featureSource.getSchema();
-						PropertyName pn = StylingUtil.getFirstPropertyName(
-								schema, ts);
-						if (pn == null) continue;
-						
-						final AttributeDescriptor labelAttribute = schema
-								.getDescriptor(pn.getPropertyName());
+						String valueString = "";
+						valueString = f.getAttribute(prop1.getPropertyName())
+								.toString();
 
-						final Object value = f.getAttribute(labelAttribute
-								.getLocalName());
+						if (prop2 != null) {
+							String valueString2 = f.getAttribute(
+									prop2.getPropertyName()).toString();
 
-						if (value == null) {
-							LOGGER
-									.info("Skipping f: getLocalName() is null for feature="
-											+ f);
-							continue;
+							if (valueString2 != null && !valueString2.isEmpty())
+								valueString += ", " + valueString2;
 						}
 
-						/**
-						 * LabelString ist z.B. "IMPETUS pluviograph". Suchwort
-						 * "plu" soll treffen. Also wird nach spaces zerlegt und
-						 * dann gesucht
-						 */
-						final String labelString = value.toString()
-								.toLowerCase();
-						if (labelString.startsWith(searchMe)) {
-							hits.add(createSearchResult(f, value.toString(), ml
-									.getTitle(), ml));
-						} else {
-							final String[] parts = labelString.trim()
-									.split(" ");
-							for (final String part : parts) {
-								if (part.startsWith(searchMe)) {
-									hits.add(createSearchResult(f, value
-											.toString(), ml.getTitle(), ml));
-									break;
-								}
-							}
-						}
-
+						hits.add(createSearchResult(f, valueString, ml
+								.getTitle(), ml));
 					}
 				} finally {
 					features.close(fi);
 				}
-
 			} catch (final IOException e) {
 				// Searching this layer failed
-				LOGGER.error(e);
+				LOGGER.error("",e);
+			} catch (CQLException e) {
+				LOGGER.error("",e);
 			}
-
 		} // next layer
 
 		// Hits from the top-most layer should appear first.



More information about the Schmitzm-commits mailing list