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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 322 - (hide annotations)
Wed Aug 26 14:12:17 2009 UTC (15 years, 6 months ago) by alfonx
Original Path: branches/1.0-gt2-2.6/src/skrueger/geotools/StyledFeatureCollection.java
File size: 12985 byte(s)
Moving to 
import org.opengis.feature.simple.SimpleFeature;

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26