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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 598 - (hide 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 alfonx 244 /*******************************************************************************
2     * Copyright (c) 2009 Martin O. J. Schmitz.
3     *
4     * This file is part of the SCHMITZM library - a collection of utility
5 alfonx 256 * classes based on Java 1.6, focusing (not only) on Java Swing
6 alfonx 244 * 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 alfonx 139 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 alfonx 335 import org.geotools.data.FeatureSource;
42 alfonx 139 import org.geotools.feature.FeatureCollection;
43 alfonx 598 import org.geotools.filter.text.cql2.CQL;
44     import org.geotools.filter.text.cql2.CQLException;
45 alfonx 139 import org.geotools.map.MapLayer;
46     import org.geotools.styling.Style;
47     import org.geotools.styling.TextSymbolizer;
48 mojays 325 import org.opengis.feature.simple.SimpleFeature;
49     import org.opengis.feature.simple.SimpleFeatureType;
50 alfonx 332 import org.opengis.feature.type.AttributeDescriptor;
51 alfonx 139 import org.opengis.filter.Filter;
52     import org.opengis.filter.expression.Expression;
53     import org.opengis.filter.expression.PropertyName;
54    
55 alfonx 598 import com.sun.jndi.toolkit.dir.SearchFilter;
56    
57     import schmitzm.geotools.feature.FeatureUtil;
58 alfonx 139 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 alfonx 456
73 alfonx 139 public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil
74 alfonx 456 .extendPackagePath(LabelSearch.class, "labelsearch"),
75     Locale.ENGLISH);
76    
77 alfonx 139 public static String R(String key, Object... values) {
78     return RESOURCE.getString(key, values);
79     }
80    
81 alfonx 509 protected final schmitzm.geotools.gui.SelectableXMapPane mapPane;
82 alfonx 139
83 alfonx 509 public LabelSearch(final schmitzm.geotools.gui.SelectableXMapPane mapPane) {
84 alfonx 139 this.mapPane = mapPane;
85     }
86    
87 alfonx 598 //
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 alfonx 456
109 alfonx 139 public List<SearchResult> search(final String string) {
110    
111 alfonx 598 final String searchMe = string.toUpperCase();
112 alfonx 139
113     final ArrayList<SearchResult> hits = new ArrayList<SearchResult>();
114    
115 alfonx 551 for (final MapLayer ml : mapPane.getMapContext().getLayers()) {
116 alfonx 139 try {
117    
118     // System.out.println("layer = "+ml.getTitle());
119    
120     if (!ml.isVisible())
121     continue;
122    
123 alfonx 456 final List<TextSymbolizer> allTS = StylingUtil
124     .getVisibleTextSymbolizers(ml.getStyle());
125 alfonx 139 if (allTS.size() == 0) {
126     // A layer without any TextSymbolizer doesn't have to be
127     // searched any more.
128     continue;
129     }
130    
131 alfonx 598 // We only deal with one TextSymbolizer so far:
132     TextSymbolizer ts = allTS.get(0);
133    
134 alfonx 456 final FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) ml
135     .getFeatureSource();
136    
137 alfonx 598 SimpleFeatureType schema = featureSource.getSchema();
138 alfonx 456
139 alfonx 598 final String typeName = schema.getName().getLocalPart();
140 alfonx 139
141 alfonx 598 PropertyName prop1 = StylingUtil.getFirstPropertyName(schema,
142     ts);
143     PropertyName prop2 = StylingUtil.getSecondPropertyName(schema,
144     ts);
145 alfonx 139
146 alfonx 598 if (StylingUtil.getFirstPropertyName(schema, ts) == null) {
147     // At least one property field we need
148     continue;
149     }
150 alfonx 139
151 alfonx 598 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 alfonx 318 final Iterator<SimpleFeature> fi = features.iterator();
187 alfonx 456 try {
188     while (fi.hasNext()) {
189     final SimpleFeature f = fi.next();
190 alfonx 139
191 alfonx 598 String valueString = "";
192     valueString = f.getAttribute(prop1.getPropertyName())
193     .toString();
194 alfonx 139
195 alfonx 598 if (prop2 != null) {
196     String valueString2 = f.getAttribute(
197     prop2.getPropertyName()).toString();
198 alfonx 139
199 alfonx 598 if (valueString2 != null && !valueString2.isEmpty())
200     valueString += ", " + valueString2;
201 alfonx 456 }
202    
203 alfonx 598 hits.add(createSearchResult(f, valueString, ml
204     .getTitle(), ml));
205 alfonx 139 }
206 alfonx 456 } finally {
207     features.close(fi);
208 alfonx 139 }
209     } catch (final IOException e) {
210     // Searching this layer failed
211 alfonx 598 LOGGER.error("",e);
212     } catch (CQLException e) {
213     LOGGER.error("",e);
214 alfonx 139 }
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 alfonx 456 protected SearchResult createSearchResult(final SimpleFeature f,
224     final String title, final String inTitle, MapLayer ml) {
225 alfonx 153 return new SearchResultFeature(f, title, inTitle, mapPane, ml);
226 alfonx 139 }
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