25 |
* |
* |
26 |
* Contributors: |
* Contributors: |
27 |
* Martin O. J. Schmitz - initial API and implementation |
* Martin O. J. Schmitz - initial API and implementation |
28 |
* Stefan A. Krüger - additional utility classes |
* Stefan A. Tzeggai - additional utility classes |
29 |
******************************************************************************/ |
******************************************************************************/ |
30 |
package skrueger.geotools.labelsearch; |
package skrueger.geotools.labelsearch; |
31 |
|
|
34 |
import java.util.Collections; |
import java.util.Collections; |
35 |
import java.util.Iterator; |
import java.util.Iterator; |
36 |
import java.util.List; |
import java.util.List; |
|
import java.util.Locale; |
|
37 |
|
|
38 |
import org.apache.log4j.Logger; |
import org.apache.log4j.Logger; |
39 |
import org.geotools.data.DefaultQuery; |
import org.geotools.data.DefaultQuery; |
40 |
import org.geotools.data.FeatureSource; |
import org.geotools.data.FeatureSource; |
41 |
import org.geotools.feature.FeatureCollection; |
import org.geotools.feature.FeatureCollection; |
42 |
|
import org.geotools.filter.text.cql2.CQL; |
43 |
|
import org.geotools.filter.text.cql2.CQLException; |
44 |
import org.geotools.map.MapLayer; |
import org.geotools.map.MapLayer; |
45 |
import org.geotools.styling.Style; |
import org.geotools.styling.Style; |
46 |
import org.geotools.styling.TextSymbolizer; |
import org.geotools.styling.TextSymbolizer; |
47 |
import org.opengis.feature.simple.SimpleFeature; |
import org.opengis.feature.simple.SimpleFeature; |
48 |
import org.opengis.feature.simple.SimpleFeatureType; |
import org.opengis.feature.simple.SimpleFeatureType; |
|
import org.opengis.feature.type.AttributeDescriptor; |
|
49 |
import org.opengis.filter.Filter; |
import org.opengis.filter.Filter; |
|
import org.opengis.filter.expression.Expression; |
|
50 |
import org.opengis.filter.expression.PropertyName; |
import org.opengis.filter.expression.PropertyName; |
51 |
|
|
52 |
|
import schmitzm.geotools.feature.FeatureUtil; |
53 |
|
import schmitzm.geotools.gui.GeotoolsGUIUtil; |
54 |
import schmitzm.geotools.styling.StylingUtil; |
import schmitzm.geotools.styling.StylingUtil; |
55 |
import schmitzm.lang.LangUtil; |
import schmitzm.lang.LangUtil; |
|
import schmitzm.lang.ResourceProvider; |
|
56 |
|
|
57 |
/** |
/** |
58 |
* This class allows to search for a {@link String} in a map. The algorithm will |
* 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 |
* analyze the {@link Style} of every visible(?) layer and determine the label |
60 |
* attribute. This field is then searched for every feature. |
* attribute. This field is then searched for every feature. |
61 |
* |
* |
62 |
* @author Stefan A. Krüger |
* @author Stefan A. Tzeggai |
63 |
* |
* |
64 |
*/ |
*/ |
65 |
public class LabelSearch { |
public class LabelSearch { |
66 |
final static private Logger LOGGER = Logger.getLogger(LabelSearch.class); |
final static private Logger LOGGER = Logger.getLogger(LabelSearch.class); |
67 |
|
|
|
public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil |
|
|
.extendPackagePath(LabelSearch.class, |
|
|
"labelsearch"), Locale.ENGLISH); |
|
|
|
|
|
|
|
|
|
|
|
|
|
68 |
public static String R(String key, Object... values) { |
public static String R(String key, Object... values) { |
69 |
return RESOURCE.getString(key, values); |
return GeotoolsGUIUtil.R(key, values); |
70 |
} |
} |
71 |
|
|
72 |
protected final schmitzm.geotools.gui.JMapPane mapPane; |
protected final schmitzm.geotools.gui.SelectableXMapPane mapPane; |
73 |
|
|
74 |
public LabelSearch(final schmitzm.geotools.gui.JMapPane mapPane) { |
public LabelSearch(final schmitzm.geotools.gui.SelectableXMapPane mapPane) { |
75 |
this.mapPane = mapPane; |
this.mapPane = mapPane; |
76 |
} |
} |
77 |
|
|
|
/** |
|
|
* The Attribute that provides the labels for this text symbolizer. |
|
|
*/ |
|
|
private AttributeDescriptor getLabelAttribute(final TextSymbolizer ts, |
|
|
final SimpleFeatureType schema) { |
|
|
if (ts == null) { |
|
|
// This layer has no labels |
|
|
return null; |
|
|
} |
|
|
|
|
|
final Expression labelExp = ts.getLabel(); |
|
|
if (labelExp instanceof PropertyName) { |
|
|
final PropertyName pn = (PropertyName) labelExp; |
|
|
final String propertyName = pn.getPropertyName(); |
|
|
return schema.getDescriptor(propertyName); |
|
|
} else { |
|
|
// When does this happen |
|
|
throw new RuntimeException("labelExp "+labelExp+" IS NOT instanceof PropertyName!"); |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
78 |
public List<SearchResult> search(final String string) { |
public List<SearchResult> search(final String string) { |
79 |
|
|
80 |
final String searchMe = string.toLowerCase(); |
final String searchMe = string.toUpperCase(); |
81 |
|
|
82 |
final ArrayList<SearchResult> hits = new ArrayList<SearchResult>(); |
final ArrayList<SearchResult> hits = new ArrayList<SearchResult>(); |
83 |
|
|
84 |
for (final MapLayer ml : mapPane.getContext().getLayers()) { |
for (final MapLayer ml : mapPane.getMapContext().getLayers()) { |
85 |
try { |
try { |
86 |
|
|
87 |
// System.out.println("layer = "+ml.getTitle()); |
// System.out.println("layer = "+ml.getTitle()); |
89 |
if (!ml.isVisible()) |
if (!ml.isVisible()) |
90 |
continue; |
continue; |
91 |
|
|
92 |
final List<TextSymbolizer> allTS = StylingUtil.getTextSymbolizers(ml |
final List<TextSymbolizer> allTS = StylingUtil |
93 |
.getStyle()); |
.getVisibleTextSymbolizers(ml.getStyle()); |
94 |
if (allTS.size() == 0) { |
if (allTS.size() == 0) { |
95 |
// A layer without any TextSymbolizer doesn't have to be |
// A layer without any TextSymbolizer doesn't have to be |
96 |
// searched any more. |
// searched any more. |
97 |
continue; |
continue; |
98 |
} |
} |
99 |
|
|
100 |
// 26 CAST! |
// We only deal with one TextSymbolizer so far: |
101 |
final FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) ml.getFeatureSource(); |
TextSymbolizer ts = allTS.get(0); |
|
|
|
|
final String typeName = featureSource.getSchema() |
|
|
.getName().getLocalPart(); |
|
102 |
|
|
103 |
// Expression labelExp = ts.getLabel(); |
final FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) ml |
104 |
// ff.like(labelExp, "*"+searchMe+"*"); |
.getFeatureSource(); |
|
// FeatureCollection features = |
|
|
// ml.getFeatureSource().getFeatures( |
|
|
// new DefaultQuery(typeName, ff.like(labelExp, |
|
|
// "*"+searchMe+"*"), properties)); |
|
|
|
|
|
final FeatureCollection<SimpleFeatureType, SimpleFeature> features = featureSource.getFeatures( |
|
|
new DefaultQuery(typeName, Filter.INCLUDE)); |
|
|
|
|
|
// new MemoryDataStore().getFeatureSource(typeName) |
|
|
|
|
|
/** |
|
|
* We do the comparison NOT with a ff.like, because that doesn't |
|
|
* support case insensitivity and i don't find a lower or UPPER |
|
|
* function. |
|
|
*/ |
|
|
final Iterator<SimpleFeature> fi = features.iterator(); |
|
|
while (fi.hasNext()) { |
|
|
final SimpleFeature f = fi.next(); |
|
105 |
|
|
106 |
final TextSymbolizer ts = StylingUtil.getTextSymbolizer(ml |
SimpleFeatureType schema = featureSource.getSchema(); |
|
.getStyle(), f); |
|
|
if (ts == null) |
|
|
continue; |
|
107 |
|
|
108 |
final AttributeDescriptor labelAttribute = getLabelAttribute(ts, featureSource.getSchema()); |
final String typeName = schema.getName().getLocalPart(); |
109 |
|
|
110 |
if (labelAttribute == null) { |
PropertyName prop1 = StylingUtil.getFirstPropertyName(schema, |
111 |
continue; |
ts); |
112 |
} |
PropertyName prop2 = StylingUtil.getSecondPropertyName(schema, |
113 |
|
ts); |
114 |
|
|
115 |
// System.out.println("labelAttrib local name" + |
if (StylingUtil.getFirstPropertyName(schema, ts) == null) { |
116 |
// labelAttribute.getLocalName()); |
// At least one property field we need |
117 |
|
continue; |
118 |
|
} |
119 |
|
|
120 |
final Object value = f |
Filter searchFilter; |
121 |
.getAttribute(labelAttribute.getLocalName()); |
String[] properties = new String[] { schema.getGeometryDescriptor().getLocalName(), prop1.getPropertyName() }; |
122 |
|
// Only one property used... |
123 |
|
|
124 |
|
searchFilter = CQL.toFilter("strToUpperCase " |
125 |
|
+ prop1.getPropertyName() + " LIKE '%" + searchMe |
126 |
|
+ "%'"); |
127 |
|
|
128 |
|
if (prop2 != null) { |
129 |
|
Filter searchFilter2 = CQL.toFilter("strToUpperCase " |
130 |
|
+ prop2.getPropertyName() + " LIKE '%" + searchMe |
131 |
|
+ "%'"); |
132 |
|
|
133 |
// System.out.println("labelAttrib value " + value); |
searchFilter = FeatureUtil.FILTER_FACTORY2.or(searchFilter, |
134 |
|
searchFilter2); |
135 |
|
|
136 |
if (value == null) { |
properties = LangUtil.extendArray(properties, prop2 |
137 |
LOGGER.info("Skipping f: getLocalName() is null for feature="+f); |
.getPropertyName()); |
138 |
continue; |
} |
139 |
} |
|
140 |
|
// Add the layer's filter if it exists |
141 |
|
Filter layerFilter = ml.getQuery().getFilter(); |
142 |
|
if (layerFilter != null && layerFilter != Filter.INCLUDE) { |
143 |
|
searchFilter = FeatureUtil.FILTER_FACTORY2.and(layerFilter, |
144 |
|
searchFilter); |
145 |
|
} |
146 |
|
|
147 |
|
// LOGGER.info("Searching for "+searchFilter.toString()); |
148 |
|
|
149 |
|
FeatureCollection<SimpleFeatureType, SimpleFeature> features = (FeatureCollection<SimpleFeatureType, SimpleFeature>) ml |
150 |
|
.getFeatureSource().getFeatures( |
151 |
|
new DefaultQuery(typeName, searchFilter, |
152 |
|
properties)); |
153 |
|
|
154 |
/** |
final Iterator<SimpleFeature> fi = features.iterator(); |
155 |
* LabelString ist z.B. "IMPETUS pluviograph". Suchwort |
try { |
156 |
* "plu" soll treffen. Also wird nach spaces zerlegt und |
while (fi.hasNext()) { |
157 |
* dann gesucht |
final SimpleFeature f = fi.next(); |
158 |
*/ |
|
159 |
final String labelString = value.toString().toLowerCase(); |
String valueString = ""; |
160 |
if (labelString.startsWith(searchMe)) { |
valueString = f.getAttribute(prop1.getPropertyName()) |
161 |
hits.add(createSearchResult(f, value.toString(), ml |
.toString(); |
162 |
.getTitle(),ml)); |
|
163 |
} else { |
if (prop2 != null) { |
164 |
final String[] parts = labelString.trim().split(" "); |
String valueString2 = f.getAttribute( |
165 |
for (final String part : parts) { |
prop2.getPropertyName()).toString(); |
166 |
if (part.startsWith(searchMe)) { |
|
167 |
hits.add(createSearchResult(f, value.toString(), ml |
if (valueString2 != null && !valueString2.isEmpty()) |
168 |
.getTitle(), ml)); |
valueString += ", " + valueString2; |
|
break; |
|
|
} |
|
169 |
} |
} |
|
} |
|
170 |
|
|
171 |
|
hits.add(createSearchResult(f, valueString, ml |
172 |
|
.getTitle(), ml)); |
173 |
|
} |
174 |
|
} finally { |
175 |
|
features.close(fi); |
176 |
} |
} |
|
|
|
177 |
} catch (final IOException e) { |
} catch (final IOException e) { |
178 |
// Searching this layer failed |
// Searching this layer failed |
179 |
LOGGER.error(e); |
LOGGER.error("",e); |
180 |
|
} catch (CQLException e) { |
181 |
|
LOGGER.error("",e); |
182 |
} |
} |
183 |
} // next layer |
} // next layer |
184 |
|
|
188 |
return hits; |
return hits; |
189 |
} |
} |
190 |
|
|
191 |
protected SearchResult createSearchResult(final SimpleFeature f, final String title, |
protected SearchResult createSearchResult(final SimpleFeature f, |
192 |
final String inTitle, MapLayer ml) { |
final String title, final String inTitle, MapLayer ml) { |
193 |
return new SearchResultFeature(f, title, inTitle, mapPane, ml); |
return new SearchResultFeature(f, title, inTitle, mapPane, ml); |
194 |
} |
} |
195 |
|
|