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