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

trunk/src/skrueger/geotools/labelsearch/LabelSearch.java revision 139 by alfonx, Thu Jun 11 12:52:58 2009 UTC branches/1.0-gt2-2.6/src/skrueger/geotools/labelsearch/LabelSearch.java revision 574 by alfonx, Wed Nov 25 08:09:42 2009 UTC
# Line 1  Line 1 
1    /*******************************************************************************
2     * Copyright (c) 2009 Martin O. J. Schmitz.
3     *
4     * This file is part of the SCHMITZM library - a collection of utility
5     * classes based on Java 1.6, focusing (not only) on Java Swing
6     * and the Geotools library.
7     *
8     * The SCHMITZM project is hosted at:
9     * http://wald.intevation.org/projects/schmitzm/
10     *
11     * This program is free software; you can redistribute it and/or
12     * modify it under the terms of the GNU Lesser General Public License
13     * as published by the Free Software Foundation; either version 3
14     * of the License, or (at your option) any later version.
15     *
16     * This program is distributed in the hope that it will be useful,
17     * but WITHOUT ANY WARRANTY; without even the implied warranty of
18     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19     * GNU General Public License for more details.
20     *
21     * You should have received a copy of the GNU Lesser General Public License (license.txt)
22     * along with this program; if not, write to the Free Software
23     * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
24     * or try this link: http://www.gnu.org/licenses/lgpl.html
25     *
26     * Contributors:
27     *     Martin O. J. Schmitz - initial API and implementation
28     *     Stefan A. Krüger - additional utility classes
29     ******************************************************************************/
30  package skrueger.geotools.labelsearch;  package skrueger.geotools.labelsearch;
31    
32  import java.io.IOException;  import java.io.IOException;
# Line 9  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.feature.AttributeType;  import org.geotools.data.FeatureSource;
 import org.geotools.feature.Feature;  
42  import org.geotools.feature.FeatureCollection;  import org.geotools.feature.FeatureCollection;
 import org.geotools.feature.FeatureType;  
