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; |
42 |
import org.geotools.feature.FeatureCollection; |
import org.geotools.feature.FeatureCollection; |
43 |
import org.geotools.feature.SimpleFeature; |
import org.geotools.filter.text.cql2.CQL; |
44 |
import org.geotools.feature.SimpleFeatureType; |
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; |
48 |
|
import org.opengis.feature.simple.SimpleFeature; |
49 |
|
import org.opengis.feature.simple.SimpleFeatureType; |
50 |
|
import org.opengis.feature.type.AttributeDescriptor; |
51 |
import org.opengis.filter.Filter; |
import org.opengis.filter.Filter; |
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; |
69 |
*/ |
*/ |
70 |
public class LabelSearch { |
public class LabelSearch { |
71 |
final static private Logger LOGGER = Logger.getLogger(LabelSearch.class); |
final static private Logger LOGGER = Logger.getLogger(LabelSearch.class); |
72 |
|
|
73 |
public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil |
public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil |
74 |
.extendPackagePath(LabelSearch.class, |
.extendPackagePath(LabelSearch.class, "labelsearch"), |
75 |
"labelsearch"), Locale.ENGLISH); |
Locale.ENGLISH); |
76 |
|
|
|
|
|
|
|
|
|
|
|
77 |
public static String R(String key, Object... values) { |
public static String R(String key, Object... values) { |
78 |
return RESOURCE.getString(key, values); |
return RESOURCE.getString(key, values); |
79 |
} |
} |
80 |
|
|
81 |
protected final schmitzm.geotools.gui.JMapPane mapPane; |
protected final schmitzm.geotools.gui.SelectableXMapPane mapPane; |
82 |
|
|
83 |
public LabelSearch(final schmitzm.geotools.gui.JMapPane mapPane) { |
public LabelSearch(final schmitzm.geotools.gui.SelectableXMapPane mapPane) { |
84 |
this.mapPane = mapPane; |
this.mapPane = mapPane; |
85 |
} |
} |
86 |
|
|
87 |
private AttributeType getLabelAttribute(final TextSymbolizer ts, |
// |
88 |
final SimpleFeatureType schema) { |
// /** |
89 |
if (ts == null) { |
// * The Attribute that provides the labels for this text symbolizer. |
90 |
// This layer has no labels |
// */ |
91 |
return null; |
// private AttributeDescriptor getLabelAttribute(final TextSymbolizer ts, |
92 |
} |
// final SimpleFeatureType schema) { |
93 |
final Expression labelExp = ts.getLabel(); |
// if (ts == null) { |
94 |
if (labelExp instanceof PropertyName) { |
// // This layer has no labels |
95 |
final PropertyName pn = (PropertyName) labelExp; |
// return null; |
96 |
final String propertyName = pn.getPropertyName(); |
// } |
97 |
return schema.getAttributeType(propertyName); |
// |
98 |
} else { |
// final Expression labelExp = ts.getLabel(); |
99 |
// When does this happen |
// if (labelExp instanceof PropertyName) { |
100 |
} |
// final PropertyName pn = (PropertyName) labelExp; |
101 |
|
// final String propertyName = pn.getPropertyName(); |
102 |
return null; |
// return schema.getDescriptor(propertyName); |
103 |
} |
// } 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 |
|
|
115 |
for (final MapLayer ml : mapPane.getContext().getLayers()) { |
for (final MapLayer ml : mapPane.getMapContext().getLayers()) { |
116 |
try { |
try { |
117 |
|
|
118 |
// System.out.println("layer = "+ml.getTitle()); |
// System.out.println("layer = "+ml.getTitle()); |
120 |
if (!ml.isVisible()) |
if (!ml.isVisible()) |
121 |
continue; |
continue; |
122 |
|
|
123 |
final List<TextSymbolizer> allTS = StylingUtil.getTextSymbolizers(ml |
final List<TextSymbolizer> allTS = StylingUtil |
124 |
.getStyle()); |
.getVisibleTextSymbolizers(ml.getStyle()); |
125 |
if (allTS.size() == 0) { |
if (allTS.size() == 0) { |
126 |
// A layer without any TextSymbolizer doesn't have to be |
// A layer without any TextSymbolizer doesn't have to be |
127 |
// searched any more. |
// searched any more. |
128 |
continue; |
continue; |
129 |
} |
} |
130 |
|
|
131 |
final String typeName = ml.getFeatureSource().getSchema() |
// We only deal with one TextSymbolizer so far: |
132 |
.getTypeName(); |
TextSymbolizer ts = allTS.get(0); |
133 |
|
|
134 |
// Expression labelExp = ts.getLabel(); |
final FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) ml |
135 |
// ff.like(labelExp, "*"+searchMe+"*"); |
.getFeatureSource(); |
|
// FeatureCollection features = |
|
|
// ml.getFeatureSource().getFeatures( |
|
|
// new DefaultQuery(typeName, ff.like(labelExp, |
|
|
// "*"+searchMe+"*"), properties)); |
|
|
|
|
|
final FeatureCollection features = ml.getFeatureSource().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(); |
|
136 |
|
|
137 |
final TextSymbolizer ts = StylingUtil.getTextSymbolizer(ml |
SimpleFeatureType schema = featureSource.getSchema(); |
|
.getStyle(), f); |
|
|
if (ts == null) |
|
|
continue; |
|
138 |
|
|
139 |
final AttributeType labelAttribute = getLabelAttribute(ts, ml |
final String typeName = schema.getName().getLocalPart(); |
|
.getFeatureSource().getSchema()); |
|
140 |
|
|
141 |
if (labelAttribute == null) { |
PropertyName prop1 = StylingUtil.getFirstPropertyName(schema, |
142 |
continue; |
ts); |
143 |
} |
PropertyName prop2 = StylingUtil.getSecondPropertyName(schema, |
144 |
|
ts); |
145 |
|
|
146 |
// System.out.println("labelAttrib local name" + |
if (StylingUtil.getFirstPropertyName(schema, ts) == null) { |
147 |
// labelAttribute.getLocalName()); |
// At least one property field we need |
148 |
|
continue; |
149 |
|
} |
150 |
|
|
151 |
final Object value = f |
Filter searchFilter; |
152 |
.getAttribute(labelAttribute.getLocalName()); |
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 |
// System.out.println("labelAttrib value " + value); |
searchFilter = FeatureUtil.FILTER_FACTORY2.or(searchFilter, |
165 |
|
searchFilter2); |
166 |
|
|
167 |
if (value == null) { |
properties = LangUtil.extendArray(properties, prop2 |
168 |
LOGGER.info("Skipping f: getLocalName() is null for feature="+f); |
.getPropertyName()); |
169 |
continue; |
} |
|
} |
|
170 |
|
|
171 |
/** |
// Add the layer's filter if it exists |
172 |
* LabelString ist z.B. "IMPETUS pluviograph". Suchwort |
Filter layerFilter = ml.getQuery().getFilter(); |
173 |
* "plu" soll treffen. Also wird nach spaces zerlegt und |
if (layerFilter != null && layerFilter != Filter.INCLUDE) { |
174 |
* dann gesucht |
searchFilter = FeatureUtil.FILTER_FACTORY2.and(layerFilter, |
175 |
*/ |
searchFilter); |
|
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; |
|
|
} |
|
|
} |
|
|
} |
|
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 |
|
|
186 |
|
final Iterator<SimpleFeature> fi = features.iterator(); |
187 |
|
try { |
188 |
|
while (fi.hasNext()) { |
189 |
|
final SimpleFeature f = fi.next(); |
190 |
|
|
191 |
|
String valueString = ""; |
192 |
|
valueString = f.getAttribute(prop1.getPropertyName()) |
193 |
|
.toString(); |
194 |
|
|
195 |
|
if (prop2 != null) { |
196 |
|
String valueString2 = f.getAttribute( |
197 |
|
prop2.getPropertyName()).toString(); |
198 |
|
|
199 |
|
if (valueString2 != null && !valueString2.isEmpty()) |
200 |
|
valueString += ", " + valueString2; |
201 |
|
} |
202 |
|
|
203 |
|
hits.add(createSearchResult(f, valueString, ml |
204 |
|
.getTitle(), ml)); |
205 |
|
} |
206 |
|
} finally { |
207 |
|
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 |
|
|
220 |
return hits; |
return hits; |
221 |
} |
} |
222 |
|
|
223 |
protected SearchResult createSearchResult(final SimpleFeature f, final String title, |
protected SearchResult createSearchResult(final SimpleFeature f, |
224 |
final String inTitle, MapLayer ml) { |
final String title, final String inTitle, MapLayer ml) { |
225 |
return new SearchResultFeature(f, title, inTitle, mapPane, ml); |
return new SearchResultFeature(f, title, inTitle, mapPane, ml); |
226 |
} |
} |
227 |
|
|