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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26