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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26