[Schmitzm-commits] r138 - trunk/src/skrueger/geotools

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Wed Jun 10 17:23:34 CEST 2009


Author: alfonx
Date: 2009-06-10 17:23:33 +0200 (Wed, 10 Jun 2009)
New Revision: 138

Added:
   trunk/src/skrueger/geotools/LabelSearch.java
   trunk/src/skrueger/geotools/SearchResult.java
   trunk/src/skrueger/geotools/SearchResultFeature.java
Log:
* Moved all classes concerning the LabelSearch tool to schmitzm

Added: trunk/src/skrueger/geotools/LabelSearch.java
===================================================================
--- trunk/src/skrueger/geotools/LabelSearch.java	2009-06-03 20:10:31 UTC (rev 137)
+++ trunk/src/skrueger/geotools/LabelSearch.java	2009-06-10 15:23:33 UTC (rev 138)
@@ -0,0 +1,169 @@
+package skrueger.geotools;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+
+import org.apache.log4j.Logger;
+import org.geotools.data.DefaultQuery;
+import org.geotools.feature.AttributeType;
+import org.geotools.feature.Feature;
+import org.geotools.feature.FeatureCollection;
+import org.geotools.feature.FeatureType;
+import org.geotools.map.MapLayer;
+import org.geotools.styling.Style;
+import org.geotools.styling.TextSymbolizer;
+import org.opengis.filter.Filter;
+import org.opengis.filter.expression.Expression;
+import org.opengis.filter.expression.PropertyName;
+
+import schmitzm.geotools.styling.StylingUtil;
+
+/**
+ * This class allows to search for a {@link String} in a map. The algorithm will
+ * analyze the {@link Style} of every visible(?) layer and determine the label
+ * attribute. This field is then searched for every feature.
+ * 
+ * @author Stefan A. Krüger
+ * 
+ */
+public class LabelSearch {
+	final static private Logger LOGGER = Logger.getLogger(LabelSearch.class);
+
+	protected final schmitzm.geotools.gui.JMapPane mapPane;
+
+	public LabelSearch(final schmitzm.geotools.gui.JMapPane mapPane) {
+		this.mapPane = mapPane;
+	}
+
+	private AttributeType getLabelAttribute(final TextSymbolizer ts,
+			final FeatureType 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.getAttributeType(propertyName);
+		} else {
+			// When does this happen
+		}
+
+		return null;
+	}
+
+	public List<SearchResult> search(final String string) {
+
+		final String searchMe = string.toLowerCase();
+
+		final ArrayList<SearchResult> hits = new ArrayList<SearchResult>();
+
+		for (final MapLayer ml : mapPane.getContext().getLayers()) {
+			try {
+
+				// System.out.println("layer = "+ml.getTitle());
+
+				if (!ml.isVisible())
+					continue;
+
+				final List<TextSymbolizer> allTS = StylingUtil.getTextSymbolizers(ml
+						.getStyle());
+				if (allTS.size() == 0) {
+					// A layer without any TextSymbolizer doesn't have to be
+					// searched any more.
+					continue;
+				}
+
+				final String typeName = ml.getFeatureSource().getSchema()
+						.getTypeName();
+
+				// Expression labelExp = ts.getLabel();
+				// ff.like(labelExp, "*"+searchMe+"*");
+				// FeatureCollection features =
+				// ml.getFeatureSource().getFeatures(
+				// new DefaultQuery(typeName, ff.like(labelExp,
+				// "*"+searchMe+"*"), properties));
+
+				final FeatureCollection features = ml.getFeatureSource().getFeatures(
+						new DefaultQuery(typeName, Filter.INCLUDE));
+
+				// new MemoryDataStore().getFeatureSource(typeName)
+
+				/**
+				 * 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.
+				 */
+				final Iterator<Feature> fi = features.iterator();
+				while (fi.hasNext()) {
+					final Feature f = fi.next();
+
+					final TextSymbolizer ts = StylingUtil.getTextSymbolizer(ml
+							.getStyle(), f);
+					if (ts == null)
+						continue;
+
+					final AttributeType labelAttribute = getLabelAttribute(ts, ml
+							.getFeatureSource().getSchema());
+
+					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="+f);
+						continue;
+					}
+
+					/**
+					 * 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()));
+					} else {
+						final String[] parts = labelString.trim().split(" ");
+						for (final String part : parts) {
+							if (part.startsWith(searchMe)) {
+								hits.add(createSearchResult(f, value.toString(), ml
+										.getTitle()));
+								break;
+							}
+						}
+					}
+
+				}
+
+			} catch (final IOException e) {
+				// Searching this layer failed
+				LOGGER.error(e);
+			}
+		} // next layer
+
+		// Hits from the top-most layer should appear first.
+		Collections.reverse(hits);
+
+		return hits;
+	}
+
+	protected SearchResult createSearchResult(final Feature f, final String title,
+			final String inTitle) {
+		return new SearchResultFeature(f, title, inTitle, mapPane);
+	}
+
+}


Property changes on: trunk/src/skrueger/geotools/LabelSearch.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: trunk/src/skrueger/geotools/SearchResult.java
===================================================================
--- trunk/src/skrueger/geotools/SearchResult.java	2009-06-03 20:10:31 UTC (rev 137)
+++ trunk/src/skrueger/geotools/SearchResult.java	2009-06-10 15:23:33 UTC (rev 138)
@@ -0,0 +1,26 @@
+package skrueger.geotools;
+
+import org.geotools.feature.Feature;
+
+public interface SearchResult {
+
+	/**
+	 * @returns a {@link String} that represents the Title/Name of this search
+	 *          result.
+	 * 
+	 */
+	String getTitle();
+
+	/**
+	 * "Open" the result. For a {@link Feature} that mean to zoom to the
+	 * location. For a {@link Map} if means to open the {@link Map}.
+	 */
+	void open();
+
+	/**
+	 * @returns a {@link String} that represents the Title/Name of "something" that contains the search
+	 *          result. This may return "" if it makes no sense.
+	 */
+	String getInTitle();
+
+}


Property changes on: trunk/src/skrueger/geotools/SearchResult.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: trunk/src/skrueger/geotools/SearchResultFeature.java
===================================================================
--- trunk/src/skrueger/geotools/SearchResultFeature.java	2009-06-03 20:10:31 UTC (rev 137)
+++ trunk/src/skrueger/geotools/SearchResultFeature.java	2009-06-10 15:23:33 UTC (rev 138)
@@ -0,0 +1,58 @@
+package skrueger.geotools;
+
+import javax.swing.SwingUtilities;
+
+import org.apache.log4j.Logger;
+import org.geotools.feature.Feature;
+
+import skrueger.geotools.LabelSearch;
+
+public class SearchResultFeature implements SearchResult {
+	final static private Logger LOGGER = Logger.getLogger(SearchResultFeature.class);
+	
+	private final Feature feature;
+	private final String title;
+	private final schmitzm.geotools.gui.JMapPane mapPane;
+	private final String inTitle;
+
+	public SearchResultFeature(Feature feature, String title, String inTitle, schmitzm.geotools.gui.JMapPane mapPane) {
+		this.feature = feature;
+		this.title = title;
+		this.inTitle = inTitle;
+		this.mapPane = mapPane;
+	}
+	
+	@Override
+	public String toString() {
+		return feature.toString();
+	}
+
+	public Feature getFeature() {
+		return feature;
+	}
+
+	@Override
+	public String getTitle() {
+		return title;
+	}
+
+	@Override
+	public void open() {
+		
+		// GuiAndTools.checkThatWeAreOnEDT();
+		if (!SwingUtilities.isEventDispatchThread()) {
+			LOGGER.error("Not on EDT");
+			throw new RuntimeException("Not on EDT!");
+		}
+		
+
+		mapPane.zoomTo(feature);
+		mapPane.refresh();
+	}
+
+	@Override
+	public String getInTitle() {
+		return inTitle;
+	}
+
+}


Property changes on: trunk/src/skrueger/geotools/SearchResultFeature.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native



More information about the Schmitzm-commits mailing list