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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 897 - (hide annotations)
Mon Jun 7 14:14:39 2010 UTC (14 years, 8 months ago) by alfonx
File MIME type: text/plain
File size: 6535 byte(s)
Das ist 2.2-SNAPSHOT

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 alfonx 862 * Stefan A. Tzeggai - additional utility classes
29 alfonx 244 ******************************************************************************/
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    
38     import org.apache.log4j.Logger;
39     import org.geotools.data.DefaultQuery;
40 alfonx 335 import org.geotools.data.FeatureSource;
41 alfonx 139 import org.geotools.feature.FeatureCollection;
42 alfonx 607 import org.geotools.filter.text.cql2.CQL;
43     import org.geotools.filter.text.cql2.CQLException;
44 alfonx 139 import org.geotools.map.MapLayer;
45     import org.geotools.styling.Style;
46     import org.geotools.styling.TextSymbolizer;
47 mojays 325 import org.opengis.feature.simple.SimpleFeature;
48     import org.opengis.feature.simple.SimpleFeatureType;
49 alfonx 139 import org.opengis.filter.Filter;
50     import org.opengis.filter.expression.PropertyName;
51    
52 alfonx 607 import schmitzm.geotools.feature.FeatureUtil;
53 alfonx 886 import schmitzm.geotools.gui.GeotoolsGUIUtil;
54 alfonx 139 import schmitzm.geotools.styling.StylingUtil;
55     import schmitzm.lang.LangUtil;
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 alfonx 862 * @author Stefan A. Tzeggai
63 alfonx 139 *
64     */
65     public class LabelSearch {
66     final static private Logger LOGGER = Logger.getLogger(LabelSearch.class);
67 alfonx 456
68 alfonx 887 /** Translations come from GTResourceBundle **/
69 alfonx 139 public static String R(String key, Object... values) {
70 alfonx 886 return GeotoolsGUIUtil.R(key, values);
71 alfonx 139 }
72    
73 alfonx 509 protected final schmitzm.geotools.gui.SelectableXMapPane mapPane;
74 alfonx 139
75 alfonx 509 public LabelSearch(final schmitzm.geotools.gui.SelectableXMapPane mapPane) {
76 alfonx 139 this.mapPane = mapPane;
77     }
78    
79     public List<SearchResult> search(final String string) {
80    
81 alfonx 607 final String searchMe = string.toUpperCase();
82 alfonx 139
83     final ArrayList<SearchResult> hits = new ArrayList<SearchResult>();
84    
85 alfonx 551 for (final MapLayer ml : mapPane.getMapContext().getLayers()) {
86 alfonx 139 try {
87    
88     // System.out.println("layer = "+ml.getTitle());
89    
90     if (!ml.isVisible())
91     continue;
92    
93 alfonx 456 final List<TextSymbolizer> allTS = StylingUtil
94     .getVisibleTextSymbolizers(ml.getStyle());
95 alfonx 139 if (allTS.size() == 0) {
96     // A layer without any TextSymbolizer doesn't have to be
97     // searched any more.
98     continue;
99     }
100    
101 alfonx 607 // We only deal with one TextSymbolizer so far:
102     TextSymbolizer ts = allTS.get(0);
103    
104 alfonx 456 final FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) ml
105     .getFeatureSource();
106    
107 alfonx 607 SimpleFeatureType schema = featureSource.getSchema();
108 alfonx 456
109 alfonx 607 final String typeName = schema.getName().getLocalPart();
110 alfonx 139
111 alfonx 607 PropertyName prop1 = StylingUtil.getFirstPropertyName(schema,
112     ts);
113     PropertyName prop2 = StylingUtil.getSecondPropertyName(schema,
114     ts);
115 alfonx 139
116 alfonx 607 if (StylingUtil.getFirstPropertyName(schema, ts) == null) {
117     // At least one property field we need
118     continue;
119     }
120 alfonx 139
121 alfonx 607 Filter searchFilter;
122     String[] properties = new String[] { schema.getGeometryDescriptor().getLocalName(), prop1.getPropertyName() };
123     // Only one property used...
124 alfonx 139
125 alfonx 607 searchFilter = CQL.toFilter("strToUpperCase "
126     + prop1.getPropertyName() + " LIKE '%" + searchMe
127     + "%'");
128 alfonx 139
129 alfonx 607 if (prop2 != null) {
130     Filter searchFilter2 = CQL.toFilter("strToUpperCase "
131     + prop2.getPropertyName() + " LIKE '%" + searchMe
132     + "%'");
133 alfonx 139
134 alfonx 607 searchFilter = FeatureUtil.FILTER_FACTORY2.or(searchFilter,
135     searchFilter2);
136 alfonx 139
137 alfonx 607 properties = LangUtil.extendArray(properties, prop2
138     .getPropertyName());
139     }
140 alfonx 139
141 alfonx 607 // Add the layer's filter if it exists
142     Filter layerFilter = ml.getQuery().getFilter();
143     if (layerFilter != null && layerFilter != Filter.INCLUDE) {
144     searchFilter = FeatureUtil.FILTER_FACTORY2.and(layerFilter,
145     searchFilter);
146     }
147    
148     // LOGGER.info("Searching for "+searchFilter.toString());
149 alfonx 139
150 alfonx 607 FeatureCollection<SimpleFeatureType, SimpleFeature> features = (FeatureCollection<SimpleFeatureType, SimpleFeature>) ml
151     .getFeatureSource().getFeatures(
152     new DefaultQuery(typeName, searchFilter,
153     properties));
154 alfonx 139
155 alfonx 607 final Iterator<SimpleFeature> fi = features.iterator();
156     try {
157     while (fi.hasNext()) {
158     final SimpleFeature f = fi.next();
159 alfonx 139
160 alfonx 607 String valueString = "";
161     valueString = f.getAttribute(prop1.getPropertyName())
162     .toString();
163 alfonx 456
164 alfonx 607 if (prop2 != null) {
165     String valueString2 = f.getAttribute(
166     prop2.getPropertyName()).toString();
167    
168     if (valueString2 != null && !valueString2.isEmpty())
169     valueString += ", " + valueString2;
170 alfonx 139 }
171 alfonx 456
172 alfonx 607 hits.add(createSearchResult(f, valueString, ml
173     .getTitle(), ml));
174 alfonx 139 }
175 alfonx 456 } finally {
176     features.close(fi);
177 alfonx 139 }
178     } catch (final IOException e) {
179     // Searching this layer failed
180 alfonx 607 LOGGER.error("",e);
181     } catch (CQLException e) {
182     LOGGER.error("",e);
183 alfonx 139 }
184     } // next layer
185    
186     // Hits from the top-most layer should appear first.
187     Collections.reverse(hits);
188    
189     return hits;
190     }
191    
192 alfonx 456 protected SearchResult createSearchResult(final SimpleFeature f,
193     final String title, final String inTitle, MapLayer ml) {
194 alfonx 153 return new SearchResultFeature(f, title, inTitle, mapPane, ml);
195 alfonx 139 }
196    
197     }

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