/[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

branches/1.0-gt2-2.6/src/skrueger/geotools/labelsearch/LabelSearch.java revision 332 by alfonx, Wed Aug 26 17:15:49 2009 UTC trunk/src/skrueger/geotools/labelsearch/LabelSearch.java revision 685 by alfonx, Wed Feb 10 15:04:02 2010 UTC
# Line 38  import java.util.Locale; Line 38  import java.util.Locale;
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.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 63  import schmitzm.lang.ResourceProvider; Line 65  import schmitzm.lang.ResourceProvider;
65   */   */
66  public class LabelSearch {  public class LabelSearch {
67          final static private Logger LOGGER = Logger.getLogger(LabelSearch.class);          final static private Logger LOGGER = Logger.getLogger(LabelSearch.class);
68            
69          public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil          public static ResourceProvider RESOURCE = new ResourceProvider(LangUtil
70          .extendPackagePath(LabelSearch.class,                          .extendPackagePath(LabelSearch.class, "labelsearch"),
71                          "labelsearch"), Locale.ENGLISH);                          Locale.ENGLISH);
72            
           
                           
                           
73          public static String R(String key, Object... values) {          public static String R(String key, Object... values) {
74                  return RESOURCE.getString(key, values);                  return RESOURCE.getString(key, values);
75          }          }
76    
77          protected final schmitzm.geotools.gui.JMapPane mapPane;          protected final schmitzm.geotools.gui.SelectableXMapPane mapPane;
78    
79          public LabelSearch(final schmitzm.geotools.gui.JMapPane mapPane) {          public LabelSearch(final schmitzm.geotools.gui.SelectableXMapPane mapPane) {
80                  this.mapPane = mapPane;                  this.mapPane = mapPane;
81          }          }
82    
         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.getAttributeType(propertyName);  
                 } else {  
                         // When does this happen  
                 }  
   
                 return null;  
         }  
   
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    
89                  for (final MapLayer ml : mapPane.getContext().getLayers()) {                  for (final MapLayer ml : mapPane.getMapContext().getLayers()) {
90                          try {                          try {
91    
92                                  // System.out.println("layer = "+ml.getTitle());                                  // System.out.println("layer = "+ml.getTitle());
# Line 113  public class LabelSearch { Line 94  public class LabelSearch {
94                                  if (!ml.isVisible())                                  if (!ml.isVisible())
95                                          continue;                                          continue;
96    
97                                  final List<TextSymbolizer> allTS = StylingUtil.getTextSymbolizers(ml                                  final List<TextSymbolizer> allTS = StylingUtil
98                                                  .getStyle());                                                  .getVisibleTextSymbolizers(ml.getStyle());
99                                  if (allTS.size() == 0) {                                  if (allTS.size() == 0) {
100                                          // A layer without any TextSymbolizer doesn't have to be                                          // A layer without any TextSymbolizer doesn't have to be
101                                          // searched any more.                                          // searched any more.
102                                          continue;                                          continue;
103                                  }                                  }
104    
105                                  final String typeName = ml.getFeatureSource().getSchema()                                  // We only deal with one TextSymbolizer so far:
106                                                  .getTypeName();                                  TextSymbolizer ts = allTS.get(0);
107    
108                                  // Expression labelExp = ts.getLabel();                                  final FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) ml
109                                  // 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();  
110    
111                                          final TextSymbolizer ts = StylingUtil.getTextSymbolizer(ml                                  SimpleFeatureType schema = featureSource.getSchema();
                                                         .getStyle(), f);  
                                         if (ts == null)  
                                                 continue;  
112    
113                                          final AttributeDescriptor labelAttribute = getLabelAttribute(ts, ml                                  final String typeName = schema.getName().getLocalPart();
                                                         .getFeatureSource().getSchema());  
114    
115                                          if (labelAttribute == null) {                                  PropertyName prop1 = StylingUtil.getFirstPropertyName(schema,
116                                                  continue;                                                  ts);
117                                          }                                  PropertyName prop2 = StylingUtil.getSecondPropertyName(schema,
118                                                    ts);
119    
120                                          // System.out.println("labelAttrib local name" +                                  if (StylingUtil.getFirstPropertyName(schema, ts) == null) {
121                                          // labelAttribute.getLocalName());                                          // At least one property field we need
122                                            continue;
123                                    }
124    
125                                          final Object value = f                                  Filter searchFilter;
126                                                          .getAttribute(labelAttribute.getLocalName());                                  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 value " + value);                                          searchFilter = FeatureUtil.FILTER_FACTORY2.or(searchFilter,
139                                                            searchFilter2);
140    
141                                          if (value == null) {                                          properties = LangUtil.extendArray(properties, prop2
142                                                  LOGGER.info("Skipping f: getLocalName() is null for feature="+f);                                                          .getPropertyName());
143                                                  continue;                                  }
144                                          }  
145                                    // 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                                          /**                                  FeatureCollection<SimpleFeatureType, SimpleFeature> features = (FeatureCollection<SimpleFeatureType, SimpleFeature>) ml
155                                           * LabelString ist z.B. "IMPETUS pluviograph". Suchwort                                                  .getFeatureSource().getFeatures(
156                                           * "plu" soll treffen. Also wird nach spaces zerlegt und                                                                  new DefaultQuery(typeName, searchFilter,
157                                           * dann gesucht                                                                                  properties));
158                                           */  
159                                          final String labelString = value.toString().toLowerCase();                                  final Iterator<SimpleFeature> fi = features.iterator();
160                                          if (labelString.startsWith(searchMe)) {                                  try {
161                                                  hits.add(createSearchResult(f, value.toString(), ml                                          while (fi.hasNext()) {
162                                                                  .getTitle(),ml));                                                  final SimpleFeature f = fi.next();
163                                          } else {  
164                                                  final String[] parts = labelString.trim().split(" ");                                                  String valueString = "";
165                                                  for (final String part : parts) {                                                  valueString = f.getAttribute(prop1.getPropertyName())
166                                                          if (part.startsWith(searchMe)) {                                                                  .toString();
167                                                                  hits.add(createSearchResult(f, value.toString(), ml  
168                                                                                  .getTitle(), ml));                                                  if (prop2 != null) {
169                                                                  break;                                                          String valueString2 = f.getAttribute(
170                                                          }                                                                          prop2.getPropertyName()).toString();
171    
172                                                            if (valueString2 != null && !valueString2.isEmpty())
173                                                                    valueString += ", " + valueString2;
174                                                  }                                                  }
                                         }  
175    
176                                                    hits.add(createSearchResult(f, valueString, ml
177                                                                    .getTitle(), ml));
178                                            }
179                                    } finally {
180                                            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    
# Line 204  public class LabelSearch { Line 193  public class LabelSearch {
193                  return hits;                  return hits;
194          }          }
195    
196          protected SearchResult createSearchResult(final SimpleFeature f, final String title,          protected SearchResult createSearchResult(final SimpleFeature f,
197                          final String inTitle, MapLayer ml) {                          final String title, final String inTitle, MapLayer ml) {
198                  return new SearchResultFeature(f, title, inTitle, mapPane, ml);                  return new SearchResultFeature(f, title, inTitle, mapPane, ml);
199          }          }
200    

Legend:
Removed from v.332  
changed lines
  Added in v.685

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26