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

Legend:
Removed from v.40  
changed lines
  Added in v.420

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26