/[schmitzm]/branches/2.1/src/skrueger/geotools/labelsearch/LabelSearch.java
ViewVC logotype

Contents of /branches/2.1/src/skrueger/geotools/labelsearch/LabelSearch.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 139 - (show annotations)
Thu Jun 11 12:52:58 2009 UTC (15 years, 8 months ago) by alfonx
Original Path: trunk/src/skrueger/geotools/labelsearch/LabelSearch.java
File MIME type: text/plain
File size: 5142 byte(s)
* Fixed a bug when creating more than 3 additional styles at once... A NPE occurred because none has been set as the selected additional style.
* Moved the i8n and Localisation for LabelSearch to schmitzm

1 package skrueger.geotools.labelsearch;
2
3 import java.io.IOException;
4 import java.util.ArrayList;
5 import java.util.Collections;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.Locale;
9
10 import org.apache.log4j.Logger;
11 import org.geotools.data.DefaultQuery;
12 import org.geotools.feature.AttributeType;
13 import org.geotools.feature.Feature;
14 import org.geotools.feature.FeatureCollection;
15 import org.geotools.feature.FeatureType;
16 import org.geotools.map.MapLayer;
17 import org.geotools.styling.Style;
18 import org.geotools.styling.TextSymbolizer;
19 import org.opengis.filter.Filter;
20 import org.opengis.filter.expression.Expression;
21 import org.opengis.filter.expression.PropertyName;
22
23 import schmitzm.geotools.styling.StylingUtil;
24 import schmitzm.lang.LangUtil;
25 import schmitzm.lang.ResourceProvider;
26
27 /**
28 * This class allows to search for a {@link String} in a map. The algorithm will
29 * analyze the {@link Style} of every visible(?) layer and determine the label
30 * attribute. This field is then searched for every feature.
31 *
32 * @author Stefan A. Krüger
33 *
34 */
35 public class LabelSearch {
36 final static private Logger LOGGER = Logger.getLogger(LabelSearch.class);
37
38 public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil
39 .extendPackagePath(LabelSearch.class,
40 "labelsearch"), Locale.ENGLISH);
41
42
43
44
45 public static String R(String key, Object... values) {
46 return RESOURCE.getString(key, values);
47 }
48
49 protected final schmitzm.geotools.gui.JMapPane mapPane;
50
51 public LabelSearch(final schmitzm.geotools.gui.JMapPane mapPane) {
52 this.mapPane = mapPane;
53 }
54
55 private AttributeType getLabelAttribute(final TextSymbolizer ts,
56 final FeatureType schema) {
57 if (ts == null) {
58 // This layer has no labels
59 return null;
60 }
61 final Expression labelExp = ts.getLabel();
62 if (labelExp instanceof PropertyName) {
63 final PropertyName pn = (PropertyName) labelExp;
64 final String propertyName = pn.getPropertyName();
65 return schema.getAttributeType(propertyName);
66 } else {
67 // When does this happen
68 }
69
70 return null;
71 }
72
73 public List<SearchResult> search(final String string) {
74
75 final String searchMe = string.toLowerCase();
76
77 final ArrayList<SearchResult> hits = new ArrayList<SearchResult>();
78
79 for (final MapLayer ml : mapPane.getContext().getLayers()) {
80 try {
81
82 // System.out.println("layer = "+ml.getTitle());
83
84 if (!ml.isVisible())
85 continue;
86
87 final List<TextSymbolizer> allTS = StylingUtil.getTextSymbolizers(ml
88 .getStyle());
89 if (allTS.size() == 0) {
90 // A layer without any TextSymbolizer doesn't have to be
91 // searched any more.
92 continue;
93 }
94
95 final String typeName = ml.getFeatureSource().getSchema()
96 .getTypeName();
97
98 // Expression labelExp = ts.getLabel();
99 // ff.like(labelExp, "*"+searchMe+"*");
100 // FeatureCollection features =
101 // ml.getFeatureSource().getFeatures(
102 // new DefaultQuery(typeName, ff.like(labelExp,
103 // "*"+searchMe+"*"), properties));
104
105 final FeatureCollection features = ml.getFeatureSource().getFeatures(
106 new DefaultQuery(typeName, Filter.INCLUDE));
107
108 // new MemoryDataStore().getFeatureSource(typeName)
109
110 /**
111 * We do the comparison NOT with a ff.like, because that doesn't
112 * support case insensitivity and i don't find a lower or UPPER
113 * function.
114 */
115 final Iterator<Feature> fi = features.iterator();
116 while (fi.hasNext()) {
117 final Feature f = fi.next();
118
119 final TextSymbolizer ts = StylingUtil.getTextSymbolizer(ml
120 .getStyle(), f);
121 if (ts == null)
122 continue;
123
124 final AttributeType labelAttribute = getLabelAttribute(ts, ml
125 .getFeatureSource().getSchema());
126
127 if (labelAttribute == null) {
128 continue;
129 }
130
131 // System.out.println("labelAttrib local name" +
132 // labelAttribute.getLocalName());
133
134 final Object value = f
135 .getAttribute(labelAttribute.getLocalName());
136
137 // System.out.println("labelAttrib value " + value);
138
139 if (value == null) {
140 LOGGER.info("Skipping f: getLocalName() is null for feature="+f);
141 continue;
142 }
143
144 /**
145 * LabelString ist z.B. "IMPETUS pluviograph". Suchwort
146 * "plu" soll treffen. Also wird nach spaces zerlegt und
147 * dann gesucht
148 */
149 final String labelString = value.toString().toLowerCase();
150 if (labelString.startsWith(searchMe)) {
151 hits.add(createSearchResult(f, value.toString(), ml
152 .getTitle()));
153 } else {
154 final String[] parts = labelString.trim().split(" ");
155 for (final String part : parts) {
156 if (part.startsWith(searchMe)) {
157 hits.add(createSearchResult(f, value.toString(), ml
158 .getTitle()));
159 break;
160 }
161 }
162 }
163
164 }
165
166 } catch (final IOException e) {
167 // Searching this layer failed
168 LOGGER.error(e);
169 }
170 } // next layer
171
172 // Hits from the top-most layer should appear first.
173 Collections.reverse(hits);
174
175 return hits;
176 }
177
178 protected SearchResult createSearchResult(final Feature f, final String title,
179 final String inTitle) {
180 return new SearchResultFeature(f, title, inTitle, mapPane);
181 }
182
183 }

Properties

Name Value
svn:eol-style native
svn:keywords Id
svn:mime-type text/plain

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26