/[schmitzm]/branches/1.0-gt2-2.6/src/skrueger/geotools/labelsearch/LabelSearch.java
ViewVC logotype

Diff of /branches/1.0-gt2-2.6/src/skrueger/geotools/labelsearch/LabelSearch.java

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

revision 455 by alfonx, Fri Sep 25 16:34:10 2009 UTC revision 456 by alfonx, Sun Oct 11 21:43:40 2009 UTC
# Line 64  import schmitzm.lang.ResourceProvider; Line 64  import schmitzm.lang.ResourceProvider;
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          }          }
# Line 83  public class LabelSearch { Line 80  public class LabelSearch {
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) {
# Line 91  public class LabelSearch { Line 88  public class LabelSearch {
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;
# Line 99  public class LabelSearch { Line 96  public class LabelSearch {
96                          return schema.getDescriptor(propertyName);                          return schema.getDescriptor(propertyName);
97                  } else {                  } else {
98                          // When does this happen                          // When does this happen
99                          throw new RuntimeException("labelExp "+labelExp+" IS NOT instanceof PropertyName!");                          throw new RuntimeException("labelExp " + labelExp
100                                            + " IS NOT instanceof PropertyName!");
101                  }                  }
102    
103          }          }
# Line 118  public class LabelSearch { Line 116  public class LabelSearch {
116                                  if (!ml.isVisible())                                  if (!ml.isVisible())
117                                          continue;                                          continue;
118    
119                                  final List<TextSymbolizer> allTS = StylingUtil.getVisibleTextSymbolizers(ml                                  final List<TextSymbolizer> allTS = StylingUtil
120                                                  .getStyle());                                                  .getVisibleTextSymbolizers(ml.getStyle());
121                                  if (allTS.size() == 0) {                                  if (allTS.size() == 0) {
122                                          // A layer without any TextSymbolizer doesn't have to be                                          // A layer without any TextSymbolizer doesn't have to be
123                                          // searched any more.                                          // searched any more.
124                                          continue;                                          continue;
125                                  }                                  }
126                                    
127                                  final FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) ml.getFeatureSource();                                  final FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) ml
128                                                                                    .getFeatureSource();
129                                  final String typeName = featureSource.getSchema()  
130                                                  .getName().getLocalPart();                                  final String typeName = featureSource.getSchema().getName()
131                                                    .getLocalPart();
132    
133                                  // Expression labelExp = ts.getLabel();                                  // Expression labelExp = ts.getLabel();
134                                  // ff.like(labelExp, "*"+searchMe+"*");                                  // ff.like(labelExp, "*"+searchMe+"*");
# Line 138  public class LabelSearch { Line 137  public class LabelSearch {
137                                  // new DefaultQuery(typeName, ff.like(labelExp,                                  // new DefaultQuery(typeName, ff.like(labelExp,
138                                  // "*"+searchMe+"*"), properties));                                  // "*"+searchMe+"*"), properties));
139    
140                                  final FeatureCollection<SimpleFeatureType, SimpleFeature> features = featureSource.getFeatures(                                  final FeatureCollection<SimpleFeatureType, SimpleFeature> features = featureSource
141                                                  new DefaultQuery(typeName, Filter.INCLUDE));                                                  .getFeatures(new DefaultQuery(typeName, Filter.INCLUDE));
142    
143                                  // new MemoryDataStore().getFeatureSource(typeName)                                  // new MemoryDataStore().getFeatureSource(typeName)
144    
# Line 149  public class LabelSearch { Line 148  public class LabelSearch {
148                                   * function.                                   * function.
149                                   */                                   */
150                                  final Iterator<SimpleFeature> fi = features.iterator();                                  final Iterator<SimpleFeature> fi = features.iterator();
151                                  while (fi.hasNext()) {                                  try {
                                         final SimpleFeature f = fi.next();  
152    
153                                          final TextSymbolizer ts = StylingUtil.getTextSymbolizer(ml                                          while (fi.hasNext()) {
154                                                          .getStyle(), f);                                                  final SimpleFeature f = fi.next();
                                         if (ts == null)  
                                                 continue;  
155    
156                                          // TODO Evaluate the Rule that belongs to the TextSymbolizer against the feature...                                                  final TextSymbolizer ts = StylingUtil
157                                          final AttributeDescriptor labelAttribute = getLabelAttribute(ts, featureSource.getSchema());                                                                  .getTextSymbolizer(ml.getStyle(), f);
158                                                    if (ts == null)
159                                                            continue;
160    
161                                                    // TODO Evaluate the Rule that belongs to the
162                                                    // TextSymbolizer against the feature...
163                                                    final AttributeDescriptor labelAttribute = getLabelAttribute(
164                                                                    ts, featureSource.getSchema());
165    
166                                          if (labelAttribute == null) {                                                  if (labelAttribute == null) {
167                                                  continue;                                                          continue;
168                                          }                                                  }
169    
170                                          // System.out.println("labelAttrib local name" +                                                  // System.out.println("labelAttrib local name" +
171                                          // labelAttribute.getLocalName());                                                  // labelAttribute.getLocalName());
172    
173                                          final Object value = f                                                  final Object value = f.getAttribute(labelAttribute
174                                                          .getAttribute(labelAttribute.getLocalName());                                                                  .getLocalName());
175    
176                                          // System.out.println("labelAttrib value " + value);                                                  // System.out.println("labelAttrib value " + value);
177    
178                                          if (value == null) {                                                  if (value == null) {
179                                                  LOGGER.info("Skipping f: getLocalName() is null for feature="+f);                                                          LOGGER
180                                                  continue;                                                                          .info("Skipping f: getLocalName() is null for feature="
181                                          }                                                                                          + f);
182                                                            continue;
183                                                    }
184    
185                                          /**                                                  /**
186                                           * LabelString ist z.B. "IMPETUS pluviograph". Suchwort                                                   * LabelString ist z.B. "IMPETUS pluviograph". Suchwort
187                                           * "plu" soll treffen. Also wird nach spaces zerlegt und                                                   * "plu" soll treffen. Also wird nach spaces zerlegt und
188                                           * dann gesucht                                                   * dann gesucht
189                                           */                                                   */
190                                          final String labelString = value.toString().toLowerCase();                                                  final String labelString = value.toString()
191                                          if (labelString.startsWith(searchMe)) {                                                                  .toLowerCase();
192                                                  hits.add(createSearchResult(f, value.toString(), ml                                                  if (labelString.startsWith(searchMe)) {
193                                                                  .getTitle(),ml));                                                          hits.add(createSearchResult(f, value.toString(), ml
194                                          } else {                                                                          .getTitle(), ml));
195                                                  final String[] parts = labelString.trim().split(" ");                                                  } else {
196                                                  for (final String part : parts) {                                                          final String[] parts = labelString.trim()
197                                                          if (part.startsWith(searchMe)) {                                                                          .split(" ");
198                                                                  hits.add(createSearchResult(f, value.toString(), ml                                                          for (final String part : parts) {
199                                                                                  .getTitle(), ml));                                                                  if (part.startsWith(searchMe)) {
200                                                                  break;                                                                          hits.add(createSearchResult(f, value
201                                                                                            .toString(), ml.getTitle(), ml));
202                                                                            break;
203                                                                    }
204                                                          }                                                          }
205                                                  }                                                  }
                                         }  
206    
207                                            }
208                                    } finally {
209                                            features.close(fi);
210                                  }                                  }
211    
212                          } catch (final IOException e) {                          } catch (final IOException e) {
213                                  // Searching this layer failed                                  // Searching this layer failed
214                                  LOGGER.error(e);                                  LOGGER.error(e);
215                          }                          }
216    
217                  } // next layer                  } // next layer
218    
219                  // Hits from the top-most layer should appear first.                  // Hits from the top-most layer should appear first.
# Line 211  public class LabelSearch { Line 222  public class LabelSearch {
222                  return hits;                  return hits;
223          }          }
224    
225          protected SearchResult createSearchResult(final SimpleFeature f, final String title,          protected SearchResult createSearchResult(final SimpleFeature f,
226                          final String inTitle, MapLayer ml) {                          final String title, final String inTitle, MapLayer ml) {
227                  return new SearchResultFeature(f, title, inTitle, mapPane, ml);                  return new SearchResultFeature(f, title, inTitle, mapPane, ml);
228          }          }
229    

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26