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

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

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26