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

Annotation of /branches/2.3.x/src/skrueger/geotools/labelsearch/LabelSearch.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 335 - (hide annotations)
Wed Aug 26 18:09:39 2009 UTC (15 years, 6 months ago) by alfonx
Original Path: branches/1.0-gt2-2.6/src/skrueger/geotools/labelsearch/LabelSearch.java
File MIME type: text/plain
File size: 7106 byte(s)
Migrating to GT2.6...
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     import org.geotools.map.MapLayer;
44     import org.geotools.styling.Style;
45     import org.geotools.styling.TextSymbolizer;
46 alfonx 335 import org.opengis.feature.Feature;
47 mojays 325 import org.opengis.feature.simple.SimpleFeature;
48     import org.opengis.feature.simple.SimpleFeatureType;
49 alfonx 332 import org.opengis.feature.type.AttributeDescriptor;
50 alfonx 335 import org.opengis.feature.type.FeatureType;
51 alfonx 139 import org.opengis.filter.Filter;
52     import org.opengis.filter.expression.Expression;
53     import org.opengis.filter.expression.PropertyName;
54    
55     import schmitzm.geotools.styling.StylingUtil;
56     import schmitzm.lang.LangUtil;
57     import schmitzm.lang.ResourceProvider;
58    
59     /**
60     * This class allows to search for a {@link String} in a map. The algorithm will
61     * analyze the {@link Style} of every visible(?) layer and determine the label
62     * attribute. This field is then searched for every feature.
63     *
64     * @author Stefan A. Krüger
65     *
66     */
67     public class LabelSearch {
68     final static private Logger LOGGER = Logger.getLogger(LabelSearch.class);
69    
70     public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil
71     .extendPackagePath(LabelSearch.class,
72     "labelsearch"), Locale.ENGLISH);
73    
74    
75    
76    
77     public static String R(String key, Object... values) {
78     return RESOURCE.getString(key, values);
79     }
80    
81     protected final schmitzm.geotools.gui.JMapPane mapPane;
82    
83     public LabelSearch(final schmitzm.geotools.gui.JMapPane mapPane) {
84     this.mapPane = mapPane;
85     }
86    
87 alfonx 335 /**
88     * The Attribute that provides the labels for this text symbolizer.
89     */
90 alfonx 332 private AttributeDescriptor getLabelAttribute(final TextSymbolizer ts,
91 alfonx 318 final SimpleFeatureType schema) {
92 alfonx 139 if (ts == null) {
93     // This layer has no labels
94     return null;
95     }
96 alfonx 335
97 alfonx 139 final Expression labelExp = ts.getLabel();
98     if (labelExp instanceof PropertyName) {
99     final PropertyName pn = (PropertyName) labelExp;
100     final String propertyName = pn.getPropertyName();
101 alfonx 335 return schema.getDescriptor(propertyName);
102 alfonx 139 } else {
103     // When does this happen
104 alfonx 335 throw new RuntimeException("labelExp "+labelExp+" IS NOT instanceof PropertyName!");
105 alfonx 139 }
106    
107     }
108    
109     public List<SearchResult> search(final String string) {
110    
111     final String searchMe = string.toLowerCase();
112    
113     final ArrayList<SearchResult> hits = new ArrayList<SearchResult>();
114    
115     for (final MapLayer ml : mapPane.getContext().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.getTextSymbolizers(ml
124     .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 alfonx 335 // 26 CAST!
132     final FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) ml.getFeatureSource();
133    
134     final String typeName = featureSource.getSchema()
135     .getName().getLocalPart();
136 alfonx 139
137     // Expression labelExp = ts.getLabel();
138     // ff.like(labelExp, "*"+searchMe+"*");
139     // FeatureCollection features =
140     // ml.getFeatureSource().getFeatures(
141     // new DefaultQuery(typeName, ff.like(labelExp,
142     // "*"+searchMe+"*"), properties));
143    
144 alfonx 335 final FeatureCollection<SimpleFeatureType, SimpleFeature> features = featureSource.getFeatures(
145 alfonx 139 new DefaultQuery(typeName, Filter.INCLUDE));
146    
147     // new MemoryDataStore().getFeatureSource(typeName)
148    
149     /**
150     * We do the comparison NOT with a ff.like, because that doesn't
151     * support case insensitivity and i don't find a lower or UPPER
152     * function.
153     */
154 alfonx 318 final Iterator<SimpleFeature> fi = features.iterator();
155 alfonx 139 while (fi.hasNext()) {
156 alfonx 318 final SimpleFeature f = fi.next();
157 alfonx 139
158     final TextSymbolizer ts = StylingUtil.getTextSymbolizer(ml
159     .getStyle(), f);
160     if (ts == null)
161     continue;
162    
163 alfonx 335 final AttributeDescriptor labelAttribute = getLabelAttribute(ts, featureSource.getSchema());
164 alfonx 139
165     if (labelAttribute == null) {
166     continue;
167     }
168    
169     // System.out.println("labelAttrib local name" +
170     // labelAttribute.getLocalName());
171    
172     final Object value = f
173     .getAttribute(labelAttribute.getLocalName());
174    
175     // System.out.println("labelAttrib value " + value);
176    
177     if (value == null) {
178     LOGGER.info("Skipping f: getLocalName() is null for feature="+f);
179     continue;
180     }
181    
182     /**
183     * LabelString ist z.B. "IMPETUS pluviograph". Suchwort
184     * "plu" soll treffen. Also wird nach spaces zerlegt und
185     * dann gesucht
186     */
187     final String labelString = value.toString().toLowerCase();
188     if (labelString.startsWith(searchMe)) {
189     hits.add(createSearchResult(f, value.toString(), ml
190 alfonx 153 .getTitle(),ml));
191 alfonx 139 } else {
192     final String[] parts = labelString.trim().split(" ");
193     for (final String part : parts) {
194     if (part.startsWith(searchMe)) {
195     hits.add(createSearchResult(f, value.toString(), ml
196 alfonx 153 .getTitle(), ml));
197 alfonx 139 break;
198     }
199     }
200     }
201    
202     }
203    
204     } catch (final IOException e) {
205     // Searching this layer failed
206     LOGGER.error(e);
207     }
208     } // next layer
209    
210     // Hits from the top-most layer should appear first.
211     Collections.reverse(hits);
212    
213     return hits;
214     }
215    
216 alfonx 318 protected SearchResult createSearchResult(final SimpleFeature f, final String title,
217 alfonx 153 final String inTitle, MapLayer ml) {
218     return new SearchResultFeature(f, title, inTitle, mapPane, ml);
219 alfonx 139 }
220    
221     }

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