1 |
|
/******************************************************************************* |
2 |
|
* Copyright (c) 2009 Martin O. J. Schmitz. |
3 |
|
* |
4 |
|
* This file is part of the SCHMITZM library - a collection of utility |
5 |
|
* classes based on Java 1.6, focusing (not only) on Java Swing |
6 |
|
* 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 |
package skrueger.geotools.labelsearch; |
package skrueger.geotools.labelsearch; |
31 |
|
|
32 |
import java.io.IOException; |
import java.io.IOException; |
38 |
|
|
39 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
40 |
import org.geotools.data.DefaultQuery; |
import org.geotools.data.DefaultQuery; |
41 |
import org.geotools.feature.AttributeType; |
import org.geotools.data.FeatureSource; |
|
import org.geotools.feature.Feature; |
|
42 |
import org.geotools.feature.FeatureCollection; |
import org.geotools.feature.FeatureCollection; |
|
import org.geotools.feature.FeatureType; |
|
43 |
import org.geotools.map.MapLayer; |
import org.geotools.map.MapLayer; |
44 |
import org.geotools.styling.Style; |
import org.geotools.styling.Style; |
45 |
import org.geotools.styling.TextSymbolizer; |
import org.geotools.styling.TextSymbolizer; |
46 |
|
import org.opengis.feature.simple.SimpleFeature; |
47 |
|
import org.opengis.feature.simple.SimpleFeatureType; |
48 |
|
import org.opengis.feature.type.AttributeDescriptor; |
49 |
import org.opengis.filter.Filter; |
import org.opengis.filter.Filter; |
50 |
import org.opengis.filter.expression.Expression; |
import org.opengis.filter.expression.Expression; |
51 |
import org.opengis.filter.expression.PropertyName; |
import org.opengis.filter.expression.PropertyName; |
82 |
this.mapPane = mapPane; |
this.mapPane = mapPane; |
83 |
} |
} |
84 |
|
|
85 |
private AttributeType getLabelAttribute(final TextSymbolizer ts, |
/** |
86 |
final FeatureType schema) { |
* The Attribute that provides the labels for this text symbolizer. |
87 |
|
*/ |
88 |
|
private AttributeDescriptor getLabelAttribute(final TextSymbolizer ts, |
89 |
|
final SimpleFeatureType schema) { |
90 |
if (ts == null) { |
if (ts == null) { |
91 |
// This layer has no labels |
// This layer has no labels |
92 |
return null; |
return null; |
93 |
} |
} |
94 |
|
|
95 |
final Expression labelExp = ts.getLabel(); |
final Expression labelExp = ts.getLabel(); |
96 |
if (labelExp instanceof PropertyName) { |
if (labelExp instanceof PropertyName) { |
97 |
final PropertyName pn = (PropertyName) labelExp; |
final PropertyName pn = (PropertyName) labelExp; |
98 |
final String propertyName = pn.getPropertyName(); |
final String propertyName = pn.getPropertyName(); |
99 |
return schema.getAttributeType(propertyName); |
return schema.getDescriptor(propertyName); |
100 |
} else { |
} else { |
101 |
// When does this happen |
// When does this happen |
102 |
|
throw new RuntimeException("labelExp "+labelExp+" IS NOT instanceof PropertyName!"); |
103 |
} |
} |
104 |
|
|
|
return null; |
|
105 |
} |
} |
106 |
|
|
107 |
public List<SearchResult> search(final String string) { |
public List<SearchResult> search(final String string) { |
118 |
if (!ml.isVisible()) |
if (!ml.isVisible()) |
119 |
continue; |
continue; |
120 |
|
|
121 |
final List<TextSymbolizer> allTS = StylingUtil.getTextSymbolizers(ml |
final List<TextSymbolizer> allTS = StylingUtil.getVisibleTextSymbolizers(ml |
122 |
.getStyle()); |
.getStyle()); |
123 |
if (allTS.size() == 0) { |
if (allTS.size() == 0) { |
124 |
// A layer without any TextSymbolizer doesn't have to be |
// A layer without any TextSymbolizer doesn't have to be |
125 |
// searched any more. |
// searched any more. |
126 |
continue; |
continue; |
127 |
} |
} |
128 |
|
|
129 |
final String typeName = ml.getFeatureSource().getSchema() |
final FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) ml.getFeatureSource(); |
130 |
.getTypeName(); |
|
131 |
|
final String typeName = featureSource.getSchema() |
132 |
|
.getName().getLocalPart(); |
133 |
|
|
134 |
// Expression labelExp = ts.getLabel(); |
// Expression labelExp = ts.getLabel(); |
135 |
// ff.like(labelExp, "*"+searchMe+"*"); |
// ff.like(labelExp, "*"+searchMe+"*"); |
138 |
// new DefaultQuery(typeName, ff.like(labelExp, |
// new DefaultQuery(typeName, ff.like(labelExp, |
139 |
// "*"+searchMe+"*"), properties)); |
// "*"+searchMe+"*"), properties)); |
140 |
|
|
141 |
final FeatureCollection features = ml.getFeatureSource().getFeatures( |
final FeatureCollection<SimpleFeatureType, SimpleFeature> features = featureSource.getFeatures( |
142 |
new DefaultQuery(typeName, Filter.INCLUDE)); |
new DefaultQuery(typeName, Filter.INCLUDE)); |
143 |
|
|
144 |
// new MemoryDataStore().getFeatureSource(typeName) |
// new MemoryDataStore().getFeatureSource(typeName) |
148 |
* support case insensitivity and i don't find a lower or UPPER |
* support case insensitivity and i don't find a lower or UPPER |
149 |
* function. |
* function. |
150 |
*/ |
*/ |
151 |
final Iterator<Feature> fi = features.iterator(); |
final Iterator<SimpleFeature> fi = features.iterator(); |
152 |
while (fi.hasNext()) { |
while (fi.hasNext()) { |
153 |
final Feature f = fi.next(); |
final SimpleFeature f = fi.next(); |
154 |
|
|
155 |
final TextSymbolizer ts = StylingUtil.getTextSymbolizer(ml |
final TextSymbolizer ts = StylingUtil.getTextSymbolizer(ml |
156 |
.getStyle(), f); |
.getStyle(), f); |
157 |
if (ts == null) |
if (ts == null) |
158 |
continue; |
continue; |
159 |
|
|
160 |
final AttributeType labelAttribute = getLabelAttribute(ts, ml |
// TODO Evaluate the Rule that belongs to the TextSymbolizer against the feature... |
161 |
.getFeatureSource().getSchema()); |
final AttributeDescriptor labelAttribute = getLabelAttribute(ts, featureSource.getSchema()); |
162 |
|
|
163 |
if (labelAttribute == null) { |
if (labelAttribute == null) { |
164 |
continue; |
continue; |
185 |
final String labelString = value.toString().toLowerCase(); |
final String labelString = value.toString().toLowerCase(); |
186 |
if (labelString.startsWith(searchMe)) { |
if (labelString.startsWith(searchMe)) { |
187 |
hits.add(createSearchResult(f, value.toString(), ml |
hits.add(createSearchResult(f, value.toString(), ml |
188 |
.getTitle())); |
.getTitle(),ml)); |
189 |
} else { |
} else { |
190 |
final String[] parts = labelString.trim().split(" "); |
final String[] parts = labelString.trim().split(" "); |
191 |
for (final String part : parts) { |
for (final String part : parts) { |
192 |
if (part.startsWith(searchMe)) { |
if (part.startsWith(searchMe)) { |
193 |
hits.add(createSearchResult(f, value.toString(), ml |
hits.add(createSearchResult(f, value.toString(), ml |
194 |
.getTitle())); |
.getTitle(), ml)); |
195 |
break; |
break; |
196 |
} |
} |
197 |
} |
} |
211 |
return hits; |
return hits; |
212 |
} |
} |
213 |
|
|
214 |
protected SearchResult createSearchResult(final Feature f, final String title, |
protected SearchResult createSearchResult(final SimpleFeature f, final String title, |
215 |
final String inTitle) { |
final String inTitle, MapLayer ml) { |
216 |
return new SearchResultFeature(f, title, inTitle, mapPane); |
return new SearchResultFeature(f, title, inTitle, mapPane, ml); |
217 |
} |
} |
218 |
|
|
219 |
} |
} |