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

Diff of /branches/2.4.x/src/skrueger/geotools/labelsearch/LabelSearch.java

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

branches/1.0-gt2-2.6/src/skrueger/geotools/labelsearch/LabelSearch.java revision 456 by alfonx, Sun Oct 11 21:43:40 2009 UTC trunk/src/skrueger/geotools/labelsearch/LabelSearch.java revision 887 by alfonx, Thu Jun 3 10:36:57 2010 UTC
# Line 25  Line 25 
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    
# Line 34  import java.util.ArrayList; Line 34  import java.util.ArrayList;
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    
68          public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil          /** Translations come from GTResourceBundle **/
                         .extendPackagePath(LabelSearch.class, "labelsearch"),  
                         Locale.ENGLISH);  
   
69          public static String R(String key, Object... values) {          public static String R(String key, Object... values) {
70                  return RESOURCE.getString(key, values);                  return GeotoolsGUIUtil.R(key, values);
71          }          }
72    
73          protected final schmitzm.geotools.gui.JMapPane mapPane;          protected final schmitzm.geotools.gui.SelectableXMapPane mapPane;
74    
75          public LabelSearch(final schmitzm.geotools.gui.JMapPane mapPane) {          public LabelSearch(final schmitzm.geotools.gui.SelectableXMapPane mapPane) {
76                  this.mapPane = mapPane;                  this.mapPane = mapPane;
77          }          }
78    
         /**  
          * 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!");  
                 }  
   
         }  
   
79          public List<SearchResult> search(final String string) {          public List<SearchResult> search(final String string) {
80    
81                  final String searchMe = string.toLowerCase();                  final String searchMe = string.toUpperCase();
82    
83                  final ArrayList<SearchResult> hits = new ArrayList<SearchResult>();                  final ArrayList<SearchResult> hits = new ArrayList<SearchResult>();
84    
85                  for (final MapLayer ml : mapPane.getContext().getLayers()) {                  for (final MapLayer ml : mapPane.getMapContext().getLayers()) {
86                          try {                          try {
87    
88                                  // System.out.println("layer = "+ml.getTitle());                                  // System.out.println("layer = "+ml.getTitle());
# Line 124  public class LabelSearch { Line 98  public class LabelSearch {
98                                          continue;                                          continue;
99                                  }                                  }
100    
101                                    // We only deal with one TextSymbolizer so far:
102                                    TextSymbolizer ts = allTS.get(0);
103    
104                                  final FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) ml                                  final FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) ml
105                                                  .getFeatureSource();                                                  .getFeatureSource();
106    
107                                  final String typeName = featureSource.getSchema().getName()                                  SimpleFeatureType schema = featureSource.getSchema();
                                                 .getLocalPart();  
108    
109                                  // 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 {  
110    
111                                          while (fi.hasNext()) {                                  PropertyName prop1 = StylingUtil.getFirstPropertyName(schema,
112                                                  final SimpleFeature f = fi.next();                                                  ts);
113                                    PropertyName prop2 = StylingUtil.getSecondPropertyName(schema,
114                                                    ts);
115    
116                                                  final TextSymbolizer ts = StylingUtil                                  if (StylingUtil.getFirstPropertyName(schema, ts) == null) {
117                                                                  .getTextSymbolizer(ml.getStyle(), f);                                          // At least one property field we need
118                                                  if (ts == null)                                          continue;
119                                                          continue;                                  }
   
                                                 // TODO Evaluate the Rule that belongs to the  
                                                 // TextSymbolizer against the feature...  
                                                 final AttributeDescriptor labelAttribute = getLabelAttribute(  
                                                                 ts, featureSource.getSchema());  
120    
121                                                  if (labelAttribute == null) {                                  Filter searchFilter;
122                                                          continue;                                  String[] properties = new String[] { schema.getGeometryDescriptor().getLocalName(), prop1.getPropertyName() };
123                                                  }                                  // Only one property used...
124    
125                                    searchFilter = CQL.toFilter("strToUpperCase "
126                                                    + prop1.getPropertyName() + " LIKE '%" + searchMe
127                                                    + "%'");
128    
129                                    if (prop2 != null) {
130                                            Filter searchFilter2 = CQL.toFilter("strToUpperCase "
131                                                            + prop2.getPropertyName() + " LIKE '%" + searchMe
132                                                            + "%'");
133    
134                                                  // System.out.println("labelAttrib local name" +                                          searchFilter = FeatureUtil.FILTER_FACTORY2.or(searchFilter,
135                                                  // labelAttribute.getLocalName());                                                          searchFilter2);
136    
137                                                  final Object value = f.getAttribute(labelAttribute                                          properties = LangUtil.extendArray(properties, prop2
138                                                                  .getLocalName());                                                          .getPropertyName());
139                                    }
140    
141                                                  // System.out.println("labelAttrib value " + value);                                  // Add the layer's filter if it exists
142                                    Filter layerFilter = ml.getQuery().getFilter();
143                                    if (layerFilter != null && layerFilter != Filter.INCLUDE) {
144                                            searchFilter = FeatureUtil.FILTER_FACTORY2.and(layerFilter,
145                                                            searchFilter);
146                                    }
147                                    
148    //                              LOGGER.info("Searching for "+searchFilter.toString());
149    
150                                                  if (value == null) {                                  FeatureCollection<SimpleFeatureType, SimpleFeature> features = (FeatureCollection<SimpleFeatureType, SimpleFeature>) ml
151                                                          LOGGER                                                  .getFeatureSource().getFeatures(
152                                                                          .info("Skipping f: getLocalName() is null for feature="                                                                  new DefaultQuery(typeName, searchFilter,
153                                                                                          + f);                                                                                  properties));
                                                         continue;  
                                                 }  
154    
155                                                  /**                                  final Iterator<SimpleFeature> fi = features.iterator();
156                                                   * LabelString ist z.B. "IMPETUS pluviograph". Suchwort                                  try {
157                                                   * "plu" soll treffen. Also wird nach spaces zerlegt und                                          while (fi.hasNext()) {
158                                                   * dann gesucht                                                  final SimpleFeature f = fi.next();
159                                                   */  
160                                                  final String labelString = value.toString()                                                  String valueString = "";
161                                                                  .toLowerCase();                                                  valueString = f.getAttribute(prop1.getPropertyName())
162                                                  if (labelString.startsWith(searchMe)) {                                                                  .toString();
163                                                          hits.add(createSearchResult(f, value.toString(), ml  
164                                                                          .getTitle(), ml));                                                  if (prop2 != null) {
165                                                  } else {                                                          String valueString2 = f.getAttribute(
166                                                          final String[] parts = labelString.trim()                                                                          prop2.getPropertyName()).toString();
167                                                                          .split(" ");  
168                                                          for (final String part : parts) {                                                          if (valueString2 != null && !valueString2.isEmpty())
169                                                                  if (part.startsWith(searchMe)) {                                                                  valueString += ", " + valueString2;
                                                                         hits.add(createSearchResult(f, value  
                                                                                         .toString(), ml.getTitle(), ml));  
                                                                         break;  
                                                                 }  
                                                         }  
170                                                  }                                                  }
171    
172                                                    hits.add(createSearchResult(f, valueString, ml
173                                                                    .getTitle(), ml));
174                                          }                                          }
175                                  } finally {                                  } finally {
176                                          features.close(fi);                                          features.close(fi);
177                                  }                                  }
   
178                          } catch (final IOException e) {                          } catch (final IOException e) {
179                                  // Searching this layer failed                                  // Searching this layer failed
180                                  LOGGER.error(e);                                  LOGGER.error("",e);
181                            } catch (CQLException e) {
182                                    LOGGER.error("",e);
183                          }                          }
   
184                  } // next layer                  } // next layer
185    
186                  // Hits from the top-most layer should appear first.                  // Hits from the top-most layer should appear first.

Legend:
Removed from v.456  
changed lines
  Added in v.887

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26