40 |
import org.geotools.data.DefaultQuery; |
import org.geotools.data.DefaultQuery; |
41 |
import org.geotools.data.FeatureSource; |
import org.geotools.data.FeatureSource; |
42 |
import org.geotools.feature.FeatureCollection; |
import org.geotools.feature.FeatureCollection; |
43 |
|
import org.geotools.filter.text.cql2.CQL; |
44 |
|
import org.geotools.filter.text.cql2.CQLException; |
45 |
import org.geotools.map.MapLayer; |
import org.geotools.map.MapLayer; |
46 |
import org.geotools.styling.Style; |
import org.geotools.styling.Style; |
47 |
import org.geotools.styling.TextSymbolizer; |
import org.geotools.styling.TextSymbolizer; |
52 |
import org.opengis.filter.expression.Expression; |
import org.opengis.filter.expression.Expression; |
53 |
import org.opengis.filter.expression.PropertyName; |
import org.opengis.filter.expression.PropertyName; |
54 |
|
|
55 |
|
import com.sun.jndi.toolkit.dir.SearchFilter; |
56 |
|
|
57 |
|
import schmitzm.geotools.feature.FeatureUtil; |
58 |
import schmitzm.geotools.styling.StylingUtil; |
import schmitzm.geotools.styling.StylingUtil; |
59 |
import schmitzm.lang.LangUtil; |
import schmitzm.lang.LangUtil; |
60 |
import schmitzm.lang.ResourceProvider; |
import schmitzm.lang.ResourceProvider; |
84 |
this.mapPane = mapPane; |
this.mapPane = mapPane; |
85 |
} |
} |
86 |
|
|
87 |
/** |
// |
88 |
* The Attribute that provides the labels for this text symbolizer. |
// /** |
89 |
*/ |
// * The Attribute that provides the labels for this text symbolizer. |
90 |
private AttributeDescriptor getLabelAttribute(final TextSymbolizer ts, |
// */ |
91 |
final SimpleFeatureType schema) { |
// private AttributeDescriptor getLabelAttribute(final TextSymbolizer ts, |
92 |
if (ts == null) { |
// final SimpleFeatureType schema) { |
93 |
// This layer has no labels |
// if (ts == null) { |
94 |
return null; |
// // This layer has no labels |
95 |
} |
// return null; |
96 |
|
// } |
97 |
final Expression labelExp = ts.getLabel(); |
// |
98 |
if (labelExp instanceof PropertyName) { |
// final Expression labelExp = ts.getLabel(); |
99 |
final PropertyName pn = (PropertyName) labelExp; |
// if (labelExp instanceof PropertyName) { |
100 |
final String propertyName = pn.getPropertyName(); |
// final PropertyName pn = (PropertyName) labelExp; |
101 |
return schema.getDescriptor(propertyName); |
// final String propertyName = pn.getPropertyName(); |
102 |
} else { |
// return schema.getDescriptor(propertyName); |
103 |
return null; |
// } else { |
104 |
} |
// return null; |
105 |
|
// } |
106 |
} |
// |
107 |
|
// } |
108 |
|
|
109 |
public List<SearchResult> search(final String string) { |
public List<SearchResult> search(final String string) { |
110 |
|
|
111 |
final String searchMe = string.toLowerCase(); |
final String searchMe = string.toUpperCase(); |
112 |
|
|
113 |
final ArrayList<SearchResult> hits = new ArrayList<SearchResult>(); |
final ArrayList<SearchResult> hits = new ArrayList<SearchResult>(); |
114 |
|
|
128 |
continue; |
continue; |
129 |
} |
} |
130 |
|
|
131 |
|
// We only deal with one TextSymbolizer so far: |
132 |
|
TextSymbolizer ts = allTS.get(0); |
133 |
|
|
134 |
final FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) ml |
final FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) ml |
135 |
.getFeatureSource(); |
.getFeatureSource(); |
136 |
|
|
137 |
final String typeName = featureSource.getSchema().getName() |
SimpleFeatureType schema = featureSource.getSchema(); |
138 |
.getLocalPart(); |
|
139 |
|
final String typeName = schema.getName().getLocalPart(); |
140 |
|
|
141 |
|
PropertyName prop1 = StylingUtil.getFirstPropertyName(schema, |
142 |
|
ts); |
143 |
|
PropertyName prop2 = StylingUtil.getSecondPropertyName(schema, |
144 |
|
ts); |
145 |
|
|
146 |
|
if (StylingUtil.getFirstPropertyName(schema, ts) == null) { |
147 |
|
// At least one property field we need |
148 |
|
continue; |
149 |
|
} |
150 |
|
|
151 |
|
Filter searchFilter; |
152 |
|
String[] properties = new String[] { schema.getGeometryDescriptor().getLocalName(), prop1.getPropertyName() }; |
153 |
|
// Only one property used... |
154 |
|
|
155 |
|
searchFilter = CQL.toFilter("strToUpperCase " |
156 |
|
+ prop1.getPropertyName() + " LIKE '%" + searchMe |
157 |
|
+ "%'"); |
158 |
|
|
159 |
|
if (prop2 != null) { |
160 |
|
Filter searchFilter2 = CQL.toFilter("strToUpperCase " |
161 |
|
+ prop2.getPropertyName() + " LIKE '%" + searchMe |
162 |
|
+ "%'"); |
163 |
|
|
164 |
|
searchFilter = FeatureUtil.FILTER_FACTORY2.or(searchFilter, |
165 |
|
searchFilter2); |
166 |
|
|
167 |
|
properties = LangUtil.extendArray(properties, prop2 |
168 |
|
.getPropertyName()); |
169 |
|
} |
170 |
|
|
171 |
|
// Add the layer's filter if it exists |
172 |
|
Filter layerFilter = ml.getQuery().getFilter(); |
173 |
|
if (layerFilter != null && layerFilter != Filter.INCLUDE) { |
174 |
|
searchFilter = FeatureUtil.FILTER_FACTORY2.and(layerFilter, |
175 |
|
searchFilter); |
176 |
|
|
177 |
|
} |
178 |
|
|
179 |
|
LOGGER.info("Searching for "+searchFilter.toString()); |
180 |
|
|
181 |
|
FeatureCollection<SimpleFeatureType, SimpleFeature> features = (FeatureCollection<SimpleFeatureType, SimpleFeature>) ml |
182 |
|
.getFeatureSource().getFeatures( |
183 |
|
new DefaultQuery(typeName, searchFilter, |
184 |
|
properties)); |
185 |
|
|
|
// Expression labelExp = ts.getLabel(); |
|
|
// ff.like(labelExp, "*"+searchMe+"*"); |
|
|
// 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. |
|
|
*/ |
|
186 |
final Iterator<SimpleFeature> fi = features.iterator(); |
final Iterator<SimpleFeature> fi = features.iterator(); |
187 |
try { |
try { |
|
|
|
188 |
while (fi.hasNext()) { |
while (fi.hasNext()) { |
189 |
final SimpleFeature f = fi.next(); |
final SimpleFeature f = fi.next(); |
190 |
|
|
191 |
final TextSymbolizer ts = StylingUtil |
String valueString = ""; |
192 |
.getTextSymbolizer(ml.getStyle(), f); |
valueString = f.getAttribute(prop1.getPropertyName()) |
193 |
if (ts == null) |
.toString(); |
194 |
continue; |
|
195 |
|
if (prop2 != null) { |
196 |
// TODO Evaluate the Rule that belongs to the |
String valueString2 = f.getAttribute( |
197 |
SimpleFeatureType schema = featureSource.getSchema(); |
prop2.getPropertyName()).toString(); |
|
PropertyName pn = StylingUtil.getFirstPropertyName( |
|
|
schema, ts); |
|
|
if (pn == null) continue; |
|
|
|
|
|
final AttributeDescriptor labelAttribute = schema |
|
|
.getDescriptor(pn.getPropertyName()); |
|
|
|
|
|
final Object value = f.getAttribute(labelAttribute |
|
|
.getLocalName()); |
|
|
|
|
|
if (value == null) { |
|
|
LOGGER |
|
|
.info("Skipping f: getLocalName() is null for feature=" |
|
|
+ f); |
|
|
continue; |
|
|
} |
|
198 |
|
|
199 |
/** |
if (valueString2 != null && !valueString2.isEmpty()) |
200 |
* LabelString ist z.B. "IMPETUS pluviograph". Suchwort |
valueString += ", " + valueString2; |
|
* "plu" soll treffen. Also wird nach spaces zerlegt und |
|
|
* dann gesucht |
|
|
*/ |
|
|
final String labelString = value.toString() |
|
|
.toLowerCase(); |
|
|
if (labelString.startsWith(searchMe)) { |
|
|
hits.add(createSearchResult(f, value.toString(), ml |
|
|
.getTitle(), ml)); |
|
|
} else { |
|
|
final String[] parts = labelString.trim() |
|
|
.split(" "); |
|
|
for (final String part : parts) { |
|
|
if (part.startsWith(searchMe)) { |
|
|
hits.add(createSearchResult(f, value |
|
|
.toString(), ml.getTitle(), ml)); |
|
|
break; |
|
|
} |
|
|
} |
|
201 |
} |
} |
202 |
|
|
203 |
|
hits.add(createSearchResult(f, valueString, ml |
204 |
|
.getTitle(), ml)); |
205 |
} |
} |
206 |
} finally { |
} finally { |
207 |
features.close(fi); |
features.close(fi); |
208 |
} |
} |
|
|
|
209 |
} catch (final IOException e) { |
} catch (final IOException e) { |
210 |
// Searching this layer failed |
// Searching this layer failed |
211 |
LOGGER.error(e); |
LOGGER.error("",e); |
212 |
|
} catch (CQLException e) { |
213 |
|
LOGGER.error("",e); |
214 |
} |
} |
|
|
|
215 |
} // next layer |
} // next layer |
216 |
|
|
217 |
// Hits from the top-most layer should appear first. |
// Hits from the top-most layer should appear first. |