43  import org.geotools.map.MapLayer;  import org.geotools.map.MapLayer;
44  import org.geotools.styling.Style;  import org.geotools.styling.Style;
45  import org.geotools.styling.TextSymbolizer;  import org.geotools.styling.TextSymbolizer;
46    import org.opengis.feature.simple.SimpleFeature;
47    import org.opengis.feature.simple.SimpleFeatureType;
48    import org.opengis.feature.type.AttributeDescriptor;
49  import org.opengis.filter.Filter;  import org.opengis.filter.Filter;
50  import org.opengis.filter.expression.Expression;  import org.opengis.filter.expression.Expression;
51  import org.opengis.filter.expression.PropertyName;  import org.opengis.filter.expression.PropertyName;
# Line 34  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          }          }
75    
76          protected final schmitzm.geotools.gui.JMapPane mapPane;          protected final schmitzm.geotools.gui.SelectableXMapPane mapPane;
77    
78          public LabelSearch(final schmitzm.geotools.gui.JMapPane mapPane) {          public LabelSearch(final schmitzm.geotools.gui.SelectableXMapPane mapPane) {
79                  this.mapPane = mapPane;                  this.mapPane = mapPane;
80          }          }
81    
82          private AttributeType getLabelAttribute(final TextSymbolizer ts,          /**
83                          final FeatureType schema) {           * The Attribute that provides the labels for this text symbolizer.
84             */
85            private AttributeDescriptor getLabelAttribute(final TextSymbolizer ts,
86                            final SimpleFeatureType schema) {
87                  if (ts == null) {                  if (ts == null) {
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;
95                          final String propertyName = pn.getPropertyName();                          final String propertyName = pn.getPropertyName();
96                          return schema.getAttributeType(propertyName);                          return schema.getDescriptor(propertyName);
97                  } else {                  } else {
98                          // When does this happen                          return null;
99                  }                  }
100    
                 return null;  
101          }          }
102    
103          public List<SearchResult> search(final String string) {          public List<SearchResult> search(final String string) {
# Line 76  public class LabelSearch { Line 106  public class LabelSearch {
106    
107                  final ArrayList<SearchResult> hits = new ArrayList<SearchResult>();                  final ArrayList<SearchResult> hits = new ArrayList<SearchResult>();
108    
109                  for (final MapLayer ml : mapPane.getContext().getLayers()) {                  for (final MapLayer ml : mapPane.getMapContext().getLayers()) {
110                          try {                          try {
111    
112                                  // System.out.println("layer = "+ml.getTitle());                                  // System.out.println("layer = "+ml.getTitle());
# Line 84  public class LabelSearch { Line 114  public class LabelSearch {
114                                  if (!ml.isVisible())                                  if (!ml.isVisible())
115                                          continue;                                          continue;
116    
117                                  final List<TextSymbolizer> allTS = StylingUtil.getTextSymbolizers(ml                                  final List<TextSymbolizer> allTS = StylingUtil
118                                                  .getStyle());                                                  .getVisibleTextSymbolizers(ml.getStyle());
119                                  if (allTS.size() == 0) {                                  if (allTS.size() == 0) {
120                                          // A layer without any TextSymbolizer doesn't have to be                                          // A layer without any TextSymbolizer doesn't have to be
121                                          // searched any more.                                          // searched any more.
122                                          continue;                                          continue;
123                                  }                                  }
124    
125                                  final String typeName = ml.getFeatureSource().getSchema()                                  final FeatureSource<SimpleFeatureType, SimpleFeature> featureSource = (FeatureSource<SimpleFeatureType, SimpleFeature>) ml
126                                                  .getTypeName();                                                  .getFeatureSource();
127    
128                                    final String typeName = featureSource.getSchema().getName()
129                                                    .getLocalPart();
130    
131                                  // Expression labelExp = ts.getLabel();                                  // Expression labelExp = ts.getLabel();
132                                  // ff.like(labelExp, "*"+searchMe+"*");                                  // ff.like(labelExp, "*"+searchMe+"*");
# Line 102  public class LabelSearch { Line 135  public class LabelSearch {
135                                  // new DefaultQuery(typeName, ff.like(labelExp,                                  // new DefaultQuery(typeName, ff.like(labelExp,
136                                  // "*"+searchMe+"*"), properties));                                  // "*"+searchMe+"*"), properties));
137    
138                                  final FeatureCollection features = ml.getFeatureSource().getFeatures(                                  final FeatureCollection<SimpleFeatureType, SimpleFeature> features = featureSource
139                                                  new DefaultQuery(typeName, Filter.INCLUDE));                                                  .getFeatures(new DefaultQuery(typeName, Filter.INCLUDE));
140    
141                                  // new MemoryDataStore().getFeatureSource(typeName)                                  // new MemoryDataStore().getFeatureSource(typeName)
142    
# Line 112  public class LabelSearch { Line 145  public class LabelSearch {
145                                   * support case insensitivity and i don't find a lower or UPPER                                   * support case insensitivity and i don't find a lower or UPPER
146                                   * function.                                   * function.
147                                   */                                   */
148                                  final Iterator<Feature> fi = features.iterator();                                  final Iterator<SimpleFeature> fi = features.iterator();
149                                  while (fi.hasNext()) {                                  try {
                                         final Feature f = fi.next();  
   
                                         final TextSymbolizer ts = StylingUtil.getTextSymbolizer(ml  
                                                         .getStyle(), f);  
                                         if (ts == null)  
                                                 continue;  
150    
151                                          final AttributeType labelAttribute = getLabelAttribute(ts, ml                                          while (fi.hasNext()) {
152                                                          .getFeatureSource().getSchema());                                                  final SimpleFeature f = fi.next();
153    
154                                          if (labelAttribute == null) {                                                  final TextSymbolizer ts = StylingUtil
155                                                  continue;                                                                  .getTextSymbolizer(ml.getStyle(), f);
156                                          }                                                  if (ts == null)
157                                                            continue;
158                                          // System.out.println("labelAttrib local name" +                                                  
159                                          // labelAttribute.getLocalName());                                                  // TODO Evaluate the Rule that belongs to the
160                                                    SimpleFeatureType schema = featureSource.getSchema();
161                                          final Object value = f                                                  PropertyName pn = StylingUtil.getFirstPropertyName(
162                                                          .getAttribute(labelAttribute.getLocalName());                                                                  schema, ts);
163                                                    if (pn == null) continue;
164                                          // System.out.println("labelAttrib value " + value);                                                  
165                                                    final AttributeDescriptor labelAttribute = schema
166                                          if (value == null) {                                                                  .getDescriptor(pn.getPropertyName());
167                                                  LOGGER.info("Skipping f: getLocalName() is null for feature="+f);  
168                                                  continue;                                                  final Object value = f.getAttribute(labelAttribute
169                                          }                                                                  .getLocalName());
170    
171                                                    if (value == null) {
172                                                            LOGGER
173                                                                            .info("Skipping f: getLocalName() is null for feature="
174                                                                                            + f);
175                                                            continue;
176                                                    }
177    
178                                          /**                                                  /**
179                                           * LabelString ist z.B. "IMPETUS pluviograph". Suchwort                                                   * LabelString ist z.B. "IMPETUS pluviograph". Suchwort
180                                           * "plu" soll treffen. Also wird nach spaces zerlegt und                                                   * "plu" soll treffen. Also wird nach spaces zerlegt und
181                                           * dann gesucht                                                   * dann gesucht
182                                           */                                                   */
183                                          final String labelString = value.toString().toLowerCase();                                                  final String labelString = value.toString()
184                                          if (labelString.startsWith(searchMe)) {                                                                  .toLowerCase();
185                                                  hits.add(createSearchResult(f, value.toString(), ml                                                  if (labelString.startsWith(searchMe)) {
186                                                                  .getTitle()));                                                          hits.add(createSearchResult(f, value.toString(), ml
187                                          } else {                                                                          .getTitle(), ml));
188                                                  final String[] parts = labelString.trim().split(" ");                                                  } else {
189                                                  for (final String part : parts) {                                                          final String[] parts = labelString.trim()
190                                                          if (part.startsWith(searchMe)) {                                                                          .split(" ");
191                                                                  hits.add(createSearchResult(f, value.toString(), ml                                                          for (final String part : parts) {
192                                                                                  .getTitle()));                                                                  if (part.startsWith(searchMe)) {
193                                                                  break;                                                                          hits.add(createSearchResult(f, value
194                                                                                            .toString(), ml.getTitle(), ml));
195                                                                            break;
196                                                                    }
197                                                          }                                                          }
198                                                  }                                                  }
                                         }  
199    
200                                            }
201                                    } finally {
202                                            features.close(fi);
203                                  }                                  }
204    
205                          } catch (final IOException e) {                          } catch (final IOException e) {
206                                  // Searching this layer failed                                  // Searching this layer failed
207                                  LOGGER.error(e);                                  LOGGER.error(e);
208                          }                          }
209    
210                  } // next layer                  } // next layer
211    
212                  // Hits from the top-most layer should appear first.                  // Hits from the top-most layer should appear first.
# Line 175  public class LabelSearch { Line 215  public class LabelSearch {
215                  return hits;                  return hits;
216          }          }
217    
218          protected SearchResult createSearchResult(final Feature f, final String title,          protected SearchResult createSearchResult(final SimpleFeature f,
219                          final String inTitle) {                          final String title, final String inTitle, MapLayer ml) {
220                  return new SearchResultFeature(f, title, inTitle, mapPane);                  return new SearchResultFeature(f, title, inTitle, mapPane, ml);
221          }          }
222    
223  }  }

Legend:
Removed from v.139  
changed lines
  Added in v.574

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26