/[schmitzm]/branches/2.4.x/src/skrueger/geotools/StyledFeatureCollection.java
ViewVC logotype

Annotation of /branches/2.4.x/src/skrueger/geotools/StyledFeatureCollection.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1383 - (hide annotations)
Wed Jan 26 13:46:20 2011 UTC (14 years, 1 month ago) by alfonx
File size: 15745 byte(s)
trunk becomes 2.4.x ... starting to create multiple modules

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26