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

Annotation of /trunk/src/skrueger/geotools/labelsearch/LabelSearch.java

Parent Directory Parent Directory | Revision Log Revision Log


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

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