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.Feature; |
43 |
|
|
import org.geotools.feature.FeatureCollection; |
44 |
|
|
import org.geotools.feature.FeatureType; |
45 |
|
|
import org.geotools.map.MapLayer; |
46 |
|
|
import org.geotools.styling.Style; |
47 |
|
|
import org.geotools.styling.TextSymbolizer; |
48 |
|
|
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 |
alfonx |
468 |
|
67 |
alfonx |
139 |
public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil |
68 |
alfonx |
468 |
.extendPackagePath(LabelSearch.class, "labelsearch"), |
69 |
|
|
Locale.ENGLISH); |
70 |
|
|
|
71 |
alfonx |
139 |
public static String R(String key, Object... values) { |
72 |
|
|
return RESOURCE.getString(key, values); |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
protected final schmitzm.geotools.gui.JMapPane mapPane; |
76 |
|
|
|
77 |
|
|
public LabelSearch(final schmitzm.geotools.gui.JMapPane mapPane) { |
78 |
|
|
this.mapPane = mapPane; |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
private AttributeType getLabelAttribute(final TextSymbolizer ts, |
82 |
|
|
final FeatureType schema) { |
83 |
|
|
if (ts == null) { |
84 |
|
|
// This layer has no labels |
85 |
|
|
return null; |
86 |
|
|
} |
87 |
|
|
final Expression labelExp = ts.getLabel(); |
88 |
|
|
if (labelExp instanceof PropertyName) { |
89 |
|
|
final PropertyName pn = (PropertyName) labelExp; |
90 |
|
|
final String propertyName = pn.getPropertyName(); |
91 |
|
|
return schema.getAttributeType(propertyName); |
92 |
|
|
} else { |
93 |
|
|
// When does this happen |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
return null; |
97 |
|
|
} |
98 |
|
|
|
99 |
|
|
public List<SearchResult> search(final String string) { |
100 |
|
|
|
101 |
|
|
final String searchMe = string.toLowerCase(); |
102 |
|
|
|
103 |
|
|
final ArrayList<SearchResult> hits = new ArrayList<SearchResult>(); |
104 |
|
|
|
105 |
|
|
for (final MapLayer ml : mapPane.getContext().getLayers()) { |
106 |
|
|
try { |
107 |
|
|
|
108 |
|
|
// System.out.println("layer = "+ml.getTitle()); |
109 |
|
|
|
110 |
|
|
if (!ml.isVisible()) |
111 |
|
|
continue; |
112 |
|
|
|
113 |
alfonx |
468 |
final List<TextSymbolizer> allTS = StylingUtil |
114 |
|
|
.getTextSymbolizers(ml.getStyle()); |
115 |
alfonx |
139 |
if (allTS.size() == 0) { |
116 |
|
|
// A layer without any TextSymbolizer doesn't have to be |
117 |
|
|
// searched any more. |
118 |
|
|
continue; |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
final String typeName = ml.getFeatureSource().getSchema() |
122 |
|
|
.getTypeName(); |
123 |
|
|
|
124 |
|
|
// Expression labelExp = ts.getLabel(); |
125 |
|
|
// ff.like(labelExp, "*"+searchMe+"*"); |
126 |
|
|
// FeatureCollection features = |
127 |
|
|
// ml.getFeatureSource().getFeatures( |
128 |
|
|
// new DefaultQuery(typeName, ff.like(labelExp, |
129 |
|
|
// "*"+searchMe+"*"), properties)); |
130 |
|
|
|
131 |
alfonx |
468 |
final FeatureCollection features = ml |
132 |
|
|
.getFeatureSource() |
133 |
|
|
.getFeatures(new DefaultQuery(typeName, Filter.INCLUDE)); |
134 |
alfonx |
139 |
|
135 |
|
|
// new MemoryDataStore().getFeatureSource(typeName) |
136 |
|
|
|
137 |
|
|
/** |
138 |
|
|
* We do the comparison NOT with a ff.like, because that doesn't |
139 |
|
|
* support case insensitivity and i don't find a lower or UPPER |
140 |
|
|
* function. |
141 |
|
|
*/ |
142 |
|
|
final Iterator<Feature> fi = features.iterator(); |
143 |
alfonx |
468 |
try { |
144 |
|
|
while (fi.hasNext()) { |
145 |
|
|
final Feature f = fi.next(); |
146 |
alfonx |
139 |
|
147 |
alfonx |
468 |
final TextSymbolizer ts = StylingUtil |
148 |
|
|
.getTextSymbolizer(ml.getStyle(), f); |
149 |
|
|
if (ts == null) |
150 |
|
|
continue; |
151 |
alfonx |
139 |
|
152 |
alfonx |
468 |
final AttributeType labelAttribute = getLabelAttribute( |
153 |
|
|
ts, ml.getFeatureSource().getSchema()); |
154 |
alfonx |
139 |
|
155 |
alfonx |
468 |
if (labelAttribute == null) { |
156 |
|
|
continue; |
157 |
|
|
} |
158 |
alfonx |
139 |
|
159 |
alfonx |
468 |
// System.out.println("labelAttrib local name" + |
160 |
|
|
// labelAttribute.getLocalName()); |
161 |
alfonx |
139 |
|
162 |
alfonx |
468 |
final Object value = f.getAttribute(labelAttribute |
163 |
|
|
.getLocalName()); |
164 |
alfonx |
139 |
|
165 |
alfonx |
468 |
// System.out.println("labelAttrib value " + value); |
166 |
alfonx |
139 |
|
167 |
alfonx |
468 |
if (value == null) { |
168 |
|
|
LOGGER |
169 |
|
|
.info("Skipping f: getLocalName() is null for feature=" |
170 |
|
|
+ f); |
171 |
|
|
continue; |
172 |
|
|
} |
173 |
alfonx |
139 |
|
174 |
alfonx |
468 |
/** |
175 |
|
|
* LabelString ist z.B. "IMPETUS pluviograph". Suchwort |
176 |
|
|
* "plu" soll treffen. Also wird nach spaces zerlegt und |
177 |
|
|
* dann gesucht |
178 |
|
|
*/ |
179 |
|
|
final String labelString = value.toString() |
180 |
|
|
.toLowerCase(); |
181 |
|
|
if (labelString.startsWith(searchMe)) { |
182 |
|
|
hits.add(createSearchResult(f, value.toString(), ml |
183 |
|
|
.getTitle(), ml)); |
184 |
|
|
} else { |
185 |
|
|
final String[] parts = labelString.trim() |
186 |
|
|
.split(" "); |
187 |
|
|
for (final String part : parts) { |
188 |
|
|
if (part.startsWith(searchMe)) { |
189 |
|
|
hits.add(createSearchResult(f, value |
190 |
|
|
.toString(), ml.getTitle(), ml)); |
191 |
|
|
break; |
192 |
|
|
} |
193 |
alfonx |
139 |
} |
194 |
|
|
} |
195 |
alfonx |
468 |
|
196 |
alfonx |
139 |
} |
197 |
alfonx |
468 |
} finally { |
198 |
|
|
features.close(fi); |
199 |
alfonx |
139 |
} |
200 |
|
|
|
201 |
|
|
} catch (final IOException e) { |
202 |
|
|
// Searching this layer failed |
203 |
|
|
LOGGER.error(e); |
204 |
|
|
} |
205 |
|
|
} // next layer |
206 |
|
|
|
207 |
|
|
// Hits from the top-most layer should appear first. |
208 |
|
|
Collections.reverse(hits); |
209 |
|
|
|
210 |
|
|
return hits; |
211 |
|
|
} |
212 |
|
|
|
213 |
alfonx |
468 |
protected SearchResult createSearchResult(final Feature f, |
214 |
|
|
final String title, final String inTitle, MapLayer ml) { |
215 |
alfonx |
153 |
return new SearchResultFeature(f, title, inTitle, mapPane, ml); |
216 |
alfonx |
139 |
} |
217 |
|
|
|
218 |
|
|
} |