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

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