/[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 127 by mojays, Sat May 30 14:29:58 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;
5  import java.util.HashMap;  import java.util.HashMap;
6  import java.util.Map;  import java.util.Map;
7    
8  import javax.swing.ImageIcon;  import javax.swing.ImageIcon;
9    
10    import org.geotools.data.FeatureSource;
11    import org.geotools.data.collection.CollectionDataStore;
12  import org.geotools.feature.AttributeType;  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;
# Line 15  import schmitzm.geotools.feature.Feature Line 18  import schmitzm.geotools.feature.Feature
18  import skrueger.AttributeMetaData;  import skrueger.AttributeMetaData;
19  import skrueger.i8n.Translation;  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. */          /** Holds the meta data for displaying a legend. */
36    protected Map<Integer,AttributeMetaData> attrMetaData = null;          protected Map<Integer, AttributeMetaData> attrMetaData = null;
37    
38    /**          /**
39     * Creates a styled {@link FeatureCollection} with language-specific informations.           * We be filled with a "virtual" {@link FeatureSource} on demand.
40     * @param fc the {@link FeatureCollection}           */
41     * @param id a unique ID for the object          private FeatureSource featureSource = null;
42     * @param title a (language-specific) short description  
43     * @param desc a (language-specific) long description          /**
44     * @param keywords (language-specific) keywords for the geo objects           * Creates a styled {@link FeatureCollection} with language-specific
45     * @param style a display style (if {@code null}, a default style is created)           * informations.
46     * @param attrMetaData meta data for displaying a legend           *
47     * @param icon an icon for the object (can be {@code null})           * @param fc
48     * @exception IllegalArgumentException if {@code null} is given as ID or geo object           *            the {@link FeatureCollection}
49     */           * @param id
50    public StyledFeatureCollection(FeatureCollection fc, String id, Translation title, Translation desc, Translation keywords, Style style, Map<Integer,AttributeMetaData> attrMetaData, ImageIcon icon) {           *            a unique ID for the object
51      super(fc, fc.getBounds(), fc.getSchema().getDefaultGeometry().getCoordinateSystem(), id, title, desc, keywords, style, icon);           * @param title
52      setAttributeMetaData( attrMetaData );           *            a (language-specific) short description
53    }           * @param desc
54             *            a (language-specific) long description
55    /**           * @param keywords
56     * Creates a styled {@link FeatureCollection} with language-specific informations.           *            (language-specific) keywords for the geo objects
57     * @param fc the {@link FeatureCollection}           * @param style
58     * @param id a unique ID for the object           *            a display style (if {@code null}, a default style is created)
59     * @param title a (language-specific) short description           * @param attrMetaData
60     * @param desc a (language-specific) long description           *            meta data for displaying a legend
61     * @param keywords (language-specific) keywords for the geo objects           * @param icon
62     * @param style a display style with attribute meta data information           *            an icon for the object (can be {@code null})
63     * @param icon an icon for the object (can be {@code null})           * @exception IllegalArgumentException
64     * @exception IllegalArgumentException if {@code null} is given as ID or geo object           *                if {@code null} is given as ID or geo object
65     */           */
66    public StyledFeatureCollection(FeatureCollection fc, String id, Translation title, Translation desc, Translation keywords, StyledMapStyle<Map<Integer,AttributeMetaData>> style, ImageIcon icon) {          public StyledFeatureCollection(FeatureCollection fc, String id,
67      super(fc, fc.getBounds(), fc.getSchema().getDefaultGeometry().getCoordinateSystem(), id, title, desc, keywords, style != null ? style.getGeoObjectStyle() : null, icon);                          Translation title, Translation desc, Translation keywords,
68      setAttributeMetaData( style != null ? style.getMetaData() : null );                          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     * Creates a styled {@link FeatureCollection} with a language-specific title,                  setAttributeMetaData(attrMetaData);
73     * no long description, no keywords, default attribute meta data and no icon.          }
74     * @param fc the {@link FeatureCollection}  
75     * @param id a unique ID for the object          /**
76     * @param title a short description           * Creates a styled {@link FeatureCollection} with language-specific
77     * @param style a display style (if {@code null}, a default style is created)           * informations.
78     * @exception IllegalArgumentException if {@code null} is given as ID or geo object           *
79     * @see #createDefaultAttributeMetaDataMap(FeatureCollection)           * @param fc
80     */           *            the {@link FeatureCollection}
81    public StyledFeatureCollection(FeatureCollection fc, String id, Translation title, Style style) {           * @param id
82      this(fc, id, title, null, null, style, null, null);           *            a unique ID for the object
83    }           * @param title
84             *            a (language-specific) short description
85    /**           * @param desc
86     * Creates a styled {@link FeatureCollection} with non-translated informations.           *            a (language-specific) long description
87     * @param fc the {@link FeatureCollection}           * @param keywords
88     * @param id a unique ID for the object           *            (language-specific) keywords for the geo objects
89     * @param title a short description           * @param style
90     * @param desc a long description           *            a display style with attribute meta data information
91     * @param keywords keywords for the geo objects           * @param icon
92     * @param style a display style (if {@code null}, a default style is created)           *            an icon for the object (can be {@code null})
93     * @param attrMetaData meta data for displaying a legend           * @exception IllegalArgumentException
94     * @param icon an icon for the object (can be {@code null})           *                if {@code null} is given as ID or geo object
95     * @exception IllegalArgumentException if {@code null} is given as ID or geo object           */
96     */          public StyledFeatureCollection(FeatureCollection fc, String id,
97    public StyledFeatureCollection(FeatureCollection fc, String id, String title, String desc, String keywords, Style style, Map<Integer,AttributeMetaData> attrMetaData, ImageIcon icon) {                          Translation title, Translation desc, Translation keywords,
98      this(fc, id, (Translation)null, null, null, style, attrMetaData, icon);                          StyledMapStyle<Map<Integer, AttributeMetaData>> style,
99      setTitle(title);                          ImageIcon icon) {
100      setDesc(desc);                  super(fc, fc.getBounds(), fc.getSchema().getDefaultGeometry()
101      setKeywords(keywords);                                  .getCoordinateSystem(), id, title, desc, keywords,
102    }                                  style != null ? style.getGeoObjectStyle() : null, icon);
103                    setAttributeMetaData(style != null ? style.getMetaData() : null);
104    /**          }
105     * Creates a styled {@link FeatureCollection} with non-translated informations.  
106     * @param fc the {@link FeatureCollection}          /**
107     * @param id a unique ID for the object           * Creates a styled {@link FeatureCollection} with a language-specific
108     * @param title a short description           * title, no long description, no keywords, default attribute meta data and
109     * @param desc a long description           * no icon.
110     * @param keywords keywords for the geo objects           *
111     * @param style a display style with attribute meta data information           * @param fc
112     * @param icon an icon for the object (can be {@code null})           *            the {@link FeatureCollection}
113     * @exception IllegalArgumentException if {@code null} is given as ID or geo object           * @param id
114     */           *            a unique ID for the object
115    public StyledFeatureCollection(FeatureCollection fc, String id, String title, String desc, String keywords, StyledMapStyle<Map<Integer,AttributeMetaData>> style, ImageIcon icon) {           * @param title
116      this(fc,           *            a short description
117           id,           * @param style
118           title,           *            a display style (if {@code null}, a default style is created)
119           desc,           * @exception IllegalArgumentException
120           keywords,           *                if {@code null} is given as ID or geo object
121           style != null ? style.getGeoObjectStyle() : null,           * @see #createDefaultAttributeMetaDataMap(FeatureCollection)
122           style != null ? style.getMetaData() : null,           */
123           icon          public StyledFeatureCollection(FeatureCollection fc, String id,
124      );                          Translation title, Style style) {
125    }                  this(fc, id, title, null, null, style, null, null);
126            }
127    /**  
128     * Creates a styled {@link FeatureCollection} with a non-translated title,          /**
129     * no long description, no keywords, default attribute meta data and no icon.           * Creates a styled {@link FeatureCollection} with non-translated
130     * @param fc the {@link FeatureCollection}           * informations.
131     * @param id a unique ID for the object           *
132     * @param title a short description           * @param fc
133     * @param style a display style (if {@code null}, a default style is created)           *            the {@link FeatureCollection}
134     * @exception IllegalArgumentException if {@code null} is given as ID or geo object           * @param id
135     * @see #createDefaultAttributeMetaDataMap(FeatureCollection)           *            a unique ID for the object
136     */           * @param title
137    public StyledFeatureCollection(FeatureCollection fc, String id, String title, Style style) {           *            a short description
138      this(fc, id, title, null, null, style, null, null);           * @param desc
139    }           *            a long description
140             * @param keywords
141    /**           *            keywords for the geo objects
142     * Creates a styled {@link FeatureCollection} with a non-translated title,           * @param style
143     * no long description, no keywords, default attribute meta data and no icon.           *            a display style (if {@code null}, a default style is created)
144     * @param fc the {@link FeatureCollection}           * @param attrMetaData
145     * @param id a unique ID for the object           *            meta data for displaying a legend
146     * @param title a short description           * @param icon
147     * @param style a display style (if {@code null}, a default style is created)           *            an icon for the object (can be {@code null})
148     * @exception IllegalArgumentException if {@code null} is given as ID or geo object           * @exception IllegalArgumentException
149     * @see #createDefaultAttributeMetaDataMap(FeatureCollection)           *                if {@code null} is given as ID or geo object
150     */           */
151    public StyledFeatureCollection(FeatureCollection fc, String id, String title, StyledMapStyle<Map<Integer,AttributeMetaData>> style) {          public StyledFeatureCollection(FeatureCollection fc, String id,
152      this(                          String title, String desc, String keywords, Style style,
153        fc,                          Map<Integer, AttributeMetaData> attrMetaData, ImageIcon icon) {
154        id,                  this(fc, id, (Translation) null, null, null, style, attrMetaData, icon);
155        title,                  setTitle(title);
156        null,                  setDesc(desc);
157        null,                  setKeywords(keywords);
158        style != null ? style.getGeoObjectStyle() : null,          }
159        style != null ? style.getMetaData() : null,  
160        null          /**
161      );           * Creates a styled {@link FeatureCollection} with non-translated
162    }           * informations.
163             *
164    /**           * @param fc
165     * Creates a default style for the {@link FeatureCollection}.           *            the {@link FeatureCollection}
166     * @see FeatureUtil#createDefaultStyle(FeatureCollection)           * @param id
167     */           *            a unique ID for the object
168    protected Style createDefaultStyle() {           * @param title
169      return FeatureUtil.createDefaultStyle( geoObject );           *            a short description
170    }           * @param desc
171             *            a long description
172    /**           * @param keywords
173     * Returns the meta data needed for displaying a legend.           *            keywords for the geo objects
174     */           * @param style
175    public Map<Integer,AttributeMetaData> getAttributeMetaDataMap() {           *            a display style with attribute meta data information
176      return attrMetaData;           * @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     * Sets the meta data needed for displaying a legend.           */
181     * If {@code legendData} is {@code null} an empty map is set, so          public StyledFeatureCollection(FeatureCollection fc, String id,
182     * {@link #getAttributeMetaDataMap()} never returns {@code null}.                          String title, String desc, String keywords,
183     * @param attrMetaData map of attribute meta data                          StyledMapStyle<Map<Integer, AttributeMetaData>> style,
184     */                          ImageIcon icon) {
185    public void setAttributeMetaData(Map<Integer,AttributeMetaData> attrMetaData) {                  this(fc, id, title, desc, keywords, style != null ? style
186      this.attrMetaData = (attrMetaData != null) ? attrMetaData : createDefaultAttributeMetaDataMap(geoObject);                                  .getGeoObjectStyle() : null, style != null ? style
187    }                                  .getMetaData() : null, icon);
   
   /**  
    * 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()  
    */  
         public URL getInfoURL() {  
                 return null;  
188          }          }
189    
190          /**          /**
191           * If true, this layer will not be shown in the legend. Default = false           * 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           * Killed by SK: 6. April 09: Ein Layer soll nicht generell auf           * @param fc
218           * verstecken/nicht verstecken gestellt werden können. Das sind           *            the {@link FeatureCollection}
219           * Eigenschaften der Karte/MapContext, ebenso wie die Reihenfolge der Layer.           * @param id
220           * Im Atlas verwaltet deshalb nun die Klasse skrueger.atlas.Map welche Layer           *            a unique ID for the object
221           * nicht in der Legende auftauchen sollen. Meines Wissens hat keiner bisher           * @param title
222           * die Funktion genutzt.           *            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            /**
237             * Creates a default style for the {@link FeatureCollection}.
238           *           *
239          public boolean isHideInLegend() {           * @see FeatureUtil#createDefaultStyle(FeatureCollection)
240                  return false;           */
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             * Sets the meta data needed for displaying a legend. If {@code legendData}
254             * is {@code null} an empty map is set, so
255             * {@link #getAttributeMetaDataMap()} never returns {@code null}.
256             *
257             * @param attrMetaData
258             *            map of attribute meta data
259             */
260            public void setAttributeMetaData(
261                            Map<Integer, AttributeMetaData> attrMetaData) {
262                    this.attrMetaData = (attrMetaData != null) ? attrMetaData
263                                    : createDefaultAttributeMetaDataMap(geoObject);
264            }
265    
266            /**
267             * Creates non-translated default meta data for a {@link FeatureCollection}
268             * with all attributes visible and no unit set.
269             *
270             * @param fc
271             *            a {@link FeatureCollection}
272             */
273            public static Map<Integer, AttributeMetaData> createDefaultAttributeMetaDataMap(
274                            FeatureCollection fc) {
275                    HashMap<Integer, AttributeMetaData> metaDataMap = new HashMap<Integer, AttributeMetaData>();
276                    FeatureType ftype = fc.getSchema();
277                    for (int i = 0; i < ftype.getAttributeCount(); i++) {
278                            AttributeType aType = ftype.getAttributeType(i);
279                            if (aType != ftype.getDefaultGeometry())
280                                    metaDataMap.put(i, new AttributeMetaData(i, // Column no.
281                                                    true, // visible
282                                                    new Translation(aType.getName()), // Column name
283                                                    new Translation(), // description
284                                                    "" // Unit
285                                    ));
286                    }
287                    return metaDataMap;
288            }
289    
290            /**
291             * Simply sets the {@link #geoObject}, {@link #crs}, {@link #envelope} and
292             * {@link #attrMetaData} to {@code null}.
293             */
294            public void dispose() {
295                    this.geoObject = null;
296                    this.envelope = null;
297                    this.crs = null;
298                    this.attrMetaData = null;
299            }
300    
301            /**
302             * Tests whether the geo object is disposed.
303             */
304            public boolean isDisposed() {
305                    return geoObject == null;
306            }
307    
308            /**
309             * Does nothing, because the {@link AbstractStyledMap} bases on existing
310             * objects (in memory) which can not be uncached and reloaded.
311             */
312            public void uncache() {
313    
314                    /** It will be recreated on the next getFetureSource() **/
315                    featureSource = null;
316    
317                    LOGGER
318                                    .warn("Uncache onyl uncached any virtual FeatureSource. Object remains in memory.");
319            }
320    
321            /*
322             * (non-Javadoc)
323             *
324             * @see skrueger.geotools.StyledMapInterface#getInfoURL()
325             */
326            public URL getInfoURL() {
327                    return null;
328            }
329    
330            /**
331             * 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.127  
changed lines
  Added in v.221

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26