/[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 685 - (hide annotations)
Wed Feb 10 15:04:02 2010 UTC (15 years ago) by alfonx
File MIME type: text/plain
File size: 6656 byte(s)
copy RC2 to trunk
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 607 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 139 import org.opengis.filter.Filter;
51     import org.opengis.filter.expression.PropertyName;
52    
53 alfonx 607 import schmitzm.geotools.feature.FeatureUtil;
54 alfonx 139 import schmitzm.geotools.styling.StylingUtil;
55     import schmitzm.lang.LangUtil;
56     import schmitzm.lang.ResourceProvider;
57    
58     /**
59     * This class allows to search for a {@link String} in a map. The algorithm will
60     * analyze the {@link Style} of every visible(?) layer and determine the label
61     * attribute. This field is then searched for every feature.
62     *
63     * @author Stefan A. Krüger
64     *
65     */
66     public class LabelSearch {
67     final static private Logger LOGGER = Logger.getLogger(LabelSearch.class);
68 alfonx 456
69 alfonx 139 public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil
70 alfonx 456 .extendPackagePath(LabelSearch.class, "labelsearch"),
71     Locale.ENGLISH);
72    
73 alfonx 139 public static String R(String key, Object... values) {
74     return RESOURCE.getString(key, values);
75     }
76    
77 alfonx 509 protected final schmitzm.geotools.gui.SelectableXMapPane mapPane;
78 alfonx 139
79 alfonx 509 public LabelSearch(final schmitzm.geotools.gui.SelectableXMapPane mapPane) {
80 alfonx 139 this.mapPane = mapPane;
81     }
82    
83     public List<SearchResult> search(final String string) {
84    
85 alfonx 607 final String searchMe = string.toUpperCase();
86 alfonx 139
87     final ArrayList<SearchResult> hits = new ArrayList<SearchResult>();
88    
89 alfonx 551 for (final MapLayer ml : mapPane.getMapContext().getLayers()) {
90 alfonx 139 try {
91    
92     // System.out.println("layer = "+ml.getTitle());
93    
94     if (!ml.isVisible())
95     continue;
96    
97 alfonx 456 final List<TextSymbolizer> allTS = StylingUtil
98     .getVisibleTextSymbolizers(ml.getStyle());
99 alfonx 139 if (allTS.size() == 0) {
100     // A layer without any TextSymbolizer doesn't have to be
101     // searched any more.
102     continue;
103     }
104    
105 alfonx 607 // We only deal with one TextSymbolizer so far:
106     TextSymbolizer ts = allTS.get(0);
107    
108 alfonx 456 final FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) ml
109     .getFeatureSource();
110    
111 alfonx 607 SimpleFeatureType schema = featureSource.getSchema();
112 alfonx 456
113 alfonx 607 final String typeName = schema.getName().getLocalPart();
114 alfonx 139
115 alfonx 607 PropertyName prop1 = StylingUtil.getFirstPropertyName(schema,
116     ts);
117     PropertyName prop2 = StylingUtil.getSecondPropertyName(schema,
118     ts);
119 alfonx 139
120 alfonx 607 if (StylingUtil.getFirstPropertyName(schema, ts) == null) {
121     // At least one property field we need
122     continue;
123     }
124 alfonx 139
125 alfonx 607 Filter searchFilter;
126     String[] properties = new String[] { schema.getGeometryDescriptor().getLocalName(), prop1.getPropertyName() };
127     // Only one property used...
128 alfonx 139
129 alfonx 607 searchFilter = CQL.toFilter("strToUpperCase "
130     + prop1.getPropertyName() + " LIKE '%" + searchMe
131     + "%'");
132 alfonx 139
133 alfonx 607 if (prop2 != null) {
134     Filter searchFilter2 = CQL.toFilter("strToUpperCase "
135     + prop2.getPropertyName() + " LIKE '%" + searchMe
136     + "%'");
137 alfonx 139
138 alfonx 607 searchFilter = FeatureUtil.FILTER_FACTORY2.or(searchFilter,
139     searchFilter2);
140 alfonx 139
141 alfonx 607 properties = LangUtil.extendArray(properties, prop2
142     .getPropertyName());
143     }
144 alfonx 139
145 alfonx 607 // Add the layer's filter if it exists
146     Filter layerFilter = ml.getQuery().getFilter();
147     if (layerFilter != null && layerFilter != Filter.INCLUDE) {
148     searchFilter = FeatureUtil.FILTER_FACTORY2.and(layerFilter,
149     searchFilter);
150     }
151    
152     // LOGGER.info("Searching for "+searchFilter.toString());
153 alfonx 139
154 alfonx 607 FeatureCollection<SimpleFeatureType, SimpleFeature> features = (FeatureCollection<SimpleFeatureType, SimpleFeature>) ml
155     .getFeatureSource().getFeatures(
156     new DefaultQuery(typeName, searchFilter,
157     properties));
158 alfonx 139
159 alfonx 607 final Iterator<SimpleFeature> fi = features.iterator();
160     try {
161     while (fi.hasNext()) {
162     final SimpleFeature f = fi.next();
163 alfonx 139
164 alfonx 607 String valueString = "";
165     valueString = f.getAttribute(prop1.getPropertyName())
166     .toString();
167 alfonx 456
168 alfonx 607 if (prop2 != null) {
169     String valueString2 = f.getAttribute(
170     prop2.getPropertyName()).toString();
171    
172     if (valueString2 != null && !valueString2.isEmpty())
173     valueString += ", " + valueString2;
174 alfonx 139 }
175 alfonx 456
176 alfonx 607 hits.add(createSearchResult(f, valueString, ml
177     .getTitle(), ml));
178 alfonx 139 }
179 alfonx 456 } finally {
180     features.close(fi);
181 alfonx 139 }
182     } catch (final IOException e) {
183     // Searching this layer failed
184 alfonx 607 LOGGER.error("",e);
185     } catch (CQLException e) {
186     LOGGER.error("",e);
187 alfonx 139 }
188     } // next layer
189    
190     // Hits from the top-most layer should appear first.
191     Collections.reverse(hits);
192    
193     return hits;
194     }
195    
196 alfonx 456 protected SearchResult createSearchResult(final SimpleFeature f,
197     final String title, final String inTitle, MapLayer ml) {
198 alfonx 153 return new SearchResultFeature(f, title, inTitle, mapPane, ml);
199 alfonx 139 }
200    
201     }

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