/[schmitzm]/trunk/src/skrueger/geotools/StyledFeatureCollection.java
ViewVC logotype

Diff of /trunk/src/skrueger/geotools/StyledFeatureCollection.java

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

trunk/src/skrueger/geotools/StyledFeatureCollection.java revision 2 by mojays, Tue Feb 24 22:43:52 2009 UTC branches/1.0-gt2-2.6/src/skrueger/geotools/StyledFeatureCollection.java revision 318 by alfonx, Wed Aug 26 13:33:32 2009 UTC
# Line 1  Line 1 
1  package skrueger.geotools;  /*******************************************************************************
2     * Copyright (c) 2009 Martin O. J. Schmitz.
3  import java.net.URL;   *
4  import java.util.Map;   * This file is part of the SCHMITZM library - a collection of utility
5  import java.util.HashMap;   * classes based on Java 1.6, focusing (not only) on Java Swing
6  import javax.swing.ImageIcon;   * and the Geotools library.
7     *
8  import org.geotools.styling.Style;   * The SCHMITZM project is hosted at:
9  import org.geotools.feature.FeatureCollection;   * http://wald.intevation.org/projects/schmitzm/
10  import org.geotools.feature.FeatureType;   *
11  import org.geotools.feature.AttributeType;   * 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  import schmitzm.geotools.feature.FeatureUtil;   * as published by the Free Software Foundation; either version 3
14     * of the License, or (at your option) any later version.
15  import skrueger.i8n.Translation;   *
16  import skrueger.AttributeMetaData;   * 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   * This class provides a simple implementation of {@link StyledMapInterface}   *
21   * for {@link FeatureCollection}. The uncache functionality is not supported,   * You should have received a copy of the GNU Lesser General Public License (license.txt)
22   * because this class bases on an existing {@link FeatureCollection} object in   * along with this program; if not, write to the Free Software
23   * memory.   * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
24   * @author <a href="mailto:[email protected]">Martin Schmitz</a> (University of Bonn/Germany)   * or try this link: http://www.gnu.org/licenses/lgpl.html
25   * @version 1.0   *
26   */   * Contributors:
27  public class StyledFeatureCollection extends AbstractStyledMap<FeatureCollection> implements StyledFeatureCollectionInterface {   *     Martin O. J. Schmitz - initial API and implementation
28     *     Stefan A. Krüger - additional utility classes
29    /** Holds the meta data for displaying a legend. */   ******************************************************************************/
30    protected Map<Integer,AttributeMetaData> attrMetaData = null;  package skrueger.geotools;
31    
32    /**  import java.io.IOException;
33     * Creates a styled {@link FeatureCollection} with language-specific informations.  import java.net.URL;
34     * @param fc the {@link FeatureCollection}  import java.util.HashMap;
35     * @param id a unique ID for the object  import java.util.Map;
36     * @param title a (language-specific) short description  
37     * @param desc a (language-specific) long description  import javax.swing.ImageIcon;
38     * @param keywords (language-specific) keywords for the geo objects  
39     * @param style a display style (if {@code null}, a default style is created)  import org.geotools.data.FeatureSource;
40     * @param attrMetaData meta data for displaying a legend  import org.geotools.data.collection.CollectionDataStore;
41     * @param icon an icon for the object (can be {@code null})  import org.geotools.feature.AttributeType;
42     * @exception IllegalArgumentException if {@code null} is given as ID or geo object  import org.geotools.feature.FeatureCollection;
43     */  import org.geotools.feature.SimpleFeatureType;
44    public StyledFeatureCollection(FeatureCollection fc, String id, Translation title, Translation desc, Translation keywords, Style style, Map<Integer,AttributeMetaData> attrMetaData, ImageIcon icon) {  import org.geotools.styling.Style;
45      super(fc, fc.getBounds(), fc.getSchema().getDefaultGeometry().getCoordinateSystem(), id, title, desc, keywords, style, icon);  
46      setAttributeMetaData( attrMetaData );  import schmitzm.geotools.feature.FeatureUtil;
47    }  import skrueger.AttributeMetaData;
48    import skrueger.i8n.Translation;
49    /**  
50     * Creates a styled {@link FeatureCollection} with language-specific informations.  /**
51     * @param fc the {@link FeatureCollection}   * This class provides a simple implementation of {@link StyledLayerInterface} for
52     * @param id a unique ID for the object   * {@link FeatureCollection}. The uncache functionality is not supported,
53     * @param title a (language-specific) short description   * because this class bases on an existing {@link FeatureCollection} object in
54     * @param desc a (language-specific) long description   * memory.
55     * @param keywords (language-specific) keywords for the geo objects   *
56     * @param style a display style with attribute meta data information   * @author <a href="mailto:[email protected]">Martin Schmitz</a>
57     * @param icon an icon for the object (can be {@code null})   *         (University of Bonn/Germany)
58     * @exception IllegalArgumentException if {@code null} is given as ID or geo object   * @version 1.0
59     */   */
60    public StyledFeatureCollection(FeatureCollection fc, String id, Translation title, Translation desc, Translation keywords, StyledMapStyle<Map<Integer,AttributeMetaData>> style, ImageIcon icon) {  public class StyledFeatureCollection extends
61      super(fc, fc.getBounds(), fc.getSchema().getDefaultGeometry().getCoordinateSystem(), id, title, desc, keywords, style != null ? style.getGeoObjectStyle() : null, icon);                  AbstractStyledLayer<FeatureCollection> implements
62      setAttributeMetaData( style != null ? style.getMetaData() : null );                  StyledFeatureCollectionInterface {
63    }  
64            /** Holds the meta data for displaying a legend. */
65    /**          protected Map<Integer, AttributeMetaData> attrMetaData = null;
66     * Creates a styled {@link FeatureCollection} with a language-specific title,  
67     * no long description, no keywords, default attribute meta data and no icon.          /**
68     * @param fc the {@link FeatureCollection}           * We be filled with a "virtual" {@link FeatureSource} on demand.
69     * @param id a unique ID for the object           */
70     * @param title a short description          private FeatureSource featureSource = null;
71     * @param style a display style (if {@code null}, a default style is created)  
72     * @exception IllegalArgumentException if {@code null} is given as ID or geo object          /**
73     * @see #createDefaultAttributeMetaDataMap(FeatureCollection)           * Creates a styled {@link FeatureCollection} with language-specific
74     */           * informations.
75    public StyledFeatureCollection(FeatureCollection fc, String id, Translation title, Style style) {           *
76      this(fc, id, title, null, null, style, null, null);           * @param fc
77    }           *            the {@link FeatureCollection}
78             * @param id
79    /**           *            a unique ID for the object
80     * Creates a styled {@link FeatureCollection} with non-translated informations.           * @param title
81     * @param fc the {@link FeatureCollection}           *            a (language-specific) short description
82     * @param id a unique ID for the object           * @param desc
83     * @param title a short description           *            a (language-specific) long description
84     * @param desc a long description           * @param keywords
85     * @param keywords keywords for the geo objects           *            (language-specific) keywords for the geo objects
86     * @param style a display style (if {@code null}, a default style is created)           * @param style
87     * @param attrMetaData meta data for displaying a legend           *            a display style (if {@code null}, a default style is created)
88     * @param icon an icon for the object (can be {@code null})           * @param attrMetaData
89     * @exception IllegalArgumentException if {@code null} is given as ID or geo object           *            meta data for displaying a legend
90     */           * @param icon
91    public StyledFeatureCollection(FeatureCollection fc, String id, String title, String desc, String keywords, Style style, Map<Integer,AttributeMetaData> attrMetaData, ImageIcon icon) {           *            an icon for the object (can be {@code null})
92      this(fc, id, (Translation)null, null, null, style, attrMetaData, icon);           * @exception IllegalArgumentException
93      setTitle(title);           *                if {@code null} is given as ID or geo object
94      setDesc(desc);           */
95      setKeywords(keywords);          public StyledFeatureCollection(FeatureCollection fc, String id,
96    }                          Translation title, Translation desc, Translation keywords,
97                            Style style, Map<Integer, AttributeMetaData> attrMetaData,
98    /**                          ImageIcon icon) {
99     * Creates a styled {@link FeatureCollection} with non-translated informations.                  super(fc, fc.getBounds(), fc.getSchema().getDefaultGeometry()
100     * @param fc the {@link FeatureCollection}                                  .getCoordinateSystem(), id, title, desc, keywords, style, icon);
101     * @param id a unique ID for the object                  setAttributeMetaData(attrMetaData);
102     * @param title a short description          }
103     * @param desc a long description  
104     * @param keywords keywords for the geo objects          /**
105     * @param style a display style with attribute meta data information           * Creates a styled {@link FeatureCollection} with language-specific
106     * @param icon an icon for the object (can be {@code null})           * informations.
107     * @exception IllegalArgumentException if {@code null} is given as ID or geo object           *
108     */           * @param fc
109    public StyledFeatureCollection(FeatureCollection fc, String id, String title, String desc, String keywords, StyledMapStyle<Map<Integer,AttributeMetaData>> style, ImageIcon icon) {           *            the {@link FeatureCollection}
110      this(fc,           * @param id
111           id,           *            a unique ID for the object
112           title,           * @param title
113           desc,           *            a (language-specific) short description
114           keywords,           * @param desc
115           style != null ? style.getGeoObjectStyle() : null,           *            a (language-specific) long description
116           style != null ? style.getMetaData() : null,           * @param keywords
117           icon           *            (language-specific) keywords for the geo objects
118      );           * @param style
119    }           *            a display style with attribute meta data information
120             * @param icon
121    /**           *            an icon for the object (can be {@code null})
122     * Creates a styled {@link FeatureCollection} with a non-translated title,           * @exception IllegalArgumentException
123     * no long description, no keywords, default attribute meta data and no icon.           *                if {@code null} is given as ID or geo object
124     * @param fc the {@link FeatureCollection}           */
125     * @param id a unique ID for the object          public StyledFeatureCollection(FeatureCollection fc, String id,
126     * @param title a short description                          Translation title, Translation desc, Translation keywords,
127     * @param style a display style (if {@code null}, a default style is created)                          StyledLayerStyle<Map<Integer, AttributeMetaData>> style,
128     * @exception IllegalArgumentException if {@code null} is given as ID or geo object                          ImageIcon icon) {
129     * @see #createDefaultAttributeMetaDataMap(FeatureCollection)                  super(fc, fc.getBounds(), fc.getSchema().getDefaultGeometry()
130     */                                  .getCoordinateSystem(), id, title, desc, keywords,
131    public StyledFeatureCollection(FeatureCollection fc, String id, String title, Style style) {                                  style != null ? style.getGeoObjectStyle() : null, icon);
132      this(fc, id, title, null, null, style, null, null);                  setAttributeMetaData(style != null ? style.getMetaData() : null);
133    }          }
134    
135    /**          /**
136     * Creates a styled {@link FeatureCollection} with a non-translated title,           * Creates a styled {@link FeatureCollection} with a language-specific
137     * no long description, no keywords, default attribute meta data and no icon.           * title, no long description, no keywords, default attribute meta data and
138     * @param fc the {@link FeatureCollection}           * no icon.
139     * @param id a unique ID for the object           *
140     * @param title a short description           * @param fc
141     * @param style a display style (if {@code null}, a default style is created)           *            the {@link FeatureCollection}
142     * @exception IllegalArgumentException if {@code null} is given as ID or geo object           * @param id
143     * @see #createDefaultAttributeMetaDataMap(FeatureCollection)           *            a unique ID for the object
144     */           * @param title
145    public StyledFeatureCollection(FeatureCollection fc, String id, String title, StyledMapStyle<Map<Integer,AttributeMetaData>> style) {           *            a short description
146      this(           * @param style
147        fc,           *            a display style (if {@code null}, a default style is created)
148        id,           * @exception IllegalArgumentException
149        title,           *                if {@code null} is given as ID or geo object
150        null,           * @see #createDefaultAttributeMetaDataMap(FeatureCollection)
151        null,           */
152        style != null ? style.getGeoObjectStyle() : null,          public StyledFeatureCollection(FeatureCollection fc, String id,
153        style != null ? style.getMetaData() : null,                          Translation title, Style style) {
154        null                  this(fc, id, title, null, null, style, null, null);
155      );          }
156    }  
157            /**
158    /**           * Creates a styled {@link FeatureCollection} with non-translated
159     * Creates a default style for the {@link FeatureCollection}.           * informations.
160     * @see FeatureUtil#createDefaultStyle(FeatureCollection)           *
161     */           * @param fc
162    protected Style createDefaultStyle() {           *            the {@link FeatureCollection}
163      return FeatureUtil.createDefaultStyle( geoObject );           * @param id
164    }           *            a unique ID for the object
165             * @param title
166    /**           *            a short description
167     * Returns the meta data needed for displaying a legend.           * @param desc
168     */           *            a long description
169    public Map<Integer,AttributeMetaData> getAttributeMetaDataMap() {           * @param keywords
170      return attrMetaData;           *            keywords for the geo objects
171    }           * @param style
172             *            a display style (if {@code null}, a default style is created)
173    /**           * @param attrMetaData
174     * Sets the meta data needed for displaying a legend.           *            meta data for displaying a legend
175     * If {@code legendData} is {@code null} an empty map is set, so           * @param icon
176     * {@link #getAttributeMetaDataMap()} never returns {@code null}.           *            an icon for the object (can be {@code null})
177     * @param attrMetaData map of attribute meta data           * @exception IllegalArgumentException
178     */           *                if {@code null} is given as ID or geo object
179    public void setAttributeMetaData(Map<Integer,AttributeMetaData> attrMetaData) {           */
180      this.attrMetaData = (attrMetaData != null) ? attrMetaData : createDefaultAttributeMetaDataMap(geoObject);          public StyledFeatureCollection(FeatureCollection fc, String id,
181    }                          String title, String desc, String keywords, Style style,
182                            Map<Integer, AttributeMetaData> attrMetaData, ImageIcon icon) {
183    /**                  this(fc, id, (Translation) null, null, null, style, attrMetaData, icon);
184     * Creates non-translated default meta data for a {@link FeatureCollection}                  setTitle(title);
185     * with all attributes visible and no unit set.                  setDesc(desc);
186     * @param fc a {@link FeatureCollection}                  setKeywords(keywords);
187     */          }
188    public static Map<Integer,AttributeMetaData> createDefaultAttributeMetaDataMap(FeatureCollection fc) {  
189      HashMap<Integer,AttributeMetaData> metaDataMap = new HashMap<Integer,AttributeMetaData>();          /**
190      FeatureType ftype = fc.getSchema();           * Creates a styled {@link FeatureCollection} with non-translated
191      for (int i=0; i<ftype.getAttributeCount(); i++) {           * informations.
192        AttributeType aType = ftype.getAttributeType(i);           *
193        if ( aType != ftype.getDefaultGeometry() )           * @param fc
194          metaDataMap.put(           *            the {@link FeatureCollection}
195            i,           * @param id
196            new AttributeMetaData(           *            a unique ID for the object
197              i,  // Column no.           * @param title
198              true, // visible           *            a short description
199              new Translation( aType.getName() ), // Column name           * @param desc
200              new Translation(), // description           *            a long description
201              "" // Unit           * @param keywords
202            )           *            keywords for the geo objects
203          );           * @param style
204      }           *            a display style with attribute meta data information
205      return metaDataMap;           * @param icon
206    }           *            an icon for the object (can be {@code null})
207             * @exception IllegalArgumentException
208    /**           *                if {@code null} is given as ID or geo object
209     * Simply sets the {@link #geoObject}, {@link #crs}, {@link #envelope} and           */
210     * {@link #attrMetaData} to {@code null}.          public StyledFeatureCollection(FeatureCollection fc, String id,
211     */                          String title, String desc, String keywords,
212    public void dispose() {                          StyledLayerStyle<Map<Integer, AttributeMetaData>> style,
213      this.geoObject    = null;                          ImageIcon icon) {
214      this.envelope     = null;                  this(fc, id, title, desc, keywords, style != null ? style
215      this.crs          = null;                                  .getGeoObjectStyle() : null, style != null ? style
216      this.attrMetaData = null;                                  .getMetaData() : null, icon);
217    }          }
218    
219    /**          /**
220     * Tests whether the geo object is disposed.           * Creates a styled {@link FeatureCollection} with a non-translated title,
221     */           * no long description, no keywords, default attribute meta data and no
222    public boolean isDisposed() {           * icon.
223      return geoObject == null;           *
224    }           * @param fc
225             *            the {@link FeatureCollection}
226    /**           * @param id
227     * Does nothing, because the {@link AbstractStyledMap} bases on existing           *            a unique ID for the object
228     * objects (in memory) which can not be uncached and reloaded.           * @param title
229     */           *            a short description
230    public void uncache() {           * @param style
231      LOGGER.warn("Uncache functionality is not supported. Object remains in memory.");           *            a display style (if {@code null}, a default style is created)
232    }           * @exception IllegalArgumentException
233             *                if {@code null} is given as ID or geo object
234             * @see #createDefaultAttributeMetaDataMap(FeatureCollection)
235    /*           */
236     * (non-Javadoc)          public StyledFeatureCollection(FeatureCollection fc, String id,
237     * @see skrueger.geotools.StyledMapInterface#getInfoURL()                          String title, Style style) {
238     */                  this(fc, id, title, null, null, style, null, null);
239          public URL getInfoURL() {          }
240                  return null;  
241          }          /**
242             * Creates a styled {@link FeatureCollection} with a non-translated title,
243          public boolean isHideInLegend() {           * no long description, no keywords, default attribute meta data and no
244                  return false;           * icon.
245          }           *
246  }           * @param fc
247             *            the {@link FeatureCollection}
248             * @param id
249             *            a unique ID for the object
250             * @param title
251             *            a short description
252             * @param style
253             *            a display style (if {@code null}, a default style is created)
254             * @exception IllegalArgumentException
255             *                if {@code null} is given as ID or geo object
256             * @see #createDefaultAttributeMetaDataMap(FeatureCollection)
257             */
258            public StyledFeatureCollection(FeatureCollection fc, String id,
259                            String title, StyledLayerStyle<Map<Integer, AttributeMetaData>> style) {
260                    this(fc, id, title, null, null, style != null ? style
261                                    .getGeoObjectStyle() : null, style != null ? style
262                                    .getMetaData() : null, null);
263            }
264    
265            /**
266             * Creates a default style for the {@link FeatureCollection}.
267             *
268             * @see FeatureUtil#createDefaultStyle(FeatureCollection)
269             */
270            protected Style createDefaultStyle() {
271                    return FeatureUtil.createDefaultStyle(geoObject);
272            }
273    
274            /**
275             * Returns the meta data needed for displaying a legend.
276             */
277            public Map<Integer, AttributeMetaData> getAttributeMetaDataMap() {
278                    return attrMetaData;
279            }
280    
281            /**
282             * Sets the meta data needed for displaying a legend. If {@code legendData}
283             * is {@code null} an empty map is set, so
284             * {@link #getAttributeMetaDataMap()} never returns {@code null}.
285             *
286             * @param attrMetaData
287             *            map of attribute meta data
288             */
289            public void setAttributeMetaData(
290                            Map<Integer, AttributeMetaData> attrMetaData) {
291                    this.attrMetaData = (attrMetaData != null) ? attrMetaData
292                                    : createDefaultAttributeMetaDataMap(geoObject);
293            }
294    
295            /**
296             * Creates non-translated default meta data for a {@link FeatureCollection}
297             * with all attributes visible and no unit set.
298             *
299             * @param fc
300             *            a {@link FeatureCollection}
301             */
302            public static Map<Integer, AttributeMetaData> createDefaultAttributeMetaDataMap(
303                            FeatureCollection fc) {
304                    HashMap<Integer, AttributeMetaData> metaDataMap = new HashMap<Integer, AttributeMetaData>();
305                    SimpleFeatureType ftype = fc.getSchema();
306                    for (int i = 0; i < ftype.getAttributeCount(); i++) {
307                            AttributeType aType = ftype.getAttributeType(i);
308                            if (aType != ftype.getDefaultGeometry())
309                                    metaDataMap.put(i, new AttributeMetaData(i, // Column no.
310                                                    true, // visible
311                                                    new Translation(aType.getName()), // Column name
312                                                    new Translation(), // description
313                                                    "" // Unit
314                                    ));
315                    }
316                    return metaDataMap;
317            }
318    
319            /**
320             * Simply sets the {@link #geoObject}, {@link #crs}, {@link #envelope} and
321             * {@link #attrMetaData} to {@code null}.
322             */
323            public void dispose() {
324                    this.geoObject = null;
325                    this.envelope = null;
326                    this.crs = null;
327                    this.attrMetaData = null;
328            }
329    
330            /**
331             * Tests whether the geo object is disposed.
332             */
333            public boolean isDisposed() {
334                    return geoObject == null;
335            }
336    
337            /**
338             * Does nothing, because the {@link AbstractStyledLayer} bases on existing
339             * objects (in memory) which can not be uncached and reloaded.
340             */
341            public void uncache() {
342    
343                    /** It will be recreated on the next getFetureSource() **/
344                    featureSource = null;
345    
346                    LOGGER
347                                    .warn("Uncache onyl uncached any virtual FeatureSource. Object remains in memory.");
348            }
349    
350            /*
351             * (non-Javadoc)
352             *
353             * @see skrueger.geotools.StyledLayerInterface#getInfoURL()
354             */
355            public URL getInfoURL() {
356                    return null;
357            }
358    
359            /**
360             * Same as {@link #getGeoObject()} method, but complies to the {@link StyledFeaturesInterface}
361             * @see {@link StyledFeaturesInterface}
362             */
363            @Override
364            public FeatureCollection getFeatureCollection() {
365                    return getGeoObject();
366            }
367    
368            /**
369             * Returns a virtual {@link FeatureSource} to access the
370             * {@link FeatureCollection}. Once created, it will be reused until
371             * {@link #uncache()} is called.<br/>
372             * @see {@link StyledFeaturesInterface}
373             */
374            @Override
375            public FeatureSource getFeatureSource() {
376                    if (featureSource == null) {
377                            CollectionDataStore store = new CollectionDataStore(getGeoObject());
378                            try {
379                                    featureSource = store.getFeatureSource(store.getTypeNames()[0]);
380                            } catch (IOException e) {
381                                    throw new RuntimeException(
382                                                    "Could not create a FeatureSource from the CollectionDataStore:",
383                                                    e);
384                            }
385                    }
386                    return featureSource;
387            }
388    
389    }

Legend:
Removed from v.2  
changed lines
  Added in v.318

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26