/[schmitzm]/branches/2.1/src/skrueger/geotools/labelsearch/LabelSearch.java
ViewVC logotype

Diff of /branches/2.1/src/skrueger/geotools/labelsearch/LabelSearch.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 604 by alfonx, Wed Dec 9 14:15:53 2009 UTC revision 607 by alfonx, Wed Dec 9 15:13:42 2009 UTC
# Line 40  import org.apache.log4j.Logger; Line 40  import org.apache.log4j.Logger;
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;
48  import org.opengis.feature.simple.SimpleFeature;  import org.opengis.feature.simple.SimpleFeature;
49  import org.opengis.feature.simple.SimpleFeatureType;  import org.opengis.feature.simple.SimpleFeatureType;
 import org.opengis.feature.type.AttributeDescriptor;  
50  import org.opengis.filter.Filter;  import org.opengis.filter.Filter;
 import org.opengis.filter.expression.Expression;  
51  import org.opengis.filter.expression.PropertyName;  import org.opengis.filter.expression.PropertyName;
52    
53    import schmitzm.geotools.feature.FeatureUtil;
54  import schmitzm.geotools.styling.StylingUtil;  import schmitzm.geotools.styling.StylingUtil;
55  import schmitzm.lang.LangUtil;  import schmitzm.lang.LangUtil;
56  import schmitzm.lang.ResourceProvider;  import schmitzm.lang.ResourceProvider;
# Line 79  public class LabelSearch { Line 80  public class LabelSearch {
80                  this.mapPane = mapPane;                  this.mapPane = mapPane;
81          }          }
82    
         /**  
          * 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!");  
                 }  
   
         }  
   
83          public List<SearchResult> search(final String string) {          public List<SearchResult> search(final String string) {
84    
85                  final String searchMe = string.toLowerCase();                  final String searchMe = string.toUpperCase();
86    
87                  final ArrayList<SearchResult> hits = new ArrayList<SearchResult>();                  final ArrayList<SearchResult> hits = new ArrayList<SearchResult>();
88    
# Line 124  public class LabelSearch { Line 102  public class LabelSearch {
102                                          continue;                                          continue;
103                                  }                                  }
104    
105                                    // We only deal with one TextSymbolizer so far:
106                                    TextSymbolizer ts = allTS.get(0);
107    
108                                  final FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) ml                                  final FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) ml
109                                                  .getFeatureSource();                                                  .getFeatureSource();
110    
111                                  final String typeName = featureSource.getSchema().getName()                                  SimpleFeatureType schema = featureSource.getSchema();
                                                 .getLocalPart();  
112    
113                                  // Expression labelExp = ts.getLabel();                                  final String typeName = schema.getName().getLocalPart();
                                 // 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.  
                                  */  
                                 final Iterator<SimpleFeature> fi = features.iterator();  
                                 try {  
114    
115                                          while (fi.hasNext()) {                                  PropertyName prop1 = StylingUtil.getFirstPropertyName(schema,
116                                                  final SimpleFeature f = fi.next();                                                  ts);
117                                    PropertyName prop2 = StylingUtil.getSecondPropertyName(schema,
118                                                    ts);
119    
120                                                  final TextSymbolizer ts = StylingUtil                                  if (StylingUtil.getFirstPropertyName(schema, ts) == null) {
121                                                                  .getTextSymbolizer(ml.getStyle(), f);                                          // At least one property field we need
122                                                  if (ts == null)                                          continue;
123                                                          continue;                                  }
   
                                                 // TODO Evaluate the Rule that belongs to the  
                                                 // TextSymbolizer against the feature...  
                                                 final AttributeDescriptor labelAttribute = getLabelAttribute(  
                                                                 ts, featureSource.getSchema());  
124    
125                                                  if (labelAttribute == null) {                                  Filter searchFilter;
126                                                          continue;                                  String[] properties = new String[] { schema.getGeometryDescriptor().getLocalName(), prop1.getPropertyName() };
127                                                  }                                  // Only one property used...
128    
129                                    searchFilter = CQL.toFilter("strToUpperCase "
130                                                    + prop1.getPropertyName() + " LIKE '%" + searchMe
131                                                    + "%'");
132    
133                                    if (prop2 != null) {
134                                            Filter searchFilter2 = CQL.toFilter("strToUpperCase "
135                                                            + prop2.getPropertyName() + " LIKE '%" + searchMe
136                                                            + "%'");
137    
138                                                  // System.out.println("labelAttrib local name" +                                          searchFilter = FeatureUtil.FILTER_FACTORY2.or(searchFilter,
139                                                  // labelAttribute.getLocalName());                                                          searchFilter2);
140    
141                                                  final Object value = f.getAttribute(labelAttribute                                          properties = LangUtil.extendArray(properties, prop2
142                                                                  .getLocalName());                                                          .getPropertyName());
143                                    }
144    
145                                                  // System.out.println("labelAttrib value " + value);                                  // Add the layer's filter if it exists
146                                    Filter layerFilter = ml.getQuery().getFilter();
147                                    if (layerFilter != null && layerFilter != Filter.INCLUDE) {
148                                            searchFilter = FeatureUtil.FILTER_FACTORY2.and(layerFilter,
149                                                            searchFilter);
150                                    }
151                                    
152    //                              LOGGER.info("Searching for "+searchFilter.toString());
153    
154                                                  if (value == null) {                                  FeatureCollection<SimpleFeatureType, SimpleFeature> features = (FeatureCollection<SimpleFeatureType, SimpleFeature>) ml
155                                                          LOGGER                                                  .getFeatureSource().getFeatures(
156                                                                          .info("Skipping f: getLocalName() is null for feature="                                                                  new DefaultQuery(typeName, searchFilter,
157                                                                                          + f);                                                                                  properties));
                                                         continue;  
                                                 }  
158    
159                                                  /**                                  final Iterator<SimpleFeature> fi = features.iterator();
160                                                   * LabelString ist z.B. "IMPETUS pluviograph". Suchwort                                  try {
161                                                   * "plu" soll treffen. Also wird nach spaces zerlegt und                                          while (fi.hasNext()) {
162                                                   * dann gesucht                                                  final SimpleFeature f = fi.next();
163                                                   */  
164                                                  final String labelString = value.toString()                                                  String valueString = "";
165                                                                  .toLowerCase();                                                  valueString = f.getAttribute(prop1.getPropertyName())
166                                                  if (labelString.startsWith(searchMe)) {                                                                  .toString();
167                                                          hits.add(createSearchResult(f, value.toString(), ml  
168                                                                          .getTitle(), ml));                                                  if (prop2 != null) {
169                                                  } else {                                                          String valueString2 = f.getAttribute(
170                                                          final String[] parts = labelString.trim()                                                                          prop2.getPropertyName()).toString();
171                                                                          .split(" ");  
172                                                          for (final String part : parts) {                                                          if (valueString2 != null && !valueString2.isEmpty())
173                                                                  if (part.startsWith(searchMe)) {                                                                  valueString += ", " + valueString2;
                                                                         hits.add(createSearchResult(f, value  
                                                                                         .toString(), ml.getTitle(), ml));  
                                                                         break;  
                                                                 }  
                                                         }  
174                                                  }                                                  }
175    
176                                                    hits.add(createSearchResult(f, valueString, ml
177                                                                    .getTitle(), ml));
178                                          }                                          }
179                                  } finally {                                  } finally {
180                                          features.close(fi);                                          features.close(fi);
181                                  }                                  }
   
182                          } catch (final IOException e) {                          } catch (final IOException e) {
183                                  // Searching this layer failed                                  // Searching this layer failed
184                                  LOGGER.error(e);                                  LOGGER.error("",e);
185                            } catch (CQLException e) {
186                                    LOGGER.error("",e);
187                          }                          }
   
188                  } // next layer                  } // next layer
189    
190                  // Hits from the top-most layer should appear first.                  // Hits from the top-most layer should appear first.

Legend:
Removed from v.604  
changed lines
  Added in v.607

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26