/[schmitzm]/branches/1.0-gt2-2.6/src/skrueger/geotools/labelsearch/LabelSearch.java
ViewVC logotype

Contents of /branches/1.0-gt2-2.6/src/skrueger/geotools/labelsearch/LabelSearch.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 598 - (show annotations)
Tue Dec 1 14:12:11 2009 UTC (15 years, 3 months ago) by alfonx
File MIME type: text/plain
File size: 7382 byte(s)
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.
1 /*******************************************************************************
2 * Copyright (c) 2009 Martin O. J. Schmitz.
3 *
4 * This file is part of the SCHMITZM library - a collection of utility
5 * classes based on Java 1.6, focusing (not only) on Java Swing
6 * and the Geotools library.
7 *
8 * The SCHMITZM project is hosted at:
9 * http://wald.intevation.org/projects/schmitzm/
10 *
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 3
14 * of the License, or (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License (license.txt)
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24 * or try this link: http://www.gnu.org/licenses/lgpl.html
25 *
26 * Contributors:
27 * Martin O. J. Schmitz - initial API and implementation
28 * Stefan A. Krüger - additional utility classes
29 ******************************************************************************/
30 package skrueger.geotools.labelsearch;
31
32 import java.io.IOException;
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.Iterator;
36 import java.util.List;
37 import java.util.Locale;
38
39 import org.apache.log4j.Logger;
40 import org.geotools.data.DefaultQuery;
41 import org.geotools.data.FeatureSource;
42 import org.geotools.feature.FeatureCollection;
43 import org.geotools.filter.text.cql2.CQL;
44 import org.geotools.filter.text.cql2.CQLException;
45 import org.geotools.map.MapLayer;
46 import org.geotools.styling.Style;
47 import org.geotools.styling.TextSymbolizer;
48 import org.opengis.feature.simple.SimpleFeature;
49 import org.opengis.feature.simple.SimpleFeatureType;
50 import org.opengis.feature.type.AttributeDescriptor;
51 import org.opengis.filter.Filter;
52 import org.opengis.filter.expression.Expression;
53 import org.opengis.filter.expression.PropertyName;
54
55 import com.sun.jndi.toolkit.dir.SearchFilter;
56
57 import schmitzm.geotools.feature.FeatureUtil;
58 import schmitzm.geotools.styling.StylingUtil;
59 import schmitzm.lang.LangUtil;
60 import schmitzm.lang.ResourceProvider;
61
62 /**
63 * This class allows to search for a {@link String} in a map. The algorithm will
64 * analyze the {@link Style} of every visible(?) layer and determine the label
65 * attribute. This field is then searched for every feature.
66 *
67 * @author Stefan A. Krüger
68 *
69 */
70 public class LabelSearch {
71 final static private Logger LOGGER = Logger.getLogger(LabelSearch.class);
72
73 public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil
74 .extendPackagePath(LabelSearch.class, "labelsearch"),
75 Locale.ENGLISH);
76
77 public static String R(String key, Object... values) {
78 return RESOURCE.getString(key, values);
79 }
80
81 protected final schmitzm.geotools.gui.SelectableXMapPane mapPane;
82
83 public LabelSearch(final schmitzm.geotools.gui.SelectableXMapPane mapPane) {
84 this.mapPane = mapPane;
85 }
86
87 //
88 // /**
89 // * The Attribute that provides the labels for this text symbolizer.
90 // */
91 // private AttributeDescriptor getLabelAttribute(final TextSymbolizer ts,
92 // final SimpleFeatureType schema) {
93 // if (ts == null) {
94 // // This layer has no labels
95 // return null;
96 // }
97 //
98 // final Expression labelExp = ts.getLabel();
99 // if (labelExp instanceof PropertyName) {
100 // final PropertyName pn = (PropertyName) labelExp;
101 // final String propertyName = pn.getPropertyName();
102 // return schema.getDescriptor(propertyName);
103 // } else {
104 // return null;
105 // }
106 //
107 // }
108
109 public List<SearchResult> search(final String string) {
110
111 final String searchMe = string.toUpperCase();
112
113 final ArrayList<SearchResult> hits = new ArrayList<SearchResult>();
114
115 for (final MapLayer ml : mapPane.getMapContext().getLayers()) {
116 try {
117
118 // System.out.println("layer = "+ml.getTitle());
119
120 if (!ml.isVisible())
121 continue;
122
123 final List<TextSymbolizer> allTS = StylingUtil
124 .getVisibleTextSymbolizers(ml.getStyle());
125 if (allTS.size() == 0) {
126 // A layer without any TextSymbolizer doesn't have to be
127 // searched any more.
128 continue;
129 }
130
131 // We only deal with one TextSymbolizer so far:
132 TextSymbolizer ts = allTS.get(0);
133
134 final FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) ml
135 .getFeatureSource();
136
137 SimpleFeatureType schema = featureSource.getSchema();
138
139 final String typeName = schema.getName().getLocalPart();
140
141 PropertyName prop1 = StylingUtil.getFirstPropertyName(schema,
142 ts);
143 PropertyName prop2 = StylingUtil.getSecondPropertyName(schema,
144 ts);
145
146 if (StylingUtil.getFirstPropertyName(schema, ts) == null) {
147 // At least one property field we need
148 continue;
149 }
150
151 Filter searchFilter;
152 String[] properties = new String[] { schema.getGeometryDescriptor().getLocalName(), prop1.getPropertyName() };
153 // Only one property used...
154
155 searchFilter = CQL.toFilter("strToUpperCase "
156 + prop1.getPropertyName() + " LIKE '%" + searchMe
157 + "%'");
158
159 if (prop2 != null) {
160 Filter searchFilter2 = CQL.toFilter("strToUpperCase "
161 + prop2.getPropertyName() + " LIKE '%" + searchMe
162 + "%'");
163
164 searchFilter = FeatureUtil.FILTER_FACTORY2.or(searchFilter,
165 searchFilter2);
166
167 properties = LangUtil.extendArray(properties, prop2
168 .getPropertyName());
169 }
170
171 // Add the layer's filter if it exists
172 Filter layerFilter = ml.getQuery().getFilter();
173 if (layerFilter != null && layerFilter != Filter.INCLUDE) {
174 searchFilter = FeatureUtil.FILTER_FACTORY2.and(layerFilter,
175 searchFilter);
176
177 }
178
179 LOGGER.info("Searching for "+searchFilter.toString());
180
181 FeatureCollection<SimpleFeatureType, SimpleFeature> features = (FeatureCollection<SimpleFeatureType, SimpleFeature>) ml
182 .getFeatureSource().getFeatures(
183 new DefaultQuery(typeName, searchFilter,
184 properties));
185
186 final Iterator<SimpleFeature> fi = features.iterator();
187 try {
188 while (fi.hasNext()) {
189 final SimpleFeature f = fi.next();
190
191 String valueString = "";
192 valueString = f.getAttribute(prop1.getPropertyName())
193 .toString();
194
195 if (prop2 != null) {
196 String valueString2 = f.getAttribute(
197 prop2.getPropertyName()).toString();
198
199 if (valueString2 != null && !valueString2.isEmpty())
200 valueString += ", " + valueString2;
201 }
202
203 hits.add(createSearchResult(f, valueString, ml
204 .getTitle(), ml));
205 }
206 } finally {
207 features.close(fi);
208 }
209 } catch (final IOException e) {
210 // Searching this layer failed
211 LOGGER.error("",e);
212 } catch (CQLException e) {
213 LOGGER.error("",e);
214 }
215 } // next layer
216
217 // Hits from the top-most layer should appear first.
218 Collections.reverse(hits);
219
220 return hits;
221 }
222
223 protected SearchResult createSearchResult(final SimpleFeature f,
224 final String title, final String inTitle, MapLayer ml) {
225 return new SearchResultFeature(f, title, inTitle, mapPane, ml);
226 }
227
228 }

